xref: /petsc/src/dm/interface/dm.c (revision fa88e482a5913848728af63f4516afb5d774e80a)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
10d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
1158cd63d5SVaclav Hapla 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;
1267a56275SMatthew G Knepley 
13e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
1440967b3bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_",0};
15412e9a14SMatthew G. Knepley 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", "unknown", "DMPolytopeType", "DM_POLYTOPE_", 0};
16a4121054SBarry Smith /*@
17de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
18a4121054SBarry Smith 
19a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
20a4121054SBarry Smith    error when you try to use the vector.
21a4121054SBarry Smith 
22d083f849SBarry Smith   Collective
23a4121054SBarry Smith 
24a4121054SBarry Smith   Input Parameter:
25a4121054SBarry Smith . comm - The communicator for the DM object
26a4121054SBarry Smith 
27a4121054SBarry Smith   Output Parameter:
28a4121054SBarry Smith . dm - The DM object
29a4121054SBarry Smith 
30a4121054SBarry Smith   Level: beginner
31a4121054SBarry Smith 
328472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
33a4121054SBarry Smith @*/
347087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
35a4121054SBarry Smith {
36a4121054SBarry Smith   DM             v;
37e5e52638SMatthew G. Knepley   PetscDS        ds;
38a4121054SBarry Smith   PetscErrorCode ierr;
39a4121054SBarry Smith 
40a4121054SBarry Smith   PetscFunctionBegin;
411411c6eeSJed Brown   PetscValidPointer(dm,2);
420298fd71SBarry Smith   *dm = NULL;
43607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
44a4121054SBarry Smith 
4573107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
46e7c4fc90SDmitry Karpeev 
4749be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
4849be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
490298fd71SBarry Smith   v->ltogmap                  = NULL;
501411c6eeSJed Brown   v->bs                       = 1;
51171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5288ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
531bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
54c58f1c22SToby Isaac   v->labels                   = NULL;
5534aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
5634aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
57c58f1c22SToby Isaac   v->depthLabel               = NULL;
58ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
591bb6d2a8SBarry Smith   v->localSection             = NULL;
601bb6d2a8SBarry Smith   v->globalSection            = NULL;
61fba222abSToby Isaac   v->defaultConstraintSection = NULL;
62fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
63c6b900c6SMatthew G. Knepley   v->L                        = NULL;
64c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
655dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
669a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
6796173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
68435a35e8SMatthew G Knepley   {
69435a35e8SMatthew G Knepley     PetscInt i;
70435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
710298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
72f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
73435a35e8SMatthew G Knepley     }
74435a35e8SMatthew G Knepley   }
75e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
76b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
77e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
7814f150ffSMatthew G. Knepley   v->dmBC = NULL;
79a8fb8f29SToby Isaac   v->coarseMesh = NULL;
80f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
81cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
82c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
83b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
844a7a4c06SLawrence Mitchell 
851411c6eeSJed Brown   *dm = v;
86a4121054SBarry Smith   PetscFunctionReturn(0);
87a4121054SBarry Smith }
88a4121054SBarry Smith 
8938221697SMatthew G. Knepley /*@
9038221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9138221697SMatthew G. Knepley 
92d083f849SBarry Smith   Collective
9338221697SMatthew G. Knepley 
9438221697SMatthew G. Knepley   Input Parameter:
9538221697SMatthew G. Knepley . dm - The original DM object
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley   Output Parameter:
9838221697SMatthew G. Knepley . newdm  - The new DM object
9938221697SMatthew G. Knepley 
10038221697SMatthew G. Knepley   Level: beginner
10138221697SMatthew G. Knepley 
1021cb8cacdSPatrick Sanan   Notes:
1031cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1041cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1051cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1061bb6d2a8SBarry Smith 
10789706ed2SPatrick Sanan   The clone is considered set up iff the original is.
10889706ed2SPatrick Sanan 
10992cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1101bb6d2a8SBarry Smith 
11138221697SMatthew G. Knepley @*/
11238221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11338221697SMatthew G. Knepley {
11438221697SMatthew G. Knepley   PetscSF        sf;
11538221697SMatthew G. Knepley   Vec            coords;
11638221697SMatthew G. Knepley   void          *ctx;
117a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11838221697SMatthew G. Knepley   PetscErrorCode ierr;
11938221697SMatthew G. Knepley 
12038221697SMatthew G. Knepley   PetscFunctionBegin;
12138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12238221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12338221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1245d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
125ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
126ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1271de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1281de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12938221697SMatthew G. Knepley   if (dm->ops->clone) {
13038221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13138221697SMatthew G. Knepley   }
1323f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13338221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13438221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13638221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
137be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
138be4c1c3eSMatthew G. Knepley     DM           ncdm;
139be4c1c3eSMatthew G. Knepley     PetscSection cs;
1405a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
141be4c1c3eSMatthew G. Knepley 
14292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
143be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1445a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1455a0206caSToby Isaac     if (pEndMax >= 0) {
146be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
14783bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
14892fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
149a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
150be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
151be4c1c3eSMatthew G. Knepley     }
152be4c1c3eSMatthew G. Knepley   }
153a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
154a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15538221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15638221697SMatthew G. Knepley   if (coords) {
15738221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15838221697SMatthew G. Knepley   } else {
15938221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16038221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16138221697SMatthew G. Knepley   }
16290b157c4SStefano Zampini   {
16390b157c4SStefano Zampini     PetscBool             isper;
164c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1655dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16690b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16790b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
168c6b900c6SMatthew G. Knepley   }
16934aa8a36SMatthew G. Knepley   {
17034aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17134aa8a36SMatthew G. Knepley 
17234aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17434aa8a36SMatthew G. Knepley   }
17538221697SMatthew G. Knepley   PetscFunctionReturn(0);
17638221697SMatthew G. Knepley }
17738221697SMatthew G. Knepley 
1789a42bb27SBarry Smith /*@C
179564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1809a42bb27SBarry Smith 
181d083f849SBarry Smith    Logically Collective on da
1829a42bb27SBarry Smith 
1839a42bb27SBarry Smith    Input Parameter:
1849a42bb27SBarry Smith +  da - initial distributed array
185e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith    Options Database:
188dd85299cSBarry Smith .   -dm_vec_type ctype
1899a42bb27SBarry Smith 
1909a42bb27SBarry Smith    Level: intermediate
1919a42bb27SBarry Smith 
192a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1939a42bb27SBarry Smith @*/
19419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1959a42bb27SBarry Smith {
1969a42bb27SBarry Smith   PetscErrorCode ierr;
1979a42bb27SBarry Smith 
1989a42bb27SBarry Smith   PetscFunctionBegin;
1999a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2009a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2029a42bb27SBarry Smith   PetscFunctionReturn(0);
2039a42bb27SBarry Smith }
2049a42bb27SBarry Smith 
205c0dedaeaSBarry Smith /*@C
206c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
207c0dedaeaSBarry Smith 
208d083f849SBarry Smith    Logically Collective on da
209c0dedaeaSBarry Smith 
210c0dedaeaSBarry Smith    Input Parameter:
211c0dedaeaSBarry Smith .  da - initial distributed array
212c0dedaeaSBarry Smith 
213c0dedaeaSBarry Smith    Output Parameter:
214c0dedaeaSBarry Smith .  ctype - the vector type
215c0dedaeaSBarry Smith 
216c0dedaeaSBarry Smith    Level: intermediate
217c0dedaeaSBarry Smith 
218a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
219c0dedaeaSBarry Smith @*/
220c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
221c0dedaeaSBarry Smith {
222c0dedaeaSBarry Smith   PetscFunctionBegin;
223c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
224c0dedaeaSBarry Smith   *ctype = da->vectype;
225c0dedaeaSBarry Smith   PetscFunctionReturn(0);
226c0dedaeaSBarry Smith }
227c0dedaeaSBarry Smith 
2285f1ad066SMatthew G Knepley /*@
22934f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley   Not collective
2325f1ad066SMatthew G Knepley 
2335f1ad066SMatthew G Knepley   Input Parameter:
2345f1ad066SMatthew G Knepley . v - The Vec
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   Output Parameter:
2375f1ad066SMatthew G Knepley . dm - The DM
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley   Level: intermediate
2405f1ad066SMatthew G Knepley 
2415f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2425f1ad066SMatthew G Knepley @*/
2435f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2445f1ad066SMatthew G Knepley {
2455f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2465f1ad066SMatthew G Knepley 
2475f1ad066SMatthew G Knepley   PetscFunctionBegin;
2485f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2495f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2505f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2515f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2525f1ad066SMatthew G Knepley }
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley /*@
255d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2565f1ad066SMatthew G Knepley 
2575f1ad066SMatthew G Knepley   Not collective
2585f1ad066SMatthew G Knepley 
2595f1ad066SMatthew G Knepley   Input Parameters:
2605f1ad066SMatthew G Knepley + v - The Vec
2615f1ad066SMatthew G Knepley - dm - The DM
2625f1ad066SMatthew G Knepley 
263d9805387SMatthew 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.
264d9805387SMatthew G. Knepley 
2655f1ad066SMatthew G Knepley   Level: intermediate
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2685f1ad066SMatthew G Knepley @*/
2695f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2705f1ad066SMatthew G Knepley {
2715f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley   PetscFunctionBegin;
2745f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
275d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2765f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2775f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2785f1ad066SMatthew G Knepley }
2795f1ad066SMatthew G Knepley 
280521d9a4cSLisandro Dalcin /*@C
2818f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2828f1509bcSBarry Smith 
283d083f849SBarry Smith    Logically Collective on dm
2848f1509bcSBarry Smith 
2858f1509bcSBarry Smith    Input Parameters:
2868f1509bcSBarry Smith +  dm - the DM context
2878f1509bcSBarry Smith -  ctype - the matrix type
2888f1509bcSBarry Smith 
2898f1509bcSBarry Smith    Options Database:
2908f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2918f1509bcSBarry Smith 
2928f1509bcSBarry Smith    Level: intermediate
2938f1509bcSBarry Smith 
2948f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2958f1509bcSBarry Smith           DMGetISColoringType()
2968f1509bcSBarry Smith @*/
2978f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2988f1509bcSBarry Smith {
2998f1509bcSBarry Smith   PetscFunctionBegin;
3008f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3018f1509bcSBarry Smith   dm->coloringtype = ctype;
3028f1509bcSBarry Smith   PetscFunctionReturn(0);
3038f1509bcSBarry Smith }
3048f1509bcSBarry Smith 
3058f1509bcSBarry Smith /*@C
3068f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
307521d9a4cSLisandro Dalcin 
308d083f849SBarry Smith    Logically Collective on dm
309521d9a4cSLisandro Dalcin 
310521d9a4cSLisandro Dalcin    Input Parameter:
3118f1509bcSBarry Smith .  dm - the DM context
3128f1509bcSBarry Smith 
3138f1509bcSBarry Smith    Output Parameter:
3148f1509bcSBarry Smith .  ctype - the matrix type
3158f1509bcSBarry Smith 
3168f1509bcSBarry Smith    Options Database:
3178f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3188f1509bcSBarry Smith 
3198f1509bcSBarry Smith    Level: intermediate
3208f1509bcSBarry Smith 
3218f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3228f1509bcSBarry Smith           DMGetISColoringType()
3238f1509bcSBarry Smith @*/
3248f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3258f1509bcSBarry Smith {
3268f1509bcSBarry Smith   PetscFunctionBegin;
3278f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3288f1509bcSBarry Smith   *ctype = dm->coloringtype;
3298f1509bcSBarry Smith   PetscFunctionReturn(0);
3308f1509bcSBarry Smith }
3318f1509bcSBarry Smith 
3328f1509bcSBarry Smith /*@C
3338f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3348f1509bcSBarry Smith 
335d083f849SBarry Smith    Logically Collective on dm
3368f1509bcSBarry Smith 
3378f1509bcSBarry Smith    Input Parameters:
338521d9a4cSLisandro Dalcin +  dm - the DM context
339a2b5a043SBarry Smith -  ctype - the matrix type
340521d9a4cSLisandro Dalcin 
341521d9a4cSLisandro Dalcin    Options Database:
342521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
343521d9a4cSLisandro Dalcin 
344521d9a4cSLisandro Dalcin    Level: intermediate
345521d9a4cSLisandro Dalcin 
346a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
347521d9a4cSLisandro Dalcin @*/
34819fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
349521d9a4cSLisandro Dalcin {
350521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35188f0584fSBarry Smith 
352521d9a4cSLisandro Dalcin   PetscFunctionBegin;
353521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
354521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
356521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
357521d9a4cSLisandro Dalcin }
358521d9a4cSLisandro Dalcin 
359c0dedaeaSBarry Smith /*@C
360c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
361c0dedaeaSBarry Smith 
362d083f849SBarry Smith    Logically Collective on dm
363c0dedaeaSBarry Smith 
364c0dedaeaSBarry Smith    Input Parameter:
365c0dedaeaSBarry Smith .  dm - the DM context
366c0dedaeaSBarry Smith 
367c0dedaeaSBarry Smith    Output Parameter:
368c0dedaeaSBarry Smith .  ctype - the matrix type
369c0dedaeaSBarry Smith 
370c0dedaeaSBarry Smith    Options Database:
371c0dedaeaSBarry Smith .   -dm_mat_type ctype
372c0dedaeaSBarry Smith 
373c0dedaeaSBarry Smith    Level: intermediate
374c0dedaeaSBarry Smith 
375a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
376c0dedaeaSBarry Smith @*/
377c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
378c0dedaeaSBarry Smith {
379c0dedaeaSBarry Smith   PetscFunctionBegin;
380c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
381c0dedaeaSBarry Smith   *ctype = dm->mattype;
382c0dedaeaSBarry Smith   PetscFunctionReturn(0);
383c0dedaeaSBarry Smith }
384c0dedaeaSBarry Smith 
385c688c046SMatthew G Knepley /*@
38634f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
387c688c046SMatthew G Knepley 
388c688c046SMatthew G Knepley   Not collective
389c688c046SMatthew G Knepley 
390c688c046SMatthew G Knepley   Input Parameter:
391c688c046SMatthew G Knepley . A - The Mat
392c688c046SMatthew G Knepley 
393c688c046SMatthew G Knepley   Output Parameter:
394c688c046SMatthew G Knepley . dm - The DM
395c688c046SMatthew G Knepley 
396c688c046SMatthew G Knepley   Level: intermediate
397c688c046SMatthew G Knepley 
3988f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3998f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4008f1509bcSBarry Smith 
401c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
402c688c046SMatthew G Knepley @*/
403c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
404c688c046SMatthew G Knepley {
405c688c046SMatthew G Knepley   PetscErrorCode ierr;
406c688c046SMatthew G Knepley 
407c688c046SMatthew G Knepley   PetscFunctionBegin;
408c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
409c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
410c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
411c688c046SMatthew G Knepley   PetscFunctionReturn(0);
412c688c046SMatthew G Knepley }
413c688c046SMatthew G Knepley 
414c688c046SMatthew G Knepley /*@
415c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
416c688c046SMatthew G Knepley 
417c688c046SMatthew G Knepley   Not collective
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley   Input Parameters:
420c688c046SMatthew G Knepley + A - The Mat
421c688c046SMatthew G Knepley - dm - The DM
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   Level: intermediate
424c688c046SMatthew G Knepley 
4258f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4268f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4278f1509bcSBarry Smith 
4288f1509bcSBarry Smith 
429c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
430c688c046SMatthew G Knepley @*/
431c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
432c688c046SMatthew G Knepley {
433c688c046SMatthew G Knepley   PetscErrorCode ierr;
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   PetscFunctionBegin;
436c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4378865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
438c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
439c688c046SMatthew G Knepley   PetscFunctionReturn(0);
440c688c046SMatthew G Knepley }
441c688c046SMatthew G Knepley 
4429a42bb27SBarry Smith /*@C
4439a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4446757b960SDave May    DM options in the database.
4459a42bb27SBarry Smith 
446d083f849SBarry Smith    Logically Collective on dm
4479a42bb27SBarry Smith 
4489a42bb27SBarry Smith    Input Parameter:
4498353ddbbSDave May +  da - the DM context
4509a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4519a42bb27SBarry Smith 
4529a42bb27SBarry Smith    Notes:
4539a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4549a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4559a42bb27SBarry Smith 
4569a42bb27SBarry Smith    Level: advanced
4579a42bb27SBarry Smith 
4589a42bb27SBarry Smith .seealso: DMSetFromOptions()
4599a42bb27SBarry Smith @*/
4607087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4619a42bb27SBarry Smith {
4629a42bb27SBarry Smith   PetscErrorCode ierr;
4639a42bb27SBarry Smith 
4649a42bb27SBarry Smith   PetscFunctionBegin;
4659a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4669a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
467691be533SLawrence Mitchell   if (dm->sf) {
468691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
469691be533SLawrence Mitchell   }
4701bb6d2a8SBarry Smith   if (dm->sectionSF) {
4711bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
472691be533SLawrence Mitchell   }
4739a42bb27SBarry Smith   PetscFunctionReturn(0);
4749a42bb27SBarry Smith }
4759a42bb27SBarry Smith 
47631697293SDave May /*@C
47731697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47831697293SDave May    DM options in the database.
47931697293SDave May 
480d083f849SBarry Smith    Logically Collective on dm
48131697293SDave May 
48231697293SDave May    Input Parameters:
48331697293SDave May +  dm - the DM context
48431697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48531697293SDave May 
48631697293SDave May    Notes:
48731697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48831697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48931697293SDave May 
49031697293SDave May    Level: advanced
49131697293SDave May 
49231697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49331697293SDave May @*/
49431697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49531697293SDave May {
49631697293SDave May   PetscErrorCode ierr;
49731697293SDave May 
49831697293SDave May   PetscFunctionBegin;
49931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50031697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50131697293SDave May   PetscFunctionReturn(0);
50231697293SDave May }
50331697293SDave May 
50431697293SDave May /*@C
50531697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50631697293SDave May    DM options in the database.
50731697293SDave May 
50831697293SDave May    Not Collective
50931697293SDave May 
51031697293SDave May    Input Parameters:
51131697293SDave May .  dm - the DM context
51231697293SDave May 
51331697293SDave May    Output Parameters:
51431697293SDave May .  prefix - pointer to the prefix string used is returned
51531697293SDave May 
51695452b02SPatrick Sanan    Notes:
51795452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51831697293SDave May    sufficient length to hold the prefix.
51931697293SDave May 
52031697293SDave May    Level: advanced
52131697293SDave May 
52231697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52331697293SDave May @*/
52431697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52531697293SDave May {
52631697293SDave May   PetscErrorCode ierr;
52731697293SDave May 
52831697293SDave May   PetscFunctionBegin;
52931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53031697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53131697293SDave May   PetscFunctionReturn(0);
53231697293SDave May }
53331697293SDave May 
53488bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53588bdff64SToby Isaac {
5366eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
53788bdff64SToby Isaac   PetscErrorCode ierr;
53888bdff64SToby Isaac 
53988bdff64SToby Isaac   PetscFunctionBegin;
540aab5bcd8SJed Brown   *ncrefct = 0;
54188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54288bdff64SToby Isaac     refct--;
54388bdff64SToby Isaac     if (recurseCoarse) {
54488bdff64SToby Isaac       PetscInt coarseCount;
54588bdff64SToby Isaac 
54688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
54788bdff64SToby Isaac       refct += coarseCount;
54888bdff64SToby Isaac     }
54988bdff64SToby Isaac   }
55088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55188bdff64SToby Isaac     refct--;
55288bdff64SToby Isaac     if (recurseFine) {
55388bdff64SToby Isaac       PetscInt fineCount;
55488bdff64SToby Isaac 
55588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
55688bdff64SToby Isaac       refct += fineCount;
55788bdff64SToby Isaac     }
55888bdff64SToby Isaac   }
55988bdff64SToby Isaac   *ncrefct = refct;
56088bdff64SToby Isaac   PetscFunctionReturn(0);
56188bdff64SToby Isaac }
56288bdff64SToby Isaac 
563f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
564354557abSToby Isaac {
5655d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
566354557abSToby Isaac   PetscErrorCode ierr;
567354557abSToby Isaac 
568354557abSToby Isaac   PetscFunctionBegin;
569354557abSToby Isaac   /* destroy the labels */
570354557abSToby Isaac   while (next) {
571354557abSToby Isaac     DMLabelLink tmp = next->next;
572354557abSToby Isaac 
5735d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
574ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
575354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
576354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
577354557abSToby Isaac     next = tmp;
578354557abSToby Isaac   }
5795d80c0bfSVaclav Hapla   dm->labels = NULL;
580354557abSToby Isaac   PetscFunctionReturn(0);
581354557abSToby Isaac }
582354557abSToby Isaac 
58347c6ae99SBarry Smith /*@
5848472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
58547c6ae99SBarry Smith 
586d083f849SBarry Smith     Collective on dm
58747c6ae99SBarry Smith 
58847c6ae99SBarry Smith     Input Parameter:
58947c6ae99SBarry Smith .   dm - the DM object to destroy
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith     Level: developer
59247c6ae99SBarry Smith 
593e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith @*/
596fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
59747c6ae99SBarry Smith {
5986eb26441SStefano Zampini   PetscInt       cnt;
599dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60047c6ae99SBarry Smith   PetscErrorCode ierr;
60147c6ae99SBarry Smith 
60247c6ae99SBarry Smith   PetscFunctionBegin;
6036bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6046bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
60587e657c6SBarry Smith 
60688bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
60788bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
60888bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
60988bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
6106bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6116bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6126eb26441SStefano Zampini 
6136eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6146eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6156eb26441SStefano Zampini 
616f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6170298fd71SBarry Smith   (*dm)->namedglobal = NULL;
618f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6192348bcf4SPeter Brune     nnext = nlink->next;
6202348bcf4SPeter 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);
6212348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6222348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6232348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6242348bcf4SPeter Brune   }
625f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6260298fd71SBarry Smith   (*dm)->namedlocal = NULL;
627f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
628f490541aSPeter Brune     nnext = nlink->next;
629f490541aSPeter 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);
630f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
631f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
632f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
633f490541aSPeter Brune   }
6342348bcf4SPeter Brune 
635b17ce1afSJed Brown   /* Destroy the list of hooks */
636c833c3b5SJed Brown   {
637c833c3b5SJed Brown     DMCoarsenHookLink link,next;
638b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
639b17ce1afSJed Brown       next = link->next;
640b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
641b17ce1afSJed Brown     }
6420298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
643c833c3b5SJed Brown   }
644c833c3b5SJed Brown   {
645c833c3b5SJed Brown     DMRefineHookLink link,next;
646c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
647c833c3b5SJed Brown       next = link->next;
648c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
649c833c3b5SJed Brown     }
6500298fd71SBarry Smith     (*dm)->refinehook = NULL;
651c833c3b5SJed Brown   }
652be081cd6SPeter Brune   {
653be081cd6SPeter Brune     DMSubDomainHookLink link,next;
654be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
655be081cd6SPeter Brune       next = link->next;
656be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
657be081cd6SPeter Brune     }
6580298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
659be081cd6SPeter Brune   }
660baf369e7SPeter Brune   {
661baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
662baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
663baf369e7SPeter Brune       next = link->next;
664baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
665baf369e7SPeter Brune     }
6660298fd71SBarry Smith     (*dm)->gtolhook = NULL;
667baf369e7SPeter Brune   }
668d4d07f1eSToby Isaac   {
669d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
670d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
671d4d07f1eSToby Isaac       next = link->next;
672d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
673d4d07f1eSToby Isaac     }
674d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
675d4d07f1eSToby Isaac   }
676aa1993deSMatthew G Knepley   /* Destroy the work arrays */
677aa1993deSMatthew G Knepley   {
678aa1993deSMatthew G Knepley     DMWorkLink link,next;
679aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
680aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
681aa1993deSMatthew G Knepley       next = link->next;
682aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
683aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
684aa1993deSMatthew G Knepley     }
6850298fd71SBarry Smith     (*dm)->workin = NULL;
686aa1993deSMatthew G Knepley   }
687c58f1c22SToby Isaac   /* destroy the labels */
688f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
689f4cdcedcSVaclav Hapla   /* destroy the fields */
690e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
691f4cdcedcSVaclav Hapla   /* destroy the boundaries */
692e6f8dbb6SToby Isaac   {
693e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
694e6f8dbb6SToby Isaac     while (next) {
695e6f8dbb6SToby Isaac       DMBoundary b = next;
696e6f8dbb6SToby Isaac 
697e6f8dbb6SToby Isaac       next = b->next;
698e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
699e6f8dbb6SToby Isaac     }
700e6f8dbb6SToby Isaac   }
701b17ce1afSJed Brown 
70252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
70352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
70452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
70552536dc3SBarry Smith 
7061a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7071a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7081a266240SBarry Smith   }
70971cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7106bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7116bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
712073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
71388ed4aceSMatthew G Knepley 
7141bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7151bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7168b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
717fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
718fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
71988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7201bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
721736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
722736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7238e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
724736995cdSBlaise Bourdin     }
725736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
726736995cdSBlaise Bourdin   }
72788bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
72888bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
72988bdff64SToby Isaac   }
7306eb26441SStefano Zampini 
731a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
73288bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
73388bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
73488bdff64SToby Isaac   }
73588bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
736f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7376636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7386636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7396636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
740412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
741412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
742412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
743ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
744ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
745ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7466636e97aSMatthew G Knepley 
747e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
74814f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
749e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
750e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
751732e2eb9SMatthew G Knepley 
752ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7536bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
754ed3c66a1SDave May   }
755c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
756435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
757732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
75847c6ae99SBarry Smith   PetscFunctionReturn(0);
75947c6ae99SBarry Smith }
76047c6ae99SBarry Smith 
761d7bf68aeSBarry Smith /*@
762d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
763d7bf68aeSBarry Smith 
764d083f849SBarry Smith     Collective on dm
765d7bf68aeSBarry Smith 
766d7bf68aeSBarry Smith     Input Parameter:
767d7bf68aeSBarry Smith .   dm - the DM object to setup
768d7bf68aeSBarry Smith 
769d7bf68aeSBarry Smith     Level: developer
770d7bf68aeSBarry Smith 
771e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
772d7bf68aeSBarry Smith 
773d7bf68aeSBarry Smith @*/
7747087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
775d7bf68aeSBarry Smith {
776d7bf68aeSBarry Smith   PetscErrorCode ierr;
777d7bf68aeSBarry Smith 
778d7bf68aeSBarry Smith   PetscFunctionBegin;
779171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7808387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
781d7bf68aeSBarry Smith   if (dm->ops->setup) {
782d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
783d7bf68aeSBarry Smith   }
7848387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
785d7bf68aeSBarry Smith   PetscFunctionReturn(0);
786d7bf68aeSBarry Smith }
787d7bf68aeSBarry Smith 
788d7bf68aeSBarry Smith /*@
789d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
790d7bf68aeSBarry Smith 
791d083f849SBarry Smith     Collective on dm
792d7bf68aeSBarry Smith 
793d7bf68aeSBarry Smith     Input Parameter:
794d7bf68aeSBarry Smith .   dm - the DM object to set options for
795d7bf68aeSBarry Smith 
796732e2eb9SMatthew G Knepley     Options Database:
7974164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7984164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7994164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8008f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
801732e2eb9SMatthew G Knepley 
802384a6580SVaclav Hapla     DMPLEX Specific Checks
803384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
804384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
805384a6580SVaclav 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()
806384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
807384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
808384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
809384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
810d7bf68aeSBarry Smith 
81195eb5ee5SVaclav Hapla     Level: intermediate
81295eb5ee5SVaclav Hapla 
81395eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
81495eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
815d7bf68aeSBarry Smith 
816d7bf68aeSBarry Smith @*/
8177087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
818d7bf68aeSBarry Smith {
8197781c08eSBarry Smith   char           typeName[256];
820ca266f36SBarry Smith   PetscBool      flg;
821d7bf68aeSBarry Smith   PetscErrorCode ierr;
822d7bf68aeSBarry Smith 
823d7bf68aeSBarry Smith   PetscFunctionBegin;
824171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82549be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
82649be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8271bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8283194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8290298fd71SBarry 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);
830a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
831f9ba7244SBarry Smith   if (flg) {
832f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
833f9ba7244SBarry Smith   }
834a264d7a6SBarry 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);
835073dac72SJed Brown   if (flg) {
836521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
837073dac72SJed Brown   }
8388f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
839f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
840e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
841f9ba7244SBarry Smith   }
842f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8430633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
84482fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
845d7bf68aeSBarry Smith   PetscFunctionReturn(0);
846d7bf68aeSBarry Smith }
847d7bf68aeSBarry Smith 
848fc9bc008SSatish Balay /*@C
849fe2efc57SMark    DMViewFromOptions - View from Options
850fe2efc57SMark 
851fe2efc57SMark    Collective on DM
852fe2efc57SMark 
853fe2efc57SMark    Input Parameters:
854fe2efc57SMark +  dm - the DM object
855736c3998SJose E. Roman .  obj - Optional object
856736c3998SJose E. Roman -  name - command line option
857fe2efc57SMark 
858fe2efc57SMark    Level: intermediate
859fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
860fe2efc57SMark @*/
861fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
862fe2efc57SMark {
863fe2efc57SMark   PetscErrorCode ierr;
864fe2efc57SMark 
865fe2efc57SMark   PetscFunctionBegin;
866fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
867fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
868fe2efc57SMark   PetscFunctionReturn(0);
869fe2efc57SMark }
870fe2efc57SMark 
871fe2efc57SMark /*@C
872224748a4SBarry Smith     DMView - Views a DM
87347c6ae99SBarry Smith 
874d083f849SBarry Smith     Collective on dm
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith     Input Parameter:
87747c6ae99SBarry Smith +   dm - the DM object to view
87847c6ae99SBarry Smith -   v - the viewer
87947c6ae99SBarry Smith 
880224748a4SBarry Smith     Level: beginner
88147c6ae99SBarry Smith 
882e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88347c6ae99SBarry Smith 
88447c6ae99SBarry Smith @*/
8857087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
88647c6ae99SBarry Smith {
88747c6ae99SBarry Smith   PetscErrorCode    ierr;
88832c0f0efSBarry Smith   PetscBool         isbinary;
88976a8abe0SBarry Smith   PetscMPIInt       size;
89076a8abe0SBarry Smith   PetscViewerFormat format;
89147c6ae99SBarry Smith 
89247c6ae99SBarry Smith   PetscFunctionBegin;
893171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8943014e516SBarry Smith   if (!v) {
895ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8963014e516SBarry Smith   }
897b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
89874903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
89974903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
90074903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
90174903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
90274903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
90374903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
90474903a4fSStefano Zampini      in an error here */
90574903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9068bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
907b1b135c8SBarry Smith 
90876a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
90976a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
91076a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
91198c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
91232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
91332c0f0efSBarry Smith   if (isbinary) {
91455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
91532c0f0efSBarry Smith     char     type[256];
91632c0f0efSBarry Smith 
917f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
91832c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
919f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
92032c0f0efSBarry Smith   }
9210c010503SBarry Smith   if (dm->ops->view) {
9220c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
92347c6ae99SBarry Smith   }
92447c6ae99SBarry Smith   PetscFunctionReturn(0);
92547c6ae99SBarry Smith }
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith /*@
9288472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
92947c6ae99SBarry Smith 
930d083f849SBarry Smith     Collective on dm
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith     Input Parameter:
93347c6ae99SBarry Smith .   dm - the DM object
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith     Output Parameter:
93647c6ae99SBarry Smith .   vec - the global vector
93747c6ae99SBarry Smith 
938073dac72SJed Brown     Level: beginner
93947c6ae99SBarry Smith 
940e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith @*/
9437087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
94447c6ae99SBarry Smith {
94547c6ae99SBarry Smith   PetscErrorCode ierr;
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith   PetscFunctionBegin;
948171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
949b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
950b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
95147c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
952c6b011d8SStefano Zampini #if defined(PETSC_USE_DEBUG)
953c6b011d8SStefano Zampini   {
954c6b011d8SStefano Zampini     DM vdm;
955c6b011d8SStefano Zampini 
956c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
957c6b011d8SStefano 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);
958c6b011d8SStefano Zampini   }
959c6b011d8SStefano Zampini #endif
96047c6ae99SBarry Smith   PetscFunctionReturn(0);
96147c6ae99SBarry Smith }
96247c6ae99SBarry Smith 
96347c6ae99SBarry Smith /*@
9648472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
96547c6ae99SBarry Smith 
96647c6ae99SBarry Smith     Not Collective
96747c6ae99SBarry Smith 
96847c6ae99SBarry Smith     Input Parameter:
96947c6ae99SBarry Smith .   dm - the DM object
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith     Output Parameter:
97247c6ae99SBarry Smith .   vec - the local vector
97347c6ae99SBarry Smith 
974073dac72SJed Brown     Level: beginner
97547c6ae99SBarry Smith 
976e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
97747c6ae99SBarry Smith 
97847c6ae99SBarry Smith @*/
9797087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
98047c6ae99SBarry Smith {
98147c6ae99SBarry Smith   PetscErrorCode ierr;
98247c6ae99SBarry Smith 
98347c6ae99SBarry Smith   PetscFunctionBegin;
984171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
985b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
986b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
98747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
988c6b011d8SStefano Zampini #if defined(PETSC_USE_DEBUG)
989c6b011d8SStefano Zampini   {
990c6b011d8SStefano Zampini     DM vdm;
991c6b011d8SStefano Zampini 
992c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
993c6b011d8SStefano 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);
994c6b011d8SStefano Zampini   }
995c6b011d8SStefano Zampini #endif
99647c6ae99SBarry Smith   PetscFunctionReturn(0);
99747c6ae99SBarry Smith }
99847c6ae99SBarry Smith 
9991411c6eeSJed Brown /*@
10001411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10011411c6eeSJed Brown 
1002d083f849SBarry Smith    Collective on dm
10031411c6eeSJed Brown 
10041411c6eeSJed Brown    Input Parameter:
10051411c6eeSJed Brown .  dm - the DM that provides the mapping
10061411c6eeSJed Brown 
10071411c6eeSJed Brown    Output Parameter:
10081411c6eeSJed Brown .  ltog - the mapping
10091411c6eeSJed Brown 
10101411c6eeSJed Brown    Level: intermediate
10111411c6eeSJed Brown 
10121411c6eeSJed Brown    Notes:
10131411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10141411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10151411c6eeSJed Brown 
1016fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10171411c6eeSJed Brown @*/
10187087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10191411c6eeSJed Brown {
10200be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10211411c6eeSJed Brown   PetscErrorCode ierr;
10221411c6eeSJed Brown 
10231411c6eeSJed Brown   PetscFunctionBegin;
10241411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10251411c6eeSJed Brown   PetscValidPointer(ltog,2);
10261411c6eeSJed Brown   if (!dm->ltogmap) {
102737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
102837d0c07bSMatthew G Knepley 
102992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
103037d0c07bSMatthew G Knepley     if (section) {
1031a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
103237d0c07bSMatthew G Knepley       PetscInt       *ltog;
1033ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
103437d0c07bSMatthew G Knepley 
1035e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1037ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1038ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
103937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1040a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
104137d0c07bSMatthew G Knepley 
104237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
104337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1044a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1045a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
104637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10471a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10481a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10491a7dc684SMatthew G. Knepley         if (dof) {
1050b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1051b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10521a7dc684SMatthew G. Knepley         }
105337d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1054a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1055a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
105637d0c07bSMatthew G Knepley         }
105737d0c07bSMatthew G Knepley       }
1058bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10590be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10600be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10610be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10620be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10637591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10647591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1065ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1066ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1067ccf3bd66SMatthew G. Knepley         n /= bs;
1068ccf3bd66SMatthew G. Knepley       }
1069ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10703bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
107137d0c07bSMatthew G Knepley     } else {
1072b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1073184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10741411c6eeSJed Brown     }
107537d0c07bSMatthew G Knepley   }
10761411c6eeSJed Brown   *ltog = dm->ltogmap;
10771411c6eeSJed Brown   PetscFunctionReturn(0);
10781411c6eeSJed Brown }
10791411c6eeSJed Brown 
10801411c6eeSJed Brown /*@
10811411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10821411c6eeSJed Brown 
10831411c6eeSJed Brown    Not Collective
10841411c6eeSJed Brown 
10851411c6eeSJed Brown    Input Parameter:
10861411c6eeSJed Brown .  dm - the DM with block structure
10871411c6eeSJed Brown 
10881411c6eeSJed Brown    Output Parameter:
10891411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10901411c6eeSJed Brown 
10911411c6eeSJed Brown    Level: intermediate
10921411c6eeSJed Brown 
1093fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10941411c6eeSJed Brown @*/
10957087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10961411c6eeSJed Brown {
10971411c6eeSJed Brown   PetscFunctionBegin;
10981411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1099534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11001411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11011411c6eeSJed Brown   *bs = dm->bs;
11021411c6eeSJed Brown   PetscFunctionReturn(0);
11031411c6eeSJed Brown }
11041411c6eeSJed Brown 
110547c6ae99SBarry Smith /*@
11068472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
110747c6ae99SBarry Smith 
1108a5bc1bf3SBarry Smith     Collective on dmc
110947c6ae99SBarry Smith 
111047c6ae99SBarry Smith     Input Parameter:
1111a5bc1bf3SBarry Smith +   dmc - the DM object
1112a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
111347c6ae99SBarry Smith 
111447c6ae99SBarry Smith     Output Parameter:
111547c6ae99SBarry Smith +  mat - the interpolation
111647c6ae99SBarry Smith -  vec - the scaling (optional)
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith     Level: developer
111947c6ae99SBarry Smith 
112095452b02SPatrick Sanan     Notes:
112195452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
112285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1123d52bd9f3SBarry Smith 
11241f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1125d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
112685afcc9aSBarry Smith 
112785afcc9aSBarry Smith 
11282ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
112947c6ae99SBarry Smith 
113047c6ae99SBarry Smith @*/
1131a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
113247c6ae99SBarry Smith {
113347c6ae99SBarry Smith   PetscErrorCode ierr;
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith   PetscFunctionBegin;
1136a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1137a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1138c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1139a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1140a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1141a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1142a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
114347c6ae99SBarry Smith   PetscFunctionReturn(0);
114447c6ae99SBarry Smith }
114547c6ae99SBarry Smith 
11463ad4599aSBarry Smith /*@
1147d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11482ed6491fSPatrick Sanan 
11492ed6491fSPatrick Sanan   Input Parameters:
11502ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11512ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11522ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11532ed6491fSPatrick Sanan 
11542ed6491fSPatrick Sanan   Output Parameter:
11552ed6491fSPatrick Sanan .    scale - the scaled vector
11562ed6491fSPatrick Sanan 
11572ed6491fSPatrick Sanan   Level: developer
11582ed6491fSPatrick Sanan 
11592ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
11602ed6491fSPatrick Sanan 
11612ed6491fSPatrick Sanan @*/
11622ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11632ed6491fSPatrick Sanan {
11642ed6491fSPatrick Sanan   PetscErrorCode ierr;
11652ed6491fSPatrick Sanan   Vec            fine;
11662ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11672ed6491fSPatrick Sanan 
11682ed6491fSPatrick Sanan   PetscFunctionBegin;
11692ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
11702ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
11712ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
11722ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
11732ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
11742ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
11752ed6491fSPatrick Sanan   PetscFunctionReturn(0);
11762ed6491fSPatrick Sanan }
11772ed6491fSPatrick Sanan 
11782ed6491fSPatrick Sanan /*@
11793ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11803ad4599aSBarry Smith 
1181a5bc1bf3SBarry Smith     Collective on dmc
11823ad4599aSBarry Smith 
11833ad4599aSBarry Smith     Input Parameter:
1184a5bc1bf3SBarry Smith +   dmc - the DM object
1185a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
11863ad4599aSBarry Smith 
11873ad4599aSBarry Smith     Output Parameter:
11883ad4599aSBarry Smith .  mat - the restriction
11893ad4599aSBarry Smith 
11903ad4599aSBarry Smith 
11913ad4599aSBarry Smith     Level: developer
11923ad4599aSBarry Smith 
119395452b02SPatrick Sanan     Notes:
119495452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
11953ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11963ad4599aSBarry Smith 
11973ad4599aSBarry Smith 
11983ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11993ad4599aSBarry Smith 
12003ad4599aSBarry Smith @*/
1201a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12023ad4599aSBarry Smith {
12033ad4599aSBarry Smith   PetscErrorCode ierr;
12043ad4599aSBarry Smith 
12053ad4599aSBarry Smith   PetscFunctionBegin;
1206a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1207a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12085a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1209a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1210a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1211a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1212a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12133ad4599aSBarry Smith   PetscFunctionReturn(0);
12143ad4599aSBarry Smith }
12153ad4599aSBarry Smith 
121647c6ae99SBarry Smith /*@
12178472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
121847c6ae99SBarry Smith 
1219a5bc1bf3SBarry Smith     Collective on dac
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith     Input Parameter:
1222a5bc1bf3SBarry Smith +   dac - the DM object
1223a5bc1bf3SBarry Smith -   daf - the second, finer DM object
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith     Output Parameter:
12266dbf9973SLawrence Mitchell .   mat - the injection
122747c6ae99SBarry Smith 
122847c6ae99SBarry Smith     Level: developer
122947c6ae99SBarry Smith 
123095452b02SPatrick Sanan    Notes:
123195452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
123285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
123385afcc9aSBarry Smith 
1234e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith @*/
1237a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
123847c6ae99SBarry Smith {
123947c6ae99SBarry Smith   PetscErrorCode ierr;
124047c6ae99SBarry Smith 
124147c6ae99SBarry Smith   PetscFunctionBegin;
1242a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1243a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12445a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1245a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1246a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1247a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1248a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
124947c6ae99SBarry Smith   PetscFunctionReturn(0);
125047c6ae99SBarry Smith }
125147c6ae99SBarry Smith 
1252b412c318SBarry Smith /*@
1253bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1254bd041c0cSMatthew G. Knepley 
1255a5bc1bf3SBarry Smith   Collective on dac
1256bd041c0cSMatthew G. Knepley 
1257bd041c0cSMatthew G. Knepley   Input Parameter:
1258a5bc1bf3SBarry Smith + dac - the DM object
1259a5bc1bf3SBarry Smith - daf - the second, finer DM object
1260bd041c0cSMatthew G. Knepley 
1261bd041c0cSMatthew G. Knepley   Output Parameter:
1262bd041c0cSMatthew G. Knepley . mat - the interpolation
1263bd041c0cSMatthew G. Knepley 
1264bd041c0cSMatthew G. Knepley   Level: developer
1265bd041c0cSMatthew G. Knepley 
1266bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1267bd041c0cSMatthew G. Knepley @*/
1268a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1269bd041c0cSMatthew G. Knepley {
1270bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1271bd041c0cSMatthew G. Knepley 
1272bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1273a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1274a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
12755a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1276a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1277a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1278bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1279bd041c0cSMatthew G. Knepley }
1280bd041c0cSMatthew G. Knepley 
1281bd041c0cSMatthew G. Knepley /*@
1282b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
128347c6ae99SBarry Smith 
1284d083f849SBarry Smith     Collective on dm
128547c6ae99SBarry Smith 
128647c6ae99SBarry Smith     Input Parameter:
128747c6ae99SBarry Smith +   dm - the DM object
12885bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
128947c6ae99SBarry Smith 
129047c6ae99SBarry Smith     Output Parameter:
129147c6ae99SBarry Smith .   coloring - the coloring
129247c6ae99SBarry Smith 
1293ec5066bdSBarry Smith     Notes:
1294ec5066bdSBarry Smith        Coloring of matrices can be computed directly from the sparse matrix nonzero structure via the MatColoring object or from the mesh from which the
1295ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1296ec5066bdSBarry Smith 
1297ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1298ec5066bdSBarry Smith 
129947c6ae99SBarry Smith     Level: developer
130047c6ae99SBarry Smith 
1301ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
130247c6ae99SBarry Smith 
1303aab9d709SJed Brown @*/
1304b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
130547c6ae99SBarry Smith {
130647c6ae99SBarry Smith   PetscErrorCode ierr;
130747c6ae99SBarry Smith 
130847c6ae99SBarry Smith   PetscFunctionBegin;
1309171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13105a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1311b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1312b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
131347c6ae99SBarry Smith   PetscFunctionReturn(0);
131447c6ae99SBarry Smith }
131547c6ae99SBarry Smith 
1316b412c318SBarry Smith /*@
13178472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
131847c6ae99SBarry Smith 
1319d083f849SBarry Smith     Collective on dm
132047c6ae99SBarry Smith 
132147c6ae99SBarry Smith     Input Parameter:
1322b412c318SBarry Smith .   dm - the DM object
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith     Output Parameter:
132547c6ae99SBarry Smith .   mat - the empty Jacobian
132647c6ae99SBarry Smith 
1327073dac72SJed Brown     Level: beginner
132847c6ae99SBarry Smith 
132995452b02SPatrick Sanan     Notes:
133095452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
133194013140SBarry Smith        do not need to do it yourself.
133294013140SBarry Smith 
133394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13347889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
133594013140SBarry Smith 
133694013140SBarry Smith        For structured grid problems, when you call MatView() on this matrix it is displayed using the global natural ordering, NOT in the ordering used
133794013140SBarry Smith        internally by PETSc.
133894013140SBarry Smith 
133994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1340aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
134194013140SBarry Smith 
1342b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
134347c6ae99SBarry Smith 
1344aab9d709SJed Brown @*/
1345b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
134647c6ae99SBarry Smith {
134747c6ae99SBarry Smith   PetscErrorCode ierr;
134847c6ae99SBarry Smith 
134947c6ae99SBarry Smith   PetscFunctionBegin;
1350171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1351c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1352b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13535a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1354fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1355b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1356c6b011d8SStefano Zampini #if defined(PETSC_USE_DEBUG)
1357c6b011d8SStefano Zampini   {
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   }
1363c6b011d8SStefano Zampini #endif
1364e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1365e5e52638SMatthew G. Knepley   if (dm->Nf) {
1366e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1367e571a35bSMatthew G. Knepley     PetscInt     Nf;
1368e571a35bSMatthew G. Knepley 
1369e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1370e571a35bSMatthew G. Knepley     if (Nf == 1) {
1371e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1372e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1373e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1374e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1375e571a35bSMatthew G. Knepley       }
1376e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1377e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1378e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1379e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1380e571a35bSMatthew G. Knepley       }
1381e571a35bSMatthew G. Knepley     }
1382e571a35bSMatthew G. Knepley   }
1383fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
138447c6ae99SBarry Smith   PetscFunctionReturn(0);
138547c6ae99SBarry Smith }
138647c6ae99SBarry Smith 
1387732e2eb9SMatthew G Knepley /*@
1388950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1389732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1390732e2eb9SMatthew G Knepley 
1391d083f849SBarry Smith   Logically Collective on dm
1392732e2eb9SMatthew G Knepley 
1393732e2eb9SMatthew G Knepley   Input Parameter:
1394732e2eb9SMatthew G Knepley + dm - the DM
1395732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1396732e2eb9SMatthew G Knepley 
1397732e2eb9SMatthew G Knepley   Level: developer
1398b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1399732e2eb9SMatthew G Knepley @*/
1400732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1401732e2eb9SMatthew G Knepley {
1402732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1403732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1404732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1405732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1406732e2eb9SMatthew G Knepley }
1407732e2eb9SMatthew G Knepley 
1408b06ff27eSHong Zhang /*@
1409b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1410b06ff27eSHong Zhang     but the array for values will not be allocated.
1411b06ff27eSHong Zhang 
1412d083f849SBarry Smith   Logically Collective on dm
1413b06ff27eSHong Zhang 
1414b06ff27eSHong Zhang   Input Parameter:
1415b06ff27eSHong Zhang + dm - the DM
1416b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1417b06ff27eSHong Zhang 
1418b06ff27eSHong Zhang   Level: developer
1419b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1420b06ff27eSHong Zhang @*/
1421b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1422b06ff27eSHong Zhang {
1423b06ff27eSHong Zhang   PetscFunctionBegin;
1424b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1425b06ff27eSHong Zhang   dm->structure_only = only;
1426b06ff27eSHong Zhang   PetscFunctionReturn(0);
1427b06ff27eSHong Zhang }
1428b06ff27eSHong Zhang 
1429a89ea682SMatthew G Knepley /*@C
1430aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1431a89ea682SMatthew G Knepley 
1432a89ea682SMatthew G Knepley   Not Collective
1433a89ea682SMatthew G Knepley 
1434a89ea682SMatthew G Knepley   Input Parameters:
1435a89ea682SMatthew G Knepley + dm - the DM object
1436aa1993deSMatthew G Knepley . count - The minium size
143769291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1438a89ea682SMatthew G Knepley 
1439a89ea682SMatthew G Knepley   Output Parameter:
1440a89ea682SMatthew G Knepley . array - the work array
1441a89ea682SMatthew G Knepley 
1442a89ea682SMatthew G Knepley   Level: developer
1443a89ea682SMatthew G Knepley 
1444a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1445a89ea682SMatthew G Knepley @*/
144669291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1447a89ea682SMatthew G Knepley {
1448a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1449aa1993deSMatthew G Knepley   DMWorkLink     link;
145069291d52SBarry Smith   PetscMPIInt    dsize;
1451a89ea682SMatthew G Knepley 
1452a89ea682SMatthew G Knepley   PetscFunctionBegin;
1453a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1454aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1455aa1993deSMatthew G Knepley   if (dm->workin) {
1456aa1993deSMatthew G Knepley     link       = dm->workin;
1457aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1458aa1993deSMatthew G Knepley   } else {
1459b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1460a89ea682SMatthew G Knepley   }
146169291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14625056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1463aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1464854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1465854ce69bSBarry Smith     link->bytes = dsize*count;
1466aa1993deSMatthew G Knepley   }
1467aa1993deSMatthew G Knepley   link->next   = dm->workout;
1468aa1993deSMatthew G Knepley   dm->workout  = link;
1469aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1470a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1471a89ea682SMatthew G Knepley }
1472a89ea682SMatthew G Knepley 
1473aa1993deSMatthew G Knepley /*@C
1474aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1475aa1993deSMatthew G Knepley 
1476aa1993deSMatthew G Knepley   Not Collective
1477aa1993deSMatthew G Knepley 
1478aa1993deSMatthew G Knepley   Input Parameters:
1479aa1993deSMatthew G Knepley + dm - the DM object
1480aa1993deSMatthew G Knepley . count - The minium size
148169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1482aa1993deSMatthew G Knepley 
1483aa1993deSMatthew G Knepley   Output Parameter:
1484aa1993deSMatthew G Knepley . array - the work array
1485aa1993deSMatthew G Knepley 
1486aa1993deSMatthew G Knepley   Level: developer
1487aa1993deSMatthew G Knepley 
148895452b02SPatrick Sanan   Developer Notes:
148995452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1490aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1491aa1993deSMatthew G Knepley @*/
149269291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1493aa1993deSMatthew G Knepley {
1494aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1495aa1993deSMatthew G Knepley 
1496aa1993deSMatthew G Knepley   PetscFunctionBegin;
1497aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1498aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1499aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1500aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1501aa1993deSMatthew G Knepley       *p           = link->next;
1502aa1993deSMatthew G Knepley       link->next   = dm->workin;
1503aa1993deSMatthew G Knepley       dm->workin   = link;
15040298fd71SBarry Smith       *(void**)mem = NULL;
1505aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1506aa1993deSMatthew G Knepley     }
1507aa1993deSMatthew G Knepley   }
1508aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1509aa1993deSMatthew G Knepley }
1510e7c4fc90SDmitry Karpeev 
1511435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1512435a35e8SMatthew G Knepley {
1513435a35e8SMatthew G Knepley   PetscFunctionBegin;
1514435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
151582f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1516435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1517435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1518435a35e8SMatthew G Knepley }
1519435a35e8SMatthew G Knepley 
15200a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
15210a50eb56SMatthew G. Knepley {
15220a50eb56SMatthew G. Knepley   PetscFunctionBegin;
15230a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1524f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
15250a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15260a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15270a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15280a50eb56SMatthew G. Knepley }
15290a50eb56SMatthew G. Knepley 
1530f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1531f9d4088aSMatthew G. Knepley {
1532f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1533f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1534f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1535f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1536f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1537f9d4088aSMatthew G. Knepley }
1538f9d4088aSMatthew G. Knepley 
1539f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1540f9d4088aSMatthew G. Knepley {
1541f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1542f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1543f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1544f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1545f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1546f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1547f9d4088aSMatthew G. Knepley }
1548f9d4088aSMatthew G. Knepley 
15494f3b5142SJed Brown /*@C
15504d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
15514d343eeaSMatthew G Knepley 
15524d343eeaSMatthew G Knepley   Not collective
15534d343eeaSMatthew G Knepley 
15544d343eeaSMatthew G Knepley   Input Parameter:
15554d343eeaSMatthew G Knepley . dm - the DM object
15564d343eeaSMatthew G Knepley 
15574d343eeaSMatthew G Knepley   Output Parameters:
15580298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
15590298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
15600298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
15614d343eeaSMatthew G Knepley 
15624d343eeaSMatthew G Knepley   Level: intermediate
15634d343eeaSMatthew G Knepley 
156421c9b008SJed Brown   Notes:
156521c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
156621c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
156721c9b008SJed Brown   PetscFree().
156821c9b008SJed Brown 
15694d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
15704d343eeaSMatthew G Knepley @*/
157137d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
15724d343eeaSMatthew G Knepley {
157337d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
15744d343eeaSMatthew G Knepley   PetscErrorCode ierr;
15754d343eeaSMatthew G Knepley 
15764d343eeaSMatthew G Knepley   PetscFunctionBegin;
15774d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
157869ca1f37SDmitry Karpeev   if (numFields) {
1579534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
158069ca1f37SDmitry Karpeev     *numFields = 0;
158169ca1f37SDmitry Karpeev   }
158237d0c07bSMatthew G Knepley   if (fieldNames) {
158337d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15840298fd71SBarry Smith     *fieldNames = NULL;
158569ca1f37SDmitry Karpeev   }
158669ca1f37SDmitry Karpeev   if (fields) {
158769ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15880298fd71SBarry Smith     *fields = NULL;
158969ca1f37SDmitry Karpeev   }
159092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
159137d0c07bSMatthew G Knepley   if (section) {
15923a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
159337d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
159437d0c07bSMatthew G Knepley 
1595e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
159637d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15973a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
159837d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
159937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
160037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
16013a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
160237d0c07bSMatthew G Knepley     }
160337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
160437d0c07bSMatthew G Knepley       PetscInt gdof;
160537d0c07bSMatthew G Knepley 
160637d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
160737d0c07bSMatthew G Knepley       if (gdof > 0) {
160837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
16093a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
161037d0c07bSMatthew G Knepley 
161137d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
161237d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
16133a544194SStefano Zampini           fpdof = fdof-fcdof;
16143a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
16153a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
16163a544194SStefano Zampini             fieldNc[f] = 1;
16173a544194SStefano Zampini           }
16183a544194SStefano Zampini           fieldSizes[f] += fpdof;
161937d0c07bSMatthew G Knepley         }
162037d0c07bSMatthew G Knepley       }
162137d0c07bSMatthew G Knepley     }
162237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1623785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
162437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
162537d0c07bSMatthew G Knepley     }
162637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
162737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
162837d0c07bSMatthew G Knepley 
162937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
163037d0c07bSMatthew G Knepley       if (gdof > 0) {
163137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
163237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
163337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
163437d0c07bSMatthew G Knepley 
163537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
163637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
163737d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
163837d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
163937d0c07bSMatthew G Knepley           }
164037d0c07bSMatthew G Knepley         }
164137d0c07bSMatthew G Knepley       }
164237d0c07bSMatthew G Knepley     }
16438865f1eaSKarl Rupp     if (numFields) *numFields = nF;
164437d0c07bSMatthew G Knepley     if (fieldNames) {
1645785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
164637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
164737d0c07bSMatthew G Knepley         const char *fieldName;
164837d0c07bSMatthew G Knepley 
164937d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
165037d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
165137d0c07bSMatthew G Knepley       }
165237d0c07bSMatthew G Knepley     }
165337d0c07bSMatthew G Knepley     if (fields) {
1654785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
165537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
16563a544194SStefano Zampini         PetscInt bs, in[2], out[2];
16573a544194SStefano Zampini 
165882f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
16593a544194SStefano Zampini         in[0] = -fieldNc[f];
16603a544194SStefano Zampini         in[1] = fieldNc[f];
16613a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
16623a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
16633a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
166437d0c07bSMatthew G Knepley       }
166537d0c07bSMatthew G Knepley     }
16663a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
16678865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
16688865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
166969ca1f37SDmitry Karpeev   }
16704d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
16714d343eeaSMatthew G Knepley }
16724d343eeaSMatthew G Knepley 
167316621825SDmitry Karpeev 
167416621825SDmitry Karpeev /*@C
167516621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
167616621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
167716621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1678e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1679e7c4fc90SDmitry Karpeev 
1680e7c4fc90SDmitry Karpeev   Not collective
1681e7c4fc90SDmitry Karpeev 
1682e7c4fc90SDmitry Karpeev   Input Parameter:
1683e7c4fc90SDmitry Karpeev . dm - the DM object
1684e7c4fc90SDmitry Karpeev 
1685e7c4fc90SDmitry Karpeev   Output Parameters:
16860298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16870298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16880298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16890298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1690e7c4fc90SDmitry Karpeev 
1691e7c4fc90SDmitry Karpeev   Level: intermediate
1692e7c4fc90SDmitry Karpeev 
1693e7c4fc90SDmitry Karpeev   Notes:
1694e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1695e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1696e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1697e7c4fc90SDmitry Karpeev 
1698e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1699e7c4fc90SDmitry Karpeev @*/
170016621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1701e7c4fc90SDmitry Karpeev {
1702e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1703e7c4fc90SDmitry Karpeev 
1704e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1705e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17068865f1eaSKarl Rupp   if (len) {
1707534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
17088865f1eaSKarl Rupp     *len = 0;
17098865f1eaSKarl Rupp   }
17108865f1eaSKarl Rupp   if (namelist) {
17118865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
17128865f1eaSKarl Rupp     *namelist = 0;
17138865f1eaSKarl Rupp   }
17148865f1eaSKarl Rupp   if (islist) {
17158865f1eaSKarl Rupp     PetscValidPointer(islist,4);
17168865f1eaSKarl Rupp     *islist = 0;
17178865f1eaSKarl Rupp   }
17188865f1eaSKarl Rupp   if (dmlist) {
17198865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
17208865f1eaSKarl Rupp     *dmlist = 0;
17218865f1eaSKarl Rupp   }
1722f3f0edfdSDmitry Karpeev   /*
1723f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1724f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1725f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1726f3f0edfdSDmitry Karpeev    */
1727ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
172816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1729435a35e8SMatthew G Knepley     PetscSection section;
1730435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1731435a35e8SMatthew G Knepley 
173292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1733435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1734435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1735f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
173603dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
173703dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
173803dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1739435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1740435a35e8SMatthew G Knepley         const char *fieldName;
1741435a35e8SMatthew G Knepley 
174203dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
174303dc3394SMatthew G. Knepley         if (namelist) {
1744435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1745435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1746435a35e8SMatthew G Knepley         }
174703dc3394SMatthew G. Knepley       }
1748435a35e8SMatthew G Knepley     } else {
174969ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1750e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
17510298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1752e7c4fc90SDmitry Karpeev     }
17538865f1eaSKarl Rupp   } else {
175416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
175516621825SDmitry Karpeev   }
175616621825SDmitry Karpeev   PetscFunctionReturn(0);
175716621825SDmitry Karpeev }
175816621825SDmitry Karpeev 
1759564cec59SMatthew G. Knepley /*@
1760435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1761435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1762435a35e8SMatthew G Knepley 
1763435a35e8SMatthew G Knepley   Not collective
1764435a35e8SMatthew G Knepley 
1765435a35e8SMatthew G Knepley   Input Parameters:
17662adcc780SMatthew G. Knepley + dm        - The DM object
17672adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
17682adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1769435a35e8SMatthew G Knepley 
1770435a35e8SMatthew G Knepley   Output Parameters:
17712adcc780SMatthew G. Knepley + is - The global indices for the subproblem
17722adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1773435a35e8SMatthew G Knepley 
17745d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17755d3b26e6SMatthew G. Knepley 
1776435a35e8SMatthew G Knepley   Level: intermediate
1777435a35e8SMatthew G Knepley 
17785d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1779435a35e8SMatthew G Knepley @*/
178037bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1781435a35e8SMatthew G Knepley {
1782435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1783435a35e8SMatthew G Knepley 
1784435a35e8SMatthew G Knepley   PetscFunctionBegin;
1785435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1786435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17878865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17888865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1789b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1790435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1791435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1792435a35e8SMatthew G Knepley }
1793435a35e8SMatthew G Knepley 
17942adcc780SMatthew G. Knepley /*@C
17952adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17962adcc780SMatthew G. Knepley 
17972adcc780SMatthew G. Knepley   Not collective
17982adcc780SMatthew G. Knepley 
17992adcc780SMatthew G. Knepley   Input Parameter:
18002adcc780SMatthew G. Knepley + dms - The DM objects
18012adcc780SMatthew G. Knepley - len - The number of DMs
18022adcc780SMatthew G. Knepley 
18032adcc780SMatthew G. Knepley   Output Parameters:
1804a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
18052adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
18062adcc780SMatthew G. Knepley 
18075d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
18085d3b26e6SMatthew G. Knepley 
18092adcc780SMatthew G. Knepley   Level: intermediate
18102adcc780SMatthew G. Knepley 
18115d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
18122adcc780SMatthew G. Knepley @*/
18132adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
18142adcc780SMatthew G. Knepley {
18152adcc780SMatthew G. Knepley   PetscInt       i;
18162adcc780SMatthew G. Knepley   PetscErrorCode ierr;
18172adcc780SMatthew G. Knepley 
18182adcc780SMatthew G. Knepley   PetscFunctionBegin;
18192adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
18202adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
18212adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1822a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
18232adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
18242adcc780SMatthew G. Knepley   if (len) {
1825b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1826b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1827b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
18282adcc780SMatthew G. Knepley   }
18292adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
18302adcc780SMatthew G. Knepley }
18312adcc780SMatthew G. Knepley 
183216621825SDmitry Karpeev 
183316621825SDmitry Karpeev /*@C
18348d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
18358d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
18368d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
18378d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
18388d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
183916621825SDmitry Karpeev 
184016621825SDmitry Karpeev   Not collective
184116621825SDmitry Karpeev 
184216621825SDmitry Karpeev   Input Parameter:
184316621825SDmitry Karpeev . dm - the DM object
184416621825SDmitry Karpeev 
184516621825SDmitry Karpeev   Output Parameters:
18460298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
18470298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
18480298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
18490298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
18500298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
185116621825SDmitry Karpeev 
185216621825SDmitry Karpeev   Level: intermediate
185316621825SDmitry Karpeev 
185416621825SDmitry Karpeev   Notes:
185516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
185616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
185716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
185816621825SDmitry Karpeev 
1859245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
186016621825SDmitry Karpeev @*/
18618d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
186216621825SDmitry Karpeev {
186316621825SDmitry Karpeev   PetscErrorCode      ierr;
1864be081cd6SPeter Brune   DMSubDomainHookLink link;
1865be081cd6SPeter Brune   PetscInt            i,l;
186616621825SDmitry Karpeev 
186716621825SDmitry Karpeev   PetscFunctionBegin;
186816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
186914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
18700298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
18710298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
18720298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
18730298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1874f3f0edfdSDmitry Karpeev   /*
1875f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1876f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1877f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1878f3f0edfdSDmitry Karpeev    */
1879ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
188016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1881be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
188214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1883f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1884be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1885be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1886be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1887be081cd6SPeter Brune         }
1888648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1889e7c4fc90SDmitry Karpeev       }
189014a18fd3SPeter Brune     }
189114a18fd3SPeter Brune     if (len) *len = l;
189214a18fd3SPeter Brune   }
1893e30e807fSPeter Brune   PetscFunctionReturn(0);
1894e30e807fSPeter Brune }
1895e30e807fSPeter Brune 
1896e30e807fSPeter Brune 
1897e30e807fSPeter Brune /*@C
1898e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1899e30e807fSPeter Brune 
1900e30e807fSPeter Brune   Not collective
1901e30e807fSPeter Brune 
1902e30e807fSPeter Brune   Input Parameters:
1903e30e807fSPeter Brune + dm - the DM object
1904e30e807fSPeter Brune . n  - the number of subdomain scatters
1905e30e807fSPeter Brune - subdms - the local subdomains
1906e30e807fSPeter Brune 
1907e30e807fSPeter Brune   Output Parameters:
1908e30e807fSPeter Brune + n     - the number of scatters returned
1909e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1910e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1911e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1912e30e807fSPeter Brune 
191395452b02SPatrick Sanan   Notes:
191495452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1915e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1916e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1917e30e807fSPeter Brune   solution and residual data.
1918e30e807fSPeter Brune 
1919e30e807fSPeter Brune   Level: developer
1920e30e807fSPeter Brune 
1921e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1922e30e807fSPeter Brune @*/
1923e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1924e30e807fSPeter Brune {
1925e30e807fSPeter Brune   PetscErrorCode ierr;
1926e30e807fSPeter Brune 
1927e30e807fSPeter Brune   PetscFunctionBegin;
1928e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1929e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1930b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1931e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1932e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1933e7c4fc90SDmitry Karpeev }
1934e7c4fc90SDmitry Karpeev 
193547c6ae99SBarry Smith /*@
193647c6ae99SBarry Smith   DMRefine - Refines a DM object
193747c6ae99SBarry Smith 
1938d083f849SBarry Smith   Collective on dm
193947c6ae99SBarry Smith 
194047c6ae99SBarry Smith   Input Parameter:
194147c6ae99SBarry Smith + dm   - the DM object
194291d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
194347c6ae99SBarry Smith 
194447c6ae99SBarry Smith   Output Parameter:
19450298fd71SBarry Smith . dmf - the refined DM, or NULL
1946ae0a1c52SMatthew G Knepley 
1947412e9a14SMatthew G. Knepley   Options Dtabase Keys:
1948412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
1949412e9a14SMatthew G. Knepley 
19500298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
195147c6ae99SBarry Smith 
195247c6ae99SBarry Smith   Level: developer
195347c6ae99SBarry Smith 
1954e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
195547c6ae99SBarry Smith @*/
19567087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
195747c6ae99SBarry Smith {
195847c6ae99SBarry Smith   PetscErrorCode   ierr;
1959c833c3b5SJed Brown   DMRefineHookLink link;
196047c6ae99SBarry Smith 
196147c6ae99SBarry Smith   PetscFunctionBegin;
1962732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1963b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
19641ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
196547c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
19664057135bSMatthew G Knepley   if (*dmf) {
196743842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
19688865f1eaSKarl Rupp 
19698cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
19708865f1eaSKarl Rupp 
1971644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
19720598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1973656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
19748865f1eaSKarl Rupp 
1975e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1976c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
19778865f1eaSKarl Rupp       if (link->refinehook) {
19788865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19798865f1eaSKarl Rupp       }
1980c833c3b5SJed Brown     }
1981c833c3b5SJed Brown   }
19821ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1983c833c3b5SJed Brown   PetscFunctionReturn(0);
1984c833c3b5SJed Brown }
1985c833c3b5SJed Brown 
1986bb9467b5SJed Brown /*@C
1987c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1988c833c3b5SJed Brown 
1989c833c3b5SJed Brown    Logically Collective
1990c833c3b5SJed Brown 
1991c833c3b5SJed Brown    Input Arguments:
1992c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1993c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1994c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19950298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1996c833c3b5SJed Brown 
1997c833c3b5SJed Brown    Calling sequence of refinehook:
1998c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1999c833c3b5SJed Brown 
2000c833c3b5SJed Brown +  coarse - coarse level DM
2001c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2002c833c3b5SJed Brown -  ctx - optional user-defined function context
2003c833c3b5SJed Brown 
2004c833c3b5SJed Brown    Calling sequence for interphook:
2005c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2006c833c3b5SJed Brown 
2007c833c3b5SJed Brown +  coarse - coarse level DM
2008c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2009c833c3b5SJed Brown .  fine - fine level DM to update
2010c833c3b5SJed Brown -  ctx - optional user-defined function context
2011c833c3b5SJed Brown 
2012c833c3b5SJed Brown    Level: advanced
2013c833c3b5SJed Brown 
2014c833c3b5SJed Brown    Notes:
2015c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2016c833c3b5SJed Brown 
2017c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2018c833c3b5SJed Brown 
2019bb9467b5SJed Brown    This function is currently not available from Fortran.
2020bb9467b5SJed Brown 
2021c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2022c833c3b5SJed Brown @*/
2023c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2024c833c3b5SJed Brown {
2025c833c3b5SJed Brown   PetscErrorCode   ierr;
2026c833c3b5SJed Brown   DMRefineHookLink link,*p;
2027c833c3b5SJed Brown 
2028c833c3b5SJed Brown   PetscFunctionBegin;
2029c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20303d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
20313d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
20323d8e3701SJed Brown   }
203395dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2034c833c3b5SJed Brown   link->refinehook = refinehook;
2035c833c3b5SJed Brown   link->interphook = interphook;
2036c833c3b5SJed Brown   link->ctx        = ctx;
20370298fd71SBarry Smith   link->next       = NULL;
2038c833c3b5SJed Brown   *p               = link;
2039c833c3b5SJed Brown   PetscFunctionReturn(0);
2040c833c3b5SJed Brown }
2041c833c3b5SJed Brown 
20423d8e3701SJed Brown /*@C
20433d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
20443d8e3701SJed Brown 
20453d8e3701SJed Brown    Logically Collective
20463d8e3701SJed Brown 
20473d8e3701SJed Brown    Input Arguments:
20483d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
20493d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
20503d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20513d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20523d8e3701SJed Brown 
20533d8e3701SJed Brown    Level: advanced
20543d8e3701SJed Brown 
20553d8e3701SJed Brown    Notes:
20563d8e3701SJed Brown    This function does nothing if the hook is not in the list.
20573d8e3701SJed Brown 
20583d8e3701SJed Brown    This function is currently not available from Fortran.
20593d8e3701SJed Brown 
20603d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20613d8e3701SJed Brown @*/
20623d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
20633d8e3701SJed Brown {
20643d8e3701SJed Brown   PetscErrorCode   ierr;
20653d8e3701SJed Brown   DMRefineHookLink link,*p;
20663d8e3701SJed Brown 
20673d8e3701SJed Brown   PetscFunctionBegin;
20683d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20693d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
20703d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
20713d8e3701SJed Brown       link = *p;
20723d8e3701SJed Brown       *p = link->next;
20733d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
20743d8e3701SJed Brown       break;
20753d8e3701SJed Brown     }
20763d8e3701SJed Brown   }
20773d8e3701SJed Brown   PetscFunctionReturn(0);
20783d8e3701SJed Brown }
20793d8e3701SJed Brown 
2080c833c3b5SJed Brown /*@
2081c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2082c833c3b5SJed Brown 
2083c833c3b5SJed Brown    Collective if any hooks are
2084c833c3b5SJed Brown 
2085c833c3b5SJed Brown    Input Arguments:
2086c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2087e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2088c833c3b5SJed Brown -  fine - finer DM to update
2089c833c3b5SJed Brown 
2090c833c3b5SJed Brown    Level: developer
2091c833c3b5SJed Brown 
2092c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2093c833c3b5SJed Brown @*/
2094c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2095c833c3b5SJed Brown {
2096c833c3b5SJed Brown   PetscErrorCode   ierr;
2097c833c3b5SJed Brown   DMRefineHookLink link;
2098c833c3b5SJed Brown 
2099c833c3b5SJed Brown   PetscFunctionBegin;
2100c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
21018865f1eaSKarl Rupp     if (link->interphook) {
21028865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
21038865f1eaSKarl Rupp     }
21044057135bSMatthew G Knepley   }
210547c6ae99SBarry Smith   PetscFunctionReturn(0);
210647c6ae99SBarry Smith }
210747c6ae99SBarry Smith 
2108eb3f98d2SBarry Smith /*@
2109eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2110eb3f98d2SBarry Smith 
2111eb3f98d2SBarry Smith     Not Collective
2112eb3f98d2SBarry Smith 
2113eb3f98d2SBarry Smith     Input Parameter:
2114eb3f98d2SBarry Smith .   dm - the DM object
2115eb3f98d2SBarry Smith 
2116eb3f98d2SBarry Smith     Output Parameter:
2117eb3f98d2SBarry Smith .   level - number of refinements
2118eb3f98d2SBarry Smith 
2119eb3f98d2SBarry Smith     Level: developer
2120eb3f98d2SBarry Smith 
21216a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2122eb3f98d2SBarry Smith 
2123eb3f98d2SBarry Smith @*/
2124eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2125eb3f98d2SBarry Smith {
2126eb3f98d2SBarry Smith   PetscFunctionBegin;
2127eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2128eb3f98d2SBarry Smith   *level = dm->levelup;
2129eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2130eb3f98d2SBarry Smith }
2131eb3f98d2SBarry Smith 
2132fef3a512SBarry Smith /*@
2133fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2134fef3a512SBarry Smith 
2135fef3a512SBarry Smith     Not Collective
2136fef3a512SBarry Smith 
2137fef3a512SBarry Smith     Input Parameter:
2138fef3a512SBarry Smith +   dm - the DM object
2139fef3a512SBarry Smith -   level - number of refinements
2140fef3a512SBarry Smith 
2141fef3a512SBarry Smith     Level: advanced
2142fef3a512SBarry Smith 
214395452b02SPatrick Sanan     Notes:
214495452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2145fef3a512SBarry Smith 
2146fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2147fef3a512SBarry Smith 
2148fef3a512SBarry Smith @*/
2149fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2150fef3a512SBarry Smith {
2151fef3a512SBarry Smith   PetscFunctionBegin;
2152fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2153fef3a512SBarry Smith   dm->levelup = level;
2154fef3a512SBarry Smith   PetscFunctionReturn(0);
2155fef3a512SBarry Smith }
2156fef3a512SBarry Smith 
2157ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2158ca3d3a14SMatthew G. Knepley {
2159ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2160ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2161ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2162ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2163ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2164ca3d3a14SMatthew G. Knepley }
2165ca3d3a14SMatthew G. Knepley 
2166ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2167ca3d3a14SMatthew G. Knepley {
2168ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2169ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2170ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2171ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2172ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2173ca3d3a14SMatthew G. Knepley }
2174ca3d3a14SMatthew G. Knepley 
2175ca3d3a14SMatthew G. Knepley /*@
2176c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2177ca3d3a14SMatthew G. Knepley 
2178ca3d3a14SMatthew G. Knepley   Input Parameter:
2179ca3d3a14SMatthew G. Knepley . dm - The DM
2180ca3d3a14SMatthew G. Knepley 
2181ca3d3a14SMatthew G. Knepley   Output Parameter:
2182ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2183ca3d3a14SMatthew G. Knepley 
2184ca3d3a14SMatthew G. Knepley   Level: developer
2185ca3d3a14SMatthew G. Knepley 
2186ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2187ca3d3a14SMatthew G. Knepley @*/
2188ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2189ca3d3a14SMatthew G. Knepley {
2190ca3d3a14SMatthew G. Knepley   Vec            tv;
2191ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2192ca3d3a14SMatthew G. Knepley 
2193ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2194ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2195534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2196ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2197ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2198ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2199ca3d3a14SMatthew G. Knepley }
2200ca3d3a14SMatthew G. Knepley 
2201ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2202ca3d3a14SMatthew G. Knepley {
2203ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2204ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2205ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2206ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2207ca3d3a14SMatthew G. Knepley 
2208ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2209ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
221092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2211ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2212ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2213ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
221492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2215ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2216ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2217ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2218ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2219ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2220ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2221ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2222ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2223ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2224ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2225ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2226ca3d3a14SMatthew G. Knepley     }
2227ca3d3a14SMatthew G. Knepley   }
2228ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2229ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2230ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2231ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2232ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2233ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2234ca3d3a14SMatthew G. Knepley       if (dof) {
2235ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2236ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2237ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2238ca3d3a14SMatthew G. Knepley 
2239ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2240ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2241ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2242580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2243ca3d3a14SMatthew G. Knepley       }
2244ca3d3a14SMatthew G. Knepley     }
2245ca3d3a14SMatthew G. Knepley   }
2246ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2247ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2248ca3d3a14SMatthew G. Knepley }
2249ca3d3a14SMatthew G. Knepley 
2250ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2251ca3d3a14SMatthew G. Knepley {
2252ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2253ca3d3a14SMatthew G. Knepley 
2254ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2255ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2256ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2257ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2258ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2259ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2260ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2261ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2262ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2263ca3d3a14SMatthew G. Knepley }
2264ca3d3a14SMatthew G. Knepley 
2265bb9467b5SJed Brown /*@C
2266baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2267baf369e7SPeter Brune 
2268baf369e7SPeter Brune    Logically Collective
2269baf369e7SPeter Brune 
2270baf369e7SPeter Brune    Input Arguments:
2271baf369e7SPeter Brune +  dm - the DM
2272baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2273baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
22740298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2275baf369e7SPeter Brune 
2276baf369e7SPeter Brune    Calling sequence for beginhook:
2277baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2278baf369e7SPeter Brune 
2279baf369e7SPeter Brune +  dm - global DM
2280baf369e7SPeter Brune .  g - global vector
2281baf369e7SPeter Brune .  mode - mode
2282baf369e7SPeter Brune .  l - local vector
2283baf369e7SPeter Brune -  ctx - optional user-defined function context
2284baf369e7SPeter Brune 
2285baf369e7SPeter Brune 
2286baf369e7SPeter Brune    Calling sequence for endhook:
2287ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2288baf369e7SPeter Brune 
2289baf369e7SPeter Brune +  global - global DM
2290baf369e7SPeter Brune -  ctx - optional user-defined function context
2291baf369e7SPeter Brune 
2292baf369e7SPeter Brune    Level: advanced
2293baf369e7SPeter Brune 
2294baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2295baf369e7SPeter Brune @*/
2296baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2297baf369e7SPeter Brune {
2298baf369e7SPeter Brune   PetscErrorCode          ierr;
2299baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2300baf369e7SPeter Brune 
2301baf369e7SPeter Brune   PetscFunctionBegin;
2302baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2303baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
230495dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2305baf369e7SPeter Brune   link->beginhook = beginhook;
2306baf369e7SPeter Brune   link->endhook   = endhook;
2307baf369e7SPeter Brune   link->ctx       = ctx;
23080298fd71SBarry Smith   link->next      = NULL;
2309baf369e7SPeter Brune   *p              = link;
2310baf369e7SPeter Brune   PetscFunctionReturn(0);
2311baf369e7SPeter Brune }
2312baf369e7SPeter Brune 
23134c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
23144c274da1SToby Isaac {
23154c274da1SToby Isaac   Mat cMat;
23164c274da1SToby Isaac   Vec cVec;
23174c274da1SToby Isaac   PetscSection section, cSec;
23184c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
23194c274da1SToby Isaac   PetscErrorCode ierr;
23204c274da1SToby Isaac 
23214c274da1SToby Isaac   PetscFunctionBegin;
23224c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23234c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23244c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
23255db9a05bSToby Isaac     PetscInt nRows;
23265db9a05bSToby Isaac 
23275db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23285db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
232992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
23307711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23314c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
23324c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23334c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23344c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23354c274da1SToby Isaac       if (dof) {
23364c274da1SToby Isaac         PetscScalar *vals;
23374c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
23384c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
23394c274da1SToby Isaac       }
23404c274da1SToby Isaac     }
23414c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23424c274da1SToby Isaac   }
23434c274da1SToby Isaac   PetscFunctionReturn(0);
23444c274da1SToby Isaac }
23454c274da1SToby Isaac 
234647c6ae99SBarry Smith /*@
234701729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
234801729b5cSPatrick Sanan 
2349d083f849SBarry Smith     Neighbor-wise Collective on dm
235001729b5cSPatrick Sanan 
235101729b5cSPatrick Sanan     Input Parameters:
235201729b5cSPatrick Sanan +   dm - the DM object
235301729b5cSPatrick Sanan .   g - the global vector
235401729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
235501729b5cSPatrick Sanan -   l - the local vector
235601729b5cSPatrick Sanan 
235701729b5cSPatrick Sanan     Notes:
235801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
235901729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
236001729b5cSPatrick Sanan 
236101729b5cSPatrick Sanan     Level: beginner
236201729b5cSPatrick Sanan 
236301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
236401729b5cSPatrick Sanan 
236501729b5cSPatrick Sanan @*/
236601729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
236701729b5cSPatrick Sanan {
236801729b5cSPatrick Sanan   PetscErrorCode ierr;
236901729b5cSPatrick Sanan 
237001729b5cSPatrick Sanan   PetscFunctionBegin;
237101729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
237201729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
237301729b5cSPatrick Sanan   PetscFunctionReturn(0);
237401729b5cSPatrick Sanan }
237501729b5cSPatrick Sanan 
237601729b5cSPatrick Sanan /*@
237747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
237847c6ae99SBarry Smith 
2379d083f849SBarry Smith     Neighbor-wise Collective on dm
238047c6ae99SBarry Smith 
238147c6ae99SBarry Smith     Input Parameters:
238247c6ae99SBarry Smith +   dm - the DM object
238347c6ae99SBarry Smith .   g - the global vector
238447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
238547c6ae99SBarry Smith -   l - the local vector
238647c6ae99SBarry Smith 
238701729b5cSPatrick Sanan     Level: intermediate
238847c6ae99SBarry Smith 
238901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
239047c6ae99SBarry Smith 
239147c6ae99SBarry Smith @*/
23927087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
239347c6ae99SBarry Smith {
23947128ae9fSMatthew G Knepley   PetscSF                 sf;
239547c6ae99SBarry Smith   PetscErrorCode          ierr;
2396baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
239747c6ae99SBarry Smith 
239847c6ae99SBarry Smith   PetscFunctionBegin;
2399171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2400baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
24018865f1eaSKarl Rupp     if (link->beginhook) {
24028865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
24038865f1eaSKarl Rupp     }
2404baf369e7SPeter Brune   }
24051bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
24067128ae9fSMatthew G Knepley   if (sf) {
2407ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2408ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
24097128ae9fSMatthew G Knepley 
241082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2411*fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2412*fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24137128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2414*fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2415*fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24167128ae9fSMatthew G Knepley   } else {
241733907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2418843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24197128ae9fSMatthew G Knepley   }
242047c6ae99SBarry Smith   PetscFunctionReturn(0);
242147c6ae99SBarry Smith }
242247c6ae99SBarry Smith 
242347c6ae99SBarry Smith /*@
242447c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
242547c6ae99SBarry Smith 
2426d083f849SBarry Smith     Neighbor-wise Collective on dm
242747c6ae99SBarry Smith 
242847c6ae99SBarry Smith     Input Parameters:
242947c6ae99SBarry Smith +   dm - the DM object
243047c6ae99SBarry Smith .   g - the global vector
243147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
243247c6ae99SBarry Smith -   l - the local vector
243347c6ae99SBarry Smith 
243401729b5cSPatrick Sanan     Level: intermediate
243547c6ae99SBarry Smith 
243601729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
243747c6ae99SBarry Smith 
243847c6ae99SBarry Smith @*/
24397087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
244047c6ae99SBarry Smith {
24417128ae9fSMatthew G Knepley   PetscSF                 sf;
244247c6ae99SBarry Smith   PetscErrorCode          ierr;
2443ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2444ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2445ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2446baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
244747c6ae99SBarry Smith 
244847c6ae99SBarry Smith   PetscFunctionBegin;
2449171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24501bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2451ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
24527128ae9fSMatthew G Knepley   if (sf) {
245382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24547128ae9fSMatthew G Knepley 
2455*fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2456*fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24577128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2458*fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2459*fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
2460ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
24617128ae9fSMatthew G Knepley   } else {
246233907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2463843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24647128ae9fSMatthew G Knepley   }
24654c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2466baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2467baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2468baf369e7SPeter Brune   }
246947c6ae99SBarry Smith   PetscFunctionReturn(0);
247047c6ae99SBarry Smith }
247147c6ae99SBarry Smith 
2472d4d07f1eSToby Isaac /*@C
2473d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2474d4d07f1eSToby Isaac 
2475d4d07f1eSToby Isaac    Logically Collective
2476d4d07f1eSToby Isaac 
2477d4d07f1eSToby Isaac    Input Arguments:
2478d4d07f1eSToby Isaac +  dm - the DM
2479d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2480d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2481d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2482d4d07f1eSToby Isaac 
2483d4d07f1eSToby Isaac    Calling sequence for beginhook:
2484d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2485d4d07f1eSToby Isaac 
2486d4d07f1eSToby Isaac +  dm - global DM
2487d4d07f1eSToby Isaac .  l - local vector
2488d4d07f1eSToby Isaac .  mode - mode
2489d4d07f1eSToby Isaac .  g - global vector
2490d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2491d4d07f1eSToby Isaac 
2492d4d07f1eSToby Isaac 
2493d4d07f1eSToby Isaac    Calling sequence for endhook:
2494d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2495d4d07f1eSToby Isaac 
2496d4d07f1eSToby Isaac +  global - global DM
2497d4d07f1eSToby Isaac .  l - local vector
2498d4d07f1eSToby Isaac .  mode - mode
2499d4d07f1eSToby Isaac .  g - global vector
2500d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2501d4d07f1eSToby Isaac 
2502d4d07f1eSToby Isaac    Level: advanced
2503d4d07f1eSToby Isaac 
2504d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2505d4d07f1eSToby Isaac @*/
2506d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2507d4d07f1eSToby Isaac {
2508d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2509d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2510d4d07f1eSToby Isaac 
2511d4d07f1eSToby Isaac   PetscFunctionBegin;
2512d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2513d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
251495dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2515d4d07f1eSToby Isaac   link->beginhook = beginhook;
2516d4d07f1eSToby Isaac   link->endhook   = endhook;
2517d4d07f1eSToby Isaac   link->ctx       = ctx;
2518d4d07f1eSToby Isaac   link->next      = NULL;
2519d4d07f1eSToby Isaac   *p              = link;
2520d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2521d4d07f1eSToby Isaac }
2522d4d07f1eSToby Isaac 
25234c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
25244c274da1SToby Isaac {
25254c274da1SToby Isaac   Mat cMat;
25264c274da1SToby Isaac   Vec cVec;
25274c274da1SToby Isaac   PetscSection section, cSec;
25284c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25294c274da1SToby Isaac   PetscErrorCode ierr;
25304c274da1SToby Isaac 
25314c274da1SToby Isaac   PetscFunctionBegin;
25324c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25334c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25344c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
25355db9a05bSToby Isaac     PetscInt nRows;
25365db9a05bSToby Isaac 
25375db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25385db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
253992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25407711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25414c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25424c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25434c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25444c274da1SToby Isaac       if (dof) {
25454c274da1SToby Isaac         PetscInt d;
25464c274da1SToby Isaac         PetscScalar *vals;
25474c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
25484c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
25494c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
25504c274da1SToby Isaac          * we just extracted */
25514c274da1SToby Isaac         for (d = 0; d < dof; d++) {
25524c274da1SToby Isaac           vals[d] = 0.;
25534c274da1SToby Isaac         }
25544c274da1SToby Isaac       }
25554c274da1SToby Isaac     }
25564c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
25574c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25584c274da1SToby Isaac   }
25594c274da1SToby Isaac   PetscFunctionReturn(0);
25604c274da1SToby Isaac }
256101729b5cSPatrick Sanan /*@
256201729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
256301729b5cSPatrick Sanan 
2564d083f849SBarry Smith     Neighbor-wise Collective on dm
256501729b5cSPatrick Sanan 
256601729b5cSPatrick Sanan     Input Parameters:
256701729b5cSPatrick Sanan +   dm - the DM object
256801729b5cSPatrick Sanan .   l - the local vector
256901729b5cSPatrick 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.
257001729b5cSPatrick Sanan -   g - the global vector
257101729b5cSPatrick Sanan 
257201729b5cSPatrick Sanan     Notes:
257301729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
257401729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
257501729b5cSPatrick Sanan 
257601729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
257701729b5cSPatrick 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.
257801729b5cSPatrick Sanan 
257901729b5cSPatrick Sanan     Level: beginner
258001729b5cSPatrick Sanan 
258101729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
258201729b5cSPatrick Sanan 
258301729b5cSPatrick Sanan @*/
258401729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
258501729b5cSPatrick Sanan {
258601729b5cSPatrick Sanan   PetscErrorCode ierr;
258701729b5cSPatrick Sanan 
258801729b5cSPatrick Sanan   PetscFunctionBegin;
258901729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
259001729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
259101729b5cSPatrick Sanan   PetscFunctionReturn(0);
259201729b5cSPatrick Sanan }
25934c274da1SToby Isaac 
259447c6ae99SBarry Smith /*@
259501729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25969a42bb27SBarry Smith 
2597d083f849SBarry Smith     Neighbor-wise Collective on dm
25989a42bb27SBarry Smith 
25999a42bb27SBarry Smith     Input Parameters:
26009a42bb27SBarry Smith +   dm - the DM object
2601f6813fd5SJed Brown .   l - the local vector
26021eb28f2eSBarry 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.
26031eb28f2eSBarry Smith -   g - the global vector
26049a42bb27SBarry Smith 
260595452b02SPatrick Sanan     Notes:
260695452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
260784330215SMatthew 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.
26089a42bb27SBarry Smith 
260901729b5cSPatrick Sanan     Level: intermediate
26109a42bb27SBarry Smith 
261101729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
26129a42bb27SBarry Smith 
26139a42bb27SBarry Smith @*/
26147087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
26159a42bb27SBarry Smith {
26167128ae9fSMatthew G Knepley   PetscSF                 sf;
261784330215SMatthew G. Knepley   PetscSection            s, gs;
2618d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2619ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2620ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2621ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2622*fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
262384330215SMatthew G. Knepley   PetscErrorCode          ierr;
26249a42bb27SBarry Smith 
26259a42bb27SBarry Smith   PetscFunctionBegin;
2626171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2627d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2628d4d07f1eSToby Isaac     if (link->beginhook) {
2629d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2630d4d07f1eSToby Isaac     }
2631d4d07f1eSToby Isaac   }
26324c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
26331bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
263492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26357128ae9fSMatthew G Knepley   switch (mode) {
26367128ae9fSMatthew G Knepley   case INSERT_VALUES:
26377128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2638304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
263984330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26407128ae9fSMatthew G Knepley   case ADD_VALUES:
26417128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2642304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
264384330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26447128ae9fSMatthew G Knepley   default:
264582f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26467128ae9fSMatthew G Knepley   }
2647ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2648ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2649ca3d3a14SMatthew G. Knepley     if (transform) {
2650ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2651ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2652ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2653ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2654*fa88e482SJed Brown     } else if (isInsert) {
2655ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2656*fa88e482SJed Brown     } else {
2657*fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2658*fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2659ca3d3a14SMatthew G. Knepley     }
2660*fa88e482SJed Brown     if (s && isInsert) {
26617128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2662*fa88e482SJed Brown     } else {
2663*fa88e482SJed Brown       ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2664*fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2665*fa88e482SJed Brown     }
2666ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2667a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
266884330215SMatthew G. Knepley     } else if (s && isInsert) {
266984330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
267084330215SMatthew G. Knepley 
2671e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
267284330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
267384330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
267484330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2675b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
267684330215SMatthew G. Knepley 
267784330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
267803442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
267984330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2680b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
268184330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
268284330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2683b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
268403442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2685b3b16f48SMatthew 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);
2686b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2687b3b16f48SMatthew G. Knepley         if (!gcdof) {
268884330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2689b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2690b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
269184330215SMatthew G. Knepley           const PetscInt *cdofs;
269284330215SMatthew G. Knepley           PetscInt        cind = 0;
269384330215SMatthew G. Knepley 
269484330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2695b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
269684330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2697b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
269884330215SMatthew G. Knepley           }
2699b3b16f48SMatthew 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);
270084330215SMatthew G. Knepley       }
2701ca3d3a14SMatthew G. Knepley     }
2702*fa88e482SJed Brown     if (g_inplace) {
2703*fa88e482SJed Brown       ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
2704*fa88e482SJed Brown     } else {
27057128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2706*fa88e482SJed Brown     }
2707ca3d3a14SMatthew G. Knepley     if (transform) {
2708ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2709ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2710*fa88e482SJed Brown     } else if (l_inplace) {
2711*fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2712ca3d3a14SMatthew G. Knepley     } else {
2713ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2714ca3d3a14SMatthew G. Knepley     }
27157128ae9fSMatthew G Knepley   } else {
2716b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2717843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27187128ae9fSMatthew G Knepley   }
27199a42bb27SBarry Smith   PetscFunctionReturn(0);
27209a42bb27SBarry Smith }
27219a42bb27SBarry Smith 
27229a42bb27SBarry Smith /*@
27239a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
272447c6ae99SBarry Smith 
2725d083f849SBarry Smith     Neighbor-wise Collective on dm
272647c6ae99SBarry Smith 
272747c6ae99SBarry Smith     Input Parameters:
272847c6ae99SBarry Smith +   dm - the DM object
2729f6813fd5SJed Brown .   l - the local vector
273047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2731f6813fd5SJed Brown -   g - the global vector
273247c6ae99SBarry Smith 
273301729b5cSPatrick Sanan     Level: intermediate
273447c6ae99SBarry Smith 
2735e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
273647c6ae99SBarry Smith 
273747c6ae99SBarry Smith @*/
27387087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
273947c6ae99SBarry Smith {
27407128ae9fSMatthew G Knepley   PetscSF                 sf;
274184330215SMatthew G. Knepley   PetscSection            s;
2742d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2743ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
274484330215SMatthew G. Knepley   PetscErrorCode          ierr;
274547c6ae99SBarry Smith 
274647c6ae99SBarry Smith   PetscFunctionBegin;
2747171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27481bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
274992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27507128ae9fSMatthew G Knepley   switch (mode) {
27517128ae9fSMatthew G Knepley   case INSERT_VALUES:
27527128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
275384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27547128ae9fSMatthew G Knepley   case ADD_VALUES:
27557128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
275684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27577128ae9fSMatthew G Knepley   default:
275882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27597128ae9fSMatthew G Knepley   }
276084330215SMatthew G. Knepley   if (sf && !isInsert) {
2761ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2762ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2763ca3d3a14SMatthew G. Knepley     Vec                tmpl;
276484330215SMatthew G. Knepley 
2765ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2766ca3d3a14SMatthew G. Knepley     if (transform) {
2767ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2768ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2769ca3d3a14SMatthew G. Knepley     } else {
2770*fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2771ca3d3a14SMatthew G. Knepley     }
2772*fa88e482SJed Brown     ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2773a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2774ca3d3a14SMatthew G. Knepley     if (transform) {
2775ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2776ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2777ca3d3a14SMatthew G. Knepley     } else {
2778*fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2779ca3d3a14SMatthew G. Knepley     }
2780*fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
278184330215SMatthew G. Knepley   } else if (s && isInsert) {
27827128ae9fSMatthew G Knepley   } else {
2783b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2784843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27857128ae9fSMatthew G Knepley   }
2786d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2787d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2788d4d07f1eSToby Isaac   }
278947c6ae99SBarry Smith   PetscFunctionReturn(0);
279047c6ae99SBarry Smith }
279147c6ae99SBarry Smith 
2792f089877aSRichard Tran Mills /*@
2793bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2794bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2795d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2796f089877aSRichard Tran Mills 
2797d083f849SBarry Smith    Neighbor-wise Collective on dm
2798f089877aSRichard Tran Mills 
2799f089877aSRichard Tran Mills    Input Parameters:
2800f089877aSRichard Tran Mills +  dm - the DM object
2801bc0a1609SRichard Tran Mills .  g - the original local vector
2802bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2803f089877aSRichard Tran Mills 
2804bc0a1609SRichard Tran Mills    Output Parameter:
2805bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2806f089877aSRichard Tran Mills 
2807f089877aSRichard Tran Mills    Level: intermediate
2808f089877aSRichard Tran Mills 
2809bc0a1609SRichard Tran Mills    Notes:
2810bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2811bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2812bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2813bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2814bc0a1609SRichard Tran Mills 
2815bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2816f089877aSRichard Tran Mills 
2817f089877aSRichard Tran Mills @*/
2818f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2819f089877aSRichard Tran Mills {
2820f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2821f089877aSRichard Tran Mills 
2822f089877aSRichard Tran Mills   PetscFunctionBegin;
2823f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2824bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2825f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2826f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2827f089877aSRichard Tran Mills }
2828f089877aSRichard Tran Mills 
2829f089877aSRichard Tran Mills /*@
2830bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2831bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2832d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2833f089877aSRichard Tran Mills 
2834d083f849SBarry Smith    Neighbor-wise Collective on dm
2835f089877aSRichard Tran Mills 
2836f089877aSRichard Tran Mills    Input Parameters:
2837bc0a1609SRichard Tran Mills +  da - the DM object
2838bc0a1609SRichard Tran Mills .  g - the original local vector
2839bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2840f089877aSRichard Tran Mills 
2841bc0a1609SRichard Tran Mills    Output Parameter:
2842bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2843f089877aSRichard Tran Mills 
2844f089877aSRichard Tran Mills    Level: intermediate
2845f089877aSRichard Tran Mills 
2846bc0a1609SRichard Tran Mills    Notes:
2847bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2848bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2849bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2850bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2851bc0a1609SRichard Tran Mills 
2852bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2853f089877aSRichard Tran Mills 
2854f089877aSRichard Tran Mills @*/
2855f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2856f089877aSRichard Tran Mills {
2857f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2858f089877aSRichard Tran Mills 
2859f089877aSRichard Tran Mills   PetscFunctionBegin;
2860f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2861bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2862f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2863f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2864f089877aSRichard Tran Mills }
2865f089877aSRichard Tran Mills 
2866f089877aSRichard Tran Mills 
286747c6ae99SBarry Smith /*@
286847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
286947c6ae99SBarry Smith 
2870d083f849SBarry Smith     Collective on dm
287147c6ae99SBarry Smith 
287247c6ae99SBarry Smith     Input Parameter:
287347c6ae99SBarry Smith +   dm - the DM object
287491d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
287547c6ae99SBarry Smith 
287647c6ae99SBarry Smith     Output Parameter:
287747c6ae99SBarry Smith .   dmc - the coarsened DM
287847c6ae99SBarry Smith 
287947c6ae99SBarry Smith     Level: developer
288047c6ae99SBarry Smith 
2881e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
288247c6ae99SBarry Smith 
288347c6ae99SBarry Smith @*/
28847087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
288547c6ae99SBarry Smith {
288647c6ae99SBarry Smith   PetscErrorCode    ierr;
2887b17ce1afSJed Brown   DMCoarsenHookLink link;
288847c6ae99SBarry Smith 
288947c6ae99SBarry Smith   PetscFunctionBegin;
2890171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2891b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
289247a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
289347c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2894b9d85ea2SLisandro Dalcin   if (*dmc) {
2895a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
289643842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28978cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2898644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
28990598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2900656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2901e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2902b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2903b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2904b17ce1afSJed Brown     }
2905b9d85ea2SLisandro Dalcin   }
290647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2907b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2908b17ce1afSJed Brown   PetscFunctionReturn(0);
2909b17ce1afSJed Brown }
2910b17ce1afSJed Brown 
2911bb9467b5SJed Brown /*@C
2912b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2913b17ce1afSJed Brown 
2914b17ce1afSJed Brown    Logically Collective
2915b17ce1afSJed Brown 
2916b17ce1afSJed Brown    Input Arguments:
2917b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2918b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2919b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
29200298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2921b17ce1afSJed Brown 
2922b17ce1afSJed Brown    Calling sequence of coarsenhook:
2923b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2924b17ce1afSJed Brown 
2925b17ce1afSJed Brown +  fine - fine level DM
2926b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2927b17ce1afSJed Brown -  ctx - optional user-defined function context
2928b17ce1afSJed Brown 
2929b17ce1afSJed Brown    Calling sequence for restricthook:
2930c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2931b17ce1afSJed Brown 
2932b17ce1afSJed Brown +  fine - fine level DM
2933b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2934c833c3b5SJed Brown .  rscale - scaling vector for restriction
2935c833c3b5SJed Brown .  inject - matrix restricting by injection
2936b17ce1afSJed Brown .  coarse - coarse level DM to update
2937b17ce1afSJed Brown -  ctx - optional user-defined function context
2938b17ce1afSJed Brown 
2939b17ce1afSJed Brown    Level: advanced
2940b17ce1afSJed Brown 
2941b17ce1afSJed Brown    Notes:
2942b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2943b17ce1afSJed Brown 
2944b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2945b17ce1afSJed Brown 
2946b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2947b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2948b17ce1afSJed Brown 
2949bb9467b5SJed Brown    This function is currently not available from Fortran.
2950bb9467b5SJed Brown 
2951dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2952b17ce1afSJed Brown @*/
2953b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2954b17ce1afSJed Brown {
2955b17ce1afSJed Brown   PetscErrorCode    ierr;
2956b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2957b17ce1afSJed Brown 
2958b17ce1afSJed Brown   PetscFunctionBegin;
2959b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
29601e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
29611e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
29621e3d8eccSJed Brown   }
296395dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2964b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2965b17ce1afSJed Brown   link->restricthook = restricthook;
2966b17ce1afSJed Brown   link->ctx          = ctx;
29670298fd71SBarry Smith   link->next         = NULL;
2968b17ce1afSJed Brown   *p                 = link;
2969b17ce1afSJed Brown   PetscFunctionReturn(0);
2970b17ce1afSJed Brown }
2971b17ce1afSJed Brown 
2972dc822a44SJed Brown /*@C
2973dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2974dc822a44SJed Brown 
2975dc822a44SJed Brown    Logically Collective
2976dc822a44SJed Brown 
2977dc822a44SJed Brown    Input Arguments:
2978dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2979dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2980dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2981dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2982dc822a44SJed Brown 
2983dc822a44SJed Brown    Level: advanced
2984dc822a44SJed Brown 
2985dc822a44SJed Brown    Notes:
2986dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2987dc822a44SJed Brown 
2988dc822a44SJed Brown    This function is currently not available from Fortran.
2989dc822a44SJed Brown 
2990dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2991dc822a44SJed Brown @*/
2992dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2993dc822a44SJed Brown {
2994dc822a44SJed Brown   PetscErrorCode    ierr;
2995dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2996dc822a44SJed Brown 
2997dc822a44SJed Brown   PetscFunctionBegin;
2998dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2999dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3000dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3001dc822a44SJed Brown       link = *p;
3002dc822a44SJed Brown       *p = link->next;
3003dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3004dc822a44SJed Brown       break;
3005dc822a44SJed Brown     }
3006dc822a44SJed Brown   }
3007dc822a44SJed Brown   PetscFunctionReturn(0);
3008dc822a44SJed Brown }
3009dc822a44SJed Brown 
3010dc822a44SJed Brown 
3011b17ce1afSJed Brown /*@
3012b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3013b17ce1afSJed Brown 
3014b17ce1afSJed Brown    Collective if any hooks are
3015b17ce1afSJed Brown 
3016b17ce1afSJed Brown    Input Arguments:
3017b17ce1afSJed Brown +  fine - finer DM to use as a base
3018b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3019e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3020b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3021e91eccc2SStefano Zampini -  coarse - coarser DM to update
3022b17ce1afSJed Brown 
3023b17ce1afSJed Brown    Level: developer
3024b17ce1afSJed Brown 
3025b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3026b17ce1afSJed Brown @*/
3027b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3028b17ce1afSJed Brown {
3029b17ce1afSJed Brown   PetscErrorCode    ierr;
3030b17ce1afSJed Brown   DMCoarsenHookLink link;
3031b17ce1afSJed Brown 
3032b17ce1afSJed Brown   PetscFunctionBegin;
3033b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
30348865f1eaSKarl Rupp     if (link->restricthook) {
30358865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
30368865f1eaSKarl Rupp     }
3037b17ce1afSJed Brown   }
303847c6ae99SBarry Smith   PetscFunctionReturn(0);
303947c6ae99SBarry Smith }
304047c6ae99SBarry Smith 
3041bb9467b5SJed Brown /*@C
3042be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
30435dbd56e3SPeter Brune 
3044d083f849SBarry Smith    Logically Collective on global
30455dbd56e3SPeter Brune 
30465dbd56e3SPeter Brune    Input Arguments:
30475dbd56e3SPeter Brune +  global - global DM
3048ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
30495dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
30500298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
30515dbd56e3SPeter Brune 
3052ec4806b8SPeter Brune 
3053ec4806b8SPeter Brune    Calling sequence for ddhook:
3054ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3055ec4806b8SPeter Brune 
3056ec4806b8SPeter Brune +  global - global DM
3057ec4806b8SPeter Brune .  block  - block DM
3058ec4806b8SPeter Brune -  ctx - optional user-defined function context
3059ec4806b8SPeter Brune 
30605dbd56e3SPeter Brune    Calling sequence for restricthook:
3061ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
30625dbd56e3SPeter Brune 
30635dbd56e3SPeter Brune +  global - global DM
30645dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
30655dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3066ec4806b8SPeter Brune .  block  - block DM
30675dbd56e3SPeter Brune -  ctx - optional user-defined function context
30685dbd56e3SPeter Brune 
30695dbd56e3SPeter Brune    Level: advanced
30705dbd56e3SPeter Brune 
30715dbd56e3SPeter Brune    Notes:
3072ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
30735dbd56e3SPeter Brune 
30745dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
30755dbd56e3SPeter Brune 
30765dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3077ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
30785dbd56e3SPeter Brune 
3079bb9467b5SJed Brown    This function is currently not available from Fortran.
3080bb9467b5SJed Brown 
30815dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
30825dbd56e3SPeter Brune @*/
3083be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
30845dbd56e3SPeter Brune {
30855dbd56e3SPeter Brune   PetscErrorCode      ierr;
3086be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
30875dbd56e3SPeter Brune 
30885dbd56e3SPeter Brune   PetscFunctionBegin;
30895dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3090b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3091b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3092b3a6b972SJed Brown   }
309395dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30945dbd56e3SPeter Brune   link->restricthook = restricthook;
3095be081cd6SPeter Brune   link->ddhook       = ddhook;
30965dbd56e3SPeter Brune   link->ctx          = ctx;
30970298fd71SBarry Smith   link->next         = NULL;
30985dbd56e3SPeter Brune   *p                 = link;
30995dbd56e3SPeter Brune   PetscFunctionReturn(0);
31005dbd56e3SPeter Brune }
31015dbd56e3SPeter Brune 
3102b3a6b972SJed Brown /*@C
3103b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3104b3a6b972SJed Brown 
3105b3a6b972SJed Brown    Logically Collective
3106b3a6b972SJed Brown 
3107b3a6b972SJed Brown    Input Arguments:
3108b3a6b972SJed Brown +  global - global DM
3109b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3110b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3111b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3112b3a6b972SJed Brown 
3113b3a6b972SJed Brown    Level: advanced
3114b3a6b972SJed Brown 
3115b3a6b972SJed Brown    Notes:
3116b3a6b972SJed Brown 
3117b3a6b972SJed Brown    This function is currently not available from Fortran.
3118b3a6b972SJed Brown 
3119b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3120b3a6b972SJed Brown @*/
3121b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3122b3a6b972SJed Brown {
3123b3a6b972SJed Brown   PetscErrorCode      ierr;
3124b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3125b3a6b972SJed Brown 
3126b3a6b972SJed Brown   PetscFunctionBegin;
3127b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3128b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3129b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3130b3a6b972SJed Brown       link = *p;
3131b3a6b972SJed Brown       *p = link->next;
3132b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3133b3a6b972SJed Brown       break;
3134b3a6b972SJed Brown     }
3135b3a6b972SJed Brown   }
3136b3a6b972SJed Brown   PetscFunctionReturn(0);
3137b3a6b972SJed Brown }
3138b3a6b972SJed Brown 
31395dbd56e3SPeter Brune /*@
3140be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
31415dbd56e3SPeter Brune 
31425dbd56e3SPeter Brune    Collective if any hooks are
31435dbd56e3SPeter Brune 
31445dbd56e3SPeter Brune    Input Arguments:
31455dbd56e3SPeter Brune +  fine - finer DM to use as a base
3146be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3147be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
31485dbd56e3SPeter Brune -  coarse - coarer DM to update
31495dbd56e3SPeter Brune 
31505dbd56e3SPeter Brune    Level: developer
31515dbd56e3SPeter Brune 
31525dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
31535dbd56e3SPeter Brune @*/
3154be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
31555dbd56e3SPeter Brune {
31565dbd56e3SPeter Brune   PetscErrorCode      ierr;
3157be081cd6SPeter Brune   DMSubDomainHookLink link;
31585dbd56e3SPeter Brune 
31595dbd56e3SPeter Brune   PetscFunctionBegin;
3160be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
31618865f1eaSKarl Rupp     if (link->restricthook) {
31628865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
31638865f1eaSKarl Rupp     }
31645dbd56e3SPeter Brune   }
31655dbd56e3SPeter Brune   PetscFunctionReturn(0);
31665dbd56e3SPeter Brune }
31675dbd56e3SPeter Brune 
31685fe1f584SPeter Brune /*@
31696a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
31705fe1f584SPeter Brune 
31715fe1f584SPeter Brune     Not Collective
31725fe1f584SPeter Brune 
31735fe1f584SPeter Brune     Input Parameter:
31745fe1f584SPeter Brune .   dm - the DM object
31755fe1f584SPeter Brune 
31765fe1f584SPeter Brune     Output Parameter:
31776a7d9d85SPeter Brune .   level - number of coarsenings
31785fe1f584SPeter Brune 
31795fe1f584SPeter Brune     Level: developer
31805fe1f584SPeter Brune 
31816a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31825fe1f584SPeter Brune 
31835fe1f584SPeter Brune @*/
31845fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
31855fe1f584SPeter Brune {
31865fe1f584SPeter Brune   PetscFunctionBegin;
31875fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3188b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
31895fe1f584SPeter Brune   *level = dm->leveldown;
31905fe1f584SPeter Brune   PetscFunctionReturn(0);
31915fe1f584SPeter Brune }
31925fe1f584SPeter Brune 
31939a64c4a8SMatthew G. Knepley /*@
31949a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31959a64c4a8SMatthew G. Knepley 
31969a64c4a8SMatthew G. Knepley     Not Collective
31979a64c4a8SMatthew G. Knepley 
31989a64c4a8SMatthew G. Knepley     Input Parameters:
31999a64c4a8SMatthew G. Knepley +   dm - the DM object
32009a64c4a8SMatthew G. Knepley -   level - number of coarsenings
32019a64c4a8SMatthew G. Knepley 
32029a64c4a8SMatthew G. Knepley     Level: developer
32039a64c4a8SMatthew G. Knepley 
32049a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
32059a64c4a8SMatthew G. Knepley @*/
32069a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
32079a64c4a8SMatthew G. Knepley {
32089a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
32099a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32109a64c4a8SMatthew G. Knepley   dm->leveldown = level;
32119a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
32129a64c4a8SMatthew G. Knepley }
32139a64c4a8SMatthew G. Knepley 
32145fe1f584SPeter Brune 
32155fe1f584SPeter Brune 
321647c6ae99SBarry Smith /*@C
321747c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
321847c6ae99SBarry Smith 
3219d083f849SBarry Smith     Collective on dm
322047c6ae99SBarry Smith 
322147c6ae99SBarry Smith     Input Parameter:
322247c6ae99SBarry Smith +   dm - the DM object
322347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
322447c6ae99SBarry Smith 
322547c6ae99SBarry Smith     Output Parameter:
322647c6ae99SBarry Smith .   dmf - the refined DM hierarchy
322747c6ae99SBarry Smith 
322847c6ae99SBarry Smith     Level: developer
322947c6ae99SBarry Smith 
3230e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
323147c6ae99SBarry Smith 
323247c6ae99SBarry Smith @*/
32337087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
323447c6ae99SBarry Smith {
323547c6ae99SBarry Smith   PetscErrorCode ierr;
323647c6ae99SBarry Smith 
323747c6ae99SBarry Smith   PetscFunctionBegin;
3238171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3239ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
324047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3241b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
324247c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
324347c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
324447c6ae99SBarry Smith   } else if (dm->ops->refine) {
324547c6ae99SBarry Smith     PetscInt i;
324647c6ae99SBarry Smith 
3247ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
324847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3249ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
325047c6ae99SBarry Smith     }
3251ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
325247c6ae99SBarry Smith   PetscFunctionReturn(0);
325347c6ae99SBarry Smith }
325447c6ae99SBarry Smith 
325547c6ae99SBarry Smith /*@C
325647c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
325747c6ae99SBarry Smith 
3258d083f849SBarry Smith     Collective on dm
325947c6ae99SBarry Smith 
326047c6ae99SBarry Smith     Input Parameter:
326147c6ae99SBarry Smith +   dm - the DM object
326247c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
326347c6ae99SBarry Smith 
326447c6ae99SBarry Smith     Output Parameter:
326547c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
326647c6ae99SBarry Smith 
326747c6ae99SBarry Smith     Level: developer
326847c6ae99SBarry Smith 
3269e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
327047c6ae99SBarry Smith 
327147c6ae99SBarry Smith @*/
32727087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
327347c6ae99SBarry Smith {
327447c6ae99SBarry Smith   PetscErrorCode ierr;
327547c6ae99SBarry Smith 
327647c6ae99SBarry Smith   PetscFunctionBegin;
3277171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3278ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
327947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
328047c6ae99SBarry Smith   PetscValidPointer(dmc,3);
328147c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
328247c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
328347c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
328447c6ae99SBarry Smith     PetscInt i;
328547c6ae99SBarry Smith 
3286ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
328747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3288ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
328947c6ae99SBarry Smith     }
3290ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
329147c6ae99SBarry Smith   PetscFunctionReturn(0);
329247c6ae99SBarry Smith }
329347c6ae99SBarry Smith 
32941a266240SBarry Smith /*@C
32951a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32961a266240SBarry Smith 
32971a266240SBarry Smith     Not Collective
32981a266240SBarry Smith 
32991a266240SBarry Smith     Input Parameters:
33001a266240SBarry Smith +   dm - the DM object
33011a266240SBarry Smith -   destroy - the destroy function
33021a266240SBarry Smith 
33031a266240SBarry Smith     Level: intermediate
33041a266240SBarry Smith 
3305e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
33061a266240SBarry Smith 
3307f07f9ceaSJed Brown @*/
33081a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
33091a266240SBarry Smith {
33101a266240SBarry Smith   PetscFunctionBegin;
3311171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33121a266240SBarry Smith   dm->ctxdestroy = destroy;
33131a266240SBarry Smith   PetscFunctionReturn(0);
33141a266240SBarry Smith }
33151a266240SBarry Smith 
3316b07ff414SBarry Smith /*@
33171b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
331847c6ae99SBarry Smith 
331947c6ae99SBarry Smith     Not Collective
332047c6ae99SBarry Smith 
332147c6ae99SBarry Smith     Input Parameters:
332247c6ae99SBarry Smith +   dm - the DM object
332347c6ae99SBarry Smith -   ctx - the user context
332447c6ae99SBarry Smith 
332547c6ae99SBarry Smith     Level: intermediate
332647c6ae99SBarry Smith 
3327e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
332847c6ae99SBarry Smith 
332947c6ae99SBarry Smith @*/
33301b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
333147c6ae99SBarry Smith {
333247c6ae99SBarry Smith   PetscFunctionBegin;
3333171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
333447c6ae99SBarry Smith   dm->ctx = ctx;
333547c6ae99SBarry Smith   PetscFunctionReturn(0);
333647c6ae99SBarry Smith }
333747c6ae99SBarry Smith 
333847c6ae99SBarry Smith /*@
33391b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
334047c6ae99SBarry Smith 
334147c6ae99SBarry Smith     Not Collective
334247c6ae99SBarry Smith 
334347c6ae99SBarry Smith     Input Parameter:
334447c6ae99SBarry Smith .   dm - the DM object
334547c6ae99SBarry Smith 
334647c6ae99SBarry Smith     Output Parameter:
334747c6ae99SBarry Smith .   ctx - the user context
334847c6ae99SBarry Smith 
334947c6ae99SBarry Smith     Level: intermediate
335047c6ae99SBarry Smith 
3351e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
335247c6ae99SBarry Smith 
335347c6ae99SBarry Smith @*/
33541b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
335547c6ae99SBarry Smith {
335647c6ae99SBarry Smith   PetscFunctionBegin;
3357171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33581b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
335947c6ae99SBarry Smith   PetscFunctionReturn(0);
336047c6ae99SBarry Smith }
336147c6ae99SBarry Smith 
336208da532bSDmitry Karpeev /*@C
3363df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
336408da532bSDmitry Karpeev 
3365d083f849SBarry Smith     Logically Collective on dm
336608da532bSDmitry Karpeev 
336708da532bSDmitry Karpeev     Input Parameter:
336808da532bSDmitry Karpeev +   dm - the DM object
33690298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
337008da532bSDmitry Karpeev 
337108da532bSDmitry Karpeev     Level: intermediate
337208da532bSDmitry Karpeev 
3373835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
337408da532bSDmitry Karpeev          DMSetJacobian()
337508da532bSDmitry Karpeev 
337608da532bSDmitry Karpeev @*/
337708da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
337808da532bSDmitry Karpeev {
337908da532bSDmitry Karpeev   PetscFunctionBegin;
33805a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
338108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
338208da532bSDmitry Karpeev   PetscFunctionReturn(0);
338308da532bSDmitry Karpeev }
338408da532bSDmitry Karpeev 
338508da532bSDmitry Karpeev /*@
338608da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
338708da532bSDmitry Karpeev 
338808da532bSDmitry Karpeev     Not Collective
338908da532bSDmitry Karpeev 
339008da532bSDmitry Karpeev     Input Parameter:
339108da532bSDmitry Karpeev .   dm - the DM object to destroy
339208da532bSDmitry Karpeev 
339308da532bSDmitry Karpeev     Output Parameter:
339408da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
339508da532bSDmitry Karpeev 
339608da532bSDmitry Karpeev     Level: developer
339708da532bSDmitry Karpeev 
339874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
339908da532bSDmitry Karpeev 
340008da532bSDmitry Karpeev @*/
340108da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
340208da532bSDmitry Karpeev {
340308da532bSDmitry Karpeev   PetscFunctionBegin;
34045a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3405534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
340608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
340708da532bSDmitry Karpeev   PetscFunctionReturn(0);
340808da532bSDmitry Karpeev }
340908da532bSDmitry Karpeev 
341008da532bSDmitry Karpeev /*@C
341108da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
341208da532bSDmitry Karpeev 
3413d083f849SBarry Smith     Logically Collective on dm
341408da532bSDmitry Karpeev 
341508da532bSDmitry Karpeev     Input Parameters:
3416907376e6SBarry Smith .   dm - the DM object
341708da532bSDmitry Karpeev 
341808da532bSDmitry Karpeev     Output parameters:
341908da532bSDmitry Karpeev +   xl - lower bound
342008da532bSDmitry Karpeev -   xu - upper bound
342108da532bSDmitry Karpeev 
3422907376e6SBarry Smith     Level: advanced
3423907376e6SBarry Smith 
342495452b02SPatrick Sanan     Notes:
342595452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
342608da532bSDmitry Karpeev 
342774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
342808da532bSDmitry Karpeev 
342908da532bSDmitry Karpeev @*/
343008da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
343108da532bSDmitry Karpeev {
343208da532bSDmitry Karpeev   PetscErrorCode ierr;
34335fd66863SKarl Rupp 
343408da532bSDmitry Karpeev   PetscFunctionBegin;
34355a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
343608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
34375a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3438b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
343908da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
344008da532bSDmitry Karpeev   PetscFunctionReturn(0);
344108da532bSDmitry Karpeev }
344208da532bSDmitry Karpeev 
3443b0ae01b7SPeter Brune /*@
3444b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3445b0ae01b7SPeter Brune 
3446b0ae01b7SPeter Brune     Not Collective
3447b0ae01b7SPeter Brune 
3448b0ae01b7SPeter Brune     Input Parameter:
3449b0ae01b7SPeter Brune .   dm - the DM object
3450b0ae01b7SPeter Brune 
3451b0ae01b7SPeter Brune     Output Parameter:
3452b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3453b0ae01b7SPeter Brune 
3454b0ae01b7SPeter Brune     Level: developer
3455b0ae01b7SPeter Brune 
34561565f0a7SPatrick Sanan .seealso DMCreateColoring()
3457b0ae01b7SPeter Brune 
3458b0ae01b7SPeter Brune @*/
3459b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3460b0ae01b7SPeter Brune {
3461b0ae01b7SPeter Brune   PetscFunctionBegin;
34625a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3463534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3464b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3465b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3466b0ae01b7SPeter Brune }
3467b0ae01b7SPeter Brune 
34683ad4599aSBarry Smith /*@
34693ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
34703ad4599aSBarry Smith 
34713ad4599aSBarry Smith     Not Collective
34723ad4599aSBarry Smith 
34733ad4599aSBarry Smith     Input Parameter:
34743ad4599aSBarry Smith .   dm - the DM object
34753ad4599aSBarry Smith 
34763ad4599aSBarry Smith     Output Parameter:
34773ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
34783ad4599aSBarry Smith 
34793ad4599aSBarry Smith     Level: developer
34803ad4599aSBarry Smith 
34811565f0a7SPatrick Sanan .seealso DMCreateRestriction()
34823ad4599aSBarry Smith 
34833ad4599aSBarry Smith @*/
34843ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
34853ad4599aSBarry Smith {
34863ad4599aSBarry Smith   PetscFunctionBegin;
34875a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3488534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34893ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34903ad4599aSBarry Smith   PetscFunctionReturn(0);
34913ad4599aSBarry Smith }
34923ad4599aSBarry Smith 
3493a7058e45SLawrence Mitchell 
3494a7058e45SLawrence Mitchell /*@
3495a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3496a7058e45SLawrence Mitchell 
3497a7058e45SLawrence Mitchell     Not Collective
3498a7058e45SLawrence Mitchell 
3499a7058e45SLawrence Mitchell     Input Parameter:
3500a7058e45SLawrence Mitchell .   dm - the DM object
3501a7058e45SLawrence Mitchell 
3502a7058e45SLawrence Mitchell     Output Parameter:
3503a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3504a7058e45SLawrence Mitchell 
3505a7058e45SLawrence Mitchell     Level: developer
3506a7058e45SLawrence Mitchell 
35071565f0a7SPatrick Sanan .seealso DMCreateInjection()
3508a7058e45SLawrence Mitchell 
3509a7058e45SLawrence Mitchell @*/
3510a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3511a7058e45SLawrence Mitchell {
35124a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
35135a84ad33SLisandro Dalcin 
3514a7058e45SLawrence Mitchell   PetscFunctionBegin;
35155a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3516534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
35175a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
35184a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
35195a84ad33SLisandro Dalcin   } else {
35205a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
35215a84ad33SLisandro Dalcin   }
3522a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3523a7058e45SLawrence Mitchell }
3524a7058e45SLawrence Mitchell 
35250298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3526264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3527264ace61SBarry Smith 
3528264ace61SBarry Smith /*@C
3529264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3530264ace61SBarry Smith 
3531d083f849SBarry Smith   Collective on dm
3532264ace61SBarry Smith 
3533264ace61SBarry Smith   Input Parameters:
3534264ace61SBarry Smith + dm     - The DM object
3535264ace61SBarry Smith - method - The name of the DM type
3536264ace61SBarry Smith 
3537264ace61SBarry Smith   Options Database Key:
3538264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3539264ace61SBarry Smith 
3540264ace61SBarry Smith   Notes:
3541e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3542264ace61SBarry Smith 
3543264ace61SBarry Smith   Level: intermediate
3544264ace61SBarry Smith 
3545264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3546264ace61SBarry Smith @*/
354719fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3548264ace61SBarry Smith {
3549264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3550264ace61SBarry Smith   PetscBool      match;
3551264ace61SBarry Smith   PetscErrorCode ierr;
3552264ace61SBarry Smith 
3553264ace61SBarry Smith   PetscFunctionBegin;
3554264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3555251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3556264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3557264ace61SBarry Smith 
35580f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35591c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3560ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3561264ace61SBarry Smith 
3562264ace61SBarry Smith   if (dm->ops->destroy) {
3563264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3564264ace61SBarry Smith   }
3565d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3566264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3567d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3568264ace61SBarry Smith   PetscFunctionReturn(0);
3569264ace61SBarry Smith }
3570264ace61SBarry Smith 
3571264ace61SBarry Smith /*@C
3572264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3573264ace61SBarry Smith 
3574264ace61SBarry Smith   Not Collective
3575264ace61SBarry Smith 
3576264ace61SBarry Smith   Input Parameter:
3577264ace61SBarry Smith . dm  - The DM
3578264ace61SBarry Smith 
3579264ace61SBarry Smith   Output Parameter:
3580264ace61SBarry Smith . type - The DM type name
3581264ace61SBarry Smith 
3582264ace61SBarry Smith   Level: intermediate
3583264ace61SBarry Smith 
3584264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3585264ace61SBarry Smith @*/
358619fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3587264ace61SBarry Smith {
3588264ace61SBarry Smith   PetscErrorCode ierr;
3589264ace61SBarry Smith 
3590264ace61SBarry Smith   PetscFunctionBegin;
3591264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3592c959eef4SJed Brown   PetscValidPointer(type,2);
3593607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3594264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3595264ace61SBarry Smith   PetscFunctionReturn(0);
3596264ace61SBarry Smith }
3597264ace61SBarry Smith 
359867a56275SMatthew G Knepley /*@C
359967a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
360067a56275SMatthew G Knepley 
3601d083f849SBarry Smith   Collective on dm
360267a56275SMatthew G Knepley 
360367a56275SMatthew G Knepley   Input Parameters:
360467a56275SMatthew G Knepley + dm - the DM
360567a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
360667a56275SMatthew G Knepley 
360767a56275SMatthew G Knepley   Output Parameter:
360867a56275SMatthew G Knepley . M - pointer to new DM
360967a56275SMatthew G Knepley 
361067a56275SMatthew G Knepley   Notes:
361167a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
361267a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
361367a56275SMatthew G Knepley   of the input DM.
361467a56275SMatthew G Knepley 
361567a56275SMatthew G Knepley   Level: intermediate
361667a56275SMatthew G Knepley 
361767a56275SMatthew G Knepley .seealso: DMCreate()
361867a56275SMatthew G Knepley @*/
361919fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
362067a56275SMatthew G Knepley {
362167a56275SMatthew G Knepley   DM             B;
362267a56275SMatthew G Knepley   char           convname[256];
3623c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
362467a56275SMatthew G Knepley   PetscErrorCode ierr;
362567a56275SMatthew G Knepley 
362667a56275SMatthew G Knepley   PetscFunctionBegin;
362767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
362867a56275SMatthew G Knepley   PetscValidType(dm,1);
362967a56275SMatthew G Knepley   PetscValidPointer(M,3);
3630251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3631c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3632c067b6caSMatthew G. Knepley   if (sametype) {
3633c067b6caSMatthew G. Knepley     *M   = dm;
3634c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3635c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3636c067b6caSMatthew G. Knepley   } else {
36370298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
363867a56275SMatthew G Knepley 
363967a56275SMatthew G Knepley     /*
364067a56275SMatthew G Knepley        Order of precedence:
364167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
364267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
364367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
364467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
364567a56275SMatthew G Knepley        5) Use a really basic converter.
364667a56275SMatthew G Knepley     */
364767a56275SMatthew G Knepley 
364867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3649a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3650a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3651a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3652a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3653a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36540005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
365567a56275SMatthew G Knepley     if (conv) goto foundconv;
365667a56275SMatthew G Knepley 
365767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
365882f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
365967a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3660a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3661a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3662a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3663a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3664a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36650005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
366667a56275SMatthew G Knepley     if (conv) {
3667fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
366867a56275SMatthew G Knepley       goto foundconv;
366967a56275SMatthew G Knepley     }
367067a56275SMatthew G Knepley 
367167a56275SMatthew G Knepley #if 0
367267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
367367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3674fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
367567a56275SMatthew G Knepley     if (conv) goto foundconv;
367667a56275SMatthew G Knepley 
367767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
367867a56275SMatthew G Knepley     if (dm->ops->convert) {
367967a56275SMatthew G Knepley       conv = dm->ops->convert;
368067a56275SMatthew G Knepley     }
368167a56275SMatthew G Knepley     if (conv) goto foundconv;
368267a56275SMatthew G Knepley #endif
368367a56275SMatthew G Knepley 
368467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
368582f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
368667a56275SMatthew G Knepley 
368767a56275SMatthew G Knepley foundconv:
368867a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
368967a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
369012fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
369190b157c4SStefano Zampini     {
369290b157c4SStefano Zampini       PetscBool             isper;
369312fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
369412fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
369590b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
369690b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
369712fa691eSMatthew G. Knepley     }
369867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
369967a56275SMatthew G Knepley   }
370067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
370167a56275SMatthew G Knepley   PetscFunctionReturn(0);
370267a56275SMatthew G Knepley }
3703264ace61SBarry Smith 
3704264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3705264ace61SBarry Smith 
3706264ace61SBarry Smith /*@C
37071c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
37081c84c290SBarry Smith 
37091c84c290SBarry Smith   Not Collective
37101c84c290SBarry Smith 
37111c84c290SBarry Smith   Input Parameters:
37121c84c290SBarry Smith + name        - The name of a new user-defined creation routine
37131c84c290SBarry Smith - create_func - The creation routine itself
37141c84c290SBarry Smith 
37151c84c290SBarry Smith   Notes:
37161c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
37171c84c290SBarry Smith 
37181c84c290SBarry Smith 
37191c84c290SBarry Smith   Sample usage:
37201c84c290SBarry Smith .vb
3721bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
37221c84c290SBarry Smith .ve
37231c84c290SBarry Smith 
37241c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
37251c84c290SBarry Smith .vb
37261c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
37271c84c290SBarry Smith     DMSetType(DM,"my_da");
37281c84c290SBarry Smith .ve
37291c84c290SBarry Smith    or at runtime via the option
37301c84c290SBarry Smith .vb
37311c84c290SBarry Smith     -da_type my_da
37321c84c290SBarry Smith .ve
3733264ace61SBarry Smith 
3734264ace61SBarry Smith   Level: advanced
37351c84c290SBarry Smith 
3736bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
37371c84c290SBarry Smith 
3738264ace61SBarry Smith @*/
3739bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3740264ace61SBarry Smith {
3741264ace61SBarry Smith   PetscErrorCode ierr;
3742264ace61SBarry Smith 
3743264ace61SBarry Smith   PetscFunctionBegin;
37441d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3745a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3746264ace61SBarry Smith   PetscFunctionReturn(0);
3747264ace61SBarry Smith }
3748264ace61SBarry Smith 
3749b859378eSBarry Smith /*@C
375055849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3751b859378eSBarry Smith 
3752d083f849SBarry Smith   Collective on viewer
3753b859378eSBarry Smith 
3754b859378eSBarry Smith   Input Parameters:
3755b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3756b859378eSBarry Smith            some related function before a call to DMLoad().
3757b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3758b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3759b859378eSBarry Smith 
3760b859378eSBarry Smith    Level: intermediate
3761b859378eSBarry Smith 
3762b859378eSBarry Smith   Notes:
376355849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3764b859378eSBarry Smith 
3765b859378eSBarry Smith   Notes for advanced users:
3766b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3767b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3768b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3769b859378eSBarry Smith   format is
3770b859378eSBarry Smith .vb
3771b859378eSBarry Smith      has not yet been determined
3772b859378eSBarry Smith .ve
3773b859378eSBarry Smith 
3774b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3775b859378eSBarry Smith @*/
3776b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3777b859378eSBarry Smith {
37789331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3779b859378eSBarry Smith   PetscErrorCode ierr;
3780b859378eSBarry Smith 
3781b859378eSBarry Smith   PetscFunctionBegin;
3782b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3783b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3784fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
378532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37869331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
378758cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
37889331c7a4SMatthew G. Knepley   if (isbinary) {
37899331c7a4SMatthew G. Knepley     PetscInt classid;
37909331c7a4SMatthew G. Knepley     char     type[256];
3791b859378eSBarry Smith 
3792060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37939200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3794060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
379532c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37969331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37979331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37989331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37999331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
380058cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3801b859378eSBarry Smith   PetscFunctionReturn(0);
3802b859378eSBarry Smith }
3803b859378eSBarry Smith 
3804b2e4378dSMatthew G. Knepley /*@
3805b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3806b2e4378dSMatthew G. Knepley 
3807b2e4378dSMatthew G. Knepley   Not collective
3808b2e4378dSMatthew G. Knepley 
3809b2e4378dSMatthew G. Knepley   Input Parameter:
3810b2e4378dSMatthew G. Knepley . dm - the DM
3811b2e4378dSMatthew G. Knepley 
3812b2e4378dSMatthew G. Knepley   Output Parameters:
3813b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3814b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3815b2e4378dSMatthew G. Knepley 
3816b2e4378dSMatthew G. Knepley   Level: beginner
3817b2e4378dSMatthew G. Knepley 
3818b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3819b2e4378dSMatthew G. Knepley 
3820b2e4378dSMatthew G. Knepley 
3821b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3822b2e4378dSMatthew G. Knepley @*/
3823b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3824b2e4378dSMatthew G. Knepley {
3825b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3826b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3827b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3828b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3829b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3830b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3831b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3832b2e4378dSMatthew G. Knepley 
3833b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3834b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3835b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3836b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3837b2e4378dSMatthew G. Knepley   if (coords) {
3838b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3839b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3840b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3841b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3842b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3843b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3844b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3845b2e4378dSMatthew G. Knepley       }
3846b2e4378dSMatthew G. Knepley     }
3847b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3848b2e4378dSMatthew G. Knepley   } else {
3849b2e4378dSMatthew G. Knepley     PetscBool isda;
3850b2e4378dSMatthew G. Knepley 
3851b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3852b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3853b2e4378dSMatthew G. Knepley   }
3854b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3855b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3856b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3857b2e4378dSMatthew G. Knepley }
3858b2e4378dSMatthew G. Knepley 
3859b2e4378dSMatthew G. Knepley /*@
3860b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3861b2e4378dSMatthew G. Knepley 
3862b2e4378dSMatthew G. Knepley   Collective
3863b2e4378dSMatthew G. Knepley 
3864b2e4378dSMatthew G. Knepley   Input Parameter:
3865b2e4378dSMatthew G. Knepley . dm - the DM
3866b2e4378dSMatthew G. Knepley 
3867b2e4378dSMatthew G. Knepley   Output Parameters:
3868b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3869b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3870b2e4378dSMatthew G. Knepley 
3871b2e4378dSMatthew G. Knepley   Level: beginner
3872b2e4378dSMatthew G. Knepley 
3873b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3874b2e4378dSMatthew G. Knepley @*/
3875b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3876b2e4378dSMatthew G. Knepley {
3877b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
38786ce308c4SMatthew G. Knepley   PetscInt       cdim;
38796ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3880b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3881b2e4378dSMatthew G. Knepley 
3882b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3883b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3884b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3885b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3886b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3887b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3888b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3889b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3890b2e4378dSMatthew G. Knepley }
3891b2e4378dSMatthew G. Knepley 
38927da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38937da65231SMatthew G Knepley 
3894a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3895a6dfd86eSKarl Rupp {
38961d47ebbbSSatish Balay   PetscInt       f;
38971b30c384SMatthew G Knepley   PetscErrorCode ierr;
38981b30c384SMatthew G Knepley 
38997da65231SMatthew G Knepley   PetscFunctionBegin;
390074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39011d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
390257622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
39037da65231SMatthew G Knepley   }
39047da65231SMatthew G Knepley   PetscFunctionReturn(0);
39057da65231SMatthew G Knepley }
39067da65231SMatthew G Knepley 
3907a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3908a6dfd86eSKarl Rupp {
39091b30c384SMatthew G Knepley   PetscInt       f, g;
39107da65231SMatthew G Knepley   PetscErrorCode ierr;
39117da65231SMatthew G Knepley 
39127da65231SMatthew G Knepley   PetscFunctionBegin;
391374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39141d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
391574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
39161d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3917e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
39187da65231SMatthew G Knepley     }
391974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
39207da65231SMatthew G Knepley   }
39217da65231SMatthew G Knepley   PetscFunctionReturn(0);
39227da65231SMatthew G Knepley }
3923e7c4fc90SDmitry Karpeev 
39246113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3925e759306cSMatthew G. Knepley {
39260c5b8624SToby Isaac   PetscInt          localSize, bs;
39270c5b8624SToby Isaac   PetscMPIInt       size;
39280c5b8624SToby Isaac   Vec               x, xglob;
39290c5b8624SToby Isaac   const PetscScalar *xarray;
3930e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3931e759306cSMatthew G. Knepley 
3932e759306cSMatthew G. Knepley   PetscFunctionBegin;
39339852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3934e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3935e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
39366113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
39370c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
39380c5b8624SToby Isaac   if (size > 1) {
39390c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
39400c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
39410c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
39420c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
39430c5b8624SToby Isaac   } else {
39440c5b8624SToby Isaac     xglob = x;
39450c5b8624SToby Isaac   }
39460c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
39470c5b8624SToby Isaac   if (size > 1) {
39480c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
39490c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
39500c5b8624SToby Isaac   }
3951e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3952e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3953e759306cSMatthew G. Knepley }
3954e759306cSMatthew G. Knepley 
395588ed4aceSMatthew G Knepley /*@
39561bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3957061576a5SJed Brown 
3958061576a5SJed Brown   Input Parameter:
3959061576a5SJed Brown . dm - The DM
3960061576a5SJed Brown 
3961061576a5SJed Brown   Output Parameter:
3962061576a5SJed Brown . section - The PetscSection
3963061576a5SJed Brown 
3964061576a5SJed Brown   Options Database Keys:
3965061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3966061576a5SJed Brown 
3967061576a5SJed Brown   Level: advanced
3968061576a5SJed Brown 
3969061576a5SJed Brown   Notes:
3970061576a5SJed Brown   Use DMGetLocalSection() in new code.
3971061576a5SJed Brown 
3972061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3973061576a5SJed Brown 
3974061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3975061576a5SJed Brown @*/
3976061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3977061576a5SJed Brown {
3978061576a5SJed Brown   PetscErrorCode ierr;
3979061576a5SJed Brown 
3980061576a5SJed Brown   PetscFunctionBegin;
3981061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3982061576a5SJed Brown   PetscFunctionReturn(0);
3983061576a5SJed Brown }
3984061576a5SJed Brown 
3985061576a5SJed Brown /*@
3986061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
398788ed4aceSMatthew G Knepley 
398888ed4aceSMatthew G Knepley   Input Parameter:
398988ed4aceSMatthew G Knepley . dm - The DM
399088ed4aceSMatthew G Knepley 
399188ed4aceSMatthew G Knepley   Output Parameter:
399288ed4aceSMatthew G Knepley . section - The PetscSection
399388ed4aceSMatthew G Knepley 
3994e5893cccSMatthew G. Knepley   Options Database Keys:
3995e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3996e5893cccSMatthew G. Knepley 
399788ed4aceSMatthew G Knepley   Level: intermediate
399888ed4aceSMatthew G Knepley 
399988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
400088ed4aceSMatthew G Knepley 
4001061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
400288ed4aceSMatthew G Knepley @*/
4003061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
40040adebc6cSBarry Smith {
4005fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4006fd59a867SMatthew G. Knepley 
400788ed4aceSMatthew G Knepley   PetscFunctionBegin;
400888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
400988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
40101bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4011e5e52638SMatthew G. Knepley     PetscInt d;
4012e5e52638SMatthew G. Knepley 
4013e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
40141bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
40151bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
40162f0f8703SMatthew G. Knepley   }
40171bb6d2a8SBarry Smith   *section = dm->localSection;
401888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
401988ed4aceSMatthew G Knepley }
402088ed4aceSMatthew G Knepley 
402188ed4aceSMatthew G Knepley /*@
40221bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4023061576a5SJed Brown 
4024061576a5SJed Brown   Input Parameters:
4025061576a5SJed Brown + dm - The DM
4026061576a5SJed Brown - section - The PetscSection
4027061576a5SJed Brown 
4028061576a5SJed Brown   Level: advanced
4029061576a5SJed Brown 
4030061576a5SJed Brown   Notes:
4031061576a5SJed Brown   Use DMSetLocalSection() in new code.
4032061576a5SJed Brown 
4033061576a5SJed Brown   Any existing Section will be destroyed
4034061576a5SJed Brown 
4035061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4036061576a5SJed Brown @*/
4037061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4038061576a5SJed Brown {
4039061576a5SJed Brown   PetscErrorCode ierr;
4040061576a5SJed Brown 
4041061576a5SJed Brown   PetscFunctionBegin;
4042061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4043061576a5SJed Brown   PetscFunctionReturn(0);
4044061576a5SJed Brown }
4045061576a5SJed Brown 
4046061576a5SJed Brown /*@
4047061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
404888ed4aceSMatthew G Knepley 
404988ed4aceSMatthew G Knepley   Input Parameters:
405088ed4aceSMatthew G Knepley + dm - The DM
405188ed4aceSMatthew G Knepley - section - The PetscSection
405288ed4aceSMatthew G Knepley 
405388ed4aceSMatthew G Knepley   Level: intermediate
405488ed4aceSMatthew G Knepley 
405588ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
405688ed4aceSMatthew G Knepley 
4057061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
405888ed4aceSMatthew G Knepley @*/
4059061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
40600adebc6cSBarry Smith {
4061c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4062af122d2aSMatthew G Knepley   PetscInt       f;
406388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
406488ed4aceSMatthew G Knepley 
406588ed4aceSMatthew G Knepley   PetscFunctionBegin;
406688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4067b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40681d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40691bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
40701bb6d2a8SBarry Smith   dm->localSection = section;
40711bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4072af122d2aSMatthew G Knepley   if (numFields) {
4073af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4074af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40750f21e855SMatthew G. Knepley       PetscObject disc;
4076af122d2aSMatthew G Knepley       const char *name;
4077af122d2aSMatthew G Knepley 
40781bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
407944a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40800f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4081af122d2aSMatthew G Knepley     }
4082af122d2aSMatthew G Knepley   }
4083e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40841bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
408588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
408688ed4aceSMatthew G Knepley }
408788ed4aceSMatthew G Knepley 
40889435951eSToby Isaac /*@
4089b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40909435951eSToby Isaac 
4091e228b242SToby Isaac   not collective
4092e228b242SToby Isaac 
40939435951eSToby Isaac   Input Parameter:
40949435951eSToby Isaac . dm - The DM
40959435951eSToby Isaac 
40969435951eSToby Isaac   Output Parameter:
40979435951eSToby 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.
40989435951eSToby 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.
40999435951eSToby Isaac 
41009435951eSToby Isaac   Level: advanced
41019435951eSToby Isaac 
41029435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
41039435951eSToby Isaac 
41049435951eSToby Isaac .seealso: DMSetDefaultConstraints()
41059435951eSToby Isaac @*/
41069435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
41079435951eSToby Isaac {
41089435951eSToby Isaac   PetscErrorCode ierr;
41099435951eSToby Isaac 
41109435951eSToby Isaac   PetscFunctionBegin;
41119435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41129435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
411345a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
411445a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
41159435951eSToby Isaac   PetscFunctionReturn(0);
41169435951eSToby Isaac }
41179435951eSToby Isaac 
41189435951eSToby Isaac /*@
4119b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
41209435951eSToby Isaac 
41219435951eSToby 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().
41229435951eSToby Isaac 
41239435951eSToby 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.
41249435951eSToby Isaac 
4125e228b242SToby Isaac   collective on dm
4126e228b242SToby Isaac 
41279435951eSToby Isaac   Input Parameters:
41289435951eSToby Isaac + dm - The DM
4129e228b242SToby 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).
4130e228b242SToby 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).
41319435951eSToby Isaac 
41329435951eSToby Isaac   Level: advanced
41339435951eSToby Isaac 
41349435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
41359435951eSToby Isaac 
41369435951eSToby Isaac .seealso: DMGetDefaultConstraints()
41379435951eSToby Isaac @*/
41389435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
41399435951eSToby Isaac {
4140e228b242SToby Isaac   PetscMPIInt result;
41419435951eSToby Isaac   PetscErrorCode ierr;
41429435951eSToby Isaac 
41439435951eSToby Isaac   PetscFunctionBegin;
41449435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4145e228b242SToby Isaac   if (section) {
4146e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4147e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4148f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4149e228b242SToby Isaac   }
4150e228b242SToby Isaac   if (mat) {
4151e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4152e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4153f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4154e228b242SToby Isaac   }
41559435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41569435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
41579435951eSToby Isaac   dm->defaultConstraintSection = section;
41589435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
41599435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
41609435951eSToby Isaac   dm->defaultConstraintMat = mat;
41619435951eSToby Isaac   PetscFunctionReturn(0);
41629435951eSToby Isaac }
41639435951eSToby Isaac 
4164497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4165507e4973SMatthew G. Knepley /*
4166507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4167507e4973SMatthew G. Knepley 
4168507e4973SMatthew G. Knepley   Input Parameters:
4169507e4973SMatthew G. Knepley + dm - The DM
4170507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4171507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4172507e4973SMatthew G. Knepley 
4173507e4973SMatthew G. Knepley   Level: intermediate
4174507e4973SMatthew G. Knepley 
41751bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4176507e4973SMatthew G. Knepley */
4177f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4178507e4973SMatthew G. Knepley {
4179507e4973SMatthew G. Knepley   MPI_Comm        comm;
4180507e4973SMatthew G. Knepley   PetscLayout     layout;
4181507e4973SMatthew G. Knepley   const PetscInt *ranges;
4182507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4183507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4184507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4185507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4186507e4973SMatthew G. Knepley 
4187507e4973SMatthew G. Knepley   PetscFunctionBegin;
4188507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4189507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4190507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4191507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4192507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4193507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4194507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4195507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4196507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4197507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4198507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4199507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4200f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4201507e4973SMatthew G. Knepley 
4202507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4203507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4204507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4205507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4206507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4207507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4208507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4209507e4973SMatthew 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;}
4210507e4973SMatthew 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;}
4211507e4973SMatthew G. Knepley     if (gdof < 0) {
4212507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4213507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4214507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4215507e4973SMatthew G. Knepley 
4216507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4217507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4218507e4973SMatthew 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;}
4219507e4973SMatthew G. Knepley       }
4220507e4973SMatthew G. Knepley     }
4221507e4973SMatthew G. Knepley   }
4222507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4223507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4224b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4225507e4973SMatthew G. Knepley   if (!gvalid) {
4226507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4227507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4228507e4973SMatthew G. Knepley   }
4229507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4230507e4973SMatthew G. Knepley }
4231f741bcd2SMatthew G. Knepley #endif
4232507e4973SMatthew G. Knepley 
423388ed4aceSMatthew G Knepley /*@
4234e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
423588ed4aceSMatthew G Knepley 
4236d083f849SBarry Smith   Collective on dm
42378b1ab98fSJed Brown 
423888ed4aceSMatthew G Knepley   Input Parameter:
423988ed4aceSMatthew G Knepley . dm - The DM
424088ed4aceSMatthew G Knepley 
424188ed4aceSMatthew G Knepley   Output Parameter:
424288ed4aceSMatthew G Knepley . section - The PetscSection
424388ed4aceSMatthew G Knepley 
424488ed4aceSMatthew G Knepley   Level: intermediate
424588ed4aceSMatthew G Knepley 
424688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
424788ed4aceSMatthew G Knepley 
424892fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
424988ed4aceSMatthew G Knepley @*/
4250e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
42510adebc6cSBarry Smith {
425288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
425388ed4aceSMatthew G Knepley 
425488ed4aceSMatthew G Knepley   PetscFunctionBegin;
425588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
425688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42571bb6d2a8SBarry Smith   if (!dm->globalSection) {
4258fd59a867SMatthew G. Knepley     PetscSection s;
4259fd59a867SMatthew G. Knepley 
426092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4261fd59a867SMatthew 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");
426233907cc2SStefano 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");
42631bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4264cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
42651bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
42661bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
426788ed4aceSMatthew G Knepley   }
42681bb6d2a8SBarry Smith   *section = dm->globalSection;
426988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
427088ed4aceSMatthew G Knepley }
427188ed4aceSMatthew G Knepley 
4272b21d0597SMatthew G Knepley /*@
4273e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4274b21d0597SMatthew G Knepley 
4275b21d0597SMatthew G Knepley   Input Parameters:
4276b21d0597SMatthew G Knepley + dm - The DM
42775080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4278b21d0597SMatthew G Knepley 
4279b21d0597SMatthew G Knepley   Level: intermediate
4280b21d0597SMatthew G Knepley 
4281b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4282b21d0597SMatthew G Knepley 
428392fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4284b21d0597SMatthew G Knepley @*/
4285e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42860adebc6cSBarry Smith {
4287b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4288b21d0597SMatthew G Knepley 
4289b21d0597SMatthew G Knepley   PetscFunctionBegin;
4290b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42915080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42921d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42931bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
42941bb6d2a8SBarry Smith   dm->globalSection = section;
4295497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
42961bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4297507e4973SMatthew G. Knepley #endif
4298b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4299b21d0597SMatthew G Knepley }
4300b21d0597SMatthew G Knepley 
430188ed4aceSMatthew G Knepley /*@
43021bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
430388ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
430488ed4aceSMatthew G Knepley 
430588ed4aceSMatthew G Knepley   Input Parameter:
430688ed4aceSMatthew G Knepley . dm - The DM
430788ed4aceSMatthew G Knepley 
430888ed4aceSMatthew G Knepley   Output Parameter:
430988ed4aceSMatthew G Knepley . sf - The PetscSF
431088ed4aceSMatthew G Knepley 
431188ed4aceSMatthew G Knepley   Level: intermediate
431288ed4aceSMatthew G Knepley 
431388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
431488ed4aceSMatthew G Knepley 
43151bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
431688ed4aceSMatthew G Knepley @*/
43171bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
43180adebc6cSBarry Smith {
431988ed4aceSMatthew G Knepley   PetscInt       nroots;
432088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
432188ed4aceSMatthew G Knepley 
432288ed4aceSMatthew G Knepley   PetscFunctionBegin;
432388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
432488ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
43251bb6d2a8SBarry Smith   if (!dm->sectionSF) {
43261bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
432733907cc2SStefano Zampini   }
43281bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
432988ed4aceSMatthew G Knepley   if (nroots < 0) {
433088ed4aceSMatthew G Knepley     PetscSection section, gSection;
433188ed4aceSMatthew G Knepley 
433292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
433331ea6d37SMatthew G Knepley     if (section) {
4334e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
43351bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
433631ea6d37SMatthew G Knepley     } else {
43370298fd71SBarry Smith       *sf = NULL;
433831ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
433931ea6d37SMatthew G Knepley     }
434088ed4aceSMatthew G Knepley   }
43411bb6d2a8SBarry Smith   *sf = dm->sectionSF;
434288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
434388ed4aceSMatthew G Knepley }
434488ed4aceSMatthew G Knepley 
434588ed4aceSMatthew G Knepley /*@
43461bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
434788ed4aceSMatthew G Knepley 
434888ed4aceSMatthew G Knepley   Input Parameters:
434988ed4aceSMatthew G Knepley + dm - The DM
435088ed4aceSMatthew G Knepley - sf - The PetscSF
435188ed4aceSMatthew G Knepley 
435288ed4aceSMatthew G Knepley   Level: intermediate
435388ed4aceSMatthew G Knepley 
435488ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
435588ed4aceSMatthew G Knepley 
43561bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
435788ed4aceSMatthew G Knepley @*/
43581bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
43590adebc6cSBarry Smith {
436088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
436188ed4aceSMatthew G Knepley 
436288ed4aceSMatthew G Knepley   PetscFunctionBegin;
436388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4364b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
436533907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
43661bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
43671bb6d2a8SBarry Smith   dm->sectionSF = sf;
436888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
436988ed4aceSMatthew G Knepley }
437088ed4aceSMatthew G Knepley 
437188ed4aceSMatthew G Knepley /*@C
43721bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
437388ed4aceSMatthew G Knepley   describing the data layout.
437488ed4aceSMatthew G Knepley 
437588ed4aceSMatthew G Knepley   Input Parameters:
437688ed4aceSMatthew G Knepley + dm - The DM
437788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
437888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
437988ed4aceSMatthew G Knepley 
43801bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
438188ed4aceSMatthew G Knepley 
43821bb6d2a8SBarry Smith   Level: developer
43831bb6d2a8SBarry Smith 
43841bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43851bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43861bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43871bb6d2a8SBarry Smith 
43881bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
438988ed4aceSMatthew G Knepley @*/
43901bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
439188ed4aceSMatthew G Knepley {
439282f516ccSBarry Smith   MPI_Comm       comm;
439388ed4aceSMatthew G Knepley   PetscLayout    layout;
439488ed4aceSMatthew G Knepley   const PetscInt *ranges;
439588ed4aceSMatthew G Knepley   PetscInt       *local;
439688ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4397ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
439888ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
439988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
440088ed4aceSMatthew G Knepley 
440188ed4aceSMatthew G Knepley   PetscFunctionBegin;
440288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4403367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
440488ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
440588ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
440688ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
440788ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
440888ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
440988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
441088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
441188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
441288ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4413ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
44146636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
441588ed4aceSMatthew G Knepley 
44166636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44176636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4418235fbf56SMatthew 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));
44196636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
442088ed4aceSMatthew G Knepley   }
4421785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4422785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
442388ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
44241f588964SMatthew G Knepley     const PetscInt *cind;
44256636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
442688ed4aceSMatthew G Knepley 
442788ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
442888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
442988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
443088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
443188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44326636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
443388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
44346636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
44356636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
44366636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4437057b4bcdSMatthew 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);
44386636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
44396636e97aSMatthew G Knepley     }
444088ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
444188ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
444288ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
444388ed4aceSMatthew G Knepley     }
444488ed4aceSMatthew G Knepley     if (gdof < 0) {
44456636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
444688ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
444788ed4aceSMatthew G Knepley 
444805376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
444931d3f06eSJed Brown         if (r < 0) r = -(r+2);
445005376888SMatthew 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);
445188ed4aceSMatthew G Knepley         remote[l].rank  = r;
445288ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
445388ed4aceSMatthew G Knepley       }
445488ed4aceSMatthew G Knepley     } else {
44556636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
445688ed4aceSMatthew G Knepley         remote[l].rank  = rank;
445788ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
445888ed4aceSMatthew G Knepley       }
445988ed4aceSMatthew G Knepley     }
446088ed4aceSMatthew G Knepley   }
44616636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
446288ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
44631bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
446488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
446588ed4aceSMatthew G Knepley }
4466af122d2aSMatthew G Knepley 
4467b21d0597SMatthew G Knepley /*@
4468b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4469b21d0597SMatthew G Knepley 
4470b21d0597SMatthew G Knepley   Input Parameter:
4471b21d0597SMatthew G Knepley . dm - The DM
4472b21d0597SMatthew G Knepley 
4473b21d0597SMatthew G Knepley   Output Parameter:
4474b21d0597SMatthew G Knepley . sf - The PetscSF
4475b21d0597SMatthew G Knepley 
4476b21d0597SMatthew G Knepley   Level: intermediate
4477b21d0597SMatthew G Knepley 
4478b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4479b21d0597SMatthew G Knepley 
44801bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4481b21d0597SMatthew G Knepley @*/
44820adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44830adebc6cSBarry Smith {
4484b21d0597SMatthew G Knepley   PetscFunctionBegin;
4485b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4486b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4487b21d0597SMatthew G Knepley   *sf = dm->sf;
4488b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4489b21d0597SMatthew G Knepley }
4490b21d0597SMatthew G Knepley 
4491057b4bcdSMatthew G Knepley /*@
4492057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4493057b4bcdSMatthew G Knepley 
4494057b4bcdSMatthew G Knepley   Input Parameters:
4495057b4bcdSMatthew G Knepley + dm - The DM
4496057b4bcdSMatthew G Knepley - sf - The PetscSF
4497057b4bcdSMatthew G Knepley 
4498057b4bcdSMatthew G Knepley   Level: intermediate
4499057b4bcdSMatthew G Knepley 
45001bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4501057b4bcdSMatthew G Knepley @*/
45020adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
45030adebc6cSBarry Smith {
4504057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4505057b4bcdSMatthew G Knepley 
4506057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4507057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4508b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4509057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
451033907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4511057b4bcdSMatthew G Knepley   dm->sf = sf;
4512057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4513057b4bcdSMatthew G Knepley }
4514057b4bcdSMatthew G Knepley 
451534aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
451634aa8a36SMatthew G. Knepley {
451734aa8a36SMatthew G. Knepley   PetscClassId   id;
451834aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
451934aa8a36SMatthew G. Knepley 
452034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
452134aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
452234aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
452334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
452434aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
452534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
452617c1d62eSMatthew G. Knepley   } else {
452717c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
452834aa8a36SMatthew G. Knepley   }
452934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
453034aa8a36SMatthew G. Knepley }
453134aa8a36SMatthew G. Knepley 
453244a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
453344a7f3ddSMatthew G. Knepley {
453444a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
453544a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
453644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
453744a7f3ddSMatthew G. Knepley 
453844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
453944a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
454044a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
454144a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
454244a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
454344a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
454444a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
454544a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
454644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
454744a7f3ddSMatthew G. Knepley }
454844a7f3ddSMatthew G. Knepley 
454944a7f3ddSMatthew G. Knepley /*@
455044a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
455144a7f3ddSMatthew G. Knepley 
4552d083f849SBarry Smith   Logically collective on dm
455344a7f3ddSMatthew G. Knepley 
455444a7f3ddSMatthew G. Knepley   Input Parameter:
455544a7f3ddSMatthew G. Knepley . dm - The DM
455644a7f3ddSMatthew G. Knepley 
455744a7f3ddSMatthew G. Knepley   Level: intermediate
455844a7f3ddSMatthew G. Knepley 
455944a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
456044a7f3ddSMatthew G. Knepley @*/
456144a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
456244a7f3ddSMatthew G. Knepley {
456344a7f3ddSMatthew G. Knepley   PetscInt       f;
456444a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
456544a7f3ddSMatthew G. Knepley 
456644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
456744a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
456844a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
456944a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
457044a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
457144a7f3ddSMatthew G. Knepley   }
457244a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
457344a7f3ddSMatthew G. Knepley   dm->fields = NULL;
457444a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
457544a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
457644a7f3ddSMatthew G. Knepley }
457744a7f3ddSMatthew G. Knepley 
4578689b5837SMatthew G. Knepley /*@
4579689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4580689b5837SMatthew G. Knepley 
4581689b5837SMatthew G. Knepley   Not collective
4582689b5837SMatthew G. Knepley 
4583689b5837SMatthew G. Knepley   Input Parameter:
4584689b5837SMatthew G. Knepley . dm - The DM
4585689b5837SMatthew G. Knepley 
4586689b5837SMatthew G. Knepley   Output Parameter:
4587689b5837SMatthew G. Knepley . Nf - The number of fields
4588689b5837SMatthew G. Knepley 
4589689b5837SMatthew G. Knepley   Level: intermediate
4590689b5837SMatthew G. Knepley 
4591689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4592689b5837SMatthew G. Knepley @*/
45930f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
45940f21e855SMatthew G. Knepley {
45950f21e855SMatthew G. Knepley   PetscFunctionBegin;
45960f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4597534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
459844a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4599af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4600af122d2aSMatthew G Knepley }
4601af122d2aSMatthew G Knepley 
4602689b5837SMatthew G. Knepley /*@
4603689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4604689b5837SMatthew G. Knepley 
4605d083f849SBarry Smith   Logically collective on dm
4606689b5837SMatthew G. Knepley 
4607689b5837SMatthew G. Knepley   Input Parameters:
4608689b5837SMatthew G. Knepley + dm - The DM
4609689b5837SMatthew G. Knepley - Nf - The number of fields
4610689b5837SMatthew G. Knepley 
4611689b5837SMatthew G. Knepley   Level: intermediate
4612689b5837SMatthew G. Knepley 
4613689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4614689b5837SMatthew G. Knepley @*/
4615af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4616af122d2aSMatthew G Knepley {
46170f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4618af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4619af122d2aSMatthew G Knepley 
4620af122d2aSMatthew G Knepley   PetscFunctionBegin;
4621af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
462244a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
46230f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
46240f21e855SMatthew G. Knepley     PetscContainer obj;
46250f21e855SMatthew G. Knepley 
46260f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
462744a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
46280f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4629af122d2aSMatthew G Knepley   }
4630af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4631af122d2aSMatthew G Knepley }
4632af122d2aSMatthew G Knepley 
4633c1929be8SMatthew G. Knepley /*@
4634c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4635c1929be8SMatthew G. Knepley 
4636c1929be8SMatthew G. Knepley   Not collective
4637c1929be8SMatthew G. Knepley 
4638c1929be8SMatthew G. Knepley   Input Parameters:
4639c1929be8SMatthew G. Knepley + dm - The DM
4640c1929be8SMatthew G. Knepley - f  - The field number
4641c1929be8SMatthew G. Knepley 
464244a7f3ddSMatthew G. Knepley   Output Parameters:
464344a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
464444a7f3ddSMatthew G. Knepley - field - The discretization object
4645c1929be8SMatthew G. Knepley 
464644a7f3ddSMatthew G. Knepley   Level: intermediate
4647c1929be8SMatthew G. Knepley 
464844a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4649c1929be8SMatthew G. Knepley @*/
465044a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4651af122d2aSMatthew G Knepley {
4652af122d2aSMatthew G Knepley   PetscFunctionBegin;
4653af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
465444a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
465544a7f3ddSMatthew 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);
465644a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
465744a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4658decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4659decb47aaSMatthew G. Knepley }
4660decb47aaSMatthew G. Knepley 
4661c1929be8SMatthew G. Knepley /*@
4662c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4663c1929be8SMatthew G. Knepley 
4664d083f849SBarry Smith   Logically collective on dm
4665c1929be8SMatthew G. Knepley 
4666c1929be8SMatthew G. Knepley   Input Parameters:
4667c1929be8SMatthew G. Knepley + dm    - The DM
4668c1929be8SMatthew G. Knepley . f     - The field number
466944a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4670c1929be8SMatthew G. Knepley - field - The discretization object
4671c1929be8SMatthew G. Knepley 
467244a7f3ddSMatthew G. Knepley   Level: intermediate
4673c1929be8SMatthew G. Knepley 
467444a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4675c1929be8SMatthew G. Knepley @*/
467644a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4677decb47aaSMatthew G. Knepley {
4678decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4679decb47aaSMatthew G. Knepley 
4680decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4681decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4682e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
468344a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4684e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
468544a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
468644a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
468744a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
468844a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
468944a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
469044a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
469144a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
469234aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
46932df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
469444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
469544a7f3ddSMatthew G. Knepley }
469644a7f3ddSMatthew G. Knepley 
469744a7f3ddSMatthew G. Knepley /*@
469844a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
469944a7f3ddSMatthew G. Knepley 
4700d083f849SBarry Smith   Logically collective on dm
470144a7f3ddSMatthew G. Knepley 
470244a7f3ddSMatthew G. Knepley   Input Parameters:
470344a7f3ddSMatthew G. Knepley + dm    - The DM
470444a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
470544a7f3ddSMatthew G. Knepley - field - The discretization object
470644a7f3ddSMatthew G. Knepley 
470744a7f3ddSMatthew G. Knepley   Level: intermediate
470844a7f3ddSMatthew G. Knepley 
470944a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
471044a7f3ddSMatthew G. Knepley @*/
471144a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
471244a7f3ddSMatthew G. Knepley {
471344a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
471444a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
471544a7f3ddSMatthew G. Knepley 
471644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
471744a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
471844a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
471944a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
472044a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
472144a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
472244a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
472344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
472444a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
472534aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
47262df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4727af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4728af122d2aSMatthew G Knepley }
47296636e97aSMatthew G Knepley 
4730e5e52638SMatthew G. Knepley /*@
4731e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4732e5e52638SMatthew G. Knepley 
4733d083f849SBarry Smith   Collective on dm
4734e5e52638SMatthew G. Knepley 
4735e5e52638SMatthew G. Knepley   Input Parameter:
4736e5e52638SMatthew G. Knepley . dm - The DM
4737e5e52638SMatthew G. Knepley 
4738e5e52638SMatthew G. Knepley   Output Parameter:
4739e5e52638SMatthew G. Knepley . newdm - The DM
4740e5e52638SMatthew G. Knepley 
4741e5e52638SMatthew G. Knepley   Level: advanced
4742e5e52638SMatthew G. Knepley 
4743e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4744e5e52638SMatthew G. Knepley @*/
4745e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4746e5e52638SMatthew G. Knepley {
4747e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4748e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4749e5e52638SMatthew G. Knepley 
4750e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4751e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4752e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4753e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4754e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4755e5e52638SMatthew G. Knepley     DMLabel     label;
4756e5e52638SMatthew G. Knepley     PetscObject field;
475734aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4758e5e52638SMatthew G. Knepley 
4759e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4760e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
476134aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
476234aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
476334aa8a36SMatthew G. Knepley   }
476434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
476534aa8a36SMatthew G. Knepley }
476634aa8a36SMatthew G. Knepley 
476734aa8a36SMatthew G. Knepley /*@
476834aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
476934aa8a36SMatthew G. Knepley 
477034aa8a36SMatthew G. Knepley   Not collective
477134aa8a36SMatthew G. Knepley 
477234aa8a36SMatthew G. Knepley   Input Parameters:
477334aa8a36SMatthew G. Knepley + dm - The DM object
477434aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
477534aa8a36SMatthew G. Knepley 
477634aa8a36SMatthew G. Knepley   Output Parameter:
477734aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
477834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
477934aa8a36SMatthew G. Knepley 
478034aa8a36SMatthew G. Knepley   Notes:
478134aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
478234aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
478334aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4784979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
478534aa8a36SMatthew G. Knepley 
478634aa8a36SMatthew G. Knepley   Level: developer
478734aa8a36SMatthew G. Knepley 
478834aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
478934aa8a36SMatthew G. Knepley @*/
479034aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
479134aa8a36SMatthew G. Knepley {
479234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
479334aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4794534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4795534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
479634aa8a36SMatthew G. Knepley   if (f < 0) {
479734aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
479834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
479934aa8a36SMatthew G. Knepley   } else {
480034aa8a36SMatthew G. Knepley     PetscInt       Nf;
480134aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
480234aa8a36SMatthew G. Knepley 
480334aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
480434aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
480534aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
480634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
480734aa8a36SMatthew G. Knepley   }
480834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
480934aa8a36SMatthew G. Knepley }
481034aa8a36SMatthew G. Knepley 
481134aa8a36SMatthew G. Knepley /*@
481234aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
481334aa8a36SMatthew G. Knepley 
481434aa8a36SMatthew G. Knepley   Not collective
481534aa8a36SMatthew G. Knepley 
481634aa8a36SMatthew G. Knepley   Input Parameters:
481734aa8a36SMatthew G. Knepley + dm         - The DM object
481834aa8a36SMatthew G. Knepley . f          - The field number
481934aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
482034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
482134aa8a36SMatthew G. Knepley 
482234aa8a36SMatthew G. Knepley   Notes:
482334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
482434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
482534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4826979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
482734aa8a36SMatthew G. Knepley 
482834aa8a36SMatthew G. Knepley   Level: developer
482934aa8a36SMatthew G. Knepley 
483034aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
483134aa8a36SMatthew G. Knepley @*/
483234aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
483334aa8a36SMatthew G. Knepley {
483434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
483534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
483634aa8a36SMatthew G. Knepley   if (f < 0) {
483734aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
483834aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
483934aa8a36SMatthew G. Knepley   } else {
484034aa8a36SMatthew G. Knepley     PetscInt       Nf;
484134aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
484234aa8a36SMatthew G. Knepley 
484334aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
484434aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
484534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
484634aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4847e5e52638SMatthew G. Knepley   }
4848e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4849e5e52638SMatthew G. Knepley }
4850e5e52638SMatthew G. Knepley 
4851b0441da4SMatthew G. Knepley /*@
4852b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4853b0441da4SMatthew G. Knepley 
4854b0441da4SMatthew G. Knepley   Not collective
4855b0441da4SMatthew G. Knepley 
4856b0441da4SMatthew G. Knepley   Input Parameters:
4857b0441da4SMatthew G. Knepley . dm - The DM object
4858b0441da4SMatthew G. Knepley 
4859b0441da4SMatthew G. Knepley   Output Parameter:
4860b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4861b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4862b0441da4SMatthew G. Knepley 
4863b0441da4SMatthew G. Knepley   Notes:
4864b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4865b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4866b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4867b0441da4SMatthew G. Knepley 
4868b0441da4SMatthew G. Knepley   Level: developer
4869b0441da4SMatthew G. Knepley 
4870b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4871b0441da4SMatthew G. Knepley @*/
4872b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4873b0441da4SMatthew G. Knepley {
4874b0441da4SMatthew G. Knepley   PetscInt       Nf;
4875b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4876b0441da4SMatthew G. Knepley 
4877b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4878b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4879534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4880534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4881b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4882b0441da4SMatthew G. Knepley   if (!Nf) {
4883b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4884b0441da4SMatthew G. Knepley   } else {
4885b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4886b0441da4SMatthew G. Knepley   }
4887b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4888b0441da4SMatthew G. Knepley }
4889b0441da4SMatthew G. Knepley 
4890b0441da4SMatthew G. Knepley /*@
4891b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4892b0441da4SMatthew G. Knepley 
4893b0441da4SMatthew G. Knepley   Not collective
4894b0441da4SMatthew G. Knepley 
4895b0441da4SMatthew G. Knepley   Input Parameters:
4896b0441da4SMatthew G. Knepley + dm         - The DM object
4897b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4898b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4899b0441da4SMatthew G. Knepley 
4900b0441da4SMatthew G. Knepley   Notes:
4901b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4902b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4903b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4904b0441da4SMatthew G. Knepley 
4905b0441da4SMatthew G. Knepley   Level: developer
4906b0441da4SMatthew G. Knepley 
4907b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4908b0441da4SMatthew G. Knepley @*/
4909b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4910b0441da4SMatthew G. Knepley {
4911b0441da4SMatthew G. Knepley   PetscInt       Nf;
4912b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4913b0441da4SMatthew G. Knepley 
4914b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4915b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4916b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4917b0441da4SMatthew G. Knepley   if (!Nf) {
4918b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4919b0441da4SMatthew G. Knepley   } else {
4920b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4921e5e52638SMatthew G. Knepley   }
4922e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4923e5e52638SMatthew G. Knepley }
4924e5e52638SMatthew G. Knepley 
4925e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4926e5e52638SMatthew G. Knepley {
4927e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4928e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4929e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4930e5e52638SMatthew G. Knepley 
4931e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4932e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4933e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4934e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4935b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4936e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4937e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4938e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4939e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4940e5e52638SMatthew G. Knepley }
4941e5e52638SMatthew G. Knepley 
4942e5e52638SMatthew G. Knepley /*@
4943e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4944e5e52638SMatthew G. Knepley 
4945e5e52638SMatthew G. Knepley   Not collective
4946e5e52638SMatthew G. Knepley 
4947e5e52638SMatthew G. Knepley   Input Parameter:
4948e5e52638SMatthew G. Knepley . dm - The DM
4949e5e52638SMatthew G. Knepley 
4950e5e52638SMatthew G. Knepley   Output Parameter:
4951e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4952e5e52638SMatthew G. Knepley 
4953e5e52638SMatthew G. Knepley   Level: intermediate
4954e5e52638SMatthew G. Knepley 
4955e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4956e5e52638SMatthew G. Knepley @*/
4957e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4958e5e52638SMatthew G. Knepley {
4959e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4960e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4961534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
4962e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4963e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4964e5e52638SMatthew G. Knepley }
4965e5e52638SMatthew G. Knepley 
4966e5e52638SMatthew G. Knepley /*@
4967e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4968e5e52638SMatthew G. Knepley 
4969d083f849SBarry Smith   Logically collective on dm
4970e5e52638SMatthew G. Knepley 
4971e5e52638SMatthew G. Knepley   Input Parameter:
4972e5e52638SMatthew G. Knepley . dm - The DM
4973e5e52638SMatthew G. Knepley 
4974e5e52638SMatthew G. Knepley   Level: intermediate
4975e5e52638SMatthew G. Knepley 
4976e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4977e5e52638SMatthew G. Knepley @*/
4978e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4979e5e52638SMatthew G. Knepley {
4980e5e52638SMatthew G. Knepley   PetscInt       s;
4981e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4982e5e52638SMatthew G. Knepley 
4983e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4984e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4985e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4986e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4987e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4988b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4989e5e52638SMatthew G. Knepley   }
4990e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4991e5e52638SMatthew G. Knepley   dm->probs = NULL;
4992e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4993e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4994e5e52638SMatthew G. Knepley }
4995e5e52638SMatthew G. Knepley 
4996e5e52638SMatthew G. Knepley /*@
4997e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4998e5e52638SMatthew G. Knepley 
4999e5e52638SMatthew G. Knepley   Not collective
5000e5e52638SMatthew G. Knepley 
5001e5e52638SMatthew G. Knepley   Input Parameter:
5002e5e52638SMatthew G. Knepley . dm    - The DM
5003e5e52638SMatthew G. Knepley 
5004e5e52638SMatthew G. Knepley   Output Parameter:
5005e5e52638SMatthew G. Knepley . prob - The default PetscDS
5006e5e52638SMatthew G. Knepley 
5007e5e52638SMatthew G. Knepley   Level: intermediate
5008e5e52638SMatthew G. Knepley 
5009e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5010e5e52638SMatthew G. Knepley @*/
5011e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5012e5e52638SMatthew G. Knepley {
5013b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5014b0143b4dSMatthew G. Knepley 
5015e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5016e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5017e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5018b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5019b0143b4dSMatthew G. Knepley     PetscDS ds;
5020b0143b4dSMatthew G. Knepley 
5021b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5022b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5023b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5024b0143b4dSMatthew G. Knepley   }
5025b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5026e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5027e5e52638SMatthew G. Knepley }
5028e5e52638SMatthew G. Knepley 
5029e5e52638SMatthew G. Knepley /*@
5030e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5031e5e52638SMatthew G. Knepley 
5032e5e52638SMatthew G. Knepley   Not collective
5033e5e52638SMatthew G. Knepley 
5034e5e52638SMatthew G. Knepley   Input Parameters:
5035e5e52638SMatthew G. Knepley + dm    - The DM
5036e5e52638SMatthew G. Knepley - point - Cell for the DS
5037e5e52638SMatthew G. Knepley 
5038e5e52638SMatthew G. Knepley   Output Parameter:
5039e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5040e5e52638SMatthew G. Knepley 
5041e5e52638SMatthew G. Knepley   Level: developer
5042e5e52638SMatthew G. Knepley 
5043b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5044e5e52638SMatthew G. Knepley @*/
5045e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5046e5e52638SMatthew G. Knepley {
5047e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5048e5e52638SMatthew G. Knepley   PetscInt       s;
5049e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5050e5e52638SMatthew G. Knepley 
5051e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5052e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5053e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5054e5e52638SMatthew G. Knepley   *prob = NULL;
5055e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5056e5e52638SMatthew G. Knepley     PetscInt val;
5057e5e52638SMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5059e5e52638SMatthew G. Knepley     else {
5060e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5061e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5062e5e52638SMatthew G. Knepley     }
5063e5e52638SMatthew G. Knepley   }
5064e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5065e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5066e5e52638SMatthew G. Knepley }
5067e5e52638SMatthew G. Knepley 
5068e5e52638SMatthew G. Knepley /*@
5069e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5070e5e52638SMatthew G. Knepley 
5071e5e52638SMatthew G. Knepley   Not collective
5072e5e52638SMatthew G. Knepley 
5073e5e52638SMatthew G. Knepley   Input Parameters:
5074e5e52638SMatthew G. Knepley + dm    - The DM
5075e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5076e5e52638SMatthew G. Knepley 
5077b3cf3223SMatthew G. Knepley   Output Parameters:
5078b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5079b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5080e5e52638SMatthew G. Knepley 
5081e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5082e5e52638SMatthew G. Knepley 
5083e5e52638SMatthew G. Knepley   Level: advanced
5084e5e52638SMatthew G. Knepley 
5085e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5086e5e52638SMatthew G. Knepley @*/
5087b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5088e5e52638SMatthew G. Knepley {
5089e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5090e5e52638SMatthew G. Knepley 
5091e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5092e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5093e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5094b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5095b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5096e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5097b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5098b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5099b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5100b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5101b3cf3223SMatthew G. Knepley     }
5102e5e52638SMatthew G. Knepley   }
51032df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5104e5e52638SMatthew G. Knepley }
5105e5e52638SMatthew G. Knepley 
5106e5e52638SMatthew G. Knepley /*@
5107e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5108e5e52638SMatthew G. Knepley 
5109e5e52638SMatthew G. Knepley   Not collective
5110e5e52638SMatthew G. Knepley 
5111e5e52638SMatthew G. Knepley   Input Parameters:
5112e5e52638SMatthew G. Knepley + dm  - The DM
5113e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5114e5e52638SMatthew G. Knepley 
5115e5e52638SMatthew G. Knepley   Output Parameters:
5116b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5117b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5118b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
5119e5e52638SMatthew G. Knepley 
5120e5e52638SMatthew G. Knepley   Level: advanced
5121e5e52638SMatthew G. Knepley 
5122e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5123e5e52638SMatthew G. Knepley @*/
5124b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5125e5e52638SMatthew G. Knepley {
5126e5e52638SMatthew G. Knepley   PetscInt       Nds;
5127e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5128e5e52638SMatthew G. Knepley 
5129e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5130e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5131e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5132e5e52638SMatthew 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);
5133e5e52638SMatthew G. Knepley   if (label) {
5134e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5135e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5136e5e52638SMatthew G. Knepley   }
5137b3cf3223SMatthew G. Knepley   if (fields) {
5138b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5139b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5140b3cf3223SMatthew G. Knepley   }
5141e5e52638SMatthew G. Knepley   if (ds) {
5142b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5143e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5144e5e52638SMatthew G. Knepley   }
5145e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5146e5e52638SMatthew G. Knepley }
5147e5e52638SMatthew G. Knepley 
5148e5e52638SMatthew G. Knepley /*@
5149e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5150e5e52638SMatthew G. Knepley 
5151d083f849SBarry Smith   Collective on dm
5152e5e52638SMatthew G. Knepley 
5153e5e52638SMatthew G. Knepley   Input Parameters:
5154e5e52638SMatthew G. Knepley + dm     - The DM
5155e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5156b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5157e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5158e5e52638SMatthew G. Knepley 
5159b3cf3223SMatthew 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,
5160b3cf3223SMatthew G. Knepley   the fields argument is ignored.
5161e5e52638SMatthew G. Knepley 
5162e5e52638SMatthew G. Knepley   Level: advanced
5163e5e52638SMatthew G. Knepley 
5164e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
5165e5e52638SMatthew G. Knepley @*/
5166b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5167e5e52638SMatthew G. Knepley {
5168e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5169e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5170e5e52638SMatthew G. Knepley 
5171e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5172e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5173e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5174e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5175e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5176e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
5177b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5178e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
5179e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
5180e5e52638SMatthew G. Knepley     }
5181e5e52638SMatthew G. Knepley   }
51821b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5183e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5184b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5185e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5186e5e52638SMatthew G. Knepley   if (!label) {
5187e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5188e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5189e5e52638SMatthew G. Knepley     Nds = 0;
5190e5e52638SMatthew G. Knepley   }
5191e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
5192b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5193e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5194e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5195e5e52638SMatthew G. Knepley }
5196e5e52638SMatthew G. Knepley 
5197e5e52638SMatthew G. Knepley /*@
5198e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5199e5e52638SMatthew G. Knepley 
5200d083f849SBarry Smith   Collective on dm
5201e5e52638SMatthew G. Knepley 
5202e5e52638SMatthew G. Knepley   Input Parameter:
5203e5e52638SMatthew G. Knepley . dm - The DM
5204e5e52638SMatthew G. Knepley 
5205e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5206e5e52638SMatthew G. Knepley 
5207e5e52638SMatthew G. Knepley   Level: intermediate
5208e5e52638SMatthew G. Knepley 
5209e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5210e5e52638SMatthew G. Knepley @*/
5211e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5212e5e52638SMatthew G. Knepley {
5213e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5214e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5215b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5216e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5217e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5218e5e52638SMatthew G. Knepley 
5219e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5220e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5221e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5222e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5223e5e52638SMatthew G. Knepley    1) NULL
5224e5e52638SMatthew G. Knepley    2) Hybrid cells
5225e5e52638SMatthew G. Knepley   */
5226e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5227e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5228e5e52638SMatthew G. Knepley   /* Create default DS */
5229b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
52302df9ee95SMatthew G. Knepley   if (!prob) {
5231b3cf3223SMatthew G. Knepley     IS        fields;
5232b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5233b3cf3223SMatthew G. Knepley 
5234b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5235b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5236b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
523788f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
523888f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
523988f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
524088f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
524188f0c812SMatthew G. Knepley 
52422df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5243b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
52442df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5245b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5246b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
52472df9ee95SMatthew G. Knepley   }
5248e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5249e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5250b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5251e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5252e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5253e5e52638SMatthew G. Knepley 
5254e5e52638SMatthew G. Knepley     if (label) {
52550122748bSMatthew G. Knepley       DM             plex;
5256412e9a14SMatthew G. Knepley       DMPolytopeType ct;
5257a2d47024SMatthew G. Knepley       IS             fields;
5258a2d47024SMatthew G. Knepley       PetscInt      *fld;
5259412e9a14SMatthew G. Knepley       PetscInt       depth;
52600122748bSMatthew G. Knepley 
52610122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
52620122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
52630122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
52640122748bSMatthew G. Knepley 
5265e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5266412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5267412e9a14SMatthew G. Knepley       switch (ct) {
5268412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5269412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5270412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5271412e9a14SMatthew G. Knepley         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5272412e9a14SMatthew G. Knepley           break;
5273412e9a14SMatthew G. Knepley         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over tensor prism cells right now");
5274412e9a14SMatthew G. Knepley       }
5275e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5276a2d47024SMatthew G. Knepley       ierr = PetscMalloc1(1, &fld);CHKERRQ(ierr);
5277a2d47024SMatthew G. Knepley       fld[0] = f;
5278a2d47024SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5279a2d47024SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5280a2d47024SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5281a2d47024SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, 1, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5282a2d47024SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, fields, probh);CHKERRQ(ierr);
5283a2d47024SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
5284e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5285e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5286e5e52638SMatthew G. Knepley       break;
5287e5e52638SMatthew G. Knepley     }
5288e5e52638SMatthew G. Knepley   }
5289e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5290b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5291e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5292e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5293e5e52638SMatthew G. Knepley 
5294e5e52638SMatthew G. Knepley     if (!label) {
5295e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5296e5e52638SMatthew G. Knepley       if (probh) {
5297e5e52638SMatthew G. Knepley         PetscFE subfe;
5298e5e52638SMatthew G. Knepley 
5299e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5300e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5301e5e52638SMatthew G. Knepley       }
5302e5e52638SMatthew G. Knepley     } else {
5303e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5304e5e52638SMatthew G. Knepley     }
5305e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5306e5e52638SMatthew G. Knepley     {
5307e5e52638SMatthew G. Knepley       PetscClassId id;
5308e5e52638SMatthew G. Knepley 
5309e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5310e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5311e5e52638SMatthew G. Knepley     }
5312e5e52638SMatthew G. Knepley   }
5313e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5314e5e52638SMatthew G. Knepley   /* Setup DSes */
5315e5e52638SMatthew G. Knepley   if (doSetup) {
5316e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5317e5e52638SMatthew G. Knepley   }
5318e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5319e5e52638SMatthew G. Knepley }
5320e5e52638SMatthew G. Knepley 
5321e5e52638SMatthew G. Knepley /*@
5322e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5323e5e52638SMatthew G. Knepley 
5324d083f849SBarry Smith   Collective on dm
5325e5e52638SMatthew G. Knepley 
5326e5e52638SMatthew G. Knepley   Input Parameter:
5327e5e52638SMatthew G. Knepley . dm - The DM
5328e5e52638SMatthew G. Knepley 
5329e5e52638SMatthew G. Knepley   Output Parameter:
5330e5e52638SMatthew G. Knepley . newdm - The DM
5331e5e52638SMatthew G. Knepley 
5332e5e52638SMatthew G. Knepley   Level: advanced
5333e5e52638SMatthew G. Knepley 
5334e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5335e5e52638SMatthew G. Knepley @*/
5336e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5337e5e52638SMatthew G. Knepley {
5338e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5339e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5340e5e52638SMatthew G. Knepley 
5341e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5342e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5343e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5344e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5345e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5346e5e52638SMatthew G. Knepley     DMLabel label;
5347b3cf3223SMatthew G. Knepley     IS      fields;
5348e5e52638SMatthew G. Knepley     PetscDS ds;
5349e5e52638SMatthew G. Knepley 
5350b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5351b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5352e5e52638SMatthew G. Knepley   }
5353e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5354e5e52638SMatthew G. Knepley }
5355e5e52638SMatthew G. Knepley 
5356e5e52638SMatthew G. Knepley /*@
5357e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5358e5e52638SMatthew G. Knepley 
5359d083f849SBarry Smith   Collective on dm
5360e5e52638SMatthew G. Knepley 
5361e5e52638SMatthew G. Knepley   Input Parameter:
5362e5e52638SMatthew G. Knepley . dm - The DM
5363e5e52638SMatthew G. Knepley 
5364e5e52638SMatthew G. Knepley   Output Parameter:
5365e5e52638SMatthew G. Knepley . newdm - The DM
5366e5e52638SMatthew G. Knepley 
5367e5e52638SMatthew G. Knepley   Level: advanced
5368e5e52638SMatthew G. Knepley 
5369e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5370e5e52638SMatthew G. Knepley @*/
5371e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5372e5e52638SMatthew G. Knepley {
5373e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5374e5e52638SMatthew G. Knepley 
5375e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5376e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5377e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5378e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5379e5e52638SMatthew G. Knepley }
5380e5e52638SMatthew G. Knepley 
5381b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5382b64e0483SPeter Brune {
5383b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5384b64e0483SPeter Brune   PetscErrorCode ierr;
5385b64e0483SPeter Brune   Vec coords,ccoords;
53866dbf9973SLawrence Mitchell   Mat inject;
5387b64e0483SPeter Brune   PetscFunctionBegin;
5388b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5389b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5390b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5391b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5392b64e0483SPeter Brune   if (coords && !ccoords) {
5393b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
53946668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
53956dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
53962adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
53976dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5398b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5399b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5400b64e0483SPeter Brune   }
5401b64e0483SPeter Brune   PetscFunctionReturn(0);
5402b64e0483SPeter Brune }
5403b64e0483SPeter Brune 
540403dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
540503dadc2fSPeter Brune {
540603dadc2fSPeter Brune   DM dm_coord,subdm_coord;
540703dadc2fSPeter Brune   PetscErrorCode ierr;
540803dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
540903dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
541003dadc2fSPeter Brune   PetscFunctionBegin;
541103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
541203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
541303dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
541403dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
541503dadc2fSPeter Brune   if (coords && !ccoords) {
541603dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
54176668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
541803dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
541924640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
542003dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
542103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
542203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
54231ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
542403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
542503dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
542603dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
542703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
542803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
542903dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
543003dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
543103dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
543203dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
543303dadc2fSPeter Brune   }
543403dadc2fSPeter Brune   PetscFunctionReturn(0);
543503dadc2fSPeter Brune }
543603dadc2fSPeter Brune 
5437c73cfb54SMatthew G. Knepley /*@
5438c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5439c73cfb54SMatthew G. Knepley 
5440c73cfb54SMatthew G. Knepley   Not collective
5441c73cfb54SMatthew G. Knepley 
5442c73cfb54SMatthew G. Knepley   Input Parameter:
5443c73cfb54SMatthew G. Knepley . dm - The DM
5444c73cfb54SMatthew G. Knepley 
5445c73cfb54SMatthew G. Knepley   Output Parameter:
5446c73cfb54SMatthew G. Knepley . dim - The topological dimension
5447c73cfb54SMatthew G. Knepley 
5448c73cfb54SMatthew G. Knepley   Level: beginner
5449c73cfb54SMatthew G. Knepley 
5450c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5451c73cfb54SMatthew G. Knepley @*/
5452c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5453c73cfb54SMatthew G. Knepley {
5454c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5455c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5456534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5457c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5458c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5459c73cfb54SMatthew G. Knepley }
5460c73cfb54SMatthew G. Knepley 
5461c73cfb54SMatthew G. Knepley /*@
5462c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5463c73cfb54SMatthew G. Knepley 
5464c73cfb54SMatthew G. Knepley   Collective on dm
5465c73cfb54SMatthew G. Knepley 
5466c73cfb54SMatthew G. Knepley   Input Parameters:
5467c73cfb54SMatthew G. Knepley + dm - The DM
5468c73cfb54SMatthew G. Knepley - dim - The topological dimension
5469c73cfb54SMatthew G. Knepley 
5470c73cfb54SMatthew G. Knepley   Level: beginner
5471c73cfb54SMatthew G. Knepley 
5472c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5473c73cfb54SMatthew G. Knepley @*/
5474c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5475c73cfb54SMatthew G. Knepley {
5476e5e52638SMatthew G. Knepley   PetscDS        ds;
5477f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5478f17e8794SMatthew G. Knepley 
5479c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5480c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5481c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5482c73cfb54SMatthew G. Knepley   dm->dim = dim;
5483e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5484e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5485c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5486c73cfb54SMatthew G. Knepley }
5487c73cfb54SMatthew G. Knepley 
5488793f3fe5SMatthew G. Knepley /*@
5489793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5490793f3fe5SMatthew G. Knepley 
5491d083f849SBarry Smith   Collective on dm
5492793f3fe5SMatthew G. Knepley 
5493793f3fe5SMatthew G. Knepley   Input Parameters:
5494793f3fe5SMatthew G. Knepley + dm - the DM
5495793f3fe5SMatthew G. Knepley - dim - the dimension
5496793f3fe5SMatthew G. Knepley 
5497793f3fe5SMatthew G. Knepley   Output Parameters:
5498793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5499aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5500793f3fe5SMatthew G. Knepley 
5501793f3fe5SMatthew G. Knepley   Note:
5502793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5503a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5504793f3fe5SMatthew G. Knepley   then the interval is empty.
5505793f3fe5SMatthew G. Knepley 
5506793f3fe5SMatthew G. Knepley   Level: intermediate
5507793f3fe5SMatthew G. Knepley 
5508793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5509793f3fe5SMatthew G. Knepley @*/
5510793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5511793f3fe5SMatthew G. Knepley {
5512793f3fe5SMatthew G. Knepley   PetscInt       d;
5513793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5514793f3fe5SMatthew G. Knepley 
5515793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5516793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5517793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5518793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5519b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5520793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5521793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5522793f3fe5SMatthew G. Knepley }
5523793f3fe5SMatthew G. Knepley 
55246636e97aSMatthew G Knepley /*@
55256636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
55266636e97aSMatthew G Knepley 
5527d083f849SBarry Smith   Collective on dm
55286636e97aSMatthew G Knepley 
55296636e97aSMatthew G Knepley   Input Parameters:
55306636e97aSMatthew G Knepley + dm - the DM
55316636e97aSMatthew G Knepley - c - coordinate vector
55326636e97aSMatthew G Knepley 
55332dd40e9bSPatrick Sanan   Notes:
55342dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
55352dd40e9bSPatrick Sanan 
55362dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
55376636e97aSMatthew G Knepley 
55386636e97aSMatthew G Knepley   Level: intermediate
55396636e97aSMatthew G Knepley 
55402dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
55416636e97aSMatthew G Knepley @*/
55426636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
55436636e97aSMatthew G Knepley {
55446636e97aSMatthew G Knepley   PetscErrorCode ierr;
55456636e97aSMatthew G Knepley 
55466636e97aSMatthew G Knepley   PetscFunctionBegin;
55476636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55486636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55496636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55506636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55516636e97aSMatthew G Knepley   dm->coordinates = c;
55526636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5553b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
555403dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
55556636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55566636e97aSMatthew G Knepley }
55576636e97aSMatthew G Knepley 
55586636e97aSMatthew G Knepley /*@
55596636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
55606636e97aSMatthew G Knepley 
55617058e716SVaclav Hapla   Not collective
55626636e97aSMatthew G Knepley 
55636636e97aSMatthew G Knepley    Input Parameters:
55646636e97aSMatthew G Knepley +  dm - the DM
55656636e97aSMatthew G Knepley -  c - coordinate vector
55666636e97aSMatthew G Knepley 
55672dd40e9bSPatrick Sanan   Notes:
55686636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
55696636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
55706636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
55716636e97aSMatthew G Knepley 
55722dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
55732dd40e9bSPatrick Sanan 
55746636e97aSMatthew G Knepley   Level: intermediate
55756636e97aSMatthew G Knepley 
55766636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
55776636e97aSMatthew G Knepley @*/
55786636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
55796636e97aSMatthew G Knepley {
55806636e97aSMatthew G Knepley   PetscErrorCode ierr;
55816636e97aSMatthew G Knepley 
55826636e97aSMatthew G Knepley   PetscFunctionBegin;
55836636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55846636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55856636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55866636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
55878865f1eaSKarl Rupp 
55886636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
55898865f1eaSKarl Rupp 
55906636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55916636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55926636e97aSMatthew G Knepley }
55936636e97aSMatthew G Knepley 
55946636e97aSMatthew G Knepley /*@
55956636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
55966636e97aSMatthew G Knepley 
5597d083f849SBarry Smith   Collective on dm
55986636e97aSMatthew G Knepley 
55996636e97aSMatthew G Knepley   Input Parameter:
56006636e97aSMatthew G Knepley . dm - the DM
56016636e97aSMatthew G Knepley 
56026636e97aSMatthew G Knepley   Output Parameter:
56036636e97aSMatthew G Knepley . c - global coordinate vector
56046636e97aSMatthew G Knepley 
56056636e97aSMatthew G Knepley   Note:
56066636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
56076636e97aSMatthew G Knepley 
56086636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
56096636e97aSMatthew G Knepley 
56106636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56116636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56126636e97aSMatthew G Knepley 
56136636e97aSMatthew G Knepley   Level: intermediate
56146636e97aSMatthew G Knepley 
56156636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
56166636e97aSMatthew G Knepley @*/
56176636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
56186636e97aSMatthew G Knepley {
56196636e97aSMatthew G Knepley   PetscErrorCode ierr;
56206636e97aSMatthew G Knepley 
56216636e97aSMatthew G Knepley   PetscFunctionBegin;
56226636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56236636e97aSMatthew G Knepley   PetscValidPointer(c,2);
56241f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
56250298fd71SBarry Smith     DM        cdm = NULL;
56261970a576SMatthew G. Knepley     PetscBool localized;
56276636e97aSMatthew G Knepley 
56286636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
56296636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
56301970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
56311970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
56321970a576SMatthew G. Knepley     if (localized) {
56331970a576SMatthew G. Knepley       PetscInt cdim;
56341970a576SMatthew G. Knepley 
56351970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
56361970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
56371970a576SMatthew G. Knepley     }
56386636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
56396636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
56406636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
56416636e97aSMatthew G Knepley   }
56426636e97aSMatthew G Knepley   *c = dm->coordinates;
56436636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56446636e97aSMatthew G Knepley }
56456636e97aSMatthew G Knepley 
56466636e97aSMatthew G Knepley /*@
564781e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
564881e9a530SVaclav Hapla 
5649d083f849SBarry Smith   Collective on dm
565081e9a530SVaclav Hapla 
565181e9a530SVaclav Hapla   Input Parameter:
565281e9a530SVaclav Hapla . dm - the DM
565381e9a530SVaclav Hapla 
565481e9a530SVaclav Hapla   Level: advanced
565581e9a530SVaclav Hapla 
565681e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
565781e9a530SVaclav Hapla @*/
565881e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
565981e9a530SVaclav Hapla {
566081e9a530SVaclav Hapla   PetscErrorCode ierr;
566181e9a530SVaclav Hapla 
566281e9a530SVaclav Hapla   PetscFunctionBegin;
566381e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
566481e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
56651970a576SMatthew G. Knepley     DM        cdm = NULL;
56661970a576SMatthew G. Knepley     PetscBool localized;
56671970a576SMatthew G. Knepley 
566881e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
566981e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
56701970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
56711970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
56721970a576SMatthew G. Knepley     if (localized) {
56731970a576SMatthew G. Knepley       PetscInt cdim;
56741970a576SMatthew G. Knepley 
56751970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
56761970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
56771970a576SMatthew G. Knepley     }
567881e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
567981e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
568081e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
568181e9a530SVaclav Hapla   }
568281e9a530SVaclav Hapla   PetscFunctionReturn(0);
568381e9a530SVaclav Hapla }
568481e9a530SVaclav Hapla 
568581e9a530SVaclav Hapla /*@
56866636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
56876636e97aSMatthew G Knepley 
5688d083f849SBarry Smith   Collective on dm
56896636e97aSMatthew G Knepley 
56906636e97aSMatthew G Knepley   Input Parameter:
56916636e97aSMatthew G Knepley . dm - the DM
56926636e97aSMatthew G Knepley 
56936636e97aSMatthew G Knepley   Output Parameter:
56946636e97aSMatthew G Knepley . c - coordinate vector
56956636e97aSMatthew G Knepley 
56966636e97aSMatthew G Knepley   Note:
56976636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
56986636e97aSMatthew G Knepley 
56996636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
57006636e97aSMatthew G Knepley 
57016636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
57026636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
57036636e97aSMatthew G Knepley 
57046636e97aSMatthew G Knepley   Level: intermediate
57056636e97aSMatthew G Knepley 
570681e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
57076636e97aSMatthew G Knepley @*/
57086636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
57096636e97aSMatthew G Knepley {
57106636e97aSMatthew G Knepley   PetscErrorCode ierr;
57116636e97aSMatthew G Knepley 
57126636e97aSMatthew G Knepley   PetscFunctionBegin;
57136636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57146636e97aSMatthew G Knepley   PetscValidPointer(c,2);
571581e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
57166636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
57176636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57186636e97aSMatthew G Knepley }
57196636e97aSMatthew G Knepley 
572081e9a530SVaclav Hapla /*@
572181e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
572281e9a530SVaclav Hapla 
572381e9a530SVaclav Hapla   Not collective
572481e9a530SVaclav Hapla 
572581e9a530SVaclav Hapla   Input Parameter:
572681e9a530SVaclav Hapla . dm - the DM
572781e9a530SVaclav Hapla 
572881e9a530SVaclav Hapla   Output Parameter:
572981e9a530SVaclav Hapla . c - coordinate vector
573081e9a530SVaclav Hapla 
573181e9a530SVaclav Hapla   Level: advanced
573281e9a530SVaclav Hapla 
573381e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
573481e9a530SVaclav Hapla @*/
573581e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
573681e9a530SVaclav Hapla {
573781e9a530SVaclav Hapla   PetscFunctionBegin;
573881e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
573981e9a530SVaclav Hapla   PetscValidPointer(c,2);
574081e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
57416636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
57426636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57436636e97aSMatthew G Knepley }
57446636e97aSMatthew G Knepley 
57452db98f8dSVaclav Hapla /*@
57462db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
57472db98f8dSVaclav Hapla 
57482db98f8dSVaclav Hapla   Not collective
57492db98f8dSVaclav Hapla 
57502db98f8dSVaclav Hapla   Input Parameter:
57512db98f8dSVaclav Hapla + dm - the DM
57522db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
57532db98f8dSVaclav Hapla 
57542db98f8dSVaclav Hapla   Output Parameter:
57552db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
57562db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
57572db98f8dSVaclav Hapla 
57582db98f8dSVaclav Hapla   Note:
57592db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
57602db98f8dSVaclav Hapla 
57612db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
57622db98f8dSVaclav Hapla 
57632db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
57642db98f8dSVaclav Hapla 
57652db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
57662db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
57672db98f8dSVaclav Hapla 
57682db98f8dSVaclav Hapla   Level: advanced
57692db98f8dSVaclav Hapla 
57702db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
57712db98f8dSVaclav Hapla @*/
57722db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
57732db98f8dSVaclav Hapla {
57742db98f8dSVaclav Hapla   PetscSection        cs, newcs;
57752db98f8dSVaclav Hapla   Vec                 coords;
57762db98f8dSVaclav Hapla   const PetscScalar   *arr;
57772db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
57782db98f8dSVaclav Hapla   PetscInt            n;
57792db98f8dSVaclav Hapla   PetscErrorCode      ierr;
57802db98f8dSVaclav Hapla 
57812db98f8dSVaclav Hapla   PetscFunctionBegin;
57822db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57832db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
57842db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
57852db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
57862db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
57871bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
57881bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
57892db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
57902db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
57912db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
57922db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
57932db98f8dSVaclav Hapla   if (pCoord) {
57942db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
57952db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
57962db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
57972db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5798ad9ac99dSVaclav Hapla   } else {
5799ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
58002db98f8dSVaclav Hapla   }
5801ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5802ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
58032db98f8dSVaclav Hapla   PetscFunctionReturn(0);
58042db98f8dSVaclav Hapla }
58052db98f8dSVaclav Hapla 
5806f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5807f19dbd58SToby Isaac {
5808f19dbd58SToby Isaac   PetscErrorCode ierr;
5809f19dbd58SToby Isaac 
5810f19dbd58SToby Isaac   PetscFunctionBegin;
5811f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5812f19dbd58SToby Isaac   PetscValidPointer(field,2);
5813f19dbd58SToby Isaac   if (!dm->coordinateField) {
5814f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5815f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5816f19dbd58SToby Isaac     }
5817f19dbd58SToby Isaac   }
5818f19dbd58SToby Isaac   *field = dm->coordinateField;
5819f19dbd58SToby Isaac   PetscFunctionReturn(0);
5820f19dbd58SToby Isaac }
5821f19dbd58SToby Isaac 
5822f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5823f19dbd58SToby Isaac {
5824f19dbd58SToby Isaac   PetscErrorCode ierr;
5825f19dbd58SToby Isaac 
5826f19dbd58SToby Isaac   PetscFunctionBegin;
5827f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5828f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5829c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5830f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5831f19dbd58SToby Isaac   dm->coordinateField = field;
5832f19dbd58SToby Isaac   PetscFunctionReturn(0);
5833f19dbd58SToby Isaac }
5834f19dbd58SToby Isaac 
58356636e97aSMatthew G Knepley /*@
58361cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
58376636e97aSMatthew G Knepley 
5838d083f849SBarry Smith   Collective on dm
58396636e97aSMatthew G Knepley 
58406636e97aSMatthew G Knepley   Input Parameter:
58416636e97aSMatthew G Knepley . dm - the DM
58426636e97aSMatthew G Knepley 
58436636e97aSMatthew G Knepley   Output Parameter:
58446636e97aSMatthew G Knepley . cdm - coordinate DM
58456636e97aSMatthew G Knepley 
58466636e97aSMatthew G Knepley   Level: intermediate
58476636e97aSMatthew G Knepley 
58481cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58496636e97aSMatthew G Knepley @*/
58506636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
58516636e97aSMatthew G Knepley {
58526636e97aSMatthew G Knepley   PetscErrorCode ierr;
58536636e97aSMatthew G Knepley 
58546636e97aSMatthew G Knepley   PetscFunctionBegin;
58556636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58566636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
58576636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5858308f8a94SToby Isaac     DM cdm;
5859308f8a94SToby Isaac 
586082f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5861308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5862308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5863308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5864308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5865308f8a94SToby Isaac     dm->coordinateDM = cdm;
58666636e97aSMatthew G Knepley   }
58676636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
58686636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58696636e97aSMatthew G Knepley }
5870e87bb0d3SMatthew G Knepley 
58711cfe2091SMatthew G. Knepley /*@
58721cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
58731cfe2091SMatthew G. Knepley 
5874d083f849SBarry Smith   Logically Collective on dm
58751cfe2091SMatthew G. Knepley 
58761cfe2091SMatthew G. Knepley   Input Parameters:
58771cfe2091SMatthew G. Knepley + dm - the DM
58781cfe2091SMatthew G. Knepley - cdm - coordinate DM
58791cfe2091SMatthew G. Knepley 
58801cfe2091SMatthew G. Knepley   Level: intermediate
58811cfe2091SMatthew G. Knepley 
58821cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58831cfe2091SMatthew G. Knepley @*/
58841cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
58851cfe2091SMatthew G. Knepley {
58861cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
58871cfe2091SMatthew G. Knepley 
58881cfe2091SMatthew G. Knepley   PetscFunctionBegin;
58891cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58901cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5891f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
58921cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
58931cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
58941cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
58951cfe2091SMatthew G. Knepley }
58961cfe2091SMatthew G. Knepley 
589746e270d4SMatthew G. Knepley /*@
589846e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
589946e270d4SMatthew G. Knepley 
590046e270d4SMatthew G. Knepley   Not Collective
590146e270d4SMatthew G. Knepley 
590246e270d4SMatthew G. Knepley   Input Parameter:
590346e270d4SMatthew G. Knepley . dm - The DM object
590446e270d4SMatthew G. Knepley 
590546e270d4SMatthew G. Knepley   Output Parameter:
590646e270d4SMatthew G. Knepley . dim - The embedding dimension
590746e270d4SMatthew G. Knepley 
590846e270d4SMatthew G. Knepley   Level: intermediate
590946e270d4SMatthew G. Knepley 
591092fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
591146e270d4SMatthew G. Knepley @*/
591246e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
591346e270d4SMatthew G. Knepley {
591446e270d4SMatthew G. Knepley   PetscFunctionBegin;
591546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5916534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
59179a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
59189a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
59199a9a41abSToby Isaac   }
592046e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
592146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
592246e270d4SMatthew G. Knepley }
592346e270d4SMatthew G. Knepley 
592446e270d4SMatthew G. Knepley /*@
592546e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
592646e270d4SMatthew G. Knepley 
592746e270d4SMatthew G. Knepley   Not Collective
592846e270d4SMatthew G. Knepley 
592946e270d4SMatthew G. Knepley   Input Parameters:
593046e270d4SMatthew G. Knepley + dm  - The DM object
593146e270d4SMatthew G. Knepley - dim - The embedding dimension
593246e270d4SMatthew G. Knepley 
593346e270d4SMatthew G. Knepley   Level: intermediate
593446e270d4SMatthew G. Knepley 
593592fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
593646e270d4SMatthew G. Knepley @*/
593746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
593846e270d4SMatthew G. Knepley {
5939e5e52638SMatthew G. Knepley   PetscDS        ds;
5940f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5941f17e8794SMatthew G. Knepley 
594246e270d4SMatthew G. Knepley   PetscFunctionBegin;
594346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
594446e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5945e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5946e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
594746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
594846e270d4SMatthew G. Knepley }
594946e270d4SMatthew G. Knepley 
5950e8abe2deSMatthew G. Knepley /*@
5951e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5952e8abe2deSMatthew G. Knepley 
5953d083f849SBarry Smith   Collective on dm
5954e8abe2deSMatthew G. Knepley 
5955e8abe2deSMatthew G. Knepley   Input Parameter:
5956e8abe2deSMatthew G. Knepley . dm - The DM object
5957e8abe2deSMatthew G. Knepley 
5958e8abe2deSMatthew G. Knepley   Output Parameter:
5959e8abe2deSMatthew G. Knepley . section - The PetscSection object
5960e8abe2deSMatthew G. Knepley 
5961e8abe2deSMatthew G. Knepley   Level: intermediate
5962e8abe2deSMatthew G. Knepley 
596392fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
5964e8abe2deSMatthew G. Knepley @*/
5965e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5966e8abe2deSMatthew G. Knepley {
5967e8abe2deSMatthew G. Knepley   DM             cdm;
5968e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5969e8abe2deSMatthew G. Knepley 
5970e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5971e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5972e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5973e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
597492fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
5975e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5976e8abe2deSMatthew G. Knepley }
5977e8abe2deSMatthew G. Knepley 
5978e8abe2deSMatthew G. Knepley /*@
5979e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5980e8abe2deSMatthew G. Knepley 
5981e8abe2deSMatthew G. Knepley   Not Collective
5982e8abe2deSMatthew G. Knepley 
5983e8abe2deSMatthew G. Knepley   Input Parameters:
5984e8abe2deSMatthew G. Knepley + dm      - The DM object
598546e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5986e8abe2deSMatthew G. Knepley - section - The PetscSection object
5987e8abe2deSMatthew G. Knepley 
5988e8abe2deSMatthew G. Knepley   Level: intermediate
5989e8abe2deSMatthew G. Knepley 
599092fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
5991e8abe2deSMatthew G. Knepley @*/
599246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5993e8abe2deSMatthew G. Knepley {
5994e8abe2deSMatthew G. Knepley   DM             cdm;
5995e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5996e8abe2deSMatthew G. Knepley 
5997e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5998e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
599946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6000e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
600192fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
600246e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
60034c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
600446e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
600546e270d4SMatthew G. Knepley 
600646e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
600746e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
600846e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
600946e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
601046e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
601146e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
601246e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
601346e270d4SMatthew G. Knepley     }
6014ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
601546e270d4SMatthew G. Knepley   }
6016e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6017e8abe2deSMatthew G. Knepley }
6018e8abe2deSMatthew G. Knepley 
60195dc8c3f7SMatthew G. Knepley /*@C
602090b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
60215dc8c3f7SMatthew G. Knepley 
60225dc8c3f7SMatthew G. Knepley   Input Parameters:
602390b157c4SStefano Zampini . dm      - The DM object
602490b157c4SStefano Zampini 
602590b157c4SStefano Zampini   Output Parameters:
602690b157c4SStefano Zampini + per     - Whether the DM is periodic or not
60275dc8c3f7SMatthew 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
60285dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
60295dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
60305dc8c3f7SMatthew G. Knepley 
60315dc8c3f7SMatthew G. Knepley   Level: developer
60325dc8c3f7SMatthew G. Knepley 
60335dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
60345dc8c3f7SMatthew G. Knepley @*/
603590b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6036c6b900c6SMatthew G. Knepley {
6037c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6038c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
603990b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6040c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6041c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
60425dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6043c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6044c6b900c6SMatthew G. Knepley }
6045c6b900c6SMatthew G. Knepley 
60465dc8c3f7SMatthew G. Knepley /*@C
60475dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
60485dc8c3f7SMatthew G. Knepley 
60495dc8c3f7SMatthew G. Knepley   Input Parameters:
60505dc8c3f7SMatthew G. Knepley + dm      - The DM object
605190b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
60525dc8c3f7SMatthew 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
60535dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
60545dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
60555dc8c3f7SMatthew G. Knepley 
60565dc8c3f7SMatthew G. Knepley   Level: developer
60575dc8c3f7SMatthew G. Knepley 
60585dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
60595dc8c3f7SMatthew G. Knepley @*/
606090b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6061c6b900c6SMatthew G. Knepley {
6062c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6063c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6064c6b900c6SMatthew G. Knepley 
6065c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6066c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
606790b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6068412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6069412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6070412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
60715dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
607290b157c4SStefano Zampini   if (maxCell) {
6073412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6074412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6075412e9a14SMatthew G. Knepley   }
6076412e9a14SMatthew G. Knepley   if (L) {
6077412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6078412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6079412e9a14SMatthew G. Knepley   }
6080412e9a14SMatthew G. Knepley   if (bd) {
6081412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6082412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
608390b157c4SStefano Zampini   }
6084072d7d67SStefano Zampini   dm->periodic = per;
6085c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6086c6b900c6SMatthew G. Knepley }
6087c6b900c6SMatthew G. Knepley 
60882e17dfb7SMatthew G. Knepley /*@
60892e17dfb7SMatthew 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.
60902e17dfb7SMatthew G. Knepley 
60912e17dfb7SMatthew G. Knepley   Input Parameters:
60922e17dfb7SMatthew G. Knepley + dm     - The DM
609365da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
609465da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
60952e17dfb7SMatthew G. Knepley 
60962e17dfb7SMatthew G. Knepley   Output Parameter:
60972e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60982e17dfb7SMatthew G. Knepley 
60992e17dfb7SMatthew G. Knepley   Level: developer
61002e17dfb7SMatthew G. Knepley 
61012e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
61022e17dfb7SMatthew G. Knepley @*/
610365da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
61042e17dfb7SMatthew G. Knepley {
61052e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
61062e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
61072e17dfb7SMatthew G. Knepley 
61082e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61092e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
61102e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61112e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61122e17dfb7SMatthew G. Knepley   } else {
611365da65dcSMatthew G. Knepley     if (endpoint) {
611465da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6115da3333bfSMatthew 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)) {
6116da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
611765da65dcSMatthew G. Knepley         } else {
6118da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
611965da65dcSMatthew G. Knepley         }
612065da65dcSMatthew G. Knepley       }
612165da65dcSMatthew G. Knepley     } else {
61222e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
61231118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
61242e17dfb7SMatthew G. Knepley       }
61252e17dfb7SMatthew G. Knepley     }
612665da65dcSMatthew G. Knepley   }
61272e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61282e17dfb7SMatthew G. Knepley }
61292e17dfb7SMatthew G. Knepley 
61302e17dfb7SMatthew G. Knepley /*
61312e17dfb7SMatthew 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.
61322e17dfb7SMatthew G. Knepley 
61332e17dfb7SMatthew G. Knepley   Input Parameters:
61342e17dfb7SMatthew G. Knepley + dm     - The DM
61352e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
61362e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
61372e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
61382e17dfb7SMatthew G. Knepley 
61392e17dfb7SMatthew G. Knepley   Output Parameter:
61402e17dfb7SMatthew G. Knepley . out - The localized coordinate point
61412e17dfb7SMatthew G. Knepley 
61422e17dfb7SMatthew G. Knepley   Level: developer
61432e17dfb7SMatthew G. Knepley 
61442e17dfb7SMatthew 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
61452e17dfb7SMatthew G. Knepley 
61462e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
61472e17dfb7SMatthew G. Knepley */
61482e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
61492e17dfb7SMatthew G. Knepley {
61502e17dfb7SMatthew G. Knepley   PetscInt d;
61512e17dfb7SMatthew G. Knepley 
61522e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61532e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61542e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61552e17dfb7SMatthew G. Knepley   } else {
61562e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6157908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
61582e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
61592e17dfb7SMatthew G. Knepley       } else {
61602e17dfb7SMatthew G. Knepley         out[d] = in[d];
61612e17dfb7SMatthew G. Knepley       }
61622e17dfb7SMatthew G. Knepley     }
61632e17dfb7SMatthew G. Knepley   }
61642e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61652e17dfb7SMatthew G. Knepley }
61662e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
61672e17dfb7SMatthew G. Knepley {
61682e17dfb7SMatthew G. Knepley   PetscInt d;
61692e17dfb7SMatthew G. Knepley 
61702e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61712e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61722e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61732e17dfb7SMatthew G. Knepley   } else {
61742e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6175908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
61762e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
61772e17dfb7SMatthew G. Knepley       } else {
61782e17dfb7SMatthew G. Knepley         out[d] = in[d];
61792e17dfb7SMatthew G. Knepley       }
61802e17dfb7SMatthew G. Knepley     }
61812e17dfb7SMatthew G. Knepley   }
61822e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61832e17dfb7SMatthew G. Knepley }
61842e17dfb7SMatthew G. Knepley 
61852e17dfb7SMatthew G. Knepley /*
61862e17dfb7SMatthew 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.
61872e17dfb7SMatthew G. Knepley 
61882e17dfb7SMatthew G. Knepley   Input Parameters:
61892e17dfb7SMatthew G. Knepley + dm     - The DM
61902e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
61912e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
61922e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
61932e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
61942e17dfb7SMatthew G. Knepley 
61952e17dfb7SMatthew G. Knepley   Output Parameter:
61962e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
61972e17dfb7SMatthew G. Knepley 
61982e17dfb7SMatthew G. Knepley   Level: developer
61992e17dfb7SMatthew G. Knepley 
62002e17dfb7SMatthew 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
62012e17dfb7SMatthew G. Knepley 
62022e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
62032e17dfb7SMatthew G. Knepley */
62042e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
62052e17dfb7SMatthew G. Knepley {
62062e17dfb7SMatthew G. Knepley   PetscInt d;
62072e17dfb7SMatthew G. Knepley 
62082e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
62092e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
62102e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
62112e17dfb7SMatthew G. Knepley   } else {
62122e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6213412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6214412e9a14SMatthew G. Knepley 
6215412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6216412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6217412e9a14SMatthew G. Knepley 
6218412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6219412e9a14SMatthew 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]));
6220412e9a14SMatthew G. Knepley         out[d] += newCoord;
62212e17dfb7SMatthew G. Knepley       } else {
62222e17dfb7SMatthew G. Knepley         out[d] += in[d];
62232e17dfb7SMatthew G. Knepley       }
62242e17dfb7SMatthew G. Knepley     }
62252e17dfb7SMatthew G. Knepley   }
62262e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
62272e17dfb7SMatthew G. Knepley }
62282e17dfb7SMatthew G. Knepley 
622936447a5eSToby Isaac /*@
62308f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
62318f700142SStefano Zampini 
62328f700142SStefano Zampini   Not collective
623336447a5eSToby Isaac 
623436447a5eSToby Isaac   Input Parameter:
623536447a5eSToby Isaac . dm - The DM
623636447a5eSToby Isaac 
623736447a5eSToby Isaac   Output Parameter:
623836447a5eSToby Isaac   areLocalized - True if localized
623936447a5eSToby Isaac 
624036447a5eSToby Isaac   Level: developer
624136447a5eSToby Isaac 
62428f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
624336447a5eSToby Isaac @*/
62448f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
624536447a5eSToby Isaac {
624636447a5eSToby Isaac   DM             cdm;
624736447a5eSToby Isaac   PetscSection   coordSection;
624846a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
624946a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
625036447a5eSToby Isaac   PetscErrorCode ierr;
625136447a5eSToby Isaac 
625236447a5eSToby Isaac   PetscFunctionBegin;
625336447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6254534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62558b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
625646a3a80fSLisandro Dalcin 
625736447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
625836447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
625946a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
62609f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
626146a3a80fSLisandro Dalcin 
62629f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
626346a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
626436447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
626536447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
626646a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
626746a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
626836447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
626946a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
627036447a5eSToby Isaac   }
62718f700142SStefano Zampini   *areLocalized = alreadyLocalized;
627236447a5eSToby Isaac   PetscFunctionReturn(0);
627336447a5eSToby Isaac }
627436447a5eSToby Isaac 
62758f700142SStefano Zampini /*@
62768f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
62778f700142SStefano Zampini 
62788f700142SStefano Zampini   Collective on dm
62798f700142SStefano Zampini 
62808f700142SStefano Zampini   Input Parameter:
62818f700142SStefano Zampini . dm - The DM
62828f700142SStefano Zampini 
62838f700142SStefano Zampini   Output Parameter:
62848f700142SStefano Zampini   areLocalized - True if localized
62858f700142SStefano Zampini 
62868f700142SStefano Zampini   Level: developer
62878f700142SStefano Zampini 
62888f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
62898f700142SStefano Zampini @*/
62908f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
62918f700142SStefano Zampini {
62928f700142SStefano Zampini   PetscBool      localized;
62938f700142SStefano Zampini   PetscErrorCode ierr;
62948f700142SStefano Zampini 
62958f700142SStefano Zampini   PetscFunctionBegin;
62968f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6297534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62988f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
62998f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
63008f700142SStefano Zampini   PetscFunctionReturn(0);
63018f700142SStefano Zampini }
630236447a5eSToby Isaac 
63032e17dfb7SMatthew G. Knepley /*@
6304492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
63052e17dfb7SMatthew G. Knepley 
63068f700142SStefano Zampini   Collective on dm
63078f700142SStefano Zampini 
63082e17dfb7SMatthew G. Knepley   Input Parameter:
63092e17dfb7SMatthew G. Knepley . dm - The DM
63102e17dfb7SMatthew G. Knepley 
63112e17dfb7SMatthew G. Knepley   Level: developer
63122e17dfb7SMatthew G. Knepley 
63138f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
63142e17dfb7SMatthew G. Knepley @*/
63152e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
63162e17dfb7SMatthew G. Knepley {
63172e17dfb7SMatthew G. Knepley   DM             cdm;
63182e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
63192e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
63203e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
63213e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6322e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
63233e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
63243e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
63252e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
63262e17dfb7SMatthew G. Knepley 
63272e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63282e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
632992c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6330f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6331f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6332f7cbd40bSStefano Zampini 
63332e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
63342e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
63352e17dfb7SMatthew G. Knepley   {
63362e17dfb7SMatthew G. Knepley     PetscBool isplex;
63372e17dfb7SMatthew G. Knepley 
63382e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
63392e17dfb7SMatthew G. Knepley     if (isplex) {
63402e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
63413e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
634269291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
63433e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
63443e922f36SToby Isaac       newStart = vStart;
63453e922f36SToby Isaac       newEnd   = vEnd;
63463e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
63473e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
63483e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
63493e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
63503e922f36SToby Isaac       }
63512e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
63522e17dfb7SMatthew G. Knepley   }
63532e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
635443eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
63552e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
63563e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6357e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
63583e922f36SToby Isaac 
63592e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
63602e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
63612e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
63622e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
63633e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
63643e922f36SToby Isaac 
636569291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63663e922f36SToby Isaac   localized = &anchor[bs];
63673e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
63683e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63693e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63703e922f36SToby Isaac 
63713e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
63723e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
63733e922f36SToby Isaac       PetscInt     b;
63743e922f36SToby Isaac 
63753e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
63763e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63773e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63783e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
63793e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
63803e922f36SToby Isaac         for (b = 0; b < bs; b++) {
63813e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
63823e922f36SToby Isaac         }
63833e922f36SToby Isaac         if (b < bs) break;
63843e922f36SToby Isaac       }
63853e922f36SToby Isaac       if (d < dof/bs) {
63863e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
63873e922f36SToby Isaac           PetscInt cdof;
63883e922f36SToby Isaac 
63893e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
63903e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
63913e922f36SToby Isaac         }
63923e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
63933e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
63943e922f36SToby Isaac       }
63953e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63963e922f36SToby Isaac     }
63973e922f36SToby Isaac   }
63983e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
63993e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
640069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
64013e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
640269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
64033e922f36SToby Isaac     PetscFunctionReturn(0);
64043e922f36SToby Isaac   }
64052e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
64062e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
64072e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
64082e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
64092e17dfb7SMatthew G. Knepley   }
64102e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
64112e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6412c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
64132e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
64142e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
64152e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
64162e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6417c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
64182e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
64192e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
64202e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
64212e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
64222e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
64232e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
64242e17dfb7SMatthew G. Knepley   }
64253e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
64263e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
64273e922f36SToby Isaac 
64282e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
64292e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
64303e922f36SToby Isaac       PetscInt     b, cdof;
64312e17dfb7SMatthew G. Knepley 
64323e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
64333e922f36SToby Isaac       if (!cdof) continue;
64342e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
64352e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
64362e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
64372e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
64382e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
64392e17dfb7SMatthew G. Knepley     }
64403e922f36SToby Isaac   }
644169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
644269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6443c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
64442e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
64452e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
64462e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
64472e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
64482e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
64492e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
64502e17dfb7SMatthew G. Knepley }
64512e17dfb7SMatthew G. Knepley 
6452e87bb0d3SMatthew G Knepley /*@
64533a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6454e87bb0d3SMatthew G Knepley 
6455d083f849SBarry Smith   Collective on v (see explanation below)
6456e87bb0d3SMatthew G Knepley 
6457e87bb0d3SMatthew G Knepley   Input Parameters:
6458e87bb0d3SMatthew G Knepley + dm - The DM
64593a93e3b7SToby Isaac . v - The Vec of points
646062a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
64613a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6462e87bb0d3SMatthew G Knepley 
646361e3bb9bSMatthew G Knepley   Output Parameter:
646462a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
646562a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
64663a93e3b7SToby Isaac 
6467e87bb0d3SMatthew G Knepley 
6468e87bb0d3SMatthew G Knepley   Level: developer
646961e3bb9bSMatthew G Knepley 
647062a38674SMatthew G. Knepley   Notes:
64713a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
647262a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
64733a93e3b7SToby Isaac 
64743a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
647562a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
64763a93e3b7SToby Isaac 
64773a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
64783a93e3b7SToby Isaac 
647962a38674SMatthew G. Knepley $    const PetscSFNode *cells;
648062a38674SMatthew G. Knepley $    PetscInt           nFound;
6481a6216909SToby Isaac $    const PetscInt    *found;
648262a38674SMatthew G. Knepley $
6483a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
64843a93e3b7SToby Isaac 
64853a93e3b7SToby 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
64863a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
64873a93e3b7SToby Isaac 
648862a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
648961e3bb9bSMatthew G Knepley @*/
649062a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6491e87bb0d3SMatthew G Knepley {
6492735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6493735aa83eSMatthew G Knepley 
6494e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6495e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6496e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6497e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
64983a93e3b7SToby Isaac   if (*cellSF) {
64993a93e3b7SToby Isaac     PetscMPIInt result;
65003a93e3b7SToby Isaac 
6501e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6502a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
65033a93e3b7SToby 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");
6504e0fc9d1bSMatthew G. Knepley   } else {
65053a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
65063a93e3b7SToby Isaac   }
6507b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
650847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
650962a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
651047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6511e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6512e87bb0d3SMatthew G Knepley }
651314f150ffSMatthew G. Knepley 
6514f4d763aaSMatthew G. Knepley /*@
6515f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6516f4d763aaSMatthew G. Knepley 
65178f700142SStefano Zampini   Collective on dm
65188f700142SStefano Zampini 
6519f4d763aaSMatthew G. Knepley   Input Parameter:
6520f4d763aaSMatthew G. Knepley . dm - The original DM
6521f4d763aaSMatthew G. Knepley 
6522f4d763aaSMatthew G. Knepley   Output Parameter:
6523f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6524f4d763aaSMatthew G. Knepley 
6525f4d763aaSMatthew G. Knepley   Level: intermediate
6526f4d763aaSMatthew G. Knepley 
6527e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6528f4d763aaSMatthew G. Knepley @*/
652914f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
653014f150ffSMatthew G. Knepley {
6531c26acbdeSMatthew G. Knepley   PetscSection   section;
65322d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
653314f150ffSMatthew G. Knepley   PetscErrorCode ierr;
653414f150ffSMatthew G. Knepley 
653514f150ffSMatthew G. Knepley   PetscFunctionBegin;
653614f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
653714f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
653892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6539c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6540127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
65412d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6542c26acbdeSMatthew G. Knepley     *odm = dm;
6543c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6544c26acbdeSMatthew G. Knepley   }
654514f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6546c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
654714f150ffSMatthew G. Knepley     PetscSF      sf;
654814f150ffSMatthew G. Knepley 
654914f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6550e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
655114f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
655292fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
655314f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
655414f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
655515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6556e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
655714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
655814f150ffSMatthew G. Knepley   }
655914f150ffSMatthew G. Knepley   *odm = dm->dmBC;
656014f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
656114f150ffSMatthew G. Knepley }
6562f4d763aaSMatthew G. Knepley 
6563f4d763aaSMatthew G. Knepley /*@
6564cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6565f4d763aaSMatthew G. Knepley 
6566f4d763aaSMatthew G. Knepley   Input Parameter:
6567f4d763aaSMatthew G. Knepley . dm - The original DM
6568f4d763aaSMatthew G. Knepley 
6569cdb7a50dSMatthew G. Knepley   Output Parameters:
6570cdb7a50dSMatthew G. Knepley + num - The output sequence number
6571cdb7a50dSMatthew G. Knepley - val - The output sequence value
6572f4d763aaSMatthew G. Knepley 
6573f4d763aaSMatthew G. Knepley   Level: intermediate
6574f4d763aaSMatthew G. Knepley 
6575f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6576f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6577f4d763aaSMatthew G. Knepley 
6578f4d763aaSMatthew G. Knepley .seealso: VecView()
6579f4d763aaSMatthew G. Knepley @*/
6580cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6581f4d763aaSMatthew G. Knepley {
6582f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6583f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6584534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6585534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6586f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6587f4d763aaSMatthew G. Knepley }
6588f4d763aaSMatthew G. Knepley 
6589f4d763aaSMatthew G. Knepley /*@
6590cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6591f4d763aaSMatthew G. Knepley 
6592f4d763aaSMatthew G. Knepley   Input Parameters:
6593f4d763aaSMatthew G. Knepley + dm - The original DM
6594cdb7a50dSMatthew G. Knepley . num - The output sequence number
6595cdb7a50dSMatthew G. Knepley - val - The output sequence value
6596f4d763aaSMatthew G. Knepley 
6597f4d763aaSMatthew G. Knepley   Level: intermediate
6598f4d763aaSMatthew G. Knepley 
6599f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6600f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6601f4d763aaSMatthew G. Knepley 
6602f4d763aaSMatthew G. Knepley .seealso: VecView()
6603f4d763aaSMatthew G. Knepley @*/
6604cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6605f4d763aaSMatthew G. Knepley {
6606f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6607f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6608f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6609cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6610cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6611cdb7a50dSMatthew G. Knepley }
6612cdb7a50dSMatthew G. Knepley 
6613cdb7a50dSMatthew G. Knepley /*@C
6614cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6615cdb7a50dSMatthew G. Knepley 
6616cdb7a50dSMatthew G. Knepley   Input Parameters:
6617cdb7a50dSMatthew G. Knepley + dm   - The original DM
6618cdb7a50dSMatthew G. Knepley . name - The sequence name
6619cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6620cdb7a50dSMatthew G. Knepley 
6621cdb7a50dSMatthew G. Knepley   Output Parameter:
6622cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6623cdb7a50dSMatthew G. Knepley 
6624cdb7a50dSMatthew G. Knepley   Level: intermediate
6625cdb7a50dSMatthew G. Knepley 
6626cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6627cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6628cdb7a50dSMatthew G. Knepley 
6629cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6630cdb7a50dSMatthew G. Knepley @*/
6631cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6632cdb7a50dSMatthew G. Knepley {
6633cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6634cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6635cdb7a50dSMatthew G. Knepley 
6636cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6637cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6638cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6639534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6640cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6641cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6642cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6643cdb7a50dSMatthew G. Knepley     PetscScalar value;
6644cdb7a50dSMatthew G. Knepley 
664539d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
66464aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6647cdb7a50dSMatthew G. Knepley #endif
6648cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6649f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6650f4d763aaSMatthew G. Knepley }
66518e4ac7eaSMatthew G. Knepley 
66528e4ac7eaSMatthew G. Knepley /*@
66538e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
66548e4ac7eaSMatthew G. Knepley 
66558e4ac7eaSMatthew G. Knepley   Not collective
66568e4ac7eaSMatthew G. Knepley 
66578e4ac7eaSMatthew G. Knepley   Input Parameter:
66588e4ac7eaSMatthew G. Knepley . dm - The DM
66598e4ac7eaSMatthew G. Knepley 
66608e4ac7eaSMatthew G. Knepley   Output Parameter:
66618e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
66628e4ac7eaSMatthew G. Knepley 
66638e4ac7eaSMatthew G. Knepley   Level: beginner
66648e4ac7eaSMatthew G. Knepley 
66658e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
66668e4ac7eaSMatthew G. Knepley @*/
66678e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
66688e4ac7eaSMatthew G. Knepley {
66698e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66708e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6671534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
66728e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66738e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66748e4ac7eaSMatthew G. Knepley }
66758e4ac7eaSMatthew G. Knepley 
66768e4ac7eaSMatthew G. Knepley /*@
66775d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
66788e4ac7eaSMatthew G. Knepley 
66798e4ac7eaSMatthew G. Knepley   Collective on dm
66808e4ac7eaSMatthew G. Knepley 
66818e4ac7eaSMatthew G. Knepley   Input Parameters:
66828e4ac7eaSMatthew G. Knepley + dm - The DM
66838e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
66848e4ac7eaSMatthew G. Knepley 
66855d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
66865d3b26e6SMatthew G. Knepley 
66878e4ac7eaSMatthew G. Knepley   Level: beginner
66888e4ac7eaSMatthew G. Knepley 
66895d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
66908e4ac7eaSMatthew G. Knepley @*/
66918e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
66928e4ac7eaSMatthew G. Knepley {
66938e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66948e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66958833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66968e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66978e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66988e4ac7eaSMatthew G. Knepley }
6699c58f1c22SToby Isaac 
6700c58f1c22SToby Isaac 
6701c58f1c22SToby Isaac /*@C
6702c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6703c58f1c22SToby Isaac 
6704c58f1c22SToby Isaac   Not Collective
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac   Input Parameters:
6707c58f1c22SToby Isaac + dm   - The DM object
6708c58f1c22SToby Isaac - name - The label name
6709c58f1c22SToby Isaac 
6710c58f1c22SToby Isaac   Level: intermediate
6711c58f1c22SToby Isaac 
6712c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6713c58f1c22SToby Isaac @*/
6714c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6715c58f1c22SToby Isaac {
67165d80c0bfSVaclav Hapla   PetscBool      flg;
67175d80c0bfSVaclav Hapla   DMLabel        label;
6718c58f1c22SToby Isaac   PetscErrorCode ierr;
6719c58f1c22SToby Isaac 
6720c58f1c22SToby Isaac   PetscFunctionBegin;
6721c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6722c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
67235d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
6724c58f1c22SToby Isaac   if (!flg) {
67255d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
67265d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
67275d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
6728c58f1c22SToby Isaac   }
6729c58f1c22SToby Isaac   PetscFunctionReturn(0);
6730c58f1c22SToby Isaac }
6731c58f1c22SToby Isaac 
6732c58f1c22SToby Isaac /*@C
6733c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6734c58f1c22SToby Isaac 
6735c58f1c22SToby Isaac   Not Collective
6736c58f1c22SToby Isaac 
6737c58f1c22SToby Isaac   Input Parameters:
6738c58f1c22SToby Isaac + dm   - The DM object
6739c58f1c22SToby Isaac . name - The label name
6740c58f1c22SToby Isaac - point - The mesh point
6741c58f1c22SToby Isaac 
6742c58f1c22SToby Isaac   Output Parameter:
6743c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6744c58f1c22SToby Isaac 
6745c58f1c22SToby Isaac   Level: beginner
6746c58f1c22SToby Isaac 
6747c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6748c58f1c22SToby Isaac @*/
6749c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6750c58f1c22SToby Isaac {
6751c58f1c22SToby Isaac   DMLabel        label;
6752c58f1c22SToby Isaac   PetscErrorCode ierr;
6753c58f1c22SToby Isaac 
6754c58f1c22SToby Isaac   PetscFunctionBegin;
6755c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6756c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6757c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
675813903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6759c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6760c58f1c22SToby Isaac   PetscFunctionReturn(0);
6761c58f1c22SToby Isaac }
6762c58f1c22SToby Isaac 
6763c58f1c22SToby Isaac /*@C
6764c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6765c58f1c22SToby Isaac 
6766c58f1c22SToby Isaac   Not Collective
6767c58f1c22SToby Isaac 
6768c58f1c22SToby Isaac   Input Parameters:
6769c58f1c22SToby Isaac + dm   - The DM object
6770c58f1c22SToby Isaac . name - The label name
6771c58f1c22SToby Isaac . point - The mesh point
6772c58f1c22SToby Isaac - value - The label value for this point
6773c58f1c22SToby Isaac 
6774c58f1c22SToby Isaac   Output Parameter:
6775c58f1c22SToby Isaac 
6776c58f1c22SToby Isaac   Level: beginner
6777c58f1c22SToby Isaac 
6778c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6779c58f1c22SToby Isaac @*/
6780c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6781c58f1c22SToby Isaac {
6782c58f1c22SToby Isaac   DMLabel        label;
6783c58f1c22SToby Isaac   PetscErrorCode ierr;
6784c58f1c22SToby Isaac 
6785c58f1c22SToby Isaac   PetscFunctionBegin;
6786c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6787c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6788c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6789c58f1c22SToby Isaac   if (!label) {
6790c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6791c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6792c58f1c22SToby Isaac   }
6793c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6794c58f1c22SToby Isaac   PetscFunctionReturn(0);
6795c58f1c22SToby Isaac }
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac /*@C
6798c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac   Not Collective
6801c58f1c22SToby Isaac 
6802c58f1c22SToby Isaac   Input Parameters:
6803c58f1c22SToby Isaac + dm   - The DM object
6804c58f1c22SToby Isaac . name - The label name
6805c58f1c22SToby Isaac . point - The mesh point
6806c58f1c22SToby Isaac - value - The label value for this point
6807c58f1c22SToby Isaac 
6808c58f1c22SToby Isaac   Output Parameter:
6809c58f1c22SToby Isaac 
6810c58f1c22SToby Isaac   Level: beginner
6811c58f1c22SToby Isaac 
6812c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6813c58f1c22SToby Isaac @*/
6814c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6815c58f1c22SToby Isaac {
6816c58f1c22SToby Isaac   DMLabel        label;
6817c58f1c22SToby Isaac   PetscErrorCode ierr;
6818c58f1c22SToby Isaac 
6819c58f1c22SToby Isaac   PetscFunctionBegin;
6820c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6821c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6822c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6823c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6824c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6825c58f1c22SToby Isaac   PetscFunctionReturn(0);
6826c58f1c22SToby Isaac }
6827c58f1c22SToby Isaac 
6828c58f1c22SToby Isaac /*@C
6829c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6830c58f1c22SToby Isaac 
6831c58f1c22SToby Isaac   Not Collective
6832c58f1c22SToby Isaac 
6833c58f1c22SToby Isaac   Input Parameters:
6834c58f1c22SToby Isaac + dm   - The DM object
6835c58f1c22SToby Isaac - name - The label name
6836c58f1c22SToby Isaac 
6837c58f1c22SToby Isaac   Output Parameter:
6838c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6839c58f1c22SToby Isaac 
6840c58f1c22SToby Isaac   Level: beginner
6841c58f1c22SToby Isaac 
6842df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6843c58f1c22SToby Isaac @*/
6844c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6845c58f1c22SToby Isaac {
6846c58f1c22SToby Isaac   DMLabel        label;
6847c58f1c22SToby Isaac   PetscErrorCode ierr;
6848c58f1c22SToby Isaac 
6849c58f1c22SToby Isaac   PetscFunctionBegin;
6850c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6851c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6852534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
6853c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6854c58f1c22SToby Isaac   *size = 0;
6855c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6856c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6857c58f1c22SToby Isaac   PetscFunctionReturn(0);
6858c58f1c22SToby Isaac }
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac /*@C
6861c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac   Not Collective
6864c58f1c22SToby Isaac 
6865c58f1c22SToby Isaac   Input Parameters:
6866c58f1c22SToby Isaac + mesh - The DM object
6867c58f1c22SToby Isaac - name - The label name
6868c58f1c22SToby Isaac 
6869c58f1c22SToby Isaac   Output Parameter:
6870c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6871c58f1c22SToby Isaac 
6872c58f1c22SToby Isaac   Level: beginner
6873c58f1c22SToby Isaac 
6874c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6875c58f1c22SToby Isaac @*/
6876c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6877c58f1c22SToby Isaac {
6878c58f1c22SToby Isaac   DMLabel        label;
6879c58f1c22SToby Isaac   PetscErrorCode ierr;
6880c58f1c22SToby Isaac 
6881c58f1c22SToby Isaac   PetscFunctionBegin;
6882c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6883c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6884c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6885c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6886c58f1c22SToby Isaac   *ids = NULL;
6887dab2e251SBlaise Bourdin  if (label) {
6888c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6889dab2e251SBlaise Bourdin   } else {
6890dab2e251SBlaise Bourdin     /* returning an empty IS */
6891dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6892dab2e251SBlaise Bourdin   }
6893c58f1c22SToby Isaac   PetscFunctionReturn(0);
6894c58f1c22SToby Isaac }
6895c58f1c22SToby Isaac 
6896c58f1c22SToby Isaac /*@C
6897c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6898c58f1c22SToby Isaac 
6899c58f1c22SToby Isaac   Not Collective
6900c58f1c22SToby Isaac 
6901c58f1c22SToby Isaac   Input Parameters:
6902c58f1c22SToby Isaac + dm - The DM object
6903c58f1c22SToby Isaac . name - The label name
6904c58f1c22SToby Isaac - value - The stratum value
6905c58f1c22SToby Isaac 
6906c58f1c22SToby Isaac   Output Parameter:
6907c58f1c22SToby Isaac . size - The stratum size
6908c58f1c22SToby Isaac 
6909c58f1c22SToby Isaac   Level: beginner
6910c58f1c22SToby Isaac 
6911c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6912c58f1c22SToby Isaac @*/
6913c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6914c58f1c22SToby Isaac {
6915c58f1c22SToby Isaac   DMLabel        label;
6916c58f1c22SToby Isaac   PetscErrorCode ierr;
6917c58f1c22SToby Isaac 
6918c58f1c22SToby Isaac   PetscFunctionBegin;
6919c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6920c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6921534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
6922c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6923c58f1c22SToby Isaac   *size = 0;
6924c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6925c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6926c58f1c22SToby Isaac   PetscFunctionReturn(0);
6927c58f1c22SToby Isaac }
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac /*@C
6930c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6931c58f1c22SToby Isaac 
6932c58f1c22SToby Isaac   Not Collective
6933c58f1c22SToby Isaac 
6934c58f1c22SToby Isaac   Input Parameters:
6935c58f1c22SToby Isaac + dm - The DM object
6936c58f1c22SToby Isaac . name - The label name
6937c58f1c22SToby Isaac - value - The stratum value
6938c58f1c22SToby Isaac 
6939c58f1c22SToby Isaac   Output Parameter:
6940c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6941c58f1c22SToby Isaac 
6942c58f1c22SToby Isaac   Level: beginner
6943c58f1c22SToby Isaac 
6944c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6945c58f1c22SToby Isaac @*/
6946c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6947c58f1c22SToby Isaac {
6948c58f1c22SToby Isaac   DMLabel        label;
6949c58f1c22SToby Isaac   PetscErrorCode ierr;
6950c58f1c22SToby Isaac 
6951c58f1c22SToby Isaac   PetscFunctionBegin;
6952c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6953c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6954c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6955c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6956c58f1c22SToby Isaac   *points = NULL;
6957c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6958c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6959c58f1c22SToby Isaac   PetscFunctionReturn(0);
6960c58f1c22SToby Isaac }
6961c58f1c22SToby Isaac 
69624de306b1SToby Isaac /*@C
69639044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69644de306b1SToby Isaac 
69654de306b1SToby Isaac   Not Collective
69664de306b1SToby Isaac 
69674de306b1SToby Isaac   Input Parameters:
69684de306b1SToby Isaac + dm - The DM object
69694de306b1SToby Isaac . name - The label name
69704de306b1SToby Isaac . value - The stratum value
69714de306b1SToby Isaac - points - The stratum points
69724de306b1SToby Isaac 
69734de306b1SToby Isaac   Level: beginner
69744de306b1SToby Isaac 
69754de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
69764de306b1SToby Isaac @*/
69774de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
69784de306b1SToby Isaac {
69794de306b1SToby Isaac   DMLabel        label;
69804de306b1SToby Isaac   PetscErrorCode ierr;
69814de306b1SToby Isaac 
69824de306b1SToby Isaac   PetscFunctionBegin;
69834de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69844de306b1SToby Isaac   PetscValidCharPointer(name, 2);
69854de306b1SToby Isaac   PetscValidPointer(points, 4);
69864de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
69874de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
69884de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
69894de306b1SToby Isaac   PetscFunctionReturn(0);
69904de306b1SToby Isaac }
69914de306b1SToby Isaac 
6992c58f1c22SToby Isaac /*@C
6993c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6994c58f1c22SToby Isaac 
6995c58f1c22SToby Isaac   Not Collective
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac   Input Parameters:
6998c58f1c22SToby Isaac + dm   - The DM object
6999c58f1c22SToby Isaac . name - The label name
7000c58f1c22SToby Isaac - value - The label value for this point
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac   Output Parameter:
7003c58f1c22SToby Isaac 
7004c58f1c22SToby Isaac   Level: beginner
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7007c58f1c22SToby Isaac @*/
7008c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7009c58f1c22SToby Isaac {
7010c58f1c22SToby Isaac   DMLabel        label;
7011c58f1c22SToby Isaac   PetscErrorCode ierr;
7012c58f1c22SToby Isaac 
7013c58f1c22SToby Isaac   PetscFunctionBegin;
7014c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7015c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7016c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7017c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7018c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7019c58f1c22SToby Isaac   PetscFunctionReturn(0);
7020c58f1c22SToby Isaac }
7021c58f1c22SToby Isaac 
7022c58f1c22SToby Isaac /*@
7023c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7024c58f1c22SToby Isaac 
7025c58f1c22SToby Isaac   Not Collective
7026c58f1c22SToby Isaac 
7027c58f1c22SToby Isaac   Input Parameter:
7028c58f1c22SToby Isaac . dm   - The DM object
7029c58f1c22SToby Isaac 
7030c58f1c22SToby Isaac   Output Parameter:
7031c58f1c22SToby Isaac . numLabels - the number of Labels
7032c58f1c22SToby Isaac 
7033c58f1c22SToby Isaac   Level: intermediate
7034c58f1c22SToby Isaac 
7035c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7036c58f1c22SToby Isaac @*/
7037c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7038c58f1c22SToby Isaac {
70395d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7040c58f1c22SToby Isaac   PetscInt  n    = 0;
7041c58f1c22SToby Isaac 
7042c58f1c22SToby Isaac   PetscFunctionBegin;
7043c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7044534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7045c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7046c58f1c22SToby Isaac   *numLabels = n;
7047c58f1c22SToby Isaac   PetscFunctionReturn(0);
7048c58f1c22SToby Isaac }
7049c58f1c22SToby Isaac 
7050c58f1c22SToby Isaac /*@C
7051c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7052c58f1c22SToby Isaac 
7053c58f1c22SToby Isaac   Not Collective
7054c58f1c22SToby Isaac 
7055c58f1c22SToby Isaac   Input Parameters:
7056c58f1c22SToby Isaac + dm - The DM object
7057c58f1c22SToby Isaac - n  - the label number
7058c58f1c22SToby Isaac 
7059c58f1c22SToby Isaac   Output Parameter:
7060c58f1c22SToby Isaac . name - the label name
7061c58f1c22SToby Isaac 
7062c58f1c22SToby Isaac   Level: intermediate
7063c58f1c22SToby Isaac 
7064c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7065c58f1c22SToby Isaac @*/
7066c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7067c58f1c22SToby Isaac {
70685d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7069c58f1c22SToby Isaac   PetscInt       l    = 0;
7070d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7071c58f1c22SToby Isaac 
7072c58f1c22SToby Isaac   PetscFunctionBegin;
7073c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7074c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7075c58f1c22SToby Isaac   while (next) {
7076c58f1c22SToby Isaac     if (l == n) {
7077d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7078c58f1c22SToby Isaac       PetscFunctionReturn(0);
7079c58f1c22SToby Isaac     }
7080c58f1c22SToby Isaac     ++l;
7081c58f1c22SToby Isaac     next = next->next;
7082c58f1c22SToby Isaac   }
7083c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7084c58f1c22SToby Isaac }
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac /*@C
7087c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7088c58f1c22SToby Isaac 
7089c58f1c22SToby Isaac   Not Collective
7090c58f1c22SToby Isaac 
7091c58f1c22SToby Isaac   Input Parameters:
7092c58f1c22SToby Isaac + dm   - The DM object
7093c58f1c22SToby Isaac - name - The label name
7094c58f1c22SToby Isaac 
7095c58f1c22SToby Isaac   Output Parameter:
7096c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7097c58f1c22SToby Isaac 
7098c58f1c22SToby Isaac   Level: intermediate
7099c58f1c22SToby Isaac 
7100c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7101c58f1c22SToby Isaac @*/
7102c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7103c58f1c22SToby Isaac {
71045d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7105d67d17b1SMatthew G. Knepley   const char    *lname;
7106c58f1c22SToby Isaac   PetscErrorCode ierr;
7107c58f1c22SToby Isaac 
7108c58f1c22SToby Isaac   PetscFunctionBegin;
7109c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7110c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7111534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7112c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7113c58f1c22SToby Isaac   while (next) {
7114d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7115d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7116c58f1c22SToby Isaac     if (*hasLabel) break;
7117c58f1c22SToby Isaac     next = next->next;
7118c58f1c22SToby Isaac   }
7119c58f1c22SToby Isaac   PetscFunctionReturn(0);
7120c58f1c22SToby Isaac }
7121c58f1c22SToby Isaac 
7122c58f1c22SToby Isaac /*@C
7123c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7124c58f1c22SToby Isaac 
7125c58f1c22SToby Isaac   Not Collective
7126c58f1c22SToby Isaac 
7127c58f1c22SToby Isaac   Input Parameters:
7128c58f1c22SToby Isaac + dm   - The DM object
7129c58f1c22SToby Isaac - name - The label name
7130c58f1c22SToby Isaac 
7131c58f1c22SToby Isaac   Output Parameter:
7132c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7133c58f1c22SToby Isaac 
7134c58f1c22SToby Isaac   Level: intermediate
7135c58f1c22SToby Isaac 
7136c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7137c58f1c22SToby Isaac @*/
7138c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7139c58f1c22SToby Isaac {
71405d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7141c58f1c22SToby Isaac   PetscBool      hasLabel;
7142d67d17b1SMatthew G. Knepley   const char    *lname;
7143c58f1c22SToby Isaac   PetscErrorCode ierr;
7144c58f1c22SToby Isaac 
7145c58f1c22SToby Isaac   PetscFunctionBegin;
7146c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7147c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7148c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7149c58f1c22SToby Isaac   *label = NULL;
7150c58f1c22SToby Isaac   while (next) {
7151d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7152d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7153c58f1c22SToby Isaac     if (hasLabel) {
7154c58f1c22SToby Isaac       *label = next->label;
7155c58f1c22SToby Isaac       break;
7156c58f1c22SToby Isaac     }
7157c58f1c22SToby Isaac     next = next->next;
7158c58f1c22SToby Isaac   }
7159c58f1c22SToby Isaac   PetscFunctionReturn(0);
7160c58f1c22SToby Isaac }
7161c58f1c22SToby Isaac 
7162c58f1c22SToby Isaac /*@C
7163c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7164c58f1c22SToby Isaac 
7165c58f1c22SToby Isaac   Not Collective
7166c58f1c22SToby Isaac 
7167c58f1c22SToby Isaac   Input Parameters:
7168c58f1c22SToby Isaac + dm - The DM object
7169c58f1c22SToby Isaac - n  - the label number
7170c58f1c22SToby Isaac 
7171c58f1c22SToby Isaac   Output Parameter:
7172c58f1c22SToby Isaac . label - the label
7173c58f1c22SToby Isaac 
7174c58f1c22SToby Isaac   Level: intermediate
7175c58f1c22SToby Isaac 
7176c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7177c58f1c22SToby Isaac @*/
7178c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7179c58f1c22SToby Isaac {
71805d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7181c58f1c22SToby Isaac   PetscInt    l    = 0;
7182c58f1c22SToby Isaac 
7183c58f1c22SToby Isaac   PetscFunctionBegin;
7184c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7185c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7186c58f1c22SToby Isaac   while (next) {
7187c58f1c22SToby Isaac     if (l == n) {
7188c58f1c22SToby Isaac       *label = next->label;
7189c58f1c22SToby Isaac       PetscFunctionReturn(0);
7190c58f1c22SToby Isaac     }
7191c58f1c22SToby Isaac     ++l;
7192c58f1c22SToby Isaac     next = next->next;
7193c58f1c22SToby Isaac   }
7194c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7195c58f1c22SToby Isaac }
7196c58f1c22SToby Isaac 
7197c58f1c22SToby Isaac /*@C
7198c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7199c58f1c22SToby Isaac 
7200c58f1c22SToby Isaac   Not Collective
7201c58f1c22SToby Isaac 
7202c58f1c22SToby Isaac   Input Parameters:
7203c58f1c22SToby Isaac + dm   - The DM object
7204c58f1c22SToby Isaac - label - The DMLabel
7205c58f1c22SToby Isaac 
7206c58f1c22SToby Isaac   Level: developer
7207c58f1c22SToby Isaac 
7208c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7209c58f1c22SToby Isaac @*/
7210c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7211c58f1c22SToby Isaac {
72125d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7213c58f1c22SToby Isaac   PetscBool      hasLabel;
7214d67d17b1SMatthew G. Knepley   const char    *lname;
72155d80c0bfSVaclav Hapla   PetscBool      flg;
7216c58f1c22SToby Isaac   PetscErrorCode ierr;
7217c58f1c22SToby Isaac 
7218c58f1c22SToby Isaac   PetscFunctionBegin;
7219c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7220d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7221d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7222d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7223c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7224c58f1c22SToby Isaac   tmpLabel->label  = label;
7225c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72265d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
72275d80c0bfSVaclav Hapla   *p = tmpLabel;
722808f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
72295d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
72305d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7231ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7232ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7233c58f1c22SToby Isaac   PetscFunctionReturn(0);
7234c58f1c22SToby Isaac }
7235c58f1c22SToby Isaac 
7236c58f1c22SToby Isaac /*@C
7237e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7238c58f1c22SToby Isaac 
7239c58f1c22SToby Isaac   Not Collective
7240c58f1c22SToby Isaac 
7241c58f1c22SToby Isaac   Input Parameters:
7242c58f1c22SToby Isaac + dm   - The DM object
7243c58f1c22SToby Isaac - name - The label name
7244c58f1c22SToby Isaac 
7245c58f1c22SToby Isaac   Output Parameter:
7246c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7247c58f1c22SToby Isaac 
7248c58f1c22SToby Isaac   Level: developer
7249c58f1c22SToby Isaac 
7250e5472504SVaclav Hapla   Notes:
7251e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7252e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7253e5472504SVaclav Hapla 
7254e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7255e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7256e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7257e5472504SVaclav Hapla 
7258e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7259c58f1c22SToby Isaac @*/
7260c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7261c58f1c22SToby Isaac {
726295d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7263c58f1c22SToby Isaac   PetscBool      hasLabel;
7264d67d17b1SMatthew G. Knepley   const char    *lname;
7265c58f1c22SToby Isaac   PetscErrorCode ierr;
7266c58f1c22SToby Isaac 
7267c58f1c22SToby Isaac   PetscFunctionBegin;
7268c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7269e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7270e5472504SVaclav Hapla   if (label) {
7271e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7272c58f1c22SToby Isaac     *label = NULL;
7273e5472504SVaclav Hapla   }
72745d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
727595d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7276d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7277c58f1c22SToby Isaac     if (hasLabel) {
727895d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7279c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
728095d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7281ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7282ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
728395d578d6SVaclav Hapla       if (label) *label = link->label;
728495d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
728595d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7286c58f1c22SToby Isaac       break;
7287c58f1c22SToby Isaac     }
7288c58f1c22SToby Isaac   }
7289c58f1c22SToby Isaac   PetscFunctionReturn(0);
7290c58f1c22SToby Isaac }
7291c58f1c22SToby Isaac 
7292306894acSVaclav Hapla /*@
7293306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7294306894acSVaclav Hapla 
7295306894acSVaclav Hapla   Not Collective
7296306894acSVaclav Hapla 
7297306894acSVaclav Hapla   Input Parameters:
7298306894acSVaclav Hapla + dm   - The DM object
7299306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7300306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7301306894acSVaclav Hapla 
7302306894acSVaclav Hapla   Level: developer
7303306894acSVaclav Hapla 
7304306894acSVaclav Hapla   Notes:
7305306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7306306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7307306894acSVaclav Hapla   *label nullified.
7308306894acSVaclav Hapla 
7309306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7310306894acSVaclav Hapla @*/
7311306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7312306894acSVaclav Hapla {
731343e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7314306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7315306894acSVaclav Hapla   PetscErrorCode ierr;
7316306894acSVaclav Hapla 
7317306894acSVaclav Hapla   PetscFunctionBegin;
7318306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7319306894acSVaclav Hapla   PetscValidPointer(label, 2);
7320f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7321306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7322306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
73235d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
732443e45a93SVaclav Hapla     if (*label == link->label) {
7325306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
732643e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7327306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7328ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
732943e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
733043e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
733143e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7332306894acSVaclav Hapla       break;
7333306894acSVaclav Hapla     }
7334306894acSVaclav Hapla   }
7335306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7336306894acSVaclav Hapla   PetscFunctionReturn(0);
7337306894acSVaclav Hapla }
7338306894acSVaclav Hapla 
7339c58f1c22SToby Isaac /*@C
7340c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7341c58f1c22SToby Isaac 
7342c58f1c22SToby Isaac   Not Collective
7343c58f1c22SToby Isaac 
7344c58f1c22SToby Isaac   Input Parameters:
7345c58f1c22SToby Isaac + dm   - The DM object
7346c58f1c22SToby Isaac - name - The label name
7347c58f1c22SToby Isaac 
7348c58f1c22SToby Isaac   Output Parameter:
7349c58f1c22SToby Isaac . output - The flag for output
7350c58f1c22SToby Isaac 
7351c58f1c22SToby Isaac   Level: developer
7352c58f1c22SToby Isaac 
7353c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7354c58f1c22SToby Isaac @*/
7355c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7356c58f1c22SToby Isaac {
73575d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7358d67d17b1SMatthew G. Knepley   const char    *lname;
7359c58f1c22SToby Isaac   PetscErrorCode ierr;
7360c58f1c22SToby Isaac 
7361c58f1c22SToby Isaac   PetscFunctionBegin;
7362c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7363c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7364c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7365c58f1c22SToby Isaac   while (next) {
7366c58f1c22SToby Isaac     PetscBool flg;
7367c58f1c22SToby Isaac 
7368d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7369d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7370c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7371c58f1c22SToby Isaac     next = next->next;
7372c58f1c22SToby Isaac   }
7373c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7374c58f1c22SToby Isaac }
7375c58f1c22SToby Isaac 
7376c58f1c22SToby Isaac /*@C
7377c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7378c58f1c22SToby Isaac 
7379c58f1c22SToby Isaac   Not Collective
7380c58f1c22SToby Isaac 
7381c58f1c22SToby Isaac   Input Parameters:
7382c58f1c22SToby Isaac + dm     - The DM object
7383c58f1c22SToby Isaac . name   - The label name
7384c58f1c22SToby Isaac - output - The flag for output
7385c58f1c22SToby Isaac 
7386c58f1c22SToby Isaac   Level: developer
7387c58f1c22SToby Isaac 
7388c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7389c58f1c22SToby Isaac @*/
7390c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7391c58f1c22SToby Isaac {
73925d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7393d67d17b1SMatthew G. Knepley   const char    *lname;
7394c58f1c22SToby Isaac   PetscErrorCode ierr;
7395c58f1c22SToby Isaac 
7396c58f1c22SToby Isaac   PetscFunctionBegin;
7397c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7398534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7399c58f1c22SToby Isaac   while (next) {
7400c58f1c22SToby Isaac     PetscBool flg;
7401c58f1c22SToby Isaac 
7402d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7403d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7404c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7405c58f1c22SToby Isaac     next = next->next;
7406c58f1c22SToby Isaac   }
7407c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7408c58f1c22SToby Isaac }
7409c58f1c22SToby Isaac 
7410c58f1c22SToby Isaac /*@
7411c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7412c58f1c22SToby Isaac 
7413d083f849SBarry Smith   Collective on dmA
7414c58f1c22SToby Isaac 
7415c58f1c22SToby Isaac   Input Parameter:
74165d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
74172e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
74185d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7419ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7420c58f1c22SToby Isaac 
7421c58f1c22SToby Isaac   Level: intermediate
7422c58f1c22SToby Isaac 
7423c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7424c58f1c22SToby Isaac 
74255d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7426c58f1c22SToby Isaac @*/
74275d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7428c58f1c22SToby Isaac {
7429c58f1c22SToby Isaac   DMLabel        label, labelNew;
7430c58f1c22SToby Isaac   const char    *name;
7431c58f1c22SToby Isaac   PetscBool      flg;
74325d80c0bfSVaclav Hapla   DMLabelLink    link;
74335d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7434c58f1c22SToby Isaac 
74355d80c0bfSVaclav Hapla   PetscFunctionBegin;
74365d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
74375d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
74385d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
74395d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
74405d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
74415d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
74425d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
74435d80c0bfSVaclav Hapla     label=link->label;
74445d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
74455d80c0bfSVaclav Hapla     if (!all) {
7446c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7447c58f1c22SToby Isaac       if (flg) continue;
74487d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
74497d5acc75SStefano Zampini       if (flg) continue;
7450ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7451ba2698f1SMatthew G. Knepley       if (flg) continue;
74525d80c0bfSVaclav Hapla     }
74535d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7454c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
74555d80c0bfSVaclav Hapla     } else {
74565d80c0bfSVaclav Hapla       labelNew = label;
74575d80c0bfSVaclav Hapla     }
7458c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
74595d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7460c58f1c22SToby Isaac   }
7461c58f1c22SToby Isaac   PetscFunctionReturn(0);
7462c58f1c22SToby Isaac }
7463a8fb8f29SToby Isaac 
7464a8fb8f29SToby Isaac /*@
7465a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7466a8fb8f29SToby Isaac 
7467a8fb8f29SToby Isaac   Input Parameter:
7468a8fb8f29SToby Isaac . dm - The DM object
7469a8fb8f29SToby Isaac 
7470a8fb8f29SToby Isaac   Output Parameter:
7471a8fb8f29SToby Isaac . cdm - The coarse DM
7472a8fb8f29SToby Isaac 
7473a8fb8f29SToby Isaac   Level: intermediate
7474a8fb8f29SToby Isaac 
7475a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7476a8fb8f29SToby Isaac @*/
7477a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7478a8fb8f29SToby Isaac {
7479a8fb8f29SToby Isaac   PetscFunctionBegin;
7480a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7481a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7482a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7483a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7484a8fb8f29SToby Isaac }
7485a8fb8f29SToby Isaac 
7486a8fb8f29SToby Isaac /*@
7487a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7488a8fb8f29SToby Isaac 
7489a8fb8f29SToby Isaac   Input Parameters:
7490a8fb8f29SToby Isaac + dm - The DM object
7491a8fb8f29SToby Isaac - cdm - The coarse DM
7492a8fb8f29SToby Isaac 
7493a8fb8f29SToby Isaac   Level: intermediate
7494a8fb8f29SToby Isaac 
7495a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7496a8fb8f29SToby Isaac @*/
7497a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7498a8fb8f29SToby Isaac {
7499a8fb8f29SToby Isaac   PetscErrorCode ierr;
7500a8fb8f29SToby Isaac 
7501a8fb8f29SToby Isaac   PetscFunctionBegin;
7502a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7503a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7504a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7505a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7506a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7507a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7508a8fb8f29SToby Isaac }
7509a8fb8f29SToby Isaac 
751088bdff64SToby Isaac /*@
751188bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
751288bdff64SToby Isaac 
751388bdff64SToby Isaac   Input Parameter:
751488bdff64SToby Isaac . dm - The DM object
751588bdff64SToby Isaac 
751688bdff64SToby Isaac   Output Parameter:
751788bdff64SToby Isaac . fdm - The fine DM
751888bdff64SToby Isaac 
751988bdff64SToby Isaac   Level: intermediate
752088bdff64SToby Isaac 
752188bdff64SToby Isaac .seealso: DMSetFineDM()
752288bdff64SToby Isaac @*/
752388bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
752488bdff64SToby Isaac {
752588bdff64SToby Isaac   PetscFunctionBegin;
752688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
752788bdff64SToby Isaac   PetscValidPointer(fdm, 2);
752888bdff64SToby Isaac   *fdm = dm->fineMesh;
752988bdff64SToby Isaac   PetscFunctionReturn(0);
753088bdff64SToby Isaac }
753188bdff64SToby Isaac 
753288bdff64SToby Isaac /*@
753388bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
753488bdff64SToby Isaac 
753588bdff64SToby Isaac   Input Parameters:
753688bdff64SToby Isaac + dm - The DM object
753788bdff64SToby Isaac - fdm - The fine DM
753888bdff64SToby Isaac 
753988bdff64SToby Isaac   Level: intermediate
754088bdff64SToby Isaac 
754188bdff64SToby Isaac .seealso: DMGetFineDM()
754288bdff64SToby Isaac @*/
754388bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
754488bdff64SToby Isaac {
754588bdff64SToby Isaac   PetscErrorCode ierr;
754688bdff64SToby Isaac 
754788bdff64SToby Isaac   PetscFunctionBegin;
754888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
754988bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
755088bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
755188bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
755288bdff64SToby Isaac   dm->fineMesh = fdm;
755388bdff64SToby Isaac   PetscFunctionReturn(0);
755488bdff64SToby Isaac }
755588bdff64SToby Isaac 
7556a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7557a6ba4734SToby Isaac 
7558a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7559a6ba4734SToby Isaac {
7560e5e52638SMatthew G. Knepley   PetscInt       d;
7561a6ba4734SToby Isaac   PetscErrorCode ierr;
7562a6ba4734SToby Isaac 
7563a6ba4734SToby Isaac   PetscFunctionBegin;
7564e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7565e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7566e5e52638SMatthew G. Knepley   }
7567a6ba4734SToby Isaac   PetscFunctionReturn(0);
7568a6ba4734SToby Isaac }
7569a6ba4734SToby Isaac 
7570a6ba4734SToby Isaac /*@C
7571a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7572a6ba4734SToby Isaac 
7573a6ba4734SToby Isaac   Input Parameters:
75744c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7575f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7576a6ba4734SToby Isaac . name        - The BC name
7577a6ba4734SToby Isaac . labelname   - The label defining constrained points
7578a6ba4734SToby Isaac . field       - The field to constrain
7579e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7580a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7581a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7582a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7583a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7584a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7585a6ba4734SToby Isaac 
7586a6ba4734SToby Isaac   Options Database Keys:
7587a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7588a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7589a6ba4734SToby Isaac 
7590a6ba4734SToby Isaac   Level: developer
7591a6ba4734SToby Isaac 
7592a6ba4734SToby Isaac .seealso: DMGetBoundary()
7593a6ba4734SToby Isaac @*/
7594a30ec4eaSSatish Balay PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(void), PetscInt numids, const PetscInt *ids, void *ctx)
7595a6ba4734SToby Isaac {
7596e5e52638SMatthew G. Knepley   PetscDS        ds;
7597a6ba4734SToby Isaac   PetscErrorCode ierr;
7598a6ba4734SToby Isaac 
7599a6ba4734SToby Isaac   PetscFunctionBegin;
7600a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7601e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7602e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7603a6ba4734SToby Isaac   PetscFunctionReturn(0);
7604a6ba4734SToby Isaac }
7605a6ba4734SToby Isaac 
7606a6ba4734SToby Isaac /*@
7607a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7608a6ba4734SToby Isaac 
7609a6ba4734SToby Isaac   Input Parameters:
7610a6ba4734SToby Isaac . dm - The mesh object
7611a6ba4734SToby Isaac 
7612a6ba4734SToby Isaac   Output Parameters:
7613a6ba4734SToby Isaac . numBd - The number of BC
7614a6ba4734SToby Isaac 
7615a6ba4734SToby Isaac   Level: intermediate
7616a6ba4734SToby Isaac 
7617a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7618a6ba4734SToby Isaac @*/
7619a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7620a6ba4734SToby Isaac {
7621e5e52638SMatthew G. Knepley   PetscDS        ds;
762258ebd649SToby Isaac   PetscErrorCode ierr;
7623a6ba4734SToby Isaac 
7624a6ba4734SToby Isaac   PetscFunctionBegin;
7625a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7626e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7627e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7628a6ba4734SToby Isaac   PetscFunctionReturn(0);
7629a6ba4734SToby Isaac }
7630a6ba4734SToby Isaac 
7631a6ba4734SToby Isaac /*@C
76321c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7633a6ba4734SToby Isaac 
7634a6ba4734SToby Isaac   Input Parameters:
7635a6ba4734SToby Isaac + dm          - The mesh object
7636a6ba4734SToby Isaac - bd          - The BC number
7637a6ba4734SToby Isaac 
7638a6ba4734SToby Isaac   Output Parameters:
7639f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7640a6ba4734SToby Isaac . name        - The BC name
7641a6ba4734SToby Isaac . labelname   - The label defining constrained points
7642a6ba4734SToby Isaac . field       - The field to constrain
7643a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7644a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7645a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7646a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7647a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7648a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7649a6ba4734SToby Isaac 
7650a6ba4734SToby Isaac   Options Database Keys:
7651a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7652a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7653a6ba4734SToby Isaac 
7654a6ba4734SToby Isaac   Level: developer
7655a6ba4734SToby Isaac 
7656a6ba4734SToby Isaac .seealso: DMAddBoundary()
7657a6ba4734SToby Isaac @*/
7658a30ec4eaSSatish Balay PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(void), PetscInt *numids, const PetscInt **ids, void **ctx)
7659a6ba4734SToby Isaac {
7660e5e52638SMatthew G. Knepley   PetscDS        ds;
766158ebd649SToby Isaac   PetscErrorCode ierr;
7662a6ba4734SToby Isaac 
7663a6ba4734SToby Isaac   PetscFunctionBegin;
7664a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7665e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7666e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7667a6ba4734SToby Isaac   PetscFunctionReturn(0);
7668a6ba4734SToby Isaac }
7669a6ba4734SToby Isaac 
7670e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7671e6f8dbb6SToby Isaac {
7672e5e52638SMatthew G. Knepley   PetscDS        ds;
7673dff059c6SToby Isaac   DMBoundary    *lastnext;
7674e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7675e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7676e6f8dbb6SToby Isaac 
7677e6f8dbb6SToby Isaac   PetscFunctionBegin;
7678e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7679e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
768047a1f5adSToby Isaac   if (dm->boundary) {
768147a1f5adSToby Isaac     DMBoundary next = dm->boundary;
768247a1f5adSToby Isaac 
768347a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
768447a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
768547a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
768647a1f5adSToby Isaac     while (next) {
768747a1f5adSToby Isaac       DMBoundary b = next;
768847a1f5adSToby Isaac 
768947a1f5adSToby Isaac       next = b->next;
769047a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7691a6ba4734SToby Isaac     }
769247a1f5adSToby Isaac     dm->boundary = NULL;
7693a6ba4734SToby Isaac   }
769447a1f5adSToby Isaac 
7695dff059c6SToby Isaac   lastnext = &(dm->boundary);
7696e6f8dbb6SToby Isaac   while (dsbound) {
7697e6f8dbb6SToby Isaac     DMBoundary dmbound;
7698e6f8dbb6SToby Isaac 
7699e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7700e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7701e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7702994fe344SLisandro 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);}
770347a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7704dff059c6SToby Isaac     *lastnext = dmbound;
7705dff059c6SToby Isaac     lastnext = &(dmbound->next);
7706dff059c6SToby Isaac     dsbound = dsbound->next;
7707a6ba4734SToby Isaac   }
7708a6ba4734SToby Isaac   PetscFunctionReturn(0);
7709a6ba4734SToby Isaac }
7710a6ba4734SToby Isaac 
7711a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7712a6ba4734SToby Isaac {
7713b95f2879SToby Isaac   DMBoundary     b;
7714a6ba4734SToby Isaac   PetscErrorCode ierr;
7715a6ba4734SToby Isaac 
7716a6ba4734SToby Isaac   PetscFunctionBegin;
7717a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7718534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7719a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7720e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7721b95f2879SToby Isaac   b = dm->boundary;
7722a6ba4734SToby Isaac   while (b && !(*isBd)) {
7723e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7724e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
77253424c85cSToby Isaac 
77263424c85cSToby Isaac     if (label) {
7727a6ba4734SToby Isaac       PetscInt i;
7728a6ba4734SToby Isaac 
7729e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7730e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7731a6ba4734SToby Isaac       }
7732a6ba4734SToby Isaac     }
7733a6ba4734SToby Isaac     b = b->next;
7734a6ba4734SToby Isaac   }
7735a6ba4734SToby Isaac   PetscFunctionReturn(0);
7736a6ba4734SToby Isaac }
77374d6f44ffSToby Isaac 
77384d6f44ffSToby Isaac /*@C
7739a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
7740a6e0b375SMatthew G. Knepley 
7741a6e0b375SMatthew G. Knepley   Collective on DM
77424d6f44ffSToby Isaac 
77434d6f44ffSToby Isaac   Input Parameters:
77444d6f44ffSToby Isaac + dm      - The DM
77450709b2feSToby Isaac . time    - The time
77464d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
77474d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
77484d6f44ffSToby Isaac - mode    - The insertion mode for values
77494d6f44ffSToby Isaac 
77504d6f44ffSToby Isaac   Output Parameter:
77514d6f44ffSToby Isaac . X - vector
77524d6f44ffSToby Isaac 
77534d6f44ffSToby Isaac    Calling sequence of func:
77540709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
77554d6f44ffSToby Isaac 
77564d6f44ffSToby Isaac +  dim - The spatial dimension
77574d6f44ffSToby Isaac .  x   - The coordinates
77584d6f44ffSToby Isaac .  Nf  - The number of fields
77594d6f44ffSToby Isaac .  u   - The output field values
77604d6f44ffSToby Isaac -  ctx - optional user-defined function context
77614d6f44ffSToby Isaac 
77624d6f44ffSToby Isaac   Level: developer
77634d6f44ffSToby Isaac 
7764a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
77654d6f44ffSToby Isaac @*/
77660709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
77674d6f44ffSToby Isaac {
77684d6f44ffSToby Isaac   Vec            localX;
77694d6f44ffSToby Isaac   PetscErrorCode ierr;
77704d6f44ffSToby Isaac 
77714d6f44ffSToby Isaac   PetscFunctionBegin;
77724d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77734d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
77740709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77754d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
77764d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
77774d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
77784d6f44ffSToby Isaac   PetscFunctionReturn(0);
77794d6f44ffSToby Isaac }
77804d6f44ffSToby Isaac 
7781a6e0b375SMatthew G. Knepley /*@C
7782a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
7783a6e0b375SMatthew G. Knepley 
7784a6e0b375SMatthew G. Knepley   Not collective
7785a6e0b375SMatthew G. Knepley 
7786a6e0b375SMatthew G. Knepley   Input Parameters:
7787a6e0b375SMatthew G. Knepley + dm      - The DM
7788a6e0b375SMatthew G. Knepley . time    - The time
7789a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7790a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7791a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7792a6e0b375SMatthew G. Knepley 
7793a6e0b375SMatthew G. Knepley   Output Parameter:
7794a6e0b375SMatthew G. Knepley . localX - vector
7795a6e0b375SMatthew G. Knepley 
7796a6e0b375SMatthew G. Knepley    Calling sequence of func:
7797a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7798a6e0b375SMatthew G. Knepley 
7799a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7800a6e0b375SMatthew G. Knepley .  x   - The coordinates
7801a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7802a6e0b375SMatthew G. Knepley .  u   - The output field values
7803a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7804a6e0b375SMatthew G. Knepley 
7805a6e0b375SMatthew G. Knepley   Level: developer
7806a6e0b375SMatthew G. Knepley 
7807a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
7808a6e0b375SMatthew G. Knepley @*/
78090709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
78104d6f44ffSToby Isaac {
78114d6f44ffSToby Isaac   PetscErrorCode ierr;
78124d6f44ffSToby Isaac 
78134d6f44ffSToby Isaac   PetscFunctionBegin;
78144d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78154d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
78160918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
78170709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
78184d6f44ffSToby Isaac   PetscFunctionReturn(0);
78194d6f44ffSToby Isaac }
78204d6f44ffSToby Isaac 
7821a6e0b375SMatthew G. Knepley /*@C
7822a6e0b375SMatthew 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.
7823a6e0b375SMatthew G. Knepley 
7824a6e0b375SMatthew G. Knepley   Collective on DM
7825a6e0b375SMatthew G. Knepley 
7826a6e0b375SMatthew G. Knepley   Input Parameters:
7827a6e0b375SMatthew G. Knepley + dm      - The DM
7828a6e0b375SMatthew G. Knepley . time    - The time
7829a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7830a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7831a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7832a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7833a6e0b375SMatthew G. Knepley 
7834a6e0b375SMatthew G. Knepley   Output Parameter:
7835a6e0b375SMatthew G. Knepley . X - vector
7836a6e0b375SMatthew G. Knepley 
7837a6e0b375SMatthew G. Knepley    Calling sequence of func:
7838a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7839a6e0b375SMatthew G. Knepley 
7840a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7841a6e0b375SMatthew G. Knepley .  x   - The coordinates
7842a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7843a6e0b375SMatthew G. Knepley .  u   - The output field values
7844a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7845a6e0b375SMatthew G. Knepley 
7846a6e0b375SMatthew G. Knepley   Level: developer
7847a6e0b375SMatthew G. Knepley 
7848a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
7849a6e0b375SMatthew G. Knepley @*/
78502c53366bSMatthew 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)
78512c53366bSMatthew G. Knepley {
78522c53366bSMatthew G. Knepley   Vec            localX;
78532c53366bSMatthew G. Knepley   PetscErrorCode ierr;
78542c53366bSMatthew G. Knepley 
78552c53366bSMatthew G. Knepley   PetscFunctionBegin;
78562c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78572c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
78582c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
78592c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
78602c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
78612c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
78622c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
78632c53366bSMatthew G. Knepley }
78642c53366bSMatthew G. Knepley 
7865a6e0b375SMatthew G. Knepley /*@C
7866a6e0b375SMatthew 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.
7867a6e0b375SMatthew G. Knepley 
7868a6e0b375SMatthew G. Knepley   Not collective
7869a6e0b375SMatthew G. Knepley 
7870a6e0b375SMatthew G. Knepley   Input Parameters:
7871a6e0b375SMatthew G. Knepley + dm      - The DM
7872a6e0b375SMatthew G. Knepley . time    - The time
7873a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7874a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7875a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7876a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7877a6e0b375SMatthew G. Knepley 
7878a6e0b375SMatthew G. Knepley   Output Parameter:
7879a6e0b375SMatthew G. Knepley . localX - vector
7880a6e0b375SMatthew G. Knepley 
7881a6e0b375SMatthew G. Knepley    Calling sequence of func:
7882a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7883a6e0b375SMatthew G. Knepley 
7884a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7885a6e0b375SMatthew G. Knepley .  x   - The coordinates
7886a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7887a6e0b375SMatthew G. Knepley .  u   - The output field values
7888a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7889a6e0b375SMatthew G. Knepley 
7890a6e0b375SMatthew G. Knepley   Level: developer
7891a6e0b375SMatthew G. Knepley 
7892a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
7893a6e0b375SMatthew G. Knepley @*/
78941c531cf8SMatthew 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)
78954d6f44ffSToby Isaac {
78964d6f44ffSToby Isaac   PetscErrorCode ierr;
78974d6f44ffSToby Isaac 
78984d6f44ffSToby Isaac   PetscFunctionBegin;
78994d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79004d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
79010918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
79021c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
79034d6f44ffSToby Isaac   PetscFunctionReturn(0);
79044d6f44ffSToby Isaac }
79052716604bSToby Isaac 
7906a6e0b375SMatthew G. Knepley /*@C
7907a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
7908a6e0b375SMatthew G. Knepley 
7909a6e0b375SMatthew G. Knepley   Not collective
7910a6e0b375SMatthew G. Knepley 
7911a6e0b375SMatthew G. Knepley   Input Parameters:
7912a6e0b375SMatthew G. Knepley + dm      - The DM
7913a6e0b375SMatthew G. Knepley . time    - The time
7914a6e0b375SMatthew G. Knepley . localU  - The input field vector
7915a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7916a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7917a6e0b375SMatthew G. Knepley 
7918a6e0b375SMatthew G. Knepley   Output Parameter:
7919a6e0b375SMatthew G. Knepley . localX  - The output vector
7920a6e0b375SMatthew G. Knepley 
7921a6e0b375SMatthew G. Knepley    Calling sequence of func:
7922a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7923a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7924a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7925a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7926a6e0b375SMatthew G. Knepley 
7927a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7928a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7929a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7930a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7931a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7932a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7933a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7934a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7935a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7936a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7937a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7938a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7939a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7940a6e0b375SMatthew G. Knepley .  t            - The current time
7941a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7942a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7943a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7944a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7945a6e0b375SMatthew G. Knepley 
7946a6e0b375SMatthew 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.
7947a6e0b375SMatthew 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
7948a6e0b375SMatthew 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
7949a6e0b375SMatthew 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.
7950a6e0b375SMatthew G. Knepley 
7951a6e0b375SMatthew G. Knepley   Level: intermediate
7952a6e0b375SMatthew G. Knepley 
7953a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
7954a6e0b375SMatthew G. Knepley @*/
79558c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
79568c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
79578c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79588c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7959191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
79608c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
79618c6c5593SMatthew G. Knepley {
79628c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
79638c6c5593SMatthew G. Knepley 
79648c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79658c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79668c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
79678c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
79680918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
79698c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
79708c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
79718c6c5593SMatthew G. Knepley }
79728c6c5593SMatthew G. Knepley 
7973a6e0b375SMatthew G. Knepley /*@C
7974a6e0b375SMatthew 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.
7975a6e0b375SMatthew G. Knepley 
7976a6e0b375SMatthew G. Knepley   Not collective
7977a6e0b375SMatthew G. Knepley 
7978a6e0b375SMatthew G. Knepley   Input Parameters:
7979a6e0b375SMatthew G. Knepley + dm      - The DM
7980a6e0b375SMatthew G. Knepley . time    - The time
7981a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
7982a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
7983a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
7984a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
7985a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7986a6e0b375SMatthew G. Knepley . localU  - The input field vector
7987a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7988a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7989a6e0b375SMatthew G. Knepley 
7990a6e0b375SMatthew G. Knepley   Output Parameter:
7991a6e0b375SMatthew G. Knepley . localX  - The output vector
7992a6e0b375SMatthew G. Knepley 
7993a6e0b375SMatthew G. Knepley    Calling sequence of func:
7994a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7995a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7996a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7997a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7998a6e0b375SMatthew G. Knepley 
7999a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8000a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8001a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8002a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8003a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8004a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8005a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8006a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8007a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8008a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8009a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8010a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8011a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8012a6e0b375SMatthew G. Knepley .  t            - The current time
8013a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8014a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8015a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8016a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8017a6e0b375SMatthew G. Knepley 
8018a6e0b375SMatthew 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.
8019a6e0b375SMatthew 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
8020a6e0b375SMatthew 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
8021a6e0b375SMatthew 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.
8022a6e0b375SMatthew G. Knepley 
8023a6e0b375SMatthew G. Knepley   Level: intermediate
8024a6e0b375SMatthew G. Knepley 
8025a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8026a6e0b375SMatthew G. Knepley @*/
80271c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
80288c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
80298c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
80308c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8031191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
80328c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
80338c6c5593SMatthew G. Knepley {
80348c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
80358c6c5593SMatthew G. Knepley 
80368c6c5593SMatthew G. Knepley   PetscFunctionBegin;
80378c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80388c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
80398c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
80400918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
80411c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
80428c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
80438c6c5593SMatthew G. Knepley }
80448c6c5593SMatthew G. Knepley 
80452716604bSToby Isaac /*@C
80462716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
80472716604bSToby Isaac 
80482716604bSToby Isaac   Input Parameters:
80492716604bSToby Isaac + dm    - The DM
80500709b2feSToby Isaac . time  - The time
80512716604bSToby Isaac . funcs - The functions to evaluate for each field component
80522716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8053574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
80542716604bSToby Isaac 
80552716604bSToby Isaac   Output Parameter:
80562716604bSToby Isaac . diff - The diff ||u - u_h||_2
80572716604bSToby Isaac 
80582716604bSToby Isaac   Level: developer
80592716604bSToby Isaac 
80601189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
80612716604bSToby Isaac @*/
80620709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
80632716604bSToby Isaac {
80642716604bSToby Isaac   PetscErrorCode ierr;
80652716604bSToby Isaac 
80662716604bSToby Isaac   PetscFunctionBegin;
80672716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8068b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80690918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
80700709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
80712716604bSToby Isaac   PetscFunctionReturn(0);
80722716604bSToby Isaac }
8073b698f381SToby Isaac 
8074b698f381SToby Isaac /*@C
8075b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8076b698f381SToby Isaac 
8077d083f849SBarry Smith   Collective on dm
8078d083f849SBarry Smith 
8079b698f381SToby Isaac   Input Parameters:
8080b698f381SToby Isaac + dm    - The DM
8081b698f381SToby Isaac , time  - The time
8082b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8083b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8084574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8085b698f381SToby Isaac - n     - The vector to project along
8086b698f381SToby Isaac 
8087b698f381SToby Isaac   Output Parameter:
8088b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8089b698f381SToby Isaac 
8090b698f381SToby Isaac   Level: developer
8091b698f381SToby Isaac 
8092b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8093b698f381SToby Isaac @*/
8094b698f381SToby 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)
8095b698f381SToby Isaac {
8096b698f381SToby Isaac   PetscErrorCode ierr;
8097b698f381SToby Isaac 
8098b698f381SToby Isaac   PetscFunctionBegin;
8099b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8100b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8101b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8102b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8103b698f381SToby Isaac   PetscFunctionReturn(0);
8104b698f381SToby Isaac }
8105b698f381SToby Isaac 
81062a16baeaSToby Isaac /*@C
81072a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
81082a16baeaSToby Isaac 
8109d083f849SBarry Smith   Collective on dm
8110d083f849SBarry Smith 
81112a16baeaSToby Isaac   Input Parameters:
81122a16baeaSToby Isaac + dm    - The DM
81132a16baeaSToby Isaac . time  - The time
81142a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
81152a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8116574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
81172a16baeaSToby Isaac 
81182a16baeaSToby Isaac   Output Parameter:
81192a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
81202a16baeaSToby Isaac 
81212a16baeaSToby Isaac   Level: developer
81222a16baeaSToby Isaac 
81231189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
81242a16baeaSToby Isaac @*/
81251189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
81262a16baeaSToby Isaac {
81272a16baeaSToby Isaac   PetscErrorCode ierr;
81282a16baeaSToby Isaac 
81292a16baeaSToby Isaac   PetscFunctionBegin;
81302a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81312a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
81320918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
81332a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
81342a16baeaSToby Isaac   PetscFunctionReturn(0);
81352a16baeaSToby Isaac }
81362a16baeaSToby Isaac 
8137df0b854cSToby Isaac /*@C
8138df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8139cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8140df0b854cSToby Isaac 
8141df0b854cSToby Isaac   Collective on dm
8142df0b854cSToby Isaac 
8143df0b854cSToby Isaac   Input parameters:
8144df0b854cSToby Isaac + dm - the pre-adaptation DM object
8145a1b0c543SToby Isaac - label - label with the flags
8146df0b854cSToby Isaac 
8147df0b854cSToby Isaac   Output parameters:
81480d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8149df0b854cSToby Isaac 
8150df0b854cSToby Isaac   Level: intermediate
81510d1cd5e0SMatthew G. Knepley 
81520d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8153df0b854cSToby Isaac @*/
81540d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8155df0b854cSToby Isaac {
8156df0b854cSToby Isaac   PetscErrorCode ierr;
8157df0b854cSToby Isaac 
8158df0b854cSToby Isaac   PetscFunctionBegin;
8159df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8160a1b0c543SToby Isaac   PetscValidPointer(label,2);
81610d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
81620d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
81636f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
81640d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
81650d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
81660d1cd5e0SMatthew G. Knepley }
81670d1cd5e0SMatthew G. Knepley 
81680d1cd5e0SMatthew G. Knepley /*@C
81690d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
81700d1cd5e0SMatthew G. Knepley 
81710d1cd5e0SMatthew G. Knepley   Input Parameters:
81720d1cd5e0SMatthew G. Knepley + dm - The DM object
81730d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
81746f25b0d8SLisandro 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_".
81750d1cd5e0SMatthew G. Knepley 
81760d1cd5e0SMatthew G. Knepley   Output Parameter:
81770d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
81780d1cd5e0SMatthew G. Knepley 
81790d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
81800d1cd5e0SMatthew G. Knepley 
81810d1cd5e0SMatthew G. Knepley   Level: advanced
81820d1cd5e0SMatthew G. Knepley 
81830d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
81840d1cd5e0SMatthew G. Knepley @*/
81850d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
81860d1cd5e0SMatthew G. Knepley {
81870d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
81880d1cd5e0SMatthew G. Knepley 
81890d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
81900d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81910d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
81920d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
81930d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
81946f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
81956f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
81960d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8197df0b854cSToby Isaac   PetscFunctionReturn(0);
8198df0b854cSToby Isaac }
8199c4088d22SMatthew G. Knepley 
8200502a2867SDave May /*@C
8201502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8202502a2867SDave May 
8203502a2867SDave May  Not Collective
8204502a2867SDave May 
8205502a2867SDave May  Input Parameter:
8206502a2867SDave May  . dm    - The DM
8207502a2867SDave May 
8208502a2867SDave May  Output Parameter:
8209502a2867SDave May  . nranks - the number of neighbours
8210502a2867SDave May  . ranks - the neighbors ranks
8211502a2867SDave May 
8212502a2867SDave May  Notes:
8213502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8214502a2867SDave May 
8215502a2867SDave May  Level: beginner
8216502a2867SDave May 
8217dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8218502a2867SDave May @*/
8219502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8220502a2867SDave May {
8221502a2867SDave May   PetscErrorCode ierr;
8222502a2867SDave May 
8223502a2867SDave May   PetscFunctionBegin;
8224502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82250918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8226502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8227502a2867SDave May   PetscFunctionReturn(0);
8228502a2867SDave May }
8229502a2867SDave May 
8230531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8231531c7667SBarry Smith 
8232531c7667SBarry Smith /*
8233531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8234531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8235531c7667SBarry Smith */
8236531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8237531c7667SBarry Smith {
8238531c7667SBarry Smith   PetscErrorCode ierr;
8239531c7667SBarry Smith 
8240531c7667SBarry Smith   PetscFunctionBegin;
8241531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8242531c7667SBarry Smith     Vec x1local;
8243531c7667SBarry Smith     DM  dm;
8244531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8245531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8246531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8247531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8248531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8249531c7667SBarry Smith     x1   = x1local;
8250531c7667SBarry Smith   }
8251531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8252531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8253531c7667SBarry Smith     DM  dm;
8254531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8255531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8256531c7667SBarry Smith   }
8257531c7667SBarry Smith   PetscFunctionReturn(0);
8258531c7667SBarry Smith }
8259531c7667SBarry Smith 
8260531c7667SBarry Smith /*@
8261531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8262531c7667SBarry Smith 
8263531c7667SBarry Smith     Input Parameter:
8264531c7667SBarry Smith .    coloring - the MatFDColoring object
8265531c7667SBarry Smith 
826695452b02SPatrick Sanan     Developer Notes:
826795452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8268531c7667SBarry Smith 
82691b266c99SBarry Smith     Level: advanced
82701b266c99SBarry Smith 
8271531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8272531c7667SBarry Smith @*/
8273531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8274531c7667SBarry Smith {
8275531c7667SBarry Smith   PetscFunctionBegin;
8276531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8277531c7667SBarry Smith   PetscFunctionReturn(0);
8278531c7667SBarry Smith }
82798320bc6fSPatrick Sanan 
82808320bc6fSPatrick Sanan /*@
82818320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
82828320bc6fSPatrick Sanan 
82838320bc6fSPatrick Sanan     Collective
82848320bc6fSPatrick Sanan 
82858320bc6fSPatrick Sanan     Input Parameters:
8286a5bc1bf3SBarry Smith +    dm1 - the first DM
82878320bc6fSPatrick Sanan -    dm2 - the second DM
82888320bc6fSPatrick Sanan 
82898320bc6fSPatrick Sanan     Output Parameters:
82908320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
82918320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
82928320bc6fSPatrick Sanan 
82938320bc6fSPatrick Sanan     Notes:
82948320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
82953d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
82968320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
82973d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
82983d862458SPatrick Sanan     decomposition, but hold different data.
82998320bc6fSPatrick Sanan 
83008320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
83018320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
83028320bc6fSPatrick Sanan 
83038320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
83048320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
83058320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
83068320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
83078320bc6fSPatrick Sanan 
83088320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
83098320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
83108320bc6fSPatrick Sanan .vb
83118320bc6fSPatrick Sanan   ...
83128320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
83138320bc6fSPatrick Sanan   if (set && compatible)  {
83148320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
83158320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
83163d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
83178320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
83188320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
83198320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
83208320bc6fSPatrick Sanan       }
83218320bc6fSPatrick Sanan     }
83228320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
83238320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
83248320bc6fSPatrick Sanan   } else {
83258320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
83268320bc6fSPatrick Sanan   }
83278320bc6fSPatrick Sanan   ...
83288320bc6fSPatrick Sanan .ve
83298320bc6fSPatrick Sanan 
83308320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
83318320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
83328320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
83338320bc6fSPatrick Sanan     always check the "set" output parameter.
83348320bc6fSPatrick Sanan 
83358320bc6fSPatrick Sanan     A DM is always compatible with itself.
83368320bc6fSPatrick Sanan 
83378320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
83388320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
83398320bc6fSPatrick Sanan     incompatible.
83408320bc6fSPatrick Sanan 
83418320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
83428320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
83438320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
83448320bc6fSPatrick Sanan 
83458320bc6fSPatrick Sanan     Developer Notes:
83463d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
83473d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8348a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
83498320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
83508320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
83513d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
83523d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
83538320bc6fSPatrick Sanan 
83548320bc6fSPatrick Sanan     Level: advanced
83558320bc6fSPatrick Sanan 
83563d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
83578320bc6fSPatrick Sanan @*/
83588320bc6fSPatrick Sanan 
8359a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
83608320bc6fSPatrick Sanan {
83618320bc6fSPatrick Sanan   PetscErrorCode ierr;
83628320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
83638320bc6fSPatrick Sanan   DMType         type,type2;
83648320bc6fSPatrick Sanan   PetscBool      sameType;
83658320bc6fSPatrick Sanan 
83668320bc6fSPatrick Sanan   PetscFunctionBegin;
8367a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
83688320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83698320bc6fSPatrick Sanan 
83708320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8371a5bc1bf3SBarry Smith   if (dm1 == dm2) {
83728320bc6fSPatrick Sanan     *set = PETSC_TRUE;
83738320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
83748320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83758320bc6fSPatrick Sanan   }
83768320bc6fSPatrick Sanan 
83778320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
83788320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
83798320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
83808320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8381a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
83828320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
83838320bc6fSPatrick Sanan     *set = PETSC_TRUE;
83848320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
83858320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83868320bc6fSPatrick Sanan   }
83878320bc6fSPatrick Sanan 
83888320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8389a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8390a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8391b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
83928320bc6fSPatrick Sanan   }
83938320bc6fSPatrick Sanan 
8394a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
83958320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8396a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
83978320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
83988320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
83998320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8400a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
84018320bc6fSPatrick Sanan   } else {
84028320bc6fSPatrick Sanan     *set = PETSC_FALSE;
84038320bc6fSPatrick Sanan   }
84048320bc6fSPatrick Sanan   PetscFunctionReturn(0);
84058320bc6fSPatrick Sanan }
8406c0f0dcc3SMatthew G. Knepley 
8407c0f0dcc3SMatthew G. Knepley /*@C
8408c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8409c0f0dcc3SMatthew G. Knepley 
8410c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8411c0f0dcc3SMatthew G. Knepley 
8412c0f0dcc3SMatthew G. Knepley   Input Parameters:
8413c0f0dcc3SMatthew G. Knepley + DM - the DM
8414c0f0dcc3SMatthew G. Knepley . f - the monitor function
8415c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8416c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8417c0f0dcc3SMatthew G. Knepley 
8418c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8419c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8420c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8421c0f0dcc3SMatthew G. Knepley 
8422c0f0dcc3SMatthew G. Knepley   Notes:
8423c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8424c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8425c0f0dcc3SMatthew G. Knepley   order in which they were set.
8426c0f0dcc3SMatthew G. Knepley 
8427c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8428c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8429c0f0dcc3SMatthew G. Knepley 
8430c0f0dcc3SMatthew G. Knepley   Level: intermediate
8431c0f0dcc3SMatthew G. Knepley 
8432c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8433c0f0dcc3SMatthew G. Knepley @*/
8434c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8435c0f0dcc3SMatthew G. Knepley {
8436c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8437c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8438c0f0dcc3SMatthew G. Knepley 
8439c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8440c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8441c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8442c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8443c0f0dcc3SMatthew G. Knepley 
8444c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8445c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8446c0f0dcc3SMatthew G. Knepley   }
8447c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8448c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8449c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8450c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8451c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8452c0f0dcc3SMatthew G. Knepley }
8453c0f0dcc3SMatthew G. Knepley 
8454c0f0dcc3SMatthew G. Knepley /*@
8455c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8456c0f0dcc3SMatthew G. Knepley 
8457c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8458c0f0dcc3SMatthew G. Knepley 
8459c0f0dcc3SMatthew G. Knepley   Input Parameter:
8460c0f0dcc3SMatthew G. Knepley . dm - the DM
8461c0f0dcc3SMatthew G. Knepley 
8462c0f0dcc3SMatthew G. Knepley   Options Database Key:
8463c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8464c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8465c0f0dcc3SMatthew G. Knepley   set via the options database
8466c0f0dcc3SMatthew G. Knepley 
8467c0f0dcc3SMatthew G. Knepley   Notes:
8468c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8469c0f0dcc3SMatthew G. Knepley 
8470c0f0dcc3SMatthew G. Knepley   Level: intermediate
8471c0f0dcc3SMatthew G. Knepley 
8472c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8473c0f0dcc3SMatthew G. Knepley @*/
8474c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8475c0f0dcc3SMatthew G. Knepley {
8476c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8477c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8478c0f0dcc3SMatthew G. Knepley 
8479c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8480c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8481c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8482c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
8483c0f0dcc3SMatthew G. Knepley   }
8484c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8485c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8486c0f0dcc3SMatthew G. Knepley }
8487c0f0dcc3SMatthew G. Knepley 
8488c0f0dcc3SMatthew G. Knepley /*@C
8489c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8490c0f0dcc3SMatthew G. Knepley 
8491c0f0dcc3SMatthew G. Knepley   Collective on DM
8492c0f0dcc3SMatthew G. Knepley 
8493c0f0dcc3SMatthew G. Knepley   Input Parameters:
8494c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8495c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8496c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8497c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8498c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8499c0f0dcc3SMatthew 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
8500c0f0dcc3SMatthew G. Knepley 
8501c0f0dcc3SMatthew G. Knepley   Output Parameter:
8502c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8503c0f0dcc3SMatthew G. Knepley 
8504c0f0dcc3SMatthew G. Knepley   Level: developer
8505c0f0dcc3SMatthew G. Knepley 
8506c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
8507c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
8508c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
8509c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
8510c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
8511c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
8512c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
8513c0f0dcc3SMatthew G. Knepley @*/
8514c0f0dcc3SMatthew 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)
8515c0f0dcc3SMatthew G. Knepley {
8516c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8517c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8518c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
8519c0f0dcc3SMatthew G. Knepley 
8520c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8521c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8522c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
8523c0f0dcc3SMatthew G. Knepley   if (*flg) {
8524c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8525c0f0dcc3SMatthew G. Knepley 
8526c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
8527c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
8528c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
8529c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
8530c0f0dcc3SMatthew G. Knepley   }
8531c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8532c0f0dcc3SMatthew G. Knepley }
8533c0f0dcc3SMatthew G. Knepley 
8534c0f0dcc3SMatthew G. Knepley /*@
8535c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8536c0f0dcc3SMatthew G. Knepley 
8537c0f0dcc3SMatthew G. Knepley    Collective on DM
8538c0f0dcc3SMatthew G. Knepley 
8539c0f0dcc3SMatthew G. Knepley    Input Parameters:
8540c0f0dcc3SMatthew G. Knepley .  dm - The DM
8541c0f0dcc3SMatthew G. Knepley 
8542c0f0dcc3SMatthew G. Knepley    Level: developer
8543c0f0dcc3SMatthew G. Knepley 
8544c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8545c0f0dcc3SMatthew G. Knepley @*/
8546c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8547c0f0dcc3SMatthew G. Knepley {
8548c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8549c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8550c0f0dcc3SMatthew G. Knepley 
8551c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8552c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8553c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8554c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8555c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
8556c0f0dcc3SMatthew G. Knepley   }
8557c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8558c0f0dcc3SMatthew G. Knepley }
8559