xref: /petsc/src/dm/interface/dm.c (revision 2ed6491fad8d8c7fb40bb87d543543c97a75f025)
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};
15ba2698f1SMatthew G. Knepley const char *const DMPolytopeTypes[] = {"point", "segment", "triangle", "quadrilateral", "segment tensor prism", "tetrahedron", "hexahedron", "triangular prism", "triangular tensor prism", "quadrilateral tensor prism", "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 
1021bb6d2a8SBarry Smith   Notes: For some DM this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1031bb6d2a8SBarry Smith          DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does
1041bb6d2a8SBarry Smith          share the PetscSection of the original DM
1051bb6d2a8SBarry Smith 
10692cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1071bb6d2a8SBarry Smith 
10838221697SMatthew G. Knepley @*/
10938221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11038221697SMatthew G. Knepley {
11138221697SMatthew G. Knepley   PetscSF        sf;
11238221697SMatthew G. Knepley   Vec            coords;
11338221697SMatthew G. Knepley   void          *ctx;
114a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11538221697SMatthew G. Knepley   PetscErrorCode ierr;
11638221697SMatthew G. Knepley 
11738221697SMatthew G. Knepley   PetscFunctionBegin;
11838221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11938221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12038221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1215d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
122ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
123ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1241de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1251de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12638221697SMatthew G. Knepley   if (dm->ops->clone) {
12738221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
12838221697SMatthew G. Knepley   }
1293f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13038221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13138221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13238221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13338221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
134be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
135be4c1c3eSMatthew G. Knepley     DM           ncdm;
136be4c1c3eSMatthew G. Knepley     PetscSection cs;
1375a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
138be4c1c3eSMatthew G. Knepley 
13992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
140be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1415a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1425a0206caSToby Isaac     if (pEndMax >= 0) {
143be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
14483bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
14592fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
146a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
147be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
148be4c1c3eSMatthew G. Knepley     }
149be4c1c3eSMatthew G. Knepley   }
150a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
151a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15238221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   if (coords) {
15438221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15538221697SMatthew G. Knepley   } else {
15638221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
15738221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
15838221697SMatthew G. Knepley   }
15990b157c4SStefano Zampini   {
16090b157c4SStefano Zampini     PetscBool             isper;
161c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1625dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16390b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16490b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
165c6b900c6SMatthew G. Knepley   }
16634aa8a36SMatthew G. Knepley   {
16734aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
16834aa8a36SMatthew G. Knepley 
16934aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17134aa8a36SMatthew G. Knepley   }
17238221697SMatthew G. Knepley   PetscFunctionReturn(0);
17338221697SMatthew G. Knepley }
17438221697SMatthew G. Knepley 
1759a42bb27SBarry Smith /*@C
176564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1779a42bb27SBarry Smith 
178d083f849SBarry Smith    Logically Collective on da
1799a42bb27SBarry Smith 
1809a42bb27SBarry Smith    Input Parameter:
1819a42bb27SBarry Smith +  da - initial distributed array
182e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1839a42bb27SBarry Smith 
1849a42bb27SBarry Smith    Options Database:
185dd85299cSBarry Smith .   -dm_vec_type ctype
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith    Level: intermediate
1889a42bb27SBarry Smith 
189a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1909a42bb27SBarry Smith @*/
19119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1929a42bb27SBarry Smith {
1939a42bb27SBarry Smith   PetscErrorCode ierr;
1949a42bb27SBarry Smith 
1959a42bb27SBarry Smith   PetscFunctionBegin;
1969a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1979a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
19819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1999a42bb27SBarry Smith   PetscFunctionReturn(0);
2009a42bb27SBarry Smith }
2019a42bb27SBarry Smith 
202c0dedaeaSBarry Smith /*@C
203c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
204c0dedaeaSBarry Smith 
205d083f849SBarry Smith    Logically Collective on da
206c0dedaeaSBarry Smith 
207c0dedaeaSBarry Smith    Input Parameter:
208c0dedaeaSBarry Smith .  da - initial distributed array
209c0dedaeaSBarry Smith 
210c0dedaeaSBarry Smith    Output Parameter:
211c0dedaeaSBarry Smith .  ctype - the vector type
212c0dedaeaSBarry Smith 
213c0dedaeaSBarry Smith    Level: intermediate
214c0dedaeaSBarry Smith 
215a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
216c0dedaeaSBarry Smith @*/
217c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
218c0dedaeaSBarry Smith {
219c0dedaeaSBarry Smith   PetscFunctionBegin;
220c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
221c0dedaeaSBarry Smith   *ctype = da->vectype;
222c0dedaeaSBarry Smith   PetscFunctionReturn(0);
223c0dedaeaSBarry Smith }
224c0dedaeaSBarry Smith 
2255f1ad066SMatthew G Knepley /*@
22634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2275f1ad066SMatthew G Knepley 
2285f1ad066SMatthew G Knepley   Not collective
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley   Input Parameter:
2315f1ad066SMatthew G Knepley . v - The Vec
2325f1ad066SMatthew G Knepley 
2335f1ad066SMatthew G Knepley   Output Parameter:
2345f1ad066SMatthew G Knepley . dm - The DM
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   Level: intermediate
2375f1ad066SMatthew G Knepley 
2385f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2395f1ad066SMatthew G Knepley @*/
2405f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2415f1ad066SMatthew G Knepley {
2425f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2435f1ad066SMatthew G Knepley 
2445f1ad066SMatthew G Knepley   PetscFunctionBegin;
2455f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2465f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2475f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2485f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2495f1ad066SMatthew G Knepley }
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley /*@
252d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley   Not collective
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley   Input Parameters:
2575f1ad066SMatthew G Knepley + v - The Vec
2585f1ad066SMatthew G Knepley - dm - The DM
2595f1ad066SMatthew G Knepley 
260d9805387SMatthew 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.
261d9805387SMatthew G. Knepley 
2625f1ad066SMatthew G Knepley   Level: intermediate
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2655f1ad066SMatthew G Knepley @*/
2665f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2675f1ad066SMatthew G Knepley {
2685f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   PetscFunctionBegin;
2715f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
272d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2735f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2745f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2755f1ad066SMatthew G Knepley }
2765f1ad066SMatthew G Knepley 
277521d9a4cSLisandro Dalcin /*@C
2788f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2798f1509bcSBarry Smith 
280d083f849SBarry Smith    Logically Collective on dm
2818f1509bcSBarry Smith 
2828f1509bcSBarry Smith    Input Parameters:
2838f1509bcSBarry Smith +  dm - the DM context
2848f1509bcSBarry Smith -  ctype - the matrix type
2858f1509bcSBarry Smith 
2868f1509bcSBarry Smith    Options Database:
2878f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2888f1509bcSBarry Smith 
2898f1509bcSBarry Smith    Level: intermediate
2908f1509bcSBarry Smith 
2918f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2928f1509bcSBarry Smith           DMGetISColoringType()
2938f1509bcSBarry Smith @*/
2948f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2958f1509bcSBarry Smith {
2968f1509bcSBarry Smith   PetscFunctionBegin;
2978f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2988f1509bcSBarry Smith   dm->coloringtype = ctype;
2998f1509bcSBarry Smith   PetscFunctionReturn(0);
3008f1509bcSBarry Smith }
3018f1509bcSBarry Smith 
3028f1509bcSBarry Smith /*@C
3038f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
304521d9a4cSLisandro Dalcin 
305d083f849SBarry Smith    Logically Collective on dm
306521d9a4cSLisandro Dalcin 
307521d9a4cSLisandro Dalcin    Input Parameter:
3088f1509bcSBarry Smith .  dm - the DM context
3098f1509bcSBarry Smith 
3108f1509bcSBarry Smith    Output Parameter:
3118f1509bcSBarry Smith .  ctype - the matrix type
3128f1509bcSBarry Smith 
3138f1509bcSBarry Smith    Options Database:
3148f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3158f1509bcSBarry Smith 
3168f1509bcSBarry Smith    Level: intermediate
3178f1509bcSBarry Smith 
3188f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3198f1509bcSBarry Smith           DMGetISColoringType()
3208f1509bcSBarry Smith @*/
3218f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3228f1509bcSBarry Smith {
3238f1509bcSBarry Smith   PetscFunctionBegin;
3248f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3258f1509bcSBarry Smith   *ctype = dm->coloringtype;
3268f1509bcSBarry Smith   PetscFunctionReturn(0);
3278f1509bcSBarry Smith }
3288f1509bcSBarry Smith 
3298f1509bcSBarry Smith /*@C
3308f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3318f1509bcSBarry Smith 
332d083f849SBarry Smith    Logically Collective on dm
3338f1509bcSBarry Smith 
3348f1509bcSBarry Smith    Input Parameters:
335521d9a4cSLisandro Dalcin +  dm - the DM context
336a2b5a043SBarry Smith -  ctype - the matrix type
337521d9a4cSLisandro Dalcin 
338521d9a4cSLisandro Dalcin    Options Database:
339521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
340521d9a4cSLisandro Dalcin 
341521d9a4cSLisandro Dalcin    Level: intermediate
342521d9a4cSLisandro Dalcin 
343a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
344521d9a4cSLisandro Dalcin @*/
34519fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
346521d9a4cSLisandro Dalcin {
347521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
34888f0584fSBarry Smith 
349521d9a4cSLisandro Dalcin   PetscFunctionBegin;
350521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
351521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
353521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
354521d9a4cSLisandro Dalcin }
355521d9a4cSLisandro Dalcin 
356c0dedaeaSBarry Smith /*@C
357c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
358c0dedaeaSBarry Smith 
359d083f849SBarry Smith    Logically Collective on dm
360c0dedaeaSBarry Smith 
361c0dedaeaSBarry Smith    Input Parameter:
362c0dedaeaSBarry Smith .  dm - the DM context
363c0dedaeaSBarry Smith 
364c0dedaeaSBarry Smith    Output Parameter:
365c0dedaeaSBarry Smith .  ctype - the matrix type
366c0dedaeaSBarry Smith 
367c0dedaeaSBarry Smith    Options Database:
368c0dedaeaSBarry Smith .   -dm_mat_type ctype
369c0dedaeaSBarry Smith 
370c0dedaeaSBarry Smith    Level: intermediate
371c0dedaeaSBarry Smith 
372a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
373c0dedaeaSBarry Smith @*/
374c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
375c0dedaeaSBarry Smith {
376c0dedaeaSBarry Smith   PetscFunctionBegin;
377c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
378c0dedaeaSBarry Smith   *ctype = dm->mattype;
379c0dedaeaSBarry Smith   PetscFunctionReturn(0);
380c0dedaeaSBarry Smith }
381c0dedaeaSBarry Smith 
382c688c046SMatthew G Knepley /*@
38334f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
384c688c046SMatthew G Knepley 
385c688c046SMatthew G Knepley   Not collective
386c688c046SMatthew G Knepley 
387c688c046SMatthew G Knepley   Input Parameter:
388c688c046SMatthew G Knepley . A - The Mat
389c688c046SMatthew G Knepley 
390c688c046SMatthew G Knepley   Output Parameter:
391c688c046SMatthew G Knepley . dm - The DM
392c688c046SMatthew G Knepley 
393c688c046SMatthew G Knepley   Level: intermediate
394c688c046SMatthew G Knepley 
3958f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3968f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
3978f1509bcSBarry Smith 
398c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
399c688c046SMatthew G Knepley @*/
400c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
401c688c046SMatthew G Knepley {
402c688c046SMatthew G Knepley   PetscErrorCode ierr;
403c688c046SMatthew G Knepley 
404c688c046SMatthew G Knepley   PetscFunctionBegin;
405c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
406c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
407c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
408c688c046SMatthew G Knepley   PetscFunctionReturn(0);
409c688c046SMatthew G Knepley }
410c688c046SMatthew G Knepley 
411c688c046SMatthew G Knepley /*@
412c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
413c688c046SMatthew G Knepley 
414c688c046SMatthew G Knepley   Not collective
415c688c046SMatthew G Knepley 
416c688c046SMatthew G Knepley   Input Parameters:
417c688c046SMatthew G Knepley + A - The Mat
418c688c046SMatthew G Knepley - dm - The DM
419c688c046SMatthew G Knepley 
420c688c046SMatthew G Knepley   Level: intermediate
421c688c046SMatthew G Knepley 
4228f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4238f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4248f1509bcSBarry Smith 
4258f1509bcSBarry Smith 
426c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
427c688c046SMatthew G Knepley @*/
428c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
429c688c046SMatthew G Knepley {
430c688c046SMatthew G Knepley   PetscErrorCode ierr;
431c688c046SMatthew G Knepley 
432c688c046SMatthew G Knepley   PetscFunctionBegin;
433c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4348865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
435c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
436c688c046SMatthew G Knepley   PetscFunctionReturn(0);
437c688c046SMatthew G Knepley }
438c688c046SMatthew G Knepley 
4399a42bb27SBarry Smith /*@C
4409a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4416757b960SDave May    DM options in the database.
4429a42bb27SBarry Smith 
443d083f849SBarry Smith    Logically Collective on dm
4449a42bb27SBarry Smith 
4459a42bb27SBarry Smith    Input Parameter:
4468353ddbbSDave May +  da - the DM context
4479a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4489a42bb27SBarry Smith 
4499a42bb27SBarry Smith    Notes:
4509a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4519a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4529a42bb27SBarry Smith 
4539a42bb27SBarry Smith    Level: advanced
4549a42bb27SBarry Smith 
4559a42bb27SBarry Smith .seealso: DMSetFromOptions()
4569a42bb27SBarry Smith @*/
4577087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4589a42bb27SBarry Smith {
4599a42bb27SBarry Smith   PetscErrorCode ierr;
4609a42bb27SBarry Smith 
4619a42bb27SBarry Smith   PetscFunctionBegin;
4629a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4639a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
464691be533SLawrence Mitchell   if (dm->sf) {
465691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
466691be533SLawrence Mitchell   }
4671bb6d2a8SBarry Smith   if (dm->sectionSF) {
4681bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
469691be533SLawrence Mitchell   }
4709a42bb27SBarry Smith   PetscFunctionReturn(0);
4719a42bb27SBarry Smith }
4729a42bb27SBarry Smith 
47331697293SDave May /*@C
47431697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47531697293SDave May    DM options in the database.
47631697293SDave May 
477d083f849SBarry Smith    Logically Collective on dm
47831697293SDave May 
47931697293SDave May    Input Parameters:
48031697293SDave May +  dm - the DM context
48131697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48231697293SDave May 
48331697293SDave May    Notes:
48431697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48531697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48631697293SDave May 
48731697293SDave May    Level: advanced
48831697293SDave May 
48931697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49031697293SDave May @*/
49131697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49231697293SDave May {
49331697293SDave May   PetscErrorCode ierr;
49431697293SDave May 
49531697293SDave May   PetscFunctionBegin;
49631697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49731697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
49831697293SDave May   PetscFunctionReturn(0);
49931697293SDave May }
50031697293SDave May 
50131697293SDave May /*@C
50231697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50331697293SDave May    DM options in the database.
50431697293SDave May 
50531697293SDave May    Not Collective
50631697293SDave May 
50731697293SDave May    Input Parameters:
50831697293SDave May .  dm - the DM context
50931697293SDave May 
51031697293SDave May    Output Parameters:
51131697293SDave May .  prefix - pointer to the prefix string used is returned
51231697293SDave May 
51395452b02SPatrick Sanan    Notes:
51495452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51531697293SDave May    sufficient length to hold the prefix.
51631697293SDave May 
51731697293SDave May    Level: advanced
51831697293SDave May 
51931697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52031697293SDave May @*/
52131697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52231697293SDave May {
52331697293SDave May   PetscErrorCode ierr;
52431697293SDave May 
52531697293SDave May   PetscFunctionBegin;
52631697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52731697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
52831697293SDave May   PetscFunctionReturn(0);
52931697293SDave May }
53031697293SDave May 
53188bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53288bdff64SToby Isaac {
53388bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
53488bdff64SToby Isaac   DMNamedVecLink nlink;
53588bdff64SToby Isaac   PetscErrorCode ierr;
53688bdff64SToby Isaac 
53788bdff64SToby Isaac   PetscFunctionBegin;
538aab5bcd8SJed Brown   *ncrefct = 0;
53988bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
54088bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
54188bdff64SToby Isaac     if (dm->localin[i])  refct--;
54288bdff64SToby Isaac     if (dm->globalin[i]) refct--;
54388bdff64SToby Isaac   }
54488bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
54588bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
54688bdff64SToby Isaac   if (dm->x) {
54788bdff64SToby Isaac     DM obj;
54888bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
54988bdff64SToby Isaac     if (obj == dm) refct--;
55088bdff64SToby Isaac   }
55188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55288bdff64SToby Isaac     refct--;
55388bdff64SToby Isaac     if (recurseCoarse) {
55488bdff64SToby Isaac       PetscInt coarseCount;
55588bdff64SToby Isaac 
55688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55788bdff64SToby Isaac       refct += coarseCount;
55888bdff64SToby Isaac     }
55988bdff64SToby Isaac   }
56088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56188bdff64SToby Isaac     refct--;
56288bdff64SToby Isaac     if (recurseFine) {
56388bdff64SToby Isaac       PetscInt fineCount;
56488bdff64SToby Isaac 
56588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56688bdff64SToby Isaac       refct += fineCount;
56788bdff64SToby Isaac     }
56888bdff64SToby Isaac   }
56988bdff64SToby Isaac   *ncrefct = refct;
57088bdff64SToby Isaac   PetscFunctionReturn(0);
57188bdff64SToby Isaac }
57288bdff64SToby Isaac 
573f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
574354557abSToby Isaac {
5755d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
576354557abSToby Isaac   PetscErrorCode ierr;
577354557abSToby Isaac 
578354557abSToby Isaac   PetscFunctionBegin;
579354557abSToby Isaac   /* destroy the labels */
580354557abSToby Isaac   while (next) {
581354557abSToby Isaac     DMLabelLink tmp = next->next;
582354557abSToby Isaac 
5835d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
584ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
585354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
586354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
587354557abSToby Isaac     next = tmp;
588354557abSToby Isaac   }
5895d80c0bfSVaclav Hapla   dm->labels = NULL;
590354557abSToby Isaac   PetscFunctionReturn(0);
591354557abSToby Isaac }
592354557abSToby Isaac 
59347c6ae99SBarry Smith /*@
5948472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59547c6ae99SBarry Smith 
596d083f849SBarry Smith     Collective on dm
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith     Input Parameter:
59947c6ae99SBarry Smith .   dm - the DM object to destroy
60047c6ae99SBarry Smith 
60147c6ae99SBarry Smith     Level: developer
60247c6ae99SBarry Smith 
603e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith @*/
606fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60747c6ae99SBarry Smith {
60888bdff64SToby Isaac   PetscInt       i, cnt;
609dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61047c6ae99SBarry Smith   PetscErrorCode ierr;
61147c6ae99SBarry Smith 
61247c6ae99SBarry Smith   PetscFunctionBegin;
6136bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6146bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61587e657c6SBarry Smith 
61688bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61788bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61888bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
61988bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
620732e2eb9SMatthew G Knepley   /*
621732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
622732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
623732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
624732e2eb9SMatthew G Knepley   */
6256bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6266bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
627732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6286bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6296bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
630732e2eb9SMatthew G Knepley   }
631f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6320298fd71SBarry Smith   (*dm)->namedglobal = NULL;
633f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6342348bcf4SPeter Brune     nnext = nlink->next;
6352348bcf4SPeter 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);
6362348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6372348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6382348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6392348bcf4SPeter Brune   }
640f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6410298fd71SBarry Smith   (*dm)->namedlocal = NULL;
642f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
643f490541aSPeter Brune     nnext = nlink->next;
644f490541aSPeter 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);
645f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
646f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
647f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
648f490541aSPeter Brune   }
6492348bcf4SPeter Brune 
650b17ce1afSJed Brown   /* Destroy the list of hooks */
651c833c3b5SJed Brown   {
652c833c3b5SJed Brown     DMCoarsenHookLink link,next;
653b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
654b17ce1afSJed Brown       next = link->next;
655b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
656b17ce1afSJed Brown     }
6570298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
658c833c3b5SJed Brown   }
659c833c3b5SJed Brown   {
660c833c3b5SJed Brown     DMRefineHookLink link,next;
661c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
662c833c3b5SJed Brown       next = link->next;
663c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
664c833c3b5SJed Brown     }
6650298fd71SBarry Smith     (*dm)->refinehook = NULL;
666c833c3b5SJed Brown   }
667be081cd6SPeter Brune   {
668be081cd6SPeter Brune     DMSubDomainHookLink link,next;
669be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
670be081cd6SPeter Brune       next = link->next;
671be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
672be081cd6SPeter Brune     }
6730298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
674be081cd6SPeter Brune   }
675baf369e7SPeter Brune   {
676baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
677baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
678baf369e7SPeter Brune       next = link->next;
679baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
680baf369e7SPeter Brune     }
6810298fd71SBarry Smith     (*dm)->gtolhook = NULL;
682baf369e7SPeter Brune   }
683d4d07f1eSToby Isaac   {
684d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
685d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
686d4d07f1eSToby Isaac       next = link->next;
687d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
688d4d07f1eSToby Isaac     }
689d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
690d4d07f1eSToby Isaac   }
691aa1993deSMatthew G Knepley   /* Destroy the work arrays */
692aa1993deSMatthew G Knepley   {
693aa1993deSMatthew G Knepley     DMWorkLink link,next;
694aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
695aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
696aa1993deSMatthew G Knepley       next = link->next;
697aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
698aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
699aa1993deSMatthew G Knepley     }
7000298fd71SBarry Smith     (*dm)->workin = NULL;
701aa1993deSMatthew G Knepley   }
702c58f1c22SToby Isaac   /* destroy the labels */
703f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
704f4cdcedcSVaclav Hapla   /* destroy the fields */
705e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
706f4cdcedcSVaclav Hapla   /* destroy the boundaries */
707e6f8dbb6SToby Isaac   {
708e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
709e6f8dbb6SToby Isaac     while (next) {
710e6f8dbb6SToby Isaac       DMBoundary b = next;
711e6f8dbb6SToby Isaac 
712e6f8dbb6SToby Isaac       next = b->next;
713e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
714e6f8dbb6SToby Isaac     }
715e6f8dbb6SToby Isaac   }
716b17ce1afSJed Brown 
71752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
71852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
71952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72052536dc3SBarry Smith 
7211a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7221a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7231a266240SBarry Smith   }
72487e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
72571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7264dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7276bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7286bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
729073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
73088ed4aceSMatthew G Knepley 
7311bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7321bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7338b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
734fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
735fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
73688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7371bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
738736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
739736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7408e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
741736995cdSBlaise Bourdin     }
742736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
743736995cdSBlaise Bourdin   }
74488bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
74588bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
74688bdff64SToby Isaac   }
747a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
74888bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
74988bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
75088bdff64SToby Isaac   }
75188bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
752f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7536636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7546636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7556636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7565dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
757ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
758ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
759ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7606636e97aSMatthew G Knepley 
761e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
76214f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
763e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
764e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
765732e2eb9SMatthew G Knepley 
766ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7676bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
768ed3c66a1SDave May   }
769c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
770435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
771732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
77247c6ae99SBarry Smith   PetscFunctionReturn(0);
77347c6ae99SBarry Smith }
77447c6ae99SBarry Smith 
775d7bf68aeSBarry Smith /*@
776d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
777d7bf68aeSBarry Smith 
778d083f849SBarry Smith     Collective on dm
779d7bf68aeSBarry Smith 
780d7bf68aeSBarry Smith     Input Parameter:
781d7bf68aeSBarry Smith .   dm - the DM object to setup
782d7bf68aeSBarry Smith 
783d7bf68aeSBarry Smith     Level: developer
784d7bf68aeSBarry Smith 
785e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
786d7bf68aeSBarry Smith 
787d7bf68aeSBarry Smith @*/
7887087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
789d7bf68aeSBarry Smith {
790d7bf68aeSBarry Smith   PetscErrorCode ierr;
791d7bf68aeSBarry Smith 
792d7bf68aeSBarry Smith   PetscFunctionBegin;
793171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7948387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
795d7bf68aeSBarry Smith   if (dm->ops->setup) {
796d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
797d7bf68aeSBarry Smith   }
7988387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
799d7bf68aeSBarry Smith   PetscFunctionReturn(0);
800d7bf68aeSBarry Smith }
801d7bf68aeSBarry Smith 
802d7bf68aeSBarry Smith /*@
803d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
804d7bf68aeSBarry Smith 
805d083f849SBarry Smith     Collective on dm
806d7bf68aeSBarry Smith 
807d7bf68aeSBarry Smith     Input Parameter:
808d7bf68aeSBarry Smith .   dm - the DM object to set options for
809d7bf68aeSBarry Smith 
810732e2eb9SMatthew G Knepley     Options Database:
8114164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8124164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8134164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8148f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
815732e2eb9SMatthew G Knepley 
816384a6580SVaclav Hapla     DMPLEX Specific Checks
817384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
818384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
819384a6580SVaclav 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()
820384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
821384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
822384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
823384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
824d7bf68aeSBarry Smith 
82595eb5ee5SVaclav Hapla     Level: intermediate
82695eb5ee5SVaclav Hapla 
82795eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
82895eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
829d7bf68aeSBarry Smith 
830d7bf68aeSBarry Smith @*/
8317087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
832d7bf68aeSBarry Smith {
8337781c08eSBarry Smith   char           typeName[256];
834ca266f36SBarry Smith   PetscBool      flg;
835d7bf68aeSBarry Smith   PetscErrorCode ierr;
836d7bf68aeSBarry Smith 
837d7bf68aeSBarry Smith   PetscFunctionBegin;
838171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83949be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
84049be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8411bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8423194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8430298fd71SBarry 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);
844a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
845f9ba7244SBarry Smith   if (flg) {
846f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
847f9ba7244SBarry Smith   }
848a264d7a6SBarry 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);
849073dac72SJed Brown   if (flg) {
850521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
851073dac72SJed Brown   }
8528f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
853f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
854e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
855f9ba7244SBarry Smith   }
856f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8570633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
85882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
859d7bf68aeSBarry Smith   PetscFunctionReturn(0);
860d7bf68aeSBarry Smith }
861d7bf68aeSBarry Smith 
862fc9bc008SSatish Balay /*@C
863fe2efc57SMark    DMViewFromOptions - View from Options
864fe2efc57SMark 
865fe2efc57SMark    Collective on DM
866fe2efc57SMark 
867fe2efc57SMark    Input Parameters:
868fe2efc57SMark +  dm - the DM object
869736c3998SJose E. Roman .  obj - Optional object
870736c3998SJose E. Roman -  name - command line option
871fe2efc57SMark 
872fe2efc57SMark    Level: intermediate
873fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
874fe2efc57SMark @*/
875fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
876fe2efc57SMark {
877fe2efc57SMark   PetscErrorCode ierr;
878fe2efc57SMark 
879fe2efc57SMark   PetscFunctionBegin;
880fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
881fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
882fe2efc57SMark   PetscFunctionReturn(0);
883fe2efc57SMark }
884fe2efc57SMark 
885fe2efc57SMark /*@C
886224748a4SBarry Smith     DMView - Views a DM
88747c6ae99SBarry Smith 
888d083f849SBarry Smith     Collective on dm
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith     Input Parameter:
89147c6ae99SBarry Smith +   dm - the DM object to view
89247c6ae99SBarry Smith -   v - the viewer
89347c6ae99SBarry Smith 
894224748a4SBarry Smith     Level: beginner
89547c6ae99SBarry Smith 
896e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith @*/
8997087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
90047c6ae99SBarry Smith {
90147c6ae99SBarry Smith   PetscErrorCode    ierr;
90232c0f0efSBarry Smith   PetscBool         isbinary;
90376a8abe0SBarry Smith   PetscMPIInt       size;
90476a8abe0SBarry Smith   PetscViewerFormat format;
90547c6ae99SBarry Smith 
90647c6ae99SBarry Smith   PetscFunctionBegin;
907171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9083014e516SBarry Smith   if (!v) {
909ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9103014e516SBarry Smith   }
911b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
91274903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
91374903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
91474903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
91574903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
91674903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
91774903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
91874903a4fSStefano Zampini      in an error here */
91974903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9208bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
921b1b135c8SBarry Smith 
92276a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
92376a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
92476a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
92598c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
92632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
92732c0f0efSBarry Smith   if (isbinary) {
92855849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
92932c0f0efSBarry Smith     char     type[256];
93032c0f0efSBarry Smith 
93132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
93232c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
93332c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
93432c0f0efSBarry Smith   }
9350c010503SBarry Smith   if (dm->ops->view) {
9360c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
93747c6ae99SBarry Smith   }
93847c6ae99SBarry Smith   PetscFunctionReturn(0);
93947c6ae99SBarry Smith }
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith /*@
9428472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
94347c6ae99SBarry Smith 
944d083f849SBarry Smith     Collective on dm
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith     Input Parameter:
94747c6ae99SBarry Smith .   dm - the DM object
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith     Output Parameter:
95047c6ae99SBarry Smith .   vec - the global vector
95147c6ae99SBarry Smith 
952073dac72SJed Brown     Level: beginner
95347c6ae99SBarry Smith 
954e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
95547c6ae99SBarry Smith 
95647c6ae99SBarry Smith @*/
9577087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
95847c6ae99SBarry Smith {
95947c6ae99SBarry Smith   PetscErrorCode ierr;
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith   PetscFunctionBegin;
962171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
963b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
964b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
96547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
96647c6ae99SBarry Smith   PetscFunctionReturn(0);
96747c6ae99SBarry Smith }
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith /*@
9708472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
97147c6ae99SBarry Smith 
97247c6ae99SBarry Smith     Not Collective
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith     Input Parameter:
97547c6ae99SBarry Smith .   dm - the DM object
97647c6ae99SBarry Smith 
97747c6ae99SBarry Smith     Output Parameter:
97847c6ae99SBarry Smith .   vec - the local vector
97947c6ae99SBarry Smith 
980073dac72SJed Brown     Level: beginner
98147c6ae99SBarry Smith 
982e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith @*/
9857087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
98647c6ae99SBarry Smith {
98747c6ae99SBarry Smith   PetscErrorCode ierr;
98847c6ae99SBarry Smith 
98947c6ae99SBarry Smith   PetscFunctionBegin;
990171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
991b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
992b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
99347c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
99447c6ae99SBarry Smith   PetscFunctionReturn(0);
99547c6ae99SBarry Smith }
99647c6ae99SBarry Smith 
9971411c6eeSJed Brown /*@
9981411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9991411c6eeSJed Brown 
1000d083f849SBarry Smith    Collective on dm
10011411c6eeSJed Brown 
10021411c6eeSJed Brown    Input Parameter:
10031411c6eeSJed Brown .  dm - the DM that provides the mapping
10041411c6eeSJed Brown 
10051411c6eeSJed Brown    Output Parameter:
10061411c6eeSJed Brown .  ltog - the mapping
10071411c6eeSJed Brown 
10081411c6eeSJed Brown    Level: intermediate
10091411c6eeSJed Brown 
10101411c6eeSJed Brown    Notes:
10111411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10121411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10131411c6eeSJed Brown 
1014fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10151411c6eeSJed Brown @*/
10167087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10171411c6eeSJed Brown {
10180be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10191411c6eeSJed Brown   PetscErrorCode ierr;
10201411c6eeSJed Brown 
10211411c6eeSJed Brown   PetscFunctionBegin;
10221411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10231411c6eeSJed Brown   PetscValidPointer(ltog,2);
10241411c6eeSJed Brown   if (!dm->ltogmap) {
102537d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
102637d0c07bSMatthew G Knepley 
102792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
102837d0c07bSMatthew G Knepley     if (section) {
1029a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
103037d0c07bSMatthew G Knepley       PetscInt       *ltog;
1031ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
103237d0c07bSMatthew G Knepley 
1033e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103437d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1035ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1036ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
103737d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1038a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
103937d0c07bSMatthew G Knepley 
104037d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
104137d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1042a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1043a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
104437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10451a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10461a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10471a7dc684SMatthew G. Knepley         if (dof) {
1048b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1049b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10501a7dc684SMatthew G. Knepley         }
105137d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1052a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1053a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
105437d0c07bSMatthew G Knepley         }
105537d0c07bSMatthew G Knepley       }
1056bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10570be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10580be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10590be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10600be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10617591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10627591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1063ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1064ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1065ccf3bd66SMatthew G. Knepley         n /= bs;
1066ccf3bd66SMatthew G. Knepley       }
1067ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10683bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
106937d0c07bSMatthew G Knepley     } else {
1070b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1071184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10721411c6eeSJed Brown     }
107337d0c07bSMatthew G Knepley   }
10741411c6eeSJed Brown   *ltog = dm->ltogmap;
10751411c6eeSJed Brown   PetscFunctionReturn(0);
10761411c6eeSJed Brown }
10771411c6eeSJed Brown 
10781411c6eeSJed Brown /*@
10791411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10801411c6eeSJed Brown 
10811411c6eeSJed Brown    Not Collective
10821411c6eeSJed Brown 
10831411c6eeSJed Brown    Input Parameter:
10841411c6eeSJed Brown .  dm - the DM with block structure
10851411c6eeSJed Brown 
10861411c6eeSJed Brown    Output Parameter:
10871411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10881411c6eeSJed Brown 
10891411c6eeSJed Brown    Level: intermediate
10901411c6eeSJed Brown 
1091fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10921411c6eeSJed Brown @*/
10937087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10941411c6eeSJed Brown {
10951411c6eeSJed Brown   PetscFunctionBegin;
10961411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1097534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
10981411c6eeSJed 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");
10991411c6eeSJed Brown   *bs = dm->bs;
11001411c6eeSJed Brown   PetscFunctionReturn(0);
11011411c6eeSJed Brown }
11021411c6eeSJed Brown 
110347c6ae99SBarry Smith /*@
11048472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
110547c6ae99SBarry Smith 
1106d083f849SBarry Smith     Collective on dm1
110747c6ae99SBarry Smith 
110847c6ae99SBarry Smith     Input Parameter:
110947c6ae99SBarry Smith +   dm1 - the DM object
111047c6ae99SBarry Smith -   dm2 - the second, finer DM object
111147c6ae99SBarry Smith 
111247c6ae99SBarry Smith     Output Parameter:
111347c6ae99SBarry Smith +  mat - the interpolation
111447c6ae99SBarry Smith -  vec - the scaling (optional)
111547c6ae99SBarry Smith 
111647c6ae99SBarry Smith     Level: developer
111747c6ae99SBarry Smith 
111895452b02SPatrick Sanan     Notes:
111995452b02SPatrick 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
112085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1121d52bd9f3SBarry Smith 
11221f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1123d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
112485afcc9aSBarry Smith 
112585afcc9aSBarry Smith 
1126*2ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
112747c6ae99SBarry Smith 
112847c6ae99SBarry Smith @*/
1129e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
113047c6ae99SBarry Smith {
113147c6ae99SBarry Smith   PetscErrorCode ierr;
113247c6ae99SBarry Smith 
113347c6ae99SBarry Smith   PetscFunctionBegin;
1134171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1135171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1136c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1137b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dm1)->type_name);
1138cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
113925296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1140cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
114147c6ae99SBarry Smith   PetscFunctionReturn(0);
114247c6ae99SBarry Smith }
114347c6ae99SBarry Smith 
11443ad4599aSBarry Smith /*@
1145*2ed6491fSPatrick Sanan     DMCreateInterpolationScale - Forms L = R*1/diag(R*1) - L.*v is like a coarse grid average of the
1146*2ed6491fSPatrick Sanan       nearby fine grid points.
1147*2ed6491fSPatrick Sanan 
1148*2ed6491fSPatrick Sanan   Input Parameters:
1149*2ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
1150*2ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
1151*2ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
1152*2ed6491fSPatrick Sanan 
1153*2ed6491fSPatrick Sanan   Output Parameter:
1154*2ed6491fSPatrick Sanan .    scale - the scaled vector
1155*2ed6491fSPatrick Sanan 
1156*2ed6491fSPatrick Sanan   Level: developer
1157*2ed6491fSPatrick Sanan 
1158*2ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
1159*2ed6491fSPatrick Sanan 
1160*2ed6491fSPatrick Sanan @*/
1161*2ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
1162*2ed6491fSPatrick Sanan {
1163*2ed6491fSPatrick Sanan   PetscErrorCode ierr;
1164*2ed6491fSPatrick Sanan   Vec            fine;
1165*2ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
1166*2ed6491fSPatrick Sanan 
1167*2ed6491fSPatrick Sanan   PetscFunctionBegin;
1168*2ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
1169*2ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
1170*2ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
1171*2ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
1172*2ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
1173*2ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
1174*2ed6491fSPatrick Sanan   PetscFunctionReturn(0);
1175*2ed6491fSPatrick Sanan }
1176*2ed6491fSPatrick Sanan 
1177*2ed6491fSPatrick Sanan /*@
11783ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11793ad4599aSBarry Smith 
1180d083f849SBarry Smith     Collective on dm1
11813ad4599aSBarry Smith 
11823ad4599aSBarry Smith     Input Parameter:
11833ad4599aSBarry Smith +   dm1 - the DM object
11843ad4599aSBarry Smith -   dm2 - the second, finer DM object
11853ad4599aSBarry Smith 
11863ad4599aSBarry Smith     Output Parameter:
11873ad4599aSBarry Smith .  mat - the restriction
11883ad4599aSBarry Smith 
11893ad4599aSBarry Smith 
11903ad4599aSBarry Smith     Level: developer
11913ad4599aSBarry Smith 
119295452b02SPatrick Sanan     Notes:
119395452b02SPatrick 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
11943ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11953ad4599aSBarry Smith 
11963ad4599aSBarry Smith 
11973ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11983ad4599aSBarry Smith 
11993ad4599aSBarry Smith @*/
12003ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
12013ad4599aSBarry Smith {
12023ad4599aSBarry Smith   PetscErrorCode ierr;
12033ad4599aSBarry Smith 
12043ad4599aSBarry Smith   PetscFunctionBegin;
12053ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
12063ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
12075a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1208b9d85ea2SLisandro Dalcin   if (!dm1->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dm1)->type_name);
12093ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
12103ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
12113ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
12123ad4599aSBarry Smith   PetscFunctionReturn(0);
12133ad4599aSBarry Smith }
12143ad4599aSBarry Smith 
121547c6ae99SBarry Smith /*@
12168472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
121747c6ae99SBarry Smith 
1218d083f849SBarry Smith     Collective on dm1
121947c6ae99SBarry Smith 
122047c6ae99SBarry Smith     Input Parameter:
122147c6ae99SBarry Smith +   dm1 - the DM object
122247c6ae99SBarry Smith -   dm2 - the second, finer DM object
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith     Output Parameter:
12256dbf9973SLawrence Mitchell .   mat - the injection
122647c6ae99SBarry Smith 
122747c6ae99SBarry Smith     Level: developer
122847c6ae99SBarry Smith 
122995452b02SPatrick Sanan    Notes:
123095452b02SPatrick 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
123185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
123285afcc9aSBarry Smith 
1233e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
123447c6ae99SBarry Smith 
123547c6ae99SBarry Smith @*/
12366dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
123747c6ae99SBarry Smith {
123847c6ae99SBarry Smith   PetscErrorCode ierr;
123947c6ae99SBarry Smith 
124047c6ae99SBarry Smith   PetscFunctionBegin;
1241171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1242171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
12435a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1244b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dm1)->type_name);
12455a84ad33SLisandro Dalcin   ierr = PetscLogEventBegin(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
12465a84ad33SLisandro Dalcin   ierr = (*dm1->ops->createinjection)(dm1,dm2,mat);CHKERRQ(ierr);
12475a84ad33SLisandro Dalcin   ierr = PetscLogEventEnd(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
124847c6ae99SBarry Smith   PetscFunctionReturn(0);
124947c6ae99SBarry Smith }
125047c6ae99SBarry Smith 
1251b412c318SBarry Smith /*@
1252bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1253bd041c0cSMatthew G. Knepley 
1254d083f849SBarry Smith   Collective on dm1
1255bd041c0cSMatthew G. Knepley 
1256bd041c0cSMatthew G. Knepley   Input Parameter:
1257bd041c0cSMatthew G. Knepley + dm1 - the DM object
1258bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1259bd041c0cSMatthew G. Knepley 
1260bd041c0cSMatthew G. Knepley   Output Parameter:
1261bd041c0cSMatthew G. Knepley . mat - the interpolation
1262bd041c0cSMatthew G. Knepley 
1263bd041c0cSMatthew G. Knepley   Level: developer
1264bd041c0cSMatthew G. Knepley 
1265bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1266bd041c0cSMatthew G. Knepley @*/
1267bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1268bd041c0cSMatthew G. Knepley {
1269bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1270bd041c0cSMatthew G. Knepley 
1271bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1272bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1273bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
12745a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1275b9d85ea2SLisandro Dalcin   if (!dm1->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dm1)->type_name);
1276bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1277bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1278bd041c0cSMatthew G. Knepley }
1279bd041c0cSMatthew G. Knepley 
1280bd041c0cSMatthew G. Knepley /*@
1281b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
128247c6ae99SBarry Smith 
1283d083f849SBarry Smith     Collective on dm
128447c6ae99SBarry Smith 
128547c6ae99SBarry Smith     Input Parameter:
128647c6ae99SBarry Smith +   dm - the DM object
12875bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
128847c6ae99SBarry Smith 
128947c6ae99SBarry Smith     Output Parameter:
129047c6ae99SBarry Smith .   coloring - the coloring
129147c6ae99SBarry Smith 
1292ec5066bdSBarry Smith     Notes:
1293ec5066bdSBarry 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
1294ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1295ec5066bdSBarry Smith 
1296ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1297ec5066bdSBarry Smith 
129847c6ae99SBarry Smith     Level: developer
129947c6ae99SBarry Smith 
1300ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
130147c6ae99SBarry Smith 
1302aab9d709SJed Brown @*/
1303b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
130447c6ae99SBarry Smith {
130547c6ae99SBarry Smith   PetscErrorCode ierr;
130647c6ae99SBarry Smith 
130747c6ae99SBarry Smith   PetscFunctionBegin;
1308171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13095a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1310b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1311b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
131247c6ae99SBarry Smith   PetscFunctionReturn(0);
131347c6ae99SBarry Smith }
131447c6ae99SBarry Smith 
1315b412c318SBarry Smith /*@
13168472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
131747c6ae99SBarry Smith 
1318d083f849SBarry Smith     Collective on dm
131947c6ae99SBarry Smith 
132047c6ae99SBarry Smith     Input Parameter:
1321b412c318SBarry Smith .   dm - the DM object
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith     Output Parameter:
132447c6ae99SBarry Smith .   mat - the empty Jacobian
132547c6ae99SBarry Smith 
1326073dac72SJed Brown     Level: beginner
132747c6ae99SBarry Smith 
132895452b02SPatrick Sanan     Notes:
132995452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
133094013140SBarry Smith        do not need to do it yourself.
133194013140SBarry Smith 
133294013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13337889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
133494013140SBarry Smith 
133594013140SBarry 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
133694013140SBarry Smith        internally by PETSc.
133794013140SBarry Smith 
133894013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1339aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
134094013140SBarry Smith 
1341b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
134247c6ae99SBarry Smith 
1343aab9d709SJed Brown @*/
1344b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
134547c6ae99SBarry Smith {
134647c6ae99SBarry Smith   PetscErrorCode ierr;
134747c6ae99SBarry Smith 
134847c6ae99SBarry Smith   PetscFunctionBegin;
1349171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1350c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1351b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13525a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1353fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1354b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1355e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1356e5e52638SMatthew G. Knepley   if (dm->Nf) {
1357e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1358e571a35bSMatthew G. Knepley     PetscInt     Nf;
1359e571a35bSMatthew G. Knepley 
1360e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1361e571a35bSMatthew G. Knepley     if (Nf == 1) {
1362e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1363e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1364e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1365e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1366e571a35bSMatthew G. Knepley       }
1367e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1368e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1369e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1370e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1371e571a35bSMatthew G. Knepley       }
1372e571a35bSMatthew G. Knepley     }
1373e571a35bSMatthew G. Knepley   }
1374fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
137547c6ae99SBarry Smith   PetscFunctionReturn(0);
137647c6ae99SBarry Smith }
137747c6ae99SBarry Smith 
1378732e2eb9SMatthew G Knepley /*@
1379950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1380732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1381732e2eb9SMatthew G Knepley 
1382d083f849SBarry Smith   Logically Collective on dm
1383732e2eb9SMatthew G Knepley 
1384732e2eb9SMatthew G Knepley   Input Parameter:
1385732e2eb9SMatthew G Knepley + dm - the DM
1386732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1387732e2eb9SMatthew G Knepley 
1388732e2eb9SMatthew G Knepley   Level: developer
1389b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1390732e2eb9SMatthew G Knepley @*/
1391732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1392732e2eb9SMatthew G Knepley {
1393732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1394732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1395732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1396732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1397732e2eb9SMatthew G Knepley }
1398732e2eb9SMatthew G Knepley 
1399b06ff27eSHong Zhang /*@
1400b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1401b06ff27eSHong Zhang     but the array for values will not be allocated.
1402b06ff27eSHong Zhang 
1403d083f849SBarry Smith   Logically Collective on dm
1404b06ff27eSHong Zhang 
1405b06ff27eSHong Zhang   Input Parameter:
1406b06ff27eSHong Zhang + dm - the DM
1407b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1408b06ff27eSHong Zhang 
1409b06ff27eSHong Zhang   Level: developer
1410b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1411b06ff27eSHong Zhang @*/
1412b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1413b06ff27eSHong Zhang {
1414b06ff27eSHong Zhang   PetscFunctionBegin;
1415b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1416b06ff27eSHong Zhang   dm->structure_only = only;
1417b06ff27eSHong Zhang   PetscFunctionReturn(0);
1418b06ff27eSHong Zhang }
1419b06ff27eSHong Zhang 
1420a89ea682SMatthew G Knepley /*@C
1421aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1422a89ea682SMatthew G Knepley 
1423a89ea682SMatthew G Knepley   Not Collective
1424a89ea682SMatthew G Knepley 
1425a89ea682SMatthew G Knepley   Input Parameters:
1426a89ea682SMatthew G Knepley + dm - the DM object
1427aa1993deSMatthew G Knepley . count - The minium size
142869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1429a89ea682SMatthew G Knepley 
1430a89ea682SMatthew G Knepley   Output Parameter:
1431a89ea682SMatthew G Knepley . array - the work array
1432a89ea682SMatthew G Knepley 
1433a89ea682SMatthew G Knepley   Level: developer
1434a89ea682SMatthew G Knepley 
1435a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1436a89ea682SMatthew G Knepley @*/
143769291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1438a89ea682SMatthew G Knepley {
1439a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1440aa1993deSMatthew G Knepley   DMWorkLink     link;
144169291d52SBarry Smith   PetscMPIInt    dsize;
1442a89ea682SMatthew G Knepley 
1443a89ea682SMatthew G Knepley   PetscFunctionBegin;
1444a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1445aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1446aa1993deSMatthew G Knepley   if (dm->workin) {
1447aa1993deSMatthew G Knepley     link       = dm->workin;
1448aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1449aa1993deSMatthew G Knepley   } else {
1450b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1451a89ea682SMatthew G Knepley   }
145269291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14535056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1454aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1455854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1456854ce69bSBarry Smith     link->bytes = dsize*count;
1457aa1993deSMatthew G Knepley   }
1458aa1993deSMatthew G Knepley   link->next   = dm->workout;
1459aa1993deSMatthew G Knepley   dm->workout  = link;
1460aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1461a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1462a89ea682SMatthew G Knepley }
1463a89ea682SMatthew G Knepley 
1464aa1993deSMatthew G Knepley /*@C
1465aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1466aa1993deSMatthew G Knepley 
1467aa1993deSMatthew G Knepley   Not Collective
1468aa1993deSMatthew G Knepley 
1469aa1993deSMatthew G Knepley   Input Parameters:
1470aa1993deSMatthew G Knepley + dm - the DM object
1471aa1993deSMatthew G Knepley . count - The minium size
147269291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1473aa1993deSMatthew G Knepley 
1474aa1993deSMatthew G Knepley   Output Parameter:
1475aa1993deSMatthew G Knepley . array - the work array
1476aa1993deSMatthew G Knepley 
1477aa1993deSMatthew G Knepley   Level: developer
1478aa1993deSMatthew G Knepley 
147995452b02SPatrick Sanan   Developer Notes:
148095452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1481aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1482aa1993deSMatthew G Knepley @*/
148369291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1484aa1993deSMatthew G Knepley {
1485aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1486aa1993deSMatthew G Knepley 
1487aa1993deSMatthew G Knepley   PetscFunctionBegin;
1488aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1489aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1490aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1491aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1492aa1993deSMatthew G Knepley       *p           = link->next;
1493aa1993deSMatthew G Knepley       link->next   = dm->workin;
1494aa1993deSMatthew G Knepley       dm->workin   = link;
14950298fd71SBarry Smith       *(void**)mem = NULL;
1496aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1497aa1993deSMatthew G Knepley     }
1498aa1993deSMatthew G Knepley   }
1499aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1500aa1993deSMatthew G Knepley }
1501e7c4fc90SDmitry Karpeev 
1502435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1503435a35e8SMatthew G Knepley {
1504435a35e8SMatthew G Knepley   PetscFunctionBegin;
1505435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
150682f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1507435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1508435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1509435a35e8SMatthew G Knepley }
1510435a35e8SMatthew G Knepley 
15110a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
15120a50eb56SMatthew G. Knepley {
15130a50eb56SMatthew G. Knepley   PetscFunctionBegin;
15140a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1515f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
15160a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15170a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15180a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15190a50eb56SMatthew G. Knepley }
15200a50eb56SMatthew G. Knepley 
1521f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1522f9d4088aSMatthew G. Knepley {
1523f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1524f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1525f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1526f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1527f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1528f9d4088aSMatthew G. Knepley }
1529f9d4088aSMatthew G. Knepley 
1530f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(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   PetscValidPointer(nullsp, 3);
1535f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1536f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1537f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1538f9d4088aSMatthew G. Knepley }
1539f9d4088aSMatthew G. Knepley 
15404f3b5142SJed Brown /*@C
15414d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
15424d343eeaSMatthew G Knepley 
15434d343eeaSMatthew G Knepley   Not collective
15444d343eeaSMatthew G Knepley 
15454d343eeaSMatthew G Knepley   Input Parameter:
15464d343eeaSMatthew G Knepley . dm - the DM object
15474d343eeaSMatthew G Knepley 
15484d343eeaSMatthew G Knepley   Output Parameters:
15490298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
15500298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
15510298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
15524d343eeaSMatthew G Knepley 
15534d343eeaSMatthew G Knepley   Level: intermediate
15544d343eeaSMatthew G Knepley 
155521c9b008SJed Brown   Notes:
155621c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
155721c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
155821c9b008SJed Brown   PetscFree().
155921c9b008SJed Brown 
15604d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
15614d343eeaSMatthew G Knepley @*/
156237d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
15634d343eeaSMatthew G Knepley {
156437d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
15654d343eeaSMatthew G Knepley   PetscErrorCode ierr;
15664d343eeaSMatthew G Knepley 
15674d343eeaSMatthew G Knepley   PetscFunctionBegin;
15684d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
156969ca1f37SDmitry Karpeev   if (numFields) {
1570534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
157169ca1f37SDmitry Karpeev     *numFields = 0;
157269ca1f37SDmitry Karpeev   }
157337d0c07bSMatthew G Knepley   if (fieldNames) {
157437d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15750298fd71SBarry Smith     *fieldNames = NULL;
157669ca1f37SDmitry Karpeev   }
157769ca1f37SDmitry Karpeev   if (fields) {
157869ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15790298fd71SBarry Smith     *fields = NULL;
158069ca1f37SDmitry Karpeev   }
158192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
158237d0c07bSMatthew G Knepley   if (section) {
15833a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
158437d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
158537d0c07bSMatthew G Knepley 
1586e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
158737d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15883a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
158937d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
159037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
159137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15923a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
159337d0c07bSMatthew G Knepley     }
159437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
159537d0c07bSMatthew G Knepley       PetscInt gdof;
159637d0c07bSMatthew G Knepley 
159737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
159837d0c07bSMatthew G Knepley       if (gdof > 0) {
159937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
16003a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
160137d0c07bSMatthew G Knepley 
160237d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
160337d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
16043a544194SStefano Zampini           fpdof = fdof-fcdof;
16053a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
16063a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
16073a544194SStefano Zampini             fieldNc[f] = 1;
16083a544194SStefano Zampini           }
16093a544194SStefano Zampini           fieldSizes[f] += fpdof;
161037d0c07bSMatthew G Knepley         }
161137d0c07bSMatthew G Knepley       }
161237d0c07bSMatthew G Knepley     }
161337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1614785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
161537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
161637d0c07bSMatthew G Knepley     }
161737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
161837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
161937d0c07bSMatthew G Knepley 
162037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
162137d0c07bSMatthew G Knepley       if (gdof > 0) {
162237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
162337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
162437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
162537d0c07bSMatthew G Knepley 
162637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
162737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
162837d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
162937d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
163037d0c07bSMatthew G Knepley           }
163137d0c07bSMatthew G Knepley         }
163237d0c07bSMatthew G Knepley       }
163337d0c07bSMatthew G Knepley     }
16348865f1eaSKarl Rupp     if (numFields) *numFields = nF;
163537d0c07bSMatthew G Knepley     if (fieldNames) {
1636785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
163737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
163837d0c07bSMatthew G Knepley         const char *fieldName;
163937d0c07bSMatthew G Knepley 
164037d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
164137d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
164237d0c07bSMatthew G Knepley       }
164337d0c07bSMatthew G Knepley     }
164437d0c07bSMatthew G Knepley     if (fields) {
1645785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
164637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
16473a544194SStefano Zampini         PetscInt bs, in[2], out[2];
16483a544194SStefano Zampini 
164982f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
16503a544194SStefano Zampini         in[0] = -fieldNc[f];
16513a544194SStefano Zampini         in[1] = fieldNc[f];
16523a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
16533a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
16543a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
165537d0c07bSMatthew G Knepley       }
165637d0c07bSMatthew G Knepley     }
16573a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
16588865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
16598865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
166069ca1f37SDmitry Karpeev   }
16614d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
16624d343eeaSMatthew G Knepley }
16634d343eeaSMatthew G Knepley 
166416621825SDmitry Karpeev 
166516621825SDmitry Karpeev /*@C
166616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
166716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
166816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1669e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1670e7c4fc90SDmitry Karpeev 
1671e7c4fc90SDmitry Karpeev   Not collective
1672e7c4fc90SDmitry Karpeev 
1673e7c4fc90SDmitry Karpeev   Input Parameter:
1674e7c4fc90SDmitry Karpeev . dm - the DM object
1675e7c4fc90SDmitry Karpeev 
1676e7c4fc90SDmitry Karpeev   Output Parameters:
16770298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16780298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16790298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16800298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1681e7c4fc90SDmitry Karpeev 
1682e7c4fc90SDmitry Karpeev   Level: intermediate
1683e7c4fc90SDmitry Karpeev 
1684e7c4fc90SDmitry Karpeev   Notes:
1685e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1686e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1687e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1688e7c4fc90SDmitry Karpeev 
1689e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1690e7c4fc90SDmitry Karpeev @*/
169116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1692e7c4fc90SDmitry Karpeev {
1693e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1694e7c4fc90SDmitry Karpeev 
1695e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1696e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16978865f1eaSKarl Rupp   if (len) {
1698534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
16998865f1eaSKarl Rupp     *len = 0;
17008865f1eaSKarl Rupp   }
17018865f1eaSKarl Rupp   if (namelist) {
17028865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
17038865f1eaSKarl Rupp     *namelist = 0;
17048865f1eaSKarl Rupp   }
17058865f1eaSKarl Rupp   if (islist) {
17068865f1eaSKarl Rupp     PetscValidPointer(islist,4);
17078865f1eaSKarl Rupp     *islist = 0;
17088865f1eaSKarl Rupp   }
17098865f1eaSKarl Rupp   if (dmlist) {
17108865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
17118865f1eaSKarl Rupp     *dmlist = 0;
17128865f1eaSKarl Rupp   }
1713f3f0edfdSDmitry Karpeev   /*
1714f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1715f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1716f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1717f3f0edfdSDmitry Karpeev    */
1718ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
171916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1720435a35e8SMatthew G Knepley     PetscSection section;
1721435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1722435a35e8SMatthew G Knepley 
172392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1724435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1725435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1726f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
172703dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
172803dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
172903dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1730435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1731435a35e8SMatthew G Knepley         const char *fieldName;
1732435a35e8SMatthew G Knepley 
173303dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
173403dc3394SMatthew G. Knepley         if (namelist) {
1735435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1736435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1737435a35e8SMatthew G Knepley         }
173803dc3394SMatthew G. Knepley       }
1739435a35e8SMatthew G Knepley     } else {
174069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1741e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
17420298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1743e7c4fc90SDmitry Karpeev     }
17448865f1eaSKarl Rupp   } else {
174516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
174616621825SDmitry Karpeev   }
174716621825SDmitry Karpeev   PetscFunctionReturn(0);
174816621825SDmitry Karpeev }
174916621825SDmitry Karpeev 
1750564cec59SMatthew G. Knepley /*@
1751435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1752435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1753435a35e8SMatthew G Knepley 
1754435a35e8SMatthew G Knepley   Not collective
1755435a35e8SMatthew G Knepley 
1756435a35e8SMatthew G Knepley   Input Parameters:
17572adcc780SMatthew G. Knepley + dm        - The DM object
17582adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
17592adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1760435a35e8SMatthew G Knepley 
1761435a35e8SMatthew G Knepley   Output Parameters:
17622adcc780SMatthew G. Knepley + is - The global indices for the subproblem
17632adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1764435a35e8SMatthew G Knepley 
17655d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17665d3b26e6SMatthew G. Knepley 
1767435a35e8SMatthew G Knepley   Level: intermediate
1768435a35e8SMatthew G Knepley 
17695d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1770435a35e8SMatthew G Knepley @*/
177137bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1772435a35e8SMatthew G Knepley {
1773435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1774435a35e8SMatthew G Knepley 
1775435a35e8SMatthew G Knepley   PetscFunctionBegin;
1776435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1777435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17788865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17798865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1780b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1781435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1782435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1783435a35e8SMatthew G Knepley }
1784435a35e8SMatthew G Knepley 
17852adcc780SMatthew G. Knepley /*@C
17862adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17872adcc780SMatthew G. Knepley 
17882adcc780SMatthew G. Knepley   Not collective
17892adcc780SMatthew G. Knepley 
17902adcc780SMatthew G. Knepley   Input Parameter:
17912adcc780SMatthew G. Knepley + dms - The DM objects
17922adcc780SMatthew G. Knepley - len - The number of DMs
17932adcc780SMatthew G. Knepley 
17942adcc780SMatthew G. Knepley   Output Parameters:
1795a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17962adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17972adcc780SMatthew G. Knepley 
17985d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17995d3b26e6SMatthew G. Knepley 
18002adcc780SMatthew G. Knepley   Level: intermediate
18012adcc780SMatthew G. Knepley 
18025d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
18032adcc780SMatthew G. Knepley @*/
18042adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
18052adcc780SMatthew G. Knepley {
18062adcc780SMatthew G. Knepley   PetscInt       i;
18072adcc780SMatthew G. Knepley   PetscErrorCode ierr;
18082adcc780SMatthew G. Knepley 
18092adcc780SMatthew G. Knepley   PetscFunctionBegin;
18102adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
18112adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
18122adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1813a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
18142adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
18152adcc780SMatthew G. Knepley   if (len) {
1816b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1817b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1818b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
18192adcc780SMatthew G. Knepley   }
18202adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
18212adcc780SMatthew G. Knepley }
18222adcc780SMatthew G. Knepley 
182316621825SDmitry Karpeev 
182416621825SDmitry Karpeev /*@C
18258d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
18268d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
18278d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
18288d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
18298d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
183016621825SDmitry Karpeev 
183116621825SDmitry Karpeev   Not collective
183216621825SDmitry Karpeev 
183316621825SDmitry Karpeev   Input Parameter:
183416621825SDmitry Karpeev . dm - the DM object
183516621825SDmitry Karpeev 
183616621825SDmitry Karpeev   Output Parameters:
18370298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
18380298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
18390298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
18400298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
18410298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
184216621825SDmitry Karpeev 
184316621825SDmitry Karpeev   Level: intermediate
184416621825SDmitry Karpeev 
184516621825SDmitry Karpeev   Notes:
184616621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
184716621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
184816621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
184916621825SDmitry Karpeev 
18508d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
185116621825SDmitry Karpeev @*/
18528d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
185316621825SDmitry Karpeev {
185416621825SDmitry Karpeev   PetscErrorCode      ierr;
1855be081cd6SPeter Brune   DMSubDomainHookLink link;
1856be081cd6SPeter Brune   PetscInt            i,l;
185716621825SDmitry Karpeev 
185816621825SDmitry Karpeev   PetscFunctionBegin;
185916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
186014a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
18610298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
18620298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
18630298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
18640298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1865f3f0edfdSDmitry Karpeev   /*
1866f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1867f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1868f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1869f3f0edfdSDmitry Karpeev    */
1870ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
187116621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1872be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
187314a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1874f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1875be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1876be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1877be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1878be081cd6SPeter Brune         }
1879648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1880e7c4fc90SDmitry Karpeev       }
188114a18fd3SPeter Brune     }
188214a18fd3SPeter Brune     if (len) *len = l;
188314a18fd3SPeter Brune   }
1884e30e807fSPeter Brune   PetscFunctionReturn(0);
1885e30e807fSPeter Brune }
1886e30e807fSPeter Brune 
1887e30e807fSPeter Brune 
1888e30e807fSPeter Brune /*@C
1889e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1890e30e807fSPeter Brune 
1891e30e807fSPeter Brune   Not collective
1892e30e807fSPeter Brune 
1893e30e807fSPeter Brune   Input Parameters:
1894e30e807fSPeter Brune + dm - the DM object
1895e30e807fSPeter Brune . n  - the number of subdomain scatters
1896e30e807fSPeter Brune - subdms - the local subdomains
1897e30e807fSPeter Brune 
1898e30e807fSPeter Brune   Output Parameters:
1899e30e807fSPeter Brune + n     - the number of scatters returned
1900e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1901e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1902e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1903e30e807fSPeter Brune 
190495452b02SPatrick Sanan   Notes:
190595452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1906e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1907e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1908e30e807fSPeter Brune   solution and residual data.
1909e30e807fSPeter Brune 
1910e30e807fSPeter Brune   Level: developer
1911e30e807fSPeter Brune 
1912e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1913e30e807fSPeter Brune @*/
1914e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1915e30e807fSPeter Brune {
1916e30e807fSPeter Brune   PetscErrorCode ierr;
1917e30e807fSPeter Brune 
1918e30e807fSPeter Brune   PetscFunctionBegin;
1919e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1920e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1921b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1922e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1923e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1924e7c4fc90SDmitry Karpeev }
1925e7c4fc90SDmitry Karpeev 
192647c6ae99SBarry Smith /*@
192747c6ae99SBarry Smith   DMRefine - Refines a DM object
192847c6ae99SBarry Smith 
1929d083f849SBarry Smith   Collective on dm
193047c6ae99SBarry Smith 
193147c6ae99SBarry Smith   Input Parameter:
193247c6ae99SBarry Smith + dm   - the DM object
193391d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
193447c6ae99SBarry Smith 
193547c6ae99SBarry Smith   Output Parameter:
19360298fd71SBarry Smith . dmf - the refined DM, or NULL
1937ae0a1c52SMatthew G Knepley 
19380298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
193947c6ae99SBarry Smith 
194047c6ae99SBarry Smith   Level: developer
194147c6ae99SBarry Smith 
1942e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
194347c6ae99SBarry Smith @*/
19447087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
194547c6ae99SBarry Smith {
194647c6ae99SBarry Smith   PetscErrorCode   ierr;
1947c833c3b5SJed Brown   DMRefineHookLink link;
194847c6ae99SBarry Smith 
194947c6ae99SBarry Smith   PetscFunctionBegin;
1950732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1951b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
19521ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
195347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
19544057135bSMatthew G Knepley   if (*dmf) {
195543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
19568865f1eaSKarl Rupp 
19578cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
19588865f1eaSKarl Rupp 
1959644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
19600598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1961656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
19628865f1eaSKarl Rupp 
1963e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1964c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
19658865f1eaSKarl Rupp       if (link->refinehook) {
19668865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19678865f1eaSKarl Rupp       }
1968c833c3b5SJed Brown     }
1969c833c3b5SJed Brown   }
19701ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1971c833c3b5SJed Brown   PetscFunctionReturn(0);
1972c833c3b5SJed Brown }
1973c833c3b5SJed Brown 
1974bb9467b5SJed Brown /*@C
1975c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1976c833c3b5SJed Brown 
1977c833c3b5SJed Brown    Logically Collective
1978c833c3b5SJed Brown 
1979c833c3b5SJed Brown    Input Arguments:
1980c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1981c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1982c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19830298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1984c833c3b5SJed Brown 
1985c833c3b5SJed Brown    Calling sequence of refinehook:
1986c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1987c833c3b5SJed Brown 
1988c833c3b5SJed Brown +  coarse - coarse level DM
1989c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1990c833c3b5SJed Brown -  ctx - optional user-defined function context
1991c833c3b5SJed Brown 
1992c833c3b5SJed Brown    Calling sequence for interphook:
1993c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1994c833c3b5SJed Brown 
1995c833c3b5SJed Brown +  coarse - coarse level DM
1996c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1997c833c3b5SJed Brown .  fine - fine level DM to update
1998c833c3b5SJed Brown -  ctx - optional user-defined function context
1999c833c3b5SJed Brown 
2000c833c3b5SJed Brown    Level: advanced
2001c833c3b5SJed Brown 
2002c833c3b5SJed Brown    Notes:
2003c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2004c833c3b5SJed Brown 
2005c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2006c833c3b5SJed Brown 
2007bb9467b5SJed Brown    This function is currently not available from Fortran.
2008bb9467b5SJed Brown 
2009c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2010c833c3b5SJed Brown @*/
2011c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2012c833c3b5SJed Brown {
2013c833c3b5SJed Brown   PetscErrorCode   ierr;
2014c833c3b5SJed Brown   DMRefineHookLink link,*p;
2015c833c3b5SJed Brown 
2016c833c3b5SJed Brown   PetscFunctionBegin;
2017c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20183d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
20193d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
20203d8e3701SJed Brown   }
202195dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2022c833c3b5SJed Brown   link->refinehook = refinehook;
2023c833c3b5SJed Brown   link->interphook = interphook;
2024c833c3b5SJed Brown   link->ctx        = ctx;
20250298fd71SBarry Smith   link->next       = NULL;
2026c833c3b5SJed Brown   *p               = link;
2027c833c3b5SJed Brown   PetscFunctionReturn(0);
2028c833c3b5SJed Brown }
2029c833c3b5SJed Brown 
20303d8e3701SJed Brown /*@C
20313d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
20323d8e3701SJed Brown 
20333d8e3701SJed Brown    Logically Collective
20343d8e3701SJed Brown 
20353d8e3701SJed Brown    Input Arguments:
20363d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
20373d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
20383d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20393d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20403d8e3701SJed Brown 
20413d8e3701SJed Brown    Level: advanced
20423d8e3701SJed Brown 
20433d8e3701SJed Brown    Notes:
20443d8e3701SJed Brown    This function does nothing if the hook is not in the list.
20453d8e3701SJed Brown 
20463d8e3701SJed Brown    This function is currently not available from Fortran.
20473d8e3701SJed Brown 
20483d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20493d8e3701SJed Brown @*/
20503d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
20513d8e3701SJed Brown {
20523d8e3701SJed Brown   PetscErrorCode   ierr;
20533d8e3701SJed Brown   DMRefineHookLink link,*p;
20543d8e3701SJed Brown 
20553d8e3701SJed Brown   PetscFunctionBegin;
20563d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20573d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
20583d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
20593d8e3701SJed Brown       link = *p;
20603d8e3701SJed Brown       *p = link->next;
20613d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
20623d8e3701SJed Brown       break;
20633d8e3701SJed Brown     }
20643d8e3701SJed Brown   }
20653d8e3701SJed Brown   PetscFunctionReturn(0);
20663d8e3701SJed Brown }
20673d8e3701SJed Brown 
2068c833c3b5SJed Brown /*@
2069c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2070c833c3b5SJed Brown 
2071c833c3b5SJed Brown    Collective if any hooks are
2072c833c3b5SJed Brown 
2073c833c3b5SJed Brown    Input Arguments:
2074c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2075e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2076c833c3b5SJed Brown -  fine - finer DM to update
2077c833c3b5SJed Brown 
2078c833c3b5SJed Brown    Level: developer
2079c833c3b5SJed Brown 
2080c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2081c833c3b5SJed Brown @*/
2082c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2083c833c3b5SJed Brown {
2084c833c3b5SJed Brown   PetscErrorCode   ierr;
2085c833c3b5SJed Brown   DMRefineHookLink link;
2086c833c3b5SJed Brown 
2087c833c3b5SJed Brown   PetscFunctionBegin;
2088c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20898865f1eaSKarl Rupp     if (link->interphook) {
20908865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20918865f1eaSKarl Rupp     }
20924057135bSMatthew G Knepley   }
209347c6ae99SBarry Smith   PetscFunctionReturn(0);
209447c6ae99SBarry Smith }
209547c6ae99SBarry Smith 
2096eb3f98d2SBarry Smith /*@
2097eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2098eb3f98d2SBarry Smith 
2099eb3f98d2SBarry Smith     Not Collective
2100eb3f98d2SBarry Smith 
2101eb3f98d2SBarry Smith     Input Parameter:
2102eb3f98d2SBarry Smith .   dm - the DM object
2103eb3f98d2SBarry Smith 
2104eb3f98d2SBarry Smith     Output Parameter:
2105eb3f98d2SBarry Smith .   level - number of refinements
2106eb3f98d2SBarry Smith 
2107eb3f98d2SBarry Smith     Level: developer
2108eb3f98d2SBarry Smith 
21096a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2110eb3f98d2SBarry Smith 
2111eb3f98d2SBarry Smith @*/
2112eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2113eb3f98d2SBarry Smith {
2114eb3f98d2SBarry Smith   PetscFunctionBegin;
2115eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2116eb3f98d2SBarry Smith   *level = dm->levelup;
2117eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2118eb3f98d2SBarry Smith }
2119eb3f98d2SBarry Smith 
2120fef3a512SBarry Smith /*@
2121fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2122fef3a512SBarry Smith 
2123fef3a512SBarry Smith     Not Collective
2124fef3a512SBarry Smith 
2125fef3a512SBarry Smith     Input Parameter:
2126fef3a512SBarry Smith +   dm - the DM object
2127fef3a512SBarry Smith -   level - number of refinements
2128fef3a512SBarry Smith 
2129fef3a512SBarry Smith     Level: advanced
2130fef3a512SBarry Smith 
213195452b02SPatrick Sanan     Notes:
213295452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2133fef3a512SBarry Smith 
2134fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2135fef3a512SBarry Smith 
2136fef3a512SBarry Smith @*/
2137fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2138fef3a512SBarry Smith {
2139fef3a512SBarry Smith   PetscFunctionBegin;
2140fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2141fef3a512SBarry Smith   dm->levelup = level;
2142fef3a512SBarry Smith   PetscFunctionReturn(0);
2143fef3a512SBarry Smith }
2144fef3a512SBarry Smith 
2145ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2146ca3d3a14SMatthew G. Knepley {
2147ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2148ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2149ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2150ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2151ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2152ca3d3a14SMatthew G. Knepley }
2153ca3d3a14SMatthew G. Knepley 
2154ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2155ca3d3a14SMatthew G. Knepley {
2156ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2157ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2158ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2159ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2160ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2161ca3d3a14SMatthew G. Knepley }
2162ca3d3a14SMatthew G. Knepley 
2163ca3d3a14SMatthew G. Knepley /*@
2164c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2165ca3d3a14SMatthew G. Knepley 
2166ca3d3a14SMatthew G. Knepley   Input Parameter:
2167ca3d3a14SMatthew G. Knepley . dm - The DM
2168ca3d3a14SMatthew G. Knepley 
2169ca3d3a14SMatthew G. Knepley   Output Parameter:
2170ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2171ca3d3a14SMatthew G. Knepley 
2172ca3d3a14SMatthew G. Knepley   Level: developer
2173ca3d3a14SMatthew G. Knepley 
2174ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2175ca3d3a14SMatthew G. Knepley @*/
2176ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2177ca3d3a14SMatthew G. Knepley {
2178ca3d3a14SMatthew G. Knepley   Vec            tv;
2179ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2180ca3d3a14SMatthew G. Knepley 
2181ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2182ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2183534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2184ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2185ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2186ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2187ca3d3a14SMatthew G. Knepley }
2188ca3d3a14SMatthew G. Knepley 
2189ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2190ca3d3a14SMatthew G. Knepley {
2191ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2192ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2193ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2194ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2195ca3d3a14SMatthew G. Knepley 
2196ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2197ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
219892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2199ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2200ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2201ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
220292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2203ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2204ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2205ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2206ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2207ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2208ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2209ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2210ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2211ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2212ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2213ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2214ca3d3a14SMatthew G. Knepley     }
2215ca3d3a14SMatthew G. Knepley   }
2216ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2217ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2218ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2219ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2220ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2221ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2222ca3d3a14SMatthew G. Knepley       if (dof) {
2223ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2224ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2225ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2226ca3d3a14SMatthew G. Knepley 
2227ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2228ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2229ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2230580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2231ca3d3a14SMatthew G. Knepley       }
2232ca3d3a14SMatthew G. Knepley     }
2233ca3d3a14SMatthew G. Knepley   }
2234ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2235ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2236ca3d3a14SMatthew G. Knepley }
2237ca3d3a14SMatthew G. Knepley 
2238ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2239ca3d3a14SMatthew G. Knepley {
2240ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2241ca3d3a14SMatthew G. Knepley 
2242ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2243ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2244ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2245ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2246ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2247ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2248ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2249ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2250ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2251ca3d3a14SMatthew G. Knepley }
2252ca3d3a14SMatthew G. Knepley 
2253bb9467b5SJed Brown /*@C
2254baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2255baf369e7SPeter Brune 
2256baf369e7SPeter Brune    Logically Collective
2257baf369e7SPeter Brune 
2258baf369e7SPeter Brune    Input Arguments:
2259baf369e7SPeter Brune +  dm - the DM
2260baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2261baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
22620298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2263baf369e7SPeter Brune 
2264baf369e7SPeter Brune    Calling sequence for beginhook:
2265baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2266baf369e7SPeter Brune 
2267baf369e7SPeter Brune +  dm - global DM
2268baf369e7SPeter Brune .  g - global vector
2269baf369e7SPeter Brune .  mode - mode
2270baf369e7SPeter Brune .  l - local vector
2271baf369e7SPeter Brune -  ctx - optional user-defined function context
2272baf369e7SPeter Brune 
2273baf369e7SPeter Brune 
2274baf369e7SPeter Brune    Calling sequence for endhook:
2275ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2276baf369e7SPeter Brune 
2277baf369e7SPeter Brune +  global - global DM
2278baf369e7SPeter Brune -  ctx - optional user-defined function context
2279baf369e7SPeter Brune 
2280baf369e7SPeter Brune    Level: advanced
2281baf369e7SPeter Brune 
2282baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2283baf369e7SPeter Brune @*/
2284baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2285baf369e7SPeter Brune {
2286baf369e7SPeter Brune   PetscErrorCode          ierr;
2287baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2288baf369e7SPeter Brune 
2289baf369e7SPeter Brune   PetscFunctionBegin;
2290baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2291baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
229295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2293baf369e7SPeter Brune   link->beginhook = beginhook;
2294baf369e7SPeter Brune   link->endhook   = endhook;
2295baf369e7SPeter Brune   link->ctx       = ctx;
22960298fd71SBarry Smith   link->next      = NULL;
2297baf369e7SPeter Brune   *p              = link;
2298baf369e7SPeter Brune   PetscFunctionReturn(0);
2299baf369e7SPeter Brune }
2300baf369e7SPeter Brune 
23014c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
23024c274da1SToby Isaac {
23034c274da1SToby Isaac   Mat cMat;
23044c274da1SToby Isaac   Vec cVec;
23054c274da1SToby Isaac   PetscSection section, cSec;
23064c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
23074c274da1SToby Isaac   PetscErrorCode ierr;
23084c274da1SToby Isaac 
23094c274da1SToby Isaac   PetscFunctionBegin;
23104c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23114c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23124c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
23135db9a05bSToby Isaac     PetscInt nRows;
23145db9a05bSToby Isaac 
23155db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23165db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
231792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
23187711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23194c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
23204c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23214c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23224c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23234c274da1SToby Isaac       if (dof) {
23244c274da1SToby Isaac         PetscScalar *vals;
23254c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
23264c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
23274c274da1SToby Isaac       }
23284c274da1SToby Isaac     }
23294c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23304c274da1SToby Isaac   }
23314c274da1SToby Isaac   PetscFunctionReturn(0);
23324c274da1SToby Isaac }
23334c274da1SToby Isaac 
233447c6ae99SBarry Smith /*@
233501729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
233601729b5cSPatrick Sanan 
2337d083f849SBarry Smith     Neighbor-wise Collective on dm
233801729b5cSPatrick Sanan 
233901729b5cSPatrick Sanan     Input Parameters:
234001729b5cSPatrick Sanan +   dm - the DM object
234101729b5cSPatrick Sanan .   g - the global vector
234201729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
234301729b5cSPatrick Sanan -   l - the local vector
234401729b5cSPatrick Sanan 
234501729b5cSPatrick Sanan     Notes:
234601729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
234701729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
234801729b5cSPatrick Sanan 
234901729b5cSPatrick Sanan     Level: beginner
235001729b5cSPatrick Sanan 
235101729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
235201729b5cSPatrick Sanan 
235301729b5cSPatrick Sanan @*/
235401729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
235501729b5cSPatrick Sanan {
235601729b5cSPatrick Sanan   PetscErrorCode ierr;
235701729b5cSPatrick Sanan 
235801729b5cSPatrick Sanan   PetscFunctionBegin;
235901729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
236001729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
236101729b5cSPatrick Sanan   PetscFunctionReturn(0);
236201729b5cSPatrick Sanan }
236301729b5cSPatrick Sanan 
236401729b5cSPatrick Sanan /*@
236547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
236647c6ae99SBarry Smith 
2367d083f849SBarry Smith     Neighbor-wise Collective on dm
236847c6ae99SBarry Smith 
236947c6ae99SBarry Smith     Input Parameters:
237047c6ae99SBarry Smith +   dm - the DM object
237147c6ae99SBarry Smith .   g - the global vector
237247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
237347c6ae99SBarry Smith -   l - the local vector
237447c6ae99SBarry Smith 
237501729b5cSPatrick Sanan     Level: intermediate
237647c6ae99SBarry Smith 
237701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
237847c6ae99SBarry Smith 
237947c6ae99SBarry Smith @*/
23807087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
238147c6ae99SBarry Smith {
23827128ae9fSMatthew G Knepley   PetscSF                 sf;
238347c6ae99SBarry Smith   PetscErrorCode          ierr;
2384baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
238547c6ae99SBarry Smith 
238647c6ae99SBarry Smith   PetscFunctionBegin;
2387171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2388baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23898865f1eaSKarl Rupp     if (link->beginhook) {
23908865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23918865f1eaSKarl Rupp     }
2392baf369e7SPeter Brune   }
23931bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
23947128ae9fSMatthew G Knepley   if (sf) {
2395ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2396ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
23977128ae9fSMatthew G Knepley 
239882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23997128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2400ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
24017128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
24027128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2403ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
24047128ae9fSMatthew G Knepley   } else {
240533907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2406843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24077128ae9fSMatthew G Knepley   }
240847c6ae99SBarry Smith   PetscFunctionReturn(0);
240947c6ae99SBarry Smith }
241047c6ae99SBarry Smith 
241147c6ae99SBarry Smith /*@
241247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
241347c6ae99SBarry Smith 
2414d083f849SBarry Smith     Neighbor-wise Collective on dm
241547c6ae99SBarry Smith 
241647c6ae99SBarry Smith     Input Parameters:
241747c6ae99SBarry Smith +   dm - the DM object
241847c6ae99SBarry Smith .   g - the global vector
241947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
242047c6ae99SBarry Smith -   l - the local vector
242147c6ae99SBarry Smith 
242201729b5cSPatrick Sanan     Level: intermediate
242347c6ae99SBarry Smith 
242401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
242547c6ae99SBarry Smith 
242647c6ae99SBarry Smith @*/
24277087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
242847c6ae99SBarry Smith {
24297128ae9fSMatthew G Knepley   PetscSF                 sf;
243047c6ae99SBarry Smith   PetscErrorCode          ierr;
2431ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2432ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2433ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2434baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
243547c6ae99SBarry Smith 
243647c6ae99SBarry Smith   PetscFunctionBegin;
2437171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24381bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2439ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
24407128ae9fSMatthew G Knepley   if (sf) {
244182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24427128ae9fSMatthew G Knepley 
24437128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2444ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
24457128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
24467128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2447ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2448ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
24497128ae9fSMatthew G Knepley   } else {
245033907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2451843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24527128ae9fSMatthew G Knepley   }
24534c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2454baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2455baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2456baf369e7SPeter Brune   }
245747c6ae99SBarry Smith   PetscFunctionReturn(0);
245847c6ae99SBarry Smith }
245947c6ae99SBarry Smith 
2460d4d07f1eSToby Isaac /*@C
2461d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2462d4d07f1eSToby Isaac 
2463d4d07f1eSToby Isaac    Logically Collective
2464d4d07f1eSToby Isaac 
2465d4d07f1eSToby Isaac    Input Arguments:
2466d4d07f1eSToby Isaac +  dm - the DM
2467d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2468d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2469d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2470d4d07f1eSToby Isaac 
2471d4d07f1eSToby Isaac    Calling sequence for beginhook:
2472d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2473d4d07f1eSToby Isaac 
2474d4d07f1eSToby Isaac +  dm - global DM
2475d4d07f1eSToby Isaac .  l - local vector
2476d4d07f1eSToby Isaac .  mode - mode
2477d4d07f1eSToby Isaac .  g - global vector
2478d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2479d4d07f1eSToby Isaac 
2480d4d07f1eSToby Isaac 
2481d4d07f1eSToby Isaac    Calling sequence for endhook:
2482d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2483d4d07f1eSToby Isaac 
2484d4d07f1eSToby Isaac +  global - global DM
2485d4d07f1eSToby Isaac .  l - local vector
2486d4d07f1eSToby Isaac .  mode - mode
2487d4d07f1eSToby Isaac .  g - global vector
2488d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2489d4d07f1eSToby Isaac 
2490d4d07f1eSToby Isaac    Level: advanced
2491d4d07f1eSToby Isaac 
2492d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2493d4d07f1eSToby Isaac @*/
2494d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2495d4d07f1eSToby Isaac {
2496d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2497d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2498d4d07f1eSToby Isaac 
2499d4d07f1eSToby Isaac   PetscFunctionBegin;
2500d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2501d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
250295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2503d4d07f1eSToby Isaac   link->beginhook = beginhook;
2504d4d07f1eSToby Isaac   link->endhook   = endhook;
2505d4d07f1eSToby Isaac   link->ctx       = ctx;
2506d4d07f1eSToby Isaac   link->next      = NULL;
2507d4d07f1eSToby Isaac   *p              = link;
2508d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2509d4d07f1eSToby Isaac }
2510d4d07f1eSToby Isaac 
25114c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
25124c274da1SToby Isaac {
25134c274da1SToby Isaac   Mat cMat;
25144c274da1SToby Isaac   Vec cVec;
25154c274da1SToby Isaac   PetscSection section, cSec;
25164c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25174c274da1SToby Isaac   PetscErrorCode ierr;
25184c274da1SToby Isaac 
25194c274da1SToby Isaac   PetscFunctionBegin;
25204c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25214c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25224c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
25235db9a05bSToby Isaac     PetscInt nRows;
25245db9a05bSToby Isaac 
25255db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25265db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
252792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25287711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25294c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25304c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25314c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25324c274da1SToby Isaac       if (dof) {
25334c274da1SToby Isaac         PetscInt d;
25344c274da1SToby Isaac         PetscScalar *vals;
25354c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
25364c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
25374c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
25384c274da1SToby Isaac          * we just extracted */
25394c274da1SToby Isaac         for (d = 0; d < dof; d++) {
25404c274da1SToby Isaac           vals[d] = 0.;
25414c274da1SToby Isaac         }
25424c274da1SToby Isaac       }
25434c274da1SToby Isaac     }
25444c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
25454c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25464c274da1SToby Isaac   }
25474c274da1SToby Isaac   PetscFunctionReturn(0);
25484c274da1SToby Isaac }
254901729b5cSPatrick Sanan /*@
255001729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
255101729b5cSPatrick Sanan 
2552d083f849SBarry Smith     Neighbor-wise Collective on dm
255301729b5cSPatrick Sanan 
255401729b5cSPatrick Sanan     Input Parameters:
255501729b5cSPatrick Sanan +   dm - the DM object
255601729b5cSPatrick Sanan .   l - the local vector
255701729b5cSPatrick 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.
255801729b5cSPatrick Sanan -   g - the global vector
255901729b5cSPatrick Sanan 
256001729b5cSPatrick Sanan     Notes:
256101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
256201729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
256301729b5cSPatrick Sanan 
256401729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
256501729b5cSPatrick 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.
256601729b5cSPatrick Sanan 
256701729b5cSPatrick Sanan     Level: beginner
256801729b5cSPatrick Sanan 
256901729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
257001729b5cSPatrick Sanan 
257101729b5cSPatrick Sanan @*/
257201729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
257301729b5cSPatrick Sanan {
257401729b5cSPatrick Sanan   PetscErrorCode ierr;
257501729b5cSPatrick Sanan 
257601729b5cSPatrick Sanan   PetscFunctionBegin;
257701729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
257801729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
257901729b5cSPatrick Sanan   PetscFunctionReturn(0);
258001729b5cSPatrick Sanan }
25814c274da1SToby Isaac 
258247c6ae99SBarry Smith /*@
258301729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25849a42bb27SBarry Smith 
2585d083f849SBarry Smith     Neighbor-wise Collective on dm
25869a42bb27SBarry Smith 
25879a42bb27SBarry Smith     Input Parameters:
25889a42bb27SBarry Smith +   dm - the DM object
2589f6813fd5SJed Brown .   l - the local vector
25901eb28f2eSBarry 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.
25911eb28f2eSBarry Smith -   g - the global vector
25929a42bb27SBarry Smith 
259395452b02SPatrick Sanan     Notes:
259495452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
259584330215SMatthew 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.
25969a42bb27SBarry Smith 
259701729b5cSPatrick Sanan     Level: intermediate
25989a42bb27SBarry Smith 
259901729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
26009a42bb27SBarry Smith 
26019a42bb27SBarry Smith @*/
26027087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
26039a42bb27SBarry Smith {
26047128ae9fSMatthew G Knepley   PetscSF                 sf;
260584330215SMatthew G. Knepley   PetscSection            s, gs;
2606d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2607ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2608ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2609ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2610ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
261184330215SMatthew G. Knepley   PetscErrorCode          ierr;
26129a42bb27SBarry Smith 
26139a42bb27SBarry Smith   PetscFunctionBegin;
2614171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2615d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2616d4d07f1eSToby Isaac     if (link->beginhook) {
2617d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2618d4d07f1eSToby Isaac     }
2619d4d07f1eSToby Isaac   }
26204c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
26211bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
262292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26237128ae9fSMatthew G Knepley   switch (mode) {
26247128ae9fSMatthew G Knepley   case INSERT_VALUES:
26257128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2626304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
262784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26287128ae9fSMatthew G Knepley   case ADD_VALUES:
26297128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2630304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
263184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26327128ae9fSMatthew G Knepley   default:
263382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26347128ae9fSMatthew G Knepley   }
2635ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2636ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2637ca3d3a14SMatthew G. Knepley     if (transform) {
2638ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2639ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2640ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2641ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2642ca3d3a14SMatthew G. Knepley     } else {
2643ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2644ca3d3a14SMatthew G. Knepley     }
26457128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2646ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2647a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
264884330215SMatthew G. Knepley     } else if (s && isInsert) {
264984330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
265084330215SMatthew G. Knepley 
2651e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
265284330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
265384330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
265484330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2655b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
265684330215SMatthew G. Knepley 
265784330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
265803442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
265984330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2660b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
266184330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
266284330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2663b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
266403442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2665b3b16f48SMatthew 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);
2666b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2667b3b16f48SMatthew G. Knepley         if (!gcdof) {
266884330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2669b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2670b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
267184330215SMatthew G. Knepley           const PetscInt *cdofs;
267284330215SMatthew G. Knepley           PetscInt        cind = 0;
267384330215SMatthew G. Knepley 
267484330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2675b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
267684330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2677b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
267884330215SMatthew G. Knepley           }
2679b3b16f48SMatthew 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);
268084330215SMatthew G. Knepley       }
2681ca3d3a14SMatthew G. Knepley     }
26827128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2683ca3d3a14SMatthew G. Knepley     if (transform) {
2684ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2685ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2686ca3d3a14SMatthew G. Knepley     } else {
2687ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2688ca3d3a14SMatthew G. Knepley     }
26897128ae9fSMatthew G Knepley   } else {
2690b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2691843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26927128ae9fSMatthew G Knepley   }
26939a42bb27SBarry Smith   PetscFunctionReturn(0);
26949a42bb27SBarry Smith }
26959a42bb27SBarry Smith 
26969a42bb27SBarry Smith /*@
26979a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
269847c6ae99SBarry Smith 
2699d083f849SBarry Smith     Neighbor-wise Collective on dm
270047c6ae99SBarry Smith 
270147c6ae99SBarry Smith     Input Parameters:
270247c6ae99SBarry Smith +   dm - the DM object
2703f6813fd5SJed Brown .   l - the local vector
270447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2705f6813fd5SJed Brown -   g - the global vector
270647c6ae99SBarry Smith 
270701729b5cSPatrick Sanan     Level: intermediate
270847c6ae99SBarry Smith 
2709e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
271047c6ae99SBarry Smith 
271147c6ae99SBarry Smith @*/
27127087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
271347c6ae99SBarry Smith {
27147128ae9fSMatthew G Knepley   PetscSF                 sf;
271584330215SMatthew G. Knepley   PetscSection            s;
2716d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2717ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
271884330215SMatthew G. Knepley   PetscErrorCode          ierr;
271947c6ae99SBarry Smith 
272047c6ae99SBarry Smith   PetscFunctionBegin;
2721171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27221bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
272392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27247128ae9fSMatthew G Knepley   switch (mode) {
27257128ae9fSMatthew G Knepley   case INSERT_VALUES:
27267128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
272784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27287128ae9fSMatthew G Knepley   case ADD_VALUES:
27297128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
273084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27317128ae9fSMatthew G Knepley   default:
273282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27337128ae9fSMatthew G Knepley   }
273484330215SMatthew G. Knepley   if (sf && !isInsert) {
2735ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2736ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2737ca3d3a14SMatthew G. Knepley     Vec                tmpl;
273884330215SMatthew G. Knepley 
2739ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2740ca3d3a14SMatthew G. Knepley     if (transform) {
2741ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2742ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2743ca3d3a14SMatthew G. Knepley     } else {
2744ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2745ca3d3a14SMatthew G. Knepley     }
27467128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2747a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2748ca3d3a14SMatthew G. Knepley     if (transform) {
2749ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2750ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2751ca3d3a14SMatthew G. Knepley     } else {
2752ae5cfb4aSMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2753ca3d3a14SMatthew G. Knepley     }
27547128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
275584330215SMatthew G. Knepley   } else if (s && isInsert) {
27567128ae9fSMatthew G Knepley   } else {
2757b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2758843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27597128ae9fSMatthew G Knepley   }
2760d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2761d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2762d4d07f1eSToby Isaac   }
276347c6ae99SBarry Smith   PetscFunctionReturn(0);
276447c6ae99SBarry Smith }
276547c6ae99SBarry Smith 
2766f089877aSRichard Tran Mills /*@
2767bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2768bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2769d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2770f089877aSRichard Tran Mills 
2771d083f849SBarry Smith    Neighbor-wise Collective on dm
2772f089877aSRichard Tran Mills 
2773f089877aSRichard Tran Mills    Input Parameters:
2774f089877aSRichard Tran Mills +  dm - the DM object
2775bc0a1609SRichard Tran Mills .  g - the original local vector
2776bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2777f089877aSRichard Tran Mills 
2778bc0a1609SRichard Tran Mills    Output Parameter:
2779bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2780f089877aSRichard Tran Mills 
2781f089877aSRichard Tran Mills    Level: intermediate
2782f089877aSRichard Tran Mills 
2783bc0a1609SRichard Tran Mills    Notes:
2784bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2785bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2786bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2787bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2788bc0a1609SRichard Tran Mills 
2789bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2790f089877aSRichard Tran Mills 
2791f089877aSRichard Tran Mills @*/
2792f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2793f089877aSRichard Tran Mills {
2794f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2795f089877aSRichard Tran Mills 
2796f089877aSRichard Tran Mills   PetscFunctionBegin;
2797f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2798bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2799f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2800f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2801f089877aSRichard Tran Mills }
2802f089877aSRichard Tran Mills 
2803f089877aSRichard Tran Mills /*@
2804bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2805bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2806d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2807f089877aSRichard Tran Mills 
2808d083f849SBarry Smith    Neighbor-wise Collective on dm
2809f089877aSRichard Tran Mills 
2810f089877aSRichard Tran Mills    Input Parameters:
2811bc0a1609SRichard Tran Mills +  da - the DM object
2812bc0a1609SRichard Tran Mills .  g - the original local vector
2813bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2814f089877aSRichard Tran Mills 
2815bc0a1609SRichard Tran Mills    Output Parameter:
2816bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2817f089877aSRichard Tran Mills 
2818f089877aSRichard Tran Mills    Level: intermediate
2819f089877aSRichard Tran Mills 
2820bc0a1609SRichard Tran Mills    Notes:
2821bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2822bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2823bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2824bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2825bc0a1609SRichard Tran Mills 
2826bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2827f089877aSRichard Tran Mills 
2828f089877aSRichard Tran Mills @*/
2829f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2830f089877aSRichard Tran Mills {
2831f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2832f089877aSRichard Tran Mills 
2833f089877aSRichard Tran Mills   PetscFunctionBegin;
2834f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2835bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2836f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2837f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2838f089877aSRichard Tran Mills }
2839f089877aSRichard Tran Mills 
2840f089877aSRichard Tran Mills 
284147c6ae99SBarry Smith /*@
284247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
284347c6ae99SBarry Smith 
2844d083f849SBarry Smith     Collective on dm
284547c6ae99SBarry Smith 
284647c6ae99SBarry Smith     Input Parameter:
284747c6ae99SBarry Smith +   dm - the DM object
284891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
284947c6ae99SBarry Smith 
285047c6ae99SBarry Smith     Output Parameter:
285147c6ae99SBarry Smith .   dmc - the coarsened DM
285247c6ae99SBarry Smith 
285347c6ae99SBarry Smith     Level: developer
285447c6ae99SBarry Smith 
2855e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
285647c6ae99SBarry Smith 
285747c6ae99SBarry Smith @*/
28587087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
285947c6ae99SBarry Smith {
286047c6ae99SBarry Smith   PetscErrorCode    ierr;
2861b17ce1afSJed Brown   DMCoarsenHookLink link;
286247c6ae99SBarry Smith 
286347c6ae99SBarry Smith   PetscFunctionBegin;
2864171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2865b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
286647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
286747c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2868b9d85ea2SLisandro Dalcin   if (*dmc) {
2869a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
287043842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28718cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2872644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
28730598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2874656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2875e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2876b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2877b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2878b17ce1afSJed Brown     }
2879b9d85ea2SLisandro Dalcin   }
288047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2881b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2882b17ce1afSJed Brown   PetscFunctionReturn(0);
2883b17ce1afSJed Brown }
2884b17ce1afSJed Brown 
2885bb9467b5SJed Brown /*@C
2886b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2887b17ce1afSJed Brown 
2888b17ce1afSJed Brown    Logically Collective
2889b17ce1afSJed Brown 
2890b17ce1afSJed Brown    Input Arguments:
2891b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2892b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2893b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
28940298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2895b17ce1afSJed Brown 
2896b17ce1afSJed Brown    Calling sequence of coarsenhook:
2897b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2898b17ce1afSJed Brown 
2899b17ce1afSJed Brown +  fine - fine level DM
2900b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2901b17ce1afSJed Brown -  ctx - optional user-defined function context
2902b17ce1afSJed Brown 
2903b17ce1afSJed Brown    Calling sequence for restricthook:
2904c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2905b17ce1afSJed Brown 
2906b17ce1afSJed Brown +  fine - fine level DM
2907b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2908c833c3b5SJed Brown .  rscale - scaling vector for restriction
2909c833c3b5SJed Brown .  inject - matrix restricting by injection
2910b17ce1afSJed Brown .  coarse - coarse level DM to update
2911b17ce1afSJed Brown -  ctx - optional user-defined function context
2912b17ce1afSJed Brown 
2913b17ce1afSJed Brown    Level: advanced
2914b17ce1afSJed Brown 
2915b17ce1afSJed Brown    Notes:
2916b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2917b17ce1afSJed Brown 
2918b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2919b17ce1afSJed Brown 
2920b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2921b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2922b17ce1afSJed Brown 
2923bb9467b5SJed Brown    This function is currently not available from Fortran.
2924bb9467b5SJed Brown 
2925dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2926b17ce1afSJed Brown @*/
2927b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2928b17ce1afSJed Brown {
2929b17ce1afSJed Brown   PetscErrorCode    ierr;
2930b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2931b17ce1afSJed Brown 
2932b17ce1afSJed Brown   PetscFunctionBegin;
2933b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
29341e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
29351e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
29361e3d8eccSJed Brown   }
293795dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2938b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2939b17ce1afSJed Brown   link->restricthook = restricthook;
2940b17ce1afSJed Brown   link->ctx          = ctx;
29410298fd71SBarry Smith   link->next         = NULL;
2942b17ce1afSJed Brown   *p                 = link;
2943b17ce1afSJed Brown   PetscFunctionReturn(0);
2944b17ce1afSJed Brown }
2945b17ce1afSJed Brown 
2946dc822a44SJed Brown /*@C
2947dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2948dc822a44SJed Brown 
2949dc822a44SJed Brown    Logically Collective
2950dc822a44SJed Brown 
2951dc822a44SJed Brown    Input Arguments:
2952dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2953dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2954dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2955dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2956dc822a44SJed Brown 
2957dc822a44SJed Brown    Level: advanced
2958dc822a44SJed Brown 
2959dc822a44SJed Brown    Notes:
2960dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2961dc822a44SJed Brown 
2962dc822a44SJed Brown    This function is currently not available from Fortran.
2963dc822a44SJed Brown 
2964dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2965dc822a44SJed Brown @*/
2966dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2967dc822a44SJed Brown {
2968dc822a44SJed Brown   PetscErrorCode    ierr;
2969dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2970dc822a44SJed Brown 
2971dc822a44SJed Brown   PetscFunctionBegin;
2972dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2973dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2974dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2975dc822a44SJed Brown       link = *p;
2976dc822a44SJed Brown       *p = link->next;
2977dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2978dc822a44SJed Brown       break;
2979dc822a44SJed Brown     }
2980dc822a44SJed Brown   }
2981dc822a44SJed Brown   PetscFunctionReturn(0);
2982dc822a44SJed Brown }
2983dc822a44SJed Brown 
2984dc822a44SJed Brown 
2985b17ce1afSJed Brown /*@
2986b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2987b17ce1afSJed Brown 
2988b17ce1afSJed Brown    Collective if any hooks are
2989b17ce1afSJed Brown 
2990b17ce1afSJed Brown    Input Arguments:
2991b17ce1afSJed Brown +  fine - finer DM to use as a base
2992b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2993e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2994b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2995e91eccc2SStefano Zampini -  coarse - coarser DM to update
2996b17ce1afSJed Brown 
2997b17ce1afSJed Brown    Level: developer
2998b17ce1afSJed Brown 
2999b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3000b17ce1afSJed Brown @*/
3001b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3002b17ce1afSJed Brown {
3003b17ce1afSJed Brown   PetscErrorCode    ierr;
3004b17ce1afSJed Brown   DMCoarsenHookLink link;
3005b17ce1afSJed Brown 
3006b17ce1afSJed Brown   PetscFunctionBegin;
3007b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
30088865f1eaSKarl Rupp     if (link->restricthook) {
30098865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
30108865f1eaSKarl Rupp     }
3011b17ce1afSJed Brown   }
301247c6ae99SBarry Smith   PetscFunctionReturn(0);
301347c6ae99SBarry Smith }
301447c6ae99SBarry Smith 
3015bb9467b5SJed Brown /*@C
3016be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
30175dbd56e3SPeter Brune 
3018d083f849SBarry Smith    Logically Collective on global
30195dbd56e3SPeter Brune 
30205dbd56e3SPeter Brune    Input Arguments:
30215dbd56e3SPeter Brune +  global - global DM
3022ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
30235dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
30240298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
30255dbd56e3SPeter Brune 
3026ec4806b8SPeter Brune 
3027ec4806b8SPeter Brune    Calling sequence for ddhook:
3028ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3029ec4806b8SPeter Brune 
3030ec4806b8SPeter Brune +  global - global DM
3031ec4806b8SPeter Brune .  block  - block DM
3032ec4806b8SPeter Brune -  ctx - optional user-defined function context
3033ec4806b8SPeter Brune 
30345dbd56e3SPeter Brune    Calling sequence for restricthook:
3035ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
30365dbd56e3SPeter Brune 
30375dbd56e3SPeter Brune +  global - global DM
30385dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
30395dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3040ec4806b8SPeter Brune .  block  - block DM
30415dbd56e3SPeter Brune -  ctx - optional user-defined function context
30425dbd56e3SPeter Brune 
30435dbd56e3SPeter Brune    Level: advanced
30445dbd56e3SPeter Brune 
30455dbd56e3SPeter Brune    Notes:
3046ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
30475dbd56e3SPeter Brune 
30485dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
30495dbd56e3SPeter Brune 
30505dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3051ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
30525dbd56e3SPeter Brune 
3053bb9467b5SJed Brown    This function is currently not available from Fortran.
3054bb9467b5SJed Brown 
30555dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
30565dbd56e3SPeter Brune @*/
3057be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
30585dbd56e3SPeter Brune {
30595dbd56e3SPeter Brune   PetscErrorCode      ierr;
3060be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
30615dbd56e3SPeter Brune 
30625dbd56e3SPeter Brune   PetscFunctionBegin;
30635dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3064b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3065b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3066b3a6b972SJed Brown   }
306795dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30685dbd56e3SPeter Brune   link->restricthook = restricthook;
3069be081cd6SPeter Brune   link->ddhook       = ddhook;
30705dbd56e3SPeter Brune   link->ctx          = ctx;
30710298fd71SBarry Smith   link->next         = NULL;
30725dbd56e3SPeter Brune   *p                 = link;
30735dbd56e3SPeter Brune   PetscFunctionReturn(0);
30745dbd56e3SPeter Brune }
30755dbd56e3SPeter Brune 
3076b3a6b972SJed Brown /*@C
3077b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3078b3a6b972SJed Brown 
3079b3a6b972SJed Brown    Logically Collective
3080b3a6b972SJed Brown 
3081b3a6b972SJed Brown    Input Arguments:
3082b3a6b972SJed Brown +  global - global DM
3083b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3084b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3085b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3086b3a6b972SJed Brown 
3087b3a6b972SJed Brown    Level: advanced
3088b3a6b972SJed Brown 
3089b3a6b972SJed Brown    Notes:
3090b3a6b972SJed Brown 
3091b3a6b972SJed Brown    This function is currently not available from Fortran.
3092b3a6b972SJed Brown 
3093b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3094b3a6b972SJed Brown @*/
3095b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3096b3a6b972SJed Brown {
3097b3a6b972SJed Brown   PetscErrorCode      ierr;
3098b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3099b3a6b972SJed Brown 
3100b3a6b972SJed Brown   PetscFunctionBegin;
3101b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3102b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3103b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3104b3a6b972SJed Brown       link = *p;
3105b3a6b972SJed Brown       *p = link->next;
3106b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3107b3a6b972SJed Brown       break;
3108b3a6b972SJed Brown     }
3109b3a6b972SJed Brown   }
3110b3a6b972SJed Brown   PetscFunctionReturn(0);
3111b3a6b972SJed Brown }
3112b3a6b972SJed Brown 
31135dbd56e3SPeter Brune /*@
3114be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
31155dbd56e3SPeter Brune 
31165dbd56e3SPeter Brune    Collective if any hooks are
31175dbd56e3SPeter Brune 
31185dbd56e3SPeter Brune    Input Arguments:
31195dbd56e3SPeter Brune +  fine - finer DM to use as a base
3120be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3121be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
31225dbd56e3SPeter Brune -  coarse - coarer DM to update
31235dbd56e3SPeter Brune 
31245dbd56e3SPeter Brune    Level: developer
31255dbd56e3SPeter Brune 
31265dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
31275dbd56e3SPeter Brune @*/
3128be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
31295dbd56e3SPeter Brune {
31305dbd56e3SPeter Brune   PetscErrorCode      ierr;
3131be081cd6SPeter Brune   DMSubDomainHookLink link;
31325dbd56e3SPeter Brune 
31335dbd56e3SPeter Brune   PetscFunctionBegin;
3134be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
31358865f1eaSKarl Rupp     if (link->restricthook) {
31368865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
31378865f1eaSKarl Rupp     }
31385dbd56e3SPeter Brune   }
31395dbd56e3SPeter Brune   PetscFunctionReturn(0);
31405dbd56e3SPeter Brune }
31415dbd56e3SPeter Brune 
31425fe1f584SPeter Brune /*@
31436a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
31445fe1f584SPeter Brune 
31455fe1f584SPeter Brune     Not Collective
31465fe1f584SPeter Brune 
31475fe1f584SPeter Brune     Input Parameter:
31485fe1f584SPeter Brune .   dm - the DM object
31495fe1f584SPeter Brune 
31505fe1f584SPeter Brune     Output Parameter:
31516a7d9d85SPeter Brune .   level - number of coarsenings
31525fe1f584SPeter Brune 
31535fe1f584SPeter Brune     Level: developer
31545fe1f584SPeter Brune 
31556a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31565fe1f584SPeter Brune 
31575fe1f584SPeter Brune @*/
31585fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
31595fe1f584SPeter Brune {
31605fe1f584SPeter Brune   PetscFunctionBegin;
31615fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3162b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
31635fe1f584SPeter Brune   *level = dm->leveldown;
31645fe1f584SPeter Brune   PetscFunctionReturn(0);
31655fe1f584SPeter Brune }
31665fe1f584SPeter Brune 
31679a64c4a8SMatthew G. Knepley /*@
31689a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31699a64c4a8SMatthew G. Knepley 
31709a64c4a8SMatthew G. Knepley     Not Collective
31719a64c4a8SMatthew G. Knepley 
31729a64c4a8SMatthew G. Knepley     Input Parameters:
31739a64c4a8SMatthew G. Knepley +   dm - the DM object
31749a64c4a8SMatthew G. Knepley -   level - number of coarsenings
31759a64c4a8SMatthew G. Knepley 
31769a64c4a8SMatthew G. Knepley     Level: developer
31779a64c4a8SMatthew G. Knepley 
31789a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31799a64c4a8SMatthew G. Knepley @*/
31809a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
31819a64c4a8SMatthew G. Knepley {
31829a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
31839a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31849a64c4a8SMatthew G. Knepley   dm->leveldown = level;
31859a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
31869a64c4a8SMatthew G. Knepley }
31879a64c4a8SMatthew G. Knepley 
31885fe1f584SPeter Brune 
31895fe1f584SPeter Brune 
319047c6ae99SBarry Smith /*@C
319147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
319247c6ae99SBarry Smith 
3193d083f849SBarry Smith     Collective on dm
319447c6ae99SBarry Smith 
319547c6ae99SBarry Smith     Input Parameter:
319647c6ae99SBarry Smith +   dm - the DM object
319747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
319847c6ae99SBarry Smith 
319947c6ae99SBarry Smith     Output Parameter:
320047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
320147c6ae99SBarry Smith 
320247c6ae99SBarry Smith     Level: developer
320347c6ae99SBarry Smith 
3204e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
320547c6ae99SBarry Smith 
320647c6ae99SBarry Smith @*/
32077087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
320847c6ae99SBarry Smith {
320947c6ae99SBarry Smith   PetscErrorCode ierr;
321047c6ae99SBarry Smith 
321147c6ae99SBarry Smith   PetscFunctionBegin;
3212171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3213ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
321447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3215b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
321647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
321747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
321847c6ae99SBarry Smith   } else if (dm->ops->refine) {
321947c6ae99SBarry Smith     PetscInt i;
322047c6ae99SBarry Smith 
3221ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
322247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3223ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
322447c6ae99SBarry Smith     }
3225ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
322647c6ae99SBarry Smith   PetscFunctionReturn(0);
322747c6ae99SBarry Smith }
322847c6ae99SBarry Smith 
322947c6ae99SBarry Smith /*@C
323047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
323147c6ae99SBarry Smith 
3232d083f849SBarry Smith     Collective on dm
323347c6ae99SBarry Smith 
323447c6ae99SBarry Smith     Input Parameter:
323547c6ae99SBarry Smith +   dm - the DM object
323647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
323747c6ae99SBarry Smith 
323847c6ae99SBarry Smith     Output Parameter:
323947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
324047c6ae99SBarry Smith 
324147c6ae99SBarry Smith     Level: developer
324247c6ae99SBarry Smith 
3243e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
324447c6ae99SBarry Smith 
324547c6ae99SBarry Smith @*/
32467087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
324747c6ae99SBarry Smith {
324847c6ae99SBarry Smith   PetscErrorCode ierr;
324947c6ae99SBarry Smith 
325047c6ae99SBarry Smith   PetscFunctionBegin;
3251171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3252ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
325347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
325447c6ae99SBarry Smith   PetscValidPointer(dmc,3);
325547c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
325647c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
325747c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
325847c6ae99SBarry Smith     PetscInt i;
325947c6ae99SBarry Smith 
3260ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
326147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3262ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
326347c6ae99SBarry Smith     }
3264ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
326547c6ae99SBarry Smith   PetscFunctionReturn(0);
326647c6ae99SBarry Smith }
326747c6ae99SBarry Smith 
32681a266240SBarry Smith /*@C
32691a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32701a266240SBarry Smith 
32711a266240SBarry Smith     Not Collective
32721a266240SBarry Smith 
32731a266240SBarry Smith     Input Parameters:
32741a266240SBarry Smith +   dm - the DM object
32751a266240SBarry Smith -   destroy - the destroy function
32761a266240SBarry Smith 
32771a266240SBarry Smith     Level: intermediate
32781a266240SBarry Smith 
3279e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
32801a266240SBarry Smith 
3281f07f9ceaSJed Brown @*/
32821a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
32831a266240SBarry Smith {
32841a266240SBarry Smith   PetscFunctionBegin;
3285171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32861a266240SBarry Smith   dm->ctxdestroy = destroy;
32871a266240SBarry Smith   PetscFunctionReturn(0);
32881a266240SBarry Smith }
32891a266240SBarry Smith 
3290b07ff414SBarry Smith /*@
32911b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
329247c6ae99SBarry Smith 
329347c6ae99SBarry Smith     Not Collective
329447c6ae99SBarry Smith 
329547c6ae99SBarry Smith     Input Parameters:
329647c6ae99SBarry Smith +   dm - the DM object
329747c6ae99SBarry Smith -   ctx - the user context
329847c6ae99SBarry Smith 
329947c6ae99SBarry Smith     Level: intermediate
330047c6ae99SBarry Smith 
3301e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
330247c6ae99SBarry Smith 
330347c6ae99SBarry Smith @*/
33041b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
330547c6ae99SBarry Smith {
330647c6ae99SBarry Smith   PetscFunctionBegin;
3307171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
330847c6ae99SBarry Smith   dm->ctx = ctx;
330947c6ae99SBarry Smith   PetscFunctionReturn(0);
331047c6ae99SBarry Smith }
331147c6ae99SBarry Smith 
331247c6ae99SBarry Smith /*@
33131b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
331447c6ae99SBarry Smith 
331547c6ae99SBarry Smith     Not Collective
331647c6ae99SBarry Smith 
331747c6ae99SBarry Smith     Input Parameter:
331847c6ae99SBarry Smith .   dm - the DM object
331947c6ae99SBarry Smith 
332047c6ae99SBarry Smith     Output Parameter:
332147c6ae99SBarry Smith .   ctx - the user context
332247c6ae99SBarry Smith 
332347c6ae99SBarry Smith     Level: intermediate
332447c6ae99SBarry Smith 
3325e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
332647c6ae99SBarry Smith 
332747c6ae99SBarry Smith @*/
33281b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
332947c6ae99SBarry Smith {
333047c6ae99SBarry Smith   PetscFunctionBegin;
3331171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33321b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
333347c6ae99SBarry Smith   PetscFunctionReturn(0);
333447c6ae99SBarry Smith }
333547c6ae99SBarry Smith 
333608da532bSDmitry Karpeev /*@C
3337df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
333808da532bSDmitry Karpeev 
3339d083f849SBarry Smith     Logically Collective on dm
334008da532bSDmitry Karpeev 
334108da532bSDmitry Karpeev     Input Parameter:
334208da532bSDmitry Karpeev +   dm - the DM object
33430298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
334408da532bSDmitry Karpeev 
334508da532bSDmitry Karpeev     Level: intermediate
334608da532bSDmitry Karpeev 
3347835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
334808da532bSDmitry Karpeev          DMSetJacobian()
334908da532bSDmitry Karpeev 
335008da532bSDmitry Karpeev @*/
335108da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
335208da532bSDmitry Karpeev {
335308da532bSDmitry Karpeev   PetscFunctionBegin;
33545a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
335508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
335608da532bSDmitry Karpeev   PetscFunctionReturn(0);
335708da532bSDmitry Karpeev }
335808da532bSDmitry Karpeev 
335908da532bSDmitry Karpeev /*@
336008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
336108da532bSDmitry Karpeev 
336208da532bSDmitry Karpeev     Not Collective
336308da532bSDmitry Karpeev 
336408da532bSDmitry Karpeev     Input Parameter:
336508da532bSDmitry Karpeev .   dm - the DM object to destroy
336608da532bSDmitry Karpeev 
336708da532bSDmitry Karpeev     Output Parameter:
336808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
336908da532bSDmitry Karpeev 
337008da532bSDmitry Karpeev     Level: developer
337108da532bSDmitry Karpeev 
337274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
337308da532bSDmitry Karpeev 
337408da532bSDmitry Karpeev @*/
337508da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
337608da532bSDmitry Karpeev {
337708da532bSDmitry Karpeev   PetscFunctionBegin;
33785a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3379534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
338008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
338108da532bSDmitry Karpeev   PetscFunctionReturn(0);
338208da532bSDmitry Karpeev }
338308da532bSDmitry Karpeev 
338408da532bSDmitry Karpeev /*@C
338508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
338608da532bSDmitry Karpeev 
3387d083f849SBarry Smith     Logically Collective on dm
338808da532bSDmitry Karpeev 
338908da532bSDmitry Karpeev     Input Parameters:
3390907376e6SBarry Smith .   dm - the DM object
339108da532bSDmitry Karpeev 
339208da532bSDmitry Karpeev     Output parameters:
339308da532bSDmitry Karpeev +   xl - lower bound
339408da532bSDmitry Karpeev -   xu - upper bound
339508da532bSDmitry Karpeev 
3396907376e6SBarry Smith     Level: advanced
3397907376e6SBarry Smith 
339895452b02SPatrick Sanan     Notes:
339995452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
340008da532bSDmitry Karpeev 
340174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
340208da532bSDmitry Karpeev 
340308da532bSDmitry Karpeev @*/
340408da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
340508da532bSDmitry Karpeev {
340608da532bSDmitry Karpeev   PetscErrorCode ierr;
34075fd66863SKarl Rupp 
340808da532bSDmitry Karpeev   PetscFunctionBegin;
34095a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
341008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
34115a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3412b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
341308da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
341408da532bSDmitry Karpeev   PetscFunctionReturn(0);
341508da532bSDmitry Karpeev }
341608da532bSDmitry Karpeev 
3417b0ae01b7SPeter Brune /*@
3418b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3419b0ae01b7SPeter Brune 
3420b0ae01b7SPeter Brune     Not Collective
3421b0ae01b7SPeter Brune 
3422b0ae01b7SPeter Brune     Input Parameter:
3423b0ae01b7SPeter Brune .   dm - the DM object
3424b0ae01b7SPeter Brune 
3425b0ae01b7SPeter Brune     Output Parameter:
3426b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3427b0ae01b7SPeter Brune 
3428b0ae01b7SPeter Brune     Level: developer
3429b0ae01b7SPeter Brune 
34301565f0a7SPatrick Sanan .seealso DMCreateColoring()
3431b0ae01b7SPeter Brune 
3432b0ae01b7SPeter Brune @*/
3433b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3434b0ae01b7SPeter Brune {
3435b0ae01b7SPeter Brune   PetscFunctionBegin;
34365a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3437534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3438b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3439b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3440b0ae01b7SPeter Brune }
3441b0ae01b7SPeter Brune 
34423ad4599aSBarry Smith /*@
34433ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
34443ad4599aSBarry Smith 
34453ad4599aSBarry Smith     Not Collective
34463ad4599aSBarry Smith 
34473ad4599aSBarry Smith     Input Parameter:
34483ad4599aSBarry Smith .   dm - the DM object
34493ad4599aSBarry Smith 
34503ad4599aSBarry Smith     Output Parameter:
34513ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
34523ad4599aSBarry Smith 
34533ad4599aSBarry Smith     Level: developer
34543ad4599aSBarry Smith 
34551565f0a7SPatrick Sanan .seealso DMCreateRestriction()
34563ad4599aSBarry Smith 
34573ad4599aSBarry Smith @*/
34583ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
34593ad4599aSBarry Smith {
34603ad4599aSBarry Smith   PetscFunctionBegin;
34615a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3462534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34633ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34643ad4599aSBarry Smith   PetscFunctionReturn(0);
34653ad4599aSBarry Smith }
34663ad4599aSBarry Smith 
3467a7058e45SLawrence Mitchell 
3468a7058e45SLawrence Mitchell /*@
3469a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3470a7058e45SLawrence Mitchell 
3471a7058e45SLawrence Mitchell     Not Collective
3472a7058e45SLawrence Mitchell 
3473a7058e45SLawrence Mitchell     Input Parameter:
3474a7058e45SLawrence Mitchell .   dm - the DM object
3475a7058e45SLawrence Mitchell 
3476a7058e45SLawrence Mitchell     Output Parameter:
3477a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3478a7058e45SLawrence Mitchell 
3479a7058e45SLawrence Mitchell     Level: developer
3480a7058e45SLawrence Mitchell 
34811565f0a7SPatrick Sanan .seealso DMCreateInjection()
3482a7058e45SLawrence Mitchell 
3483a7058e45SLawrence Mitchell @*/
3484a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3485a7058e45SLawrence Mitchell {
34864a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
34875a84ad33SLisandro Dalcin 
3488a7058e45SLawrence Mitchell   PetscFunctionBegin;
34895a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3490534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34915a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
34924a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
34935a84ad33SLisandro Dalcin   } else {
34945a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
34955a84ad33SLisandro Dalcin   }
3496a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3497a7058e45SLawrence Mitchell }
3498a7058e45SLawrence Mitchell 
34995a84ad33SLisandro Dalcin 
3500748fac09SDmitry Karpeev /*@C
350108da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
350208da532bSDmitry Karpeev 
3503d083f849SBarry Smith     Collective on dm
350408da532bSDmitry Karpeev 
350508da532bSDmitry Karpeev     Input Parameter:
350608da532bSDmitry Karpeev +   dm - the DM object
35070298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
350808da532bSDmitry Karpeev 
350908da532bSDmitry Karpeev     Level: developer
351008da532bSDmitry Karpeev 
351174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
351208da532bSDmitry Karpeev 
351308da532bSDmitry Karpeev @*/
351408da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
351508da532bSDmitry Karpeev {
351608da532bSDmitry Karpeev   PetscErrorCode ierr;
35175fd66863SKarl Rupp 
351808da532bSDmitry Karpeev   PetscFunctionBegin;
3519b9d85ea2SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
352008da532bSDmitry Karpeev   if (x) {
352108da532bSDmitry Karpeev     if (!dm->x) {
352208da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
352308da532bSDmitry Karpeev     }
352408da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
35258865f1eaSKarl Rupp   } else if (dm->x) {
352608da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
352708da532bSDmitry Karpeev   }
352808da532bSDmitry Karpeev   PetscFunctionReturn(0);
352908da532bSDmitry Karpeev }
353008da532bSDmitry Karpeev 
35310298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3532264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3533264ace61SBarry Smith 
3534264ace61SBarry Smith /*@C
3535264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3536264ace61SBarry Smith 
3537d083f849SBarry Smith   Collective on dm
3538264ace61SBarry Smith 
3539264ace61SBarry Smith   Input Parameters:
3540264ace61SBarry Smith + dm     - The DM object
3541264ace61SBarry Smith - method - The name of the DM type
3542264ace61SBarry Smith 
3543264ace61SBarry Smith   Options Database Key:
3544264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3545264ace61SBarry Smith 
3546264ace61SBarry Smith   Notes:
3547e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3548264ace61SBarry Smith 
3549264ace61SBarry Smith   Level: intermediate
3550264ace61SBarry Smith 
3551264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3552264ace61SBarry Smith @*/
355319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3554264ace61SBarry Smith {
3555264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3556264ace61SBarry Smith   PetscBool      match;
3557264ace61SBarry Smith   PetscErrorCode ierr;
3558264ace61SBarry Smith 
3559264ace61SBarry Smith   PetscFunctionBegin;
3560264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3561251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3562264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3563264ace61SBarry Smith 
35640f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35651c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3566ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3567264ace61SBarry Smith 
3568264ace61SBarry Smith   if (dm->ops->destroy) {
3569264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3570264ace61SBarry Smith   }
3571d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3572264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3573d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3574264ace61SBarry Smith   PetscFunctionReturn(0);
3575264ace61SBarry Smith }
3576264ace61SBarry Smith 
3577264ace61SBarry Smith /*@C
3578264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3579264ace61SBarry Smith 
3580264ace61SBarry Smith   Not Collective
3581264ace61SBarry Smith 
3582264ace61SBarry Smith   Input Parameter:
3583264ace61SBarry Smith . dm  - The DM
3584264ace61SBarry Smith 
3585264ace61SBarry Smith   Output Parameter:
3586264ace61SBarry Smith . type - The DM type name
3587264ace61SBarry Smith 
3588264ace61SBarry Smith   Level: intermediate
3589264ace61SBarry Smith 
3590264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3591264ace61SBarry Smith @*/
359219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3593264ace61SBarry Smith {
3594264ace61SBarry Smith   PetscErrorCode ierr;
3595264ace61SBarry Smith 
3596264ace61SBarry Smith   PetscFunctionBegin;
3597264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3598c959eef4SJed Brown   PetscValidPointer(type,2);
3599607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3600264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3601264ace61SBarry Smith   PetscFunctionReturn(0);
3602264ace61SBarry Smith }
3603264ace61SBarry Smith 
360467a56275SMatthew G Knepley /*@C
360567a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
360667a56275SMatthew G Knepley 
3607d083f849SBarry Smith   Collective on dm
360867a56275SMatthew G Knepley 
360967a56275SMatthew G Knepley   Input Parameters:
361067a56275SMatthew G Knepley + dm - the DM
361167a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
361267a56275SMatthew G Knepley 
361367a56275SMatthew G Knepley   Output Parameter:
361467a56275SMatthew G Knepley . M - pointer to new DM
361567a56275SMatthew G Knepley 
361667a56275SMatthew G Knepley   Notes:
361767a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
361867a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
361967a56275SMatthew G Knepley   of the input DM.
362067a56275SMatthew G Knepley 
362167a56275SMatthew G Knepley   Level: intermediate
362267a56275SMatthew G Knepley 
362367a56275SMatthew G Knepley .seealso: DMCreate()
362467a56275SMatthew G Knepley @*/
362519fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
362667a56275SMatthew G Knepley {
362767a56275SMatthew G Knepley   DM             B;
362867a56275SMatthew G Knepley   char           convname[256];
3629c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
363067a56275SMatthew G Knepley   PetscErrorCode ierr;
363167a56275SMatthew G Knepley 
363267a56275SMatthew G Knepley   PetscFunctionBegin;
363367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
363467a56275SMatthew G Knepley   PetscValidType(dm,1);
363567a56275SMatthew G Knepley   PetscValidPointer(M,3);
3636251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3637c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3638c067b6caSMatthew G. Knepley   if (sametype) {
3639c067b6caSMatthew G. Knepley     *M   = dm;
3640c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3641c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3642c067b6caSMatthew G. Knepley   } else {
36430298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
364467a56275SMatthew G Knepley 
364567a56275SMatthew G Knepley     /*
364667a56275SMatthew G Knepley        Order of precedence:
364767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
364867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
364967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
365067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
365167a56275SMatthew G Knepley        5) Use a really basic converter.
365267a56275SMatthew G Knepley     */
365367a56275SMatthew G Knepley 
365467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3655a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3656a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3657a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3658a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3659a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36600005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
366167a56275SMatthew G Knepley     if (conv) goto foundconv;
366267a56275SMatthew G Knepley 
366367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
366482f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
366567a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3666a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3667a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3668a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3669a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3670a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36710005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
367267a56275SMatthew G Knepley     if (conv) {
3673fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
367467a56275SMatthew G Knepley       goto foundconv;
367567a56275SMatthew G Knepley     }
367667a56275SMatthew G Knepley 
367767a56275SMatthew G Knepley #if 0
367867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
367967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3680fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
368167a56275SMatthew G Knepley     if (conv) goto foundconv;
368267a56275SMatthew G Knepley 
368367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
368467a56275SMatthew G Knepley     if (dm->ops->convert) {
368567a56275SMatthew G Knepley       conv = dm->ops->convert;
368667a56275SMatthew G Knepley     }
368767a56275SMatthew G Knepley     if (conv) goto foundconv;
368867a56275SMatthew G Knepley #endif
368967a56275SMatthew G Knepley 
369067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
369182f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
369267a56275SMatthew G Knepley 
369367a56275SMatthew G Knepley foundconv:
369467a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
369567a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
369612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
369790b157c4SStefano Zampini     {
369890b157c4SStefano Zampini       PetscBool             isper;
369912fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
370012fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
370190b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
370290b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
370312fa691eSMatthew G. Knepley     }
370467a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
370567a56275SMatthew G Knepley   }
370667a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
370767a56275SMatthew G Knepley   PetscFunctionReturn(0);
370867a56275SMatthew G Knepley }
3709264ace61SBarry Smith 
3710264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3711264ace61SBarry Smith 
3712264ace61SBarry Smith /*@C
37131c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
37141c84c290SBarry Smith 
37151c84c290SBarry Smith   Not Collective
37161c84c290SBarry Smith 
37171c84c290SBarry Smith   Input Parameters:
37181c84c290SBarry Smith + name        - The name of a new user-defined creation routine
37191c84c290SBarry Smith - create_func - The creation routine itself
37201c84c290SBarry Smith 
37211c84c290SBarry Smith   Notes:
37221c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
37231c84c290SBarry Smith 
37241c84c290SBarry Smith 
37251c84c290SBarry Smith   Sample usage:
37261c84c290SBarry Smith .vb
3727bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
37281c84c290SBarry Smith .ve
37291c84c290SBarry Smith 
37301c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
37311c84c290SBarry Smith .vb
37321c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
37331c84c290SBarry Smith     DMSetType(DM,"my_da");
37341c84c290SBarry Smith .ve
37351c84c290SBarry Smith    or at runtime via the option
37361c84c290SBarry Smith .vb
37371c84c290SBarry Smith     -da_type my_da
37381c84c290SBarry Smith .ve
3739264ace61SBarry Smith 
3740264ace61SBarry Smith   Level: advanced
37411c84c290SBarry Smith 
3742bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
37431c84c290SBarry Smith 
3744264ace61SBarry Smith @*/
3745bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3746264ace61SBarry Smith {
3747264ace61SBarry Smith   PetscErrorCode ierr;
3748264ace61SBarry Smith 
3749264ace61SBarry Smith   PetscFunctionBegin;
37501d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3751a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3752264ace61SBarry Smith   PetscFunctionReturn(0);
3753264ace61SBarry Smith }
3754264ace61SBarry Smith 
3755b859378eSBarry Smith /*@C
375655849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3757b859378eSBarry Smith 
3758d083f849SBarry Smith   Collective on viewer
3759b859378eSBarry Smith 
3760b859378eSBarry Smith   Input Parameters:
3761b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3762b859378eSBarry Smith            some related function before a call to DMLoad().
3763b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3764b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3765b859378eSBarry Smith 
3766b859378eSBarry Smith    Level: intermediate
3767b859378eSBarry Smith 
3768b859378eSBarry Smith   Notes:
376955849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3770b859378eSBarry Smith 
3771b859378eSBarry Smith   Notes for advanced users:
3772b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3773b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3774b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3775b859378eSBarry Smith   format is
3776b859378eSBarry Smith .vb
3777b859378eSBarry Smith      has not yet been determined
3778b859378eSBarry Smith .ve
3779b859378eSBarry Smith 
3780b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3781b859378eSBarry Smith @*/
3782b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3783b859378eSBarry Smith {
37849331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3785b859378eSBarry Smith   PetscErrorCode ierr;
3786b859378eSBarry Smith 
3787b859378eSBarry Smith   PetscFunctionBegin;
3788b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3789b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3790fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
379132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37929331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
379358cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
37949331c7a4SMatthew G. Knepley   if (isbinary) {
37959331c7a4SMatthew G. Knepley     PetscInt classid;
37969331c7a4SMatthew G. Knepley     char     type[256];
3797b859378eSBarry Smith 
3798060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37999200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3800060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
380132c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
38029331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
38039331c7a4SMatthew G. Knepley   } else if (ishdf5) {
38049331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
38059331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
380658cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3807b859378eSBarry Smith   PetscFunctionReturn(0);
3808b859378eSBarry Smith }
3809b859378eSBarry Smith 
3810b2e4378dSMatthew G. Knepley /*@
3811b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3812b2e4378dSMatthew G. Knepley 
3813b2e4378dSMatthew G. Knepley   Not collective
3814b2e4378dSMatthew G. Knepley 
3815b2e4378dSMatthew G. Knepley   Input Parameter:
3816b2e4378dSMatthew G. Knepley . dm - the DM
3817b2e4378dSMatthew G. Knepley 
3818b2e4378dSMatthew G. Knepley   Output Parameters:
3819b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3820b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3821b2e4378dSMatthew G. Knepley 
3822b2e4378dSMatthew G. Knepley   Level: beginner
3823b2e4378dSMatthew G. Knepley 
3824b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3825b2e4378dSMatthew G. Knepley 
3826b2e4378dSMatthew G. Knepley 
3827b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3828b2e4378dSMatthew G. Knepley @*/
3829b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3830b2e4378dSMatthew G. Knepley {
3831b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3832b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3833b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3834b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3835b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3836b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3837b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3838b2e4378dSMatthew G. Knepley 
3839b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3840b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3841b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3842b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3843b2e4378dSMatthew G. Knepley   if (coords) {
3844b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3845b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3846b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3847b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3848b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3849b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3850b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3851b2e4378dSMatthew G. Knepley       }
3852b2e4378dSMatthew G. Knepley     }
3853b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3854b2e4378dSMatthew G. Knepley   } else {
3855b2e4378dSMatthew G. Knepley     PetscBool isda;
3856b2e4378dSMatthew G. Knepley 
3857b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3858b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3859b2e4378dSMatthew G. Knepley   }
3860b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3861b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3862b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3863b2e4378dSMatthew G. Knepley }
3864b2e4378dSMatthew G. Knepley 
3865b2e4378dSMatthew G. Knepley /*@
3866b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3867b2e4378dSMatthew G. Knepley 
3868b2e4378dSMatthew G. Knepley   Collective
3869b2e4378dSMatthew G. Knepley 
3870b2e4378dSMatthew G. Knepley   Input Parameter:
3871b2e4378dSMatthew G. Knepley . dm - the DM
3872b2e4378dSMatthew G. Knepley 
3873b2e4378dSMatthew G. Knepley   Output Parameters:
3874b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3875b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3876b2e4378dSMatthew G. Knepley 
3877b2e4378dSMatthew G. Knepley   Level: beginner
3878b2e4378dSMatthew G. Knepley 
3879b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3880b2e4378dSMatthew G. Knepley @*/
3881b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3882b2e4378dSMatthew G. Knepley {
3883b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
38846ce308c4SMatthew G. Knepley   PetscInt       cdim;
38856ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3886b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3887b2e4378dSMatthew G. Knepley 
3888b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3889b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3890b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3891b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3892b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3893b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3894b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3895b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3896b2e4378dSMatthew G. Knepley }
3897b2e4378dSMatthew G. Knepley 
38987da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38997da65231SMatthew G Knepley 
3900a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3901a6dfd86eSKarl Rupp {
39021d47ebbbSSatish Balay   PetscInt       f;
39031b30c384SMatthew G Knepley   PetscErrorCode ierr;
39041b30c384SMatthew G Knepley 
39057da65231SMatthew G Knepley   PetscFunctionBegin;
390674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39071d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
390857622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
39097da65231SMatthew G Knepley   }
39107da65231SMatthew G Knepley   PetscFunctionReturn(0);
39117da65231SMatthew G Knepley }
39127da65231SMatthew G Knepley 
3913a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3914a6dfd86eSKarl Rupp {
39151b30c384SMatthew G Knepley   PetscInt       f, g;
39167da65231SMatthew G Knepley   PetscErrorCode ierr;
39177da65231SMatthew G Knepley 
39187da65231SMatthew G Knepley   PetscFunctionBegin;
391974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39201d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
392174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
39221d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3923e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
39247da65231SMatthew G Knepley     }
392574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
39267da65231SMatthew G Knepley   }
39277da65231SMatthew G Knepley   PetscFunctionReturn(0);
39287da65231SMatthew G Knepley }
3929e7c4fc90SDmitry Karpeev 
39306113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3931e759306cSMatthew G. Knepley {
39320c5b8624SToby Isaac   PetscInt          localSize, bs;
39330c5b8624SToby Isaac   PetscMPIInt       size;
39340c5b8624SToby Isaac   Vec               x, xglob;
39350c5b8624SToby Isaac   const PetscScalar *xarray;
3936e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3937e759306cSMatthew G. Knepley 
3938e759306cSMatthew G. Knepley   PetscFunctionBegin;
39399852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3940e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3941e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
39426113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
39430c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
39440c5b8624SToby Isaac   if (size > 1) {
39450c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
39460c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
39470c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
39480c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
39490c5b8624SToby Isaac   } else {
39500c5b8624SToby Isaac     xglob = x;
39510c5b8624SToby Isaac   }
39520c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
39530c5b8624SToby Isaac   if (size > 1) {
39540c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
39550c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
39560c5b8624SToby Isaac   }
3957e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3958e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3959e759306cSMatthew G. Knepley }
3960e759306cSMatthew G. Knepley 
396188ed4aceSMatthew G Knepley /*@
39621bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3963061576a5SJed Brown 
3964061576a5SJed Brown   Input Parameter:
3965061576a5SJed Brown . dm - The DM
3966061576a5SJed Brown 
3967061576a5SJed Brown   Output Parameter:
3968061576a5SJed Brown . section - The PetscSection
3969061576a5SJed Brown 
3970061576a5SJed Brown   Options Database Keys:
3971061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3972061576a5SJed Brown 
3973061576a5SJed Brown   Level: advanced
3974061576a5SJed Brown 
3975061576a5SJed Brown   Notes:
3976061576a5SJed Brown   Use DMGetLocalSection() in new code.
3977061576a5SJed Brown 
3978061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3979061576a5SJed Brown 
3980061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3981061576a5SJed Brown @*/
3982061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3983061576a5SJed Brown {
3984061576a5SJed Brown   PetscErrorCode ierr;
3985061576a5SJed Brown 
3986061576a5SJed Brown   PetscFunctionBegin;
3987061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3988061576a5SJed Brown   PetscFunctionReturn(0);
3989061576a5SJed Brown }
3990061576a5SJed Brown 
3991061576a5SJed Brown /*@
3992061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
399388ed4aceSMatthew G Knepley 
399488ed4aceSMatthew G Knepley   Input Parameter:
399588ed4aceSMatthew G Knepley . dm - The DM
399688ed4aceSMatthew G Knepley 
399788ed4aceSMatthew G Knepley   Output Parameter:
399888ed4aceSMatthew G Knepley . section - The PetscSection
399988ed4aceSMatthew G Knepley 
4000e5893cccSMatthew G. Knepley   Options Database Keys:
4001e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4002e5893cccSMatthew G. Knepley 
400388ed4aceSMatthew G Knepley   Level: intermediate
400488ed4aceSMatthew G Knepley 
400588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
400688ed4aceSMatthew G Knepley 
4007061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
400888ed4aceSMatthew G Knepley @*/
4009061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
40100adebc6cSBarry Smith {
4011fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4012fd59a867SMatthew G. Knepley 
401388ed4aceSMatthew G Knepley   PetscFunctionBegin;
401488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
401588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
40161bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4017e5e52638SMatthew G. Knepley     PetscInt d;
4018e5e52638SMatthew G. Knepley 
4019e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
40201bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
40211bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
40222f0f8703SMatthew G. Knepley   }
40231bb6d2a8SBarry Smith   *section = dm->localSection;
402488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
402588ed4aceSMatthew G Knepley }
402688ed4aceSMatthew G Knepley 
402788ed4aceSMatthew G Knepley /*@
40281bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4029061576a5SJed Brown 
4030061576a5SJed Brown   Input Parameters:
4031061576a5SJed Brown + dm - The DM
4032061576a5SJed Brown - section - The PetscSection
4033061576a5SJed Brown 
4034061576a5SJed Brown   Level: advanced
4035061576a5SJed Brown 
4036061576a5SJed Brown   Notes:
4037061576a5SJed Brown   Use DMSetLocalSection() in new code.
4038061576a5SJed Brown 
4039061576a5SJed Brown   Any existing Section will be destroyed
4040061576a5SJed Brown 
4041061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4042061576a5SJed Brown @*/
4043061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4044061576a5SJed Brown {
4045061576a5SJed Brown   PetscErrorCode ierr;
4046061576a5SJed Brown 
4047061576a5SJed Brown   PetscFunctionBegin;
4048061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4049061576a5SJed Brown   PetscFunctionReturn(0);
4050061576a5SJed Brown }
4051061576a5SJed Brown 
4052061576a5SJed Brown /*@
4053061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
405488ed4aceSMatthew G Knepley 
405588ed4aceSMatthew G Knepley   Input Parameters:
405688ed4aceSMatthew G Knepley + dm - The DM
405788ed4aceSMatthew G Knepley - section - The PetscSection
405888ed4aceSMatthew G Knepley 
405988ed4aceSMatthew G Knepley   Level: intermediate
406088ed4aceSMatthew G Knepley 
406188ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
406288ed4aceSMatthew G Knepley 
4063061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
406488ed4aceSMatthew G Knepley @*/
4065061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
40660adebc6cSBarry Smith {
4067c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4068af122d2aSMatthew G Knepley   PetscInt       f;
406988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
407088ed4aceSMatthew G Knepley 
407188ed4aceSMatthew G Knepley   PetscFunctionBegin;
407288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4073b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40741d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40751bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
40761bb6d2a8SBarry Smith   dm->localSection = section;
40771bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4078af122d2aSMatthew G Knepley   if (numFields) {
4079af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4080af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40810f21e855SMatthew G. Knepley       PetscObject disc;
4082af122d2aSMatthew G Knepley       const char *name;
4083af122d2aSMatthew G Knepley 
40841bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
408544a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40860f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4087af122d2aSMatthew G Knepley     }
4088af122d2aSMatthew G Knepley   }
4089e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40901bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
409188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
409288ed4aceSMatthew G Knepley }
409388ed4aceSMatthew G Knepley 
40949435951eSToby Isaac /*@
4095b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40969435951eSToby Isaac 
4097e228b242SToby Isaac   not collective
4098e228b242SToby Isaac 
40999435951eSToby Isaac   Input Parameter:
41009435951eSToby Isaac . dm - The DM
41019435951eSToby Isaac 
41029435951eSToby Isaac   Output Parameter:
41039435951eSToby 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.
41049435951eSToby 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.
41059435951eSToby Isaac 
41069435951eSToby Isaac   Level: advanced
41079435951eSToby Isaac 
41089435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
41099435951eSToby Isaac 
41109435951eSToby Isaac .seealso: DMSetDefaultConstraints()
41119435951eSToby Isaac @*/
41129435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
41139435951eSToby Isaac {
41149435951eSToby Isaac   PetscErrorCode ierr;
41159435951eSToby Isaac 
41169435951eSToby Isaac   PetscFunctionBegin;
41179435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41189435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
411945a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
412045a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
41219435951eSToby Isaac   PetscFunctionReturn(0);
41229435951eSToby Isaac }
41239435951eSToby Isaac 
41249435951eSToby Isaac /*@
4125b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
41269435951eSToby Isaac 
41279435951eSToby 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().
41289435951eSToby Isaac 
41299435951eSToby 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.
41309435951eSToby Isaac 
4131e228b242SToby Isaac   collective on dm
4132e228b242SToby Isaac 
41339435951eSToby Isaac   Input Parameters:
41349435951eSToby Isaac + dm - The DM
4135e228b242SToby 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).
4136e228b242SToby 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).
41379435951eSToby Isaac 
41389435951eSToby Isaac   Level: advanced
41399435951eSToby Isaac 
41409435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
41419435951eSToby Isaac 
41429435951eSToby Isaac .seealso: DMGetDefaultConstraints()
41439435951eSToby Isaac @*/
41449435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
41459435951eSToby Isaac {
4146e228b242SToby Isaac   PetscMPIInt result;
41479435951eSToby Isaac   PetscErrorCode ierr;
41489435951eSToby Isaac 
41499435951eSToby Isaac   PetscFunctionBegin;
41509435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4151e228b242SToby Isaac   if (section) {
4152e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4153e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4154f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4155e228b242SToby Isaac   }
4156e228b242SToby Isaac   if (mat) {
4157e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4158e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4159f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4160e228b242SToby Isaac   }
41619435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41629435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
41639435951eSToby Isaac   dm->defaultConstraintSection = section;
41649435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
41659435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
41669435951eSToby Isaac   dm->defaultConstraintMat = mat;
41679435951eSToby Isaac   PetscFunctionReturn(0);
41689435951eSToby Isaac }
41699435951eSToby Isaac 
4170497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4171507e4973SMatthew G. Knepley /*
4172507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4173507e4973SMatthew G. Knepley 
4174507e4973SMatthew G. Knepley   Input Parameters:
4175507e4973SMatthew G. Knepley + dm - The DM
4176507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4177507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4178507e4973SMatthew G. Knepley 
4179507e4973SMatthew G. Knepley   Level: intermediate
4180507e4973SMatthew G. Knepley 
41811bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4182507e4973SMatthew G. Knepley */
4183f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4184507e4973SMatthew G. Knepley {
4185507e4973SMatthew G. Knepley   MPI_Comm        comm;
4186507e4973SMatthew G. Knepley   PetscLayout     layout;
4187507e4973SMatthew G. Knepley   const PetscInt *ranges;
4188507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4189507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4190507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4191507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4192507e4973SMatthew G. Knepley 
4193507e4973SMatthew G. Knepley   PetscFunctionBegin;
4194507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4195507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4196507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4197507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4198507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4199507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4200507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4201507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4202507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4203507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4204507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4205507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4206f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4207507e4973SMatthew G. Knepley 
4208507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4209507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4210507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4211507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4212507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4213507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4214507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4215507e4973SMatthew 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;}
4216507e4973SMatthew 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;}
4217507e4973SMatthew G. Knepley     if (gdof < 0) {
4218507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4219507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4220507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4221507e4973SMatthew G. Knepley 
4222507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4223507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4224507e4973SMatthew 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;}
4225507e4973SMatthew G. Knepley       }
4226507e4973SMatthew G. Knepley     }
4227507e4973SMatthew G. Knepley   }
4228507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4229507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4230b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4231507e4973SMatthew G. Knepley   if (!gvalid) {
4232507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4233507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4234507e4973SMatthew G. Knepley   }
4235507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4236507e4973SMatthew G. Knepley }
4237f741bcd2SMatthew G. Knepley #endif
4238507e4973SMatthew G. Knepley 
423988ed4aceSMatthew G Knepley /*@
4240e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
424188ed4aceSMatthew G Knepley 
4242d083f849SBarry Smith   Collective on dm
42438b1ab98fSJed Brown 
424488ed4aceSMatthew G Knepley   Input Parameter:
424588ed4aceSMatthew G Knepley . dm - The DM
424688ed4aceSMatthew G Knepley 
424788ed4aceSMatthew G Knepley   Output Parameter:
424888ed4aceSMatthew G Knepley . section - The PetscSection
424988ed4aceSMatthew G Knepley 
425088ed4aceSMatthew G Knepley   Level: intermediate
425188ed4aceSMatthew G Knepley 
425288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
425388ed4aceSMatthew G Knepley 
425492fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
425588ed4aceSMatthew G Knepley @*/
4256e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
42570adebc6cSBarry Smith {
425888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
425988ed4aceSMatthew G Knepley 
426088ed4aceSMatthew G Knepley   PetscFunctionBegin;
426188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
426288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42631bb6d2a8SBarry Smith   if (!dm->globalSection) {
4264fd59a867SMatthew G. Knepley     PetscSection s;
4265fd59a867SMatthew G. Knepley 
426692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4267fd59a867SMatthew 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");
426833907cc2SStefano 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");
42691bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4270cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
42711bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
42721bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
427388ed4aceSMatthew G Knepley   }
42741bb6d2a8SBarry Smith   *section = dm->globalSection;
427588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
427688ed4aceSMatthew G Knepley }
427788ed4aceSMatthew G Knepley 
4278b21d0597SMatthew G Knepley /*@
4279e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4280b21d0597SMatthew G Knepley 
4281b21d0597SMatthew G Knepley   Input Parameters:
4282b21d0597SMatthew G Knepley + dm - The DM
42835080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4284b21d0597SMatthew G Knepley 
4285b21d0597SMatthew G Knepley   Level: intermediate
4286b21d0597SMatthew G Knepley 
4287b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4288b21d0597SMatthew G Knepley 
428992fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4290b21d0597SMatthew G Knepley @*/
4291e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42920adebc6cSBarry Smith {
4293b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4294b21d0597SMatthew G Knepley 
4295b21d0597SMatthew G Knepley   PetscFunctionBegin;
4296b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42975080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42981d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42991bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
43001bb6d2a8SBarry Smith   dm->globalSection = section;
4301497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
43021bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4303507e4973SMatthew G. Knepley #endif
4304b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4305b21d0597SMatthew G Knepley }
4306b21d0597SMatthew G Knepley 
430788ed4aceSMatthew G Knepley /*@
43081bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
430988ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
431088ed4aceSMatthew G Knepley 
431188ed4aceSMatthew G Knepley   Input Parameter:
431288ed4aceSMatthew G Knepley . dm - The DM
431388ed4aceSMatthew G Knepley 
431488ed4aceSMatthew G Knepley   Output Parameter:
431588ed4aceSMatthew G Knepley . sf - The PetscSF
431688ed4aceSMatthew G Knepley 
431788ed4aceSMatthew G Knepley   Level: intermediate
431888ed4aceSMatthew G Knepley 
431988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
432088ed4aceSMatthew G Knepley 
43211bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
432288ed4aceSMatthew G Knepley @*/
43231bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
43240adebc6cSBarry Smith {
432588ed4aceSMatthew G Knepley   PetscInt       nroots;
432688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
432788ed4aceSMatthew G Knepley 
432888ed4aceSMatthew G Knepley   PetscFunctionBegin;
432988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
433088ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
43311bb6d2a8SBarry Smith   if (!dm->sectionSF) {
43321bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
433333907cc2SStefano Zampini   }
43341bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
433588ed4aceSMatthew G Knepley   if (nroots < 0) {
433688ed4aceSMatthew G Knepley     PetscSection section, gSection;
433788ed4aceSMatthew G Knepley 
433892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
433931ea6d37SMatthew G Knepley     if (section) {
4340e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
43411bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
434231ea6d37SMatthew G Knepley     } else {
43430298fd71SBarry Smith       *sf = NULL;
434431ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
434531ea6d37SMatthew G Knepley     }
434688ed4aceSMatthew G Knepley   }
43471bb6d2a8SBarry Smith   *sf = dm->sectionSF;
434888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
434988ed4aceSMatthew G Knepley }
435088ed4aceSMatthew G Knepley 
435188ed4aceSMatthew G Knepley /*@
43521bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
435388ed4aceSMatthew G Knepley 
435488ed4aceSMatthew G Knepley   Input Parameters:
435588ed4aceSMatthew G Knepley + dm - The DM
435688ed4aceSMatthew G Knepley - sf - The PetscSF
435788ed4aceSMatthew G Knepley 
435888ed4aceSMatthew G Knepley   Level: intermediate
435988ed4aceSMatthew G Knepley 
436088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
436188ed4aceSMatthew G Knepley 
43621bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
436388ed4aceSMatthew G Knepley @*/
43641bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
43650adebc6cSBarry Smith {
436688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
436788ed4aceSMatthew G Knepley 
436888ed4aceSMatthew G Knepley   PetscFunctionBegin;
436988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4370b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
437133907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
43721bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
43731bb6d2a8SBarry Smith   dm->sectionSF = sf;
437488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
437588ed4aceSMatthew G Knepley }
437688ed4aceSMatthew G Knepley 
437788ed4aceSMatthew G Knepley /*@C
43781bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
437988ed4aceSMatthew G Knepley   describing the data layout.
438088ed4aceSMatthew G Knepley 
438188ed4aceSMatthew G Knepley   Input Parameters:
438288ed4aceSMatthew G Knepley + dm - The DM
438388ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
438488ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
438588ed4aceSMatthew G Knepley 
43861bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
438788ed4aceSMatthew G Knepley 
43881bb6d2a8SBarry Smith   Level: developer
43891bb6d2a8SBarry Smith 
43901bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43911bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43921bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43931bb6d2a8SBarry Smith 
43941bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
439588ed4aceSMatthew G Knepley @*/
43961bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
439788ed4aceSMatthew G Knepley {
439882f516ccSBarry Smith   MPI_Comm       comm;
439988ed4aceSMatthew G Knepley   PetscLayout    layout;
440088ed4aceSMatthew G Knepley   const PetscInt *ranges;
440188ed4aceSMatthew G Knepley   PetscInt       *local;
440288ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4403ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
440488ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
440588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
440688ed4aceSMatthew G Knepley 
440788ed4aceSMatthew G Knepley   PetscFunctionBegin;
440888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4409367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
441088ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
441188ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
441288ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
441388ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
441488ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
441588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
441688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
441788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
441888ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4419ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
44206636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
442188ed4aceSMatthew G Knepley 
44226636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44236636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4424235fbf56SMatthew 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));
44256636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
442688ed4aceSMatthew G Knepley   }
4427785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4428785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
442988ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
44301f588964SMatthew G Knepley     const PetscInt *cind;
44316636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
443288ed4aceSMatthew G Knepley 
443388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
443488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
443588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
443688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
443788ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44386636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
443988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
44406636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
44416636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
44426636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4443057b4bcdSMatthew 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);
44446636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
44456636e97aSMatthew G Knepley     }
444688ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
444788ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
444888ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
444988ed4aceSMatthew G Knepley     }
445088ed4aceSMatthew G Knepley     if (gdof < 0) {
44516636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
445288ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
445388ed4aceSMatthew G Knepley 
445405376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
445531d3f06eSJed Brown         if (r < 0) r = -(r+2);
445605376888SMatthew 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);
445788ed4aceSMatthew G Knepley         remote[l].rank  = r;
445888ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
445988ed4aceSMatthew G Knepley       }
446088ed4aceSMatthew G Knepley     } else {
44616636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
446288ed4aceSMatthew G Knepley         remote[l].rank  = rank;
446388ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
446488ed4aceSMatthew G Knepley       }
446588ed4aceSMatthew G Knepley     }
446688ed4aceSMatthew G Knepley   }
44676636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
446888ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
44691bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
447088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
447188ed4aceSMatthew G Knepley }
4472af122d2aSMatthew G Knepley 
4473b21d0597SMatthew G Knepley /*@
4474b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4475b21d0597SMatthew G Knepley 
4476b21d0597SMatthew G Knepley   Input Parameter:
4477b21d0597SMatthew G Knepley . dm - The DM
4478b21d0597SMatthew G Knepley 
4479b21d0597SMatthew G Knepley   Output Parameter:
4480b21d0597SMatthew G Knepley . sf - The PetscSF
4481b21d0597SMatthew G Knepley 
4482b21d0597SMatthew G Knepley   Level: intermediate
4483b21d0597SMatthew G Knepley 
4484b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4485b21d0597SMatthew G Knepley 
44861bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4487b21d0597SMatthew G Knepley @*/
44880adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44890adebc6cSBarry Smith {
4490b21d0597SMatthew G Knepley   PetscFunctionBegin;
4491b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4492b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4493b21d0597SMatthew G Knepley   *sf = dm->sf;
4494b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4495b21d0597SMatthew G Knepley }
4496b21d0597SMatthew G Knepley 
4497057b4bcdSMatthew G Knepley /*@
4498057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4499057b4bcdSMatthew G Knepley 
4500057b4bcdSMatthew G Knepley   Input Parameters:
4501057b4bcdSMatthew G Knepley + dm - The DM
4502057b4bcdSMatthew G Knepley - sf - The PetscSF
4503057b4bcdSMatthew G Knepley 
4504057b4bcdSMatthew G Knepley   Level: intermediate
4505057b4bcdSMatthew G Knepley 
45061bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4507057b4bcdSMatthew G Knepley @*/
45080adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
45090adebc6cSBarry Smith {
4510057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4511057b4bcdSMatthew G Knepley 
4512057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4513057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4514b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4515057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
451633907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4517057b4bcdSMatthew G Knepley   dm->sf = sf;
4518057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4519057b4bcdSMatthew G Knepley }
4520057b4bcdSMatthew G Knepley 
452134aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
452234aa8a36SMatthew G. Knepley {
452334aa8a36SMatthew G. Knepley   PetscClassId   id;
452434aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
452534aa8a36SMatthew G. Knepley 
452634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
452734aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
452834aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
452934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
453034aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
453134aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
453217c1d62eSMatthew G. Knepley   } else {
453317c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
453434aa8a36SMatthew G. Knepley   }
453534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
453634aa8a36SMatthew G. Knepley }
453734aa8a36SMatthew G. Knepley 
453844a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
453944a7f3ddSMatthew G. Knepley {
454044a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
454144a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
454244a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
454344a7f3ddSMatthew G. Knepley 
454444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
454544a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
454644a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
454744a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
454844a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
454944a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
455044a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
455144a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
455244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
455344a7f3ddSMatthew G. Knepley }
455444a7f3ddSMatthew G. Knepley 
455544a7f3ddSMatthew G. Knepley /*@
455644a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
455744a7f3ddSMatthew G. Knepley 
4558d083f849SBarry Smith   Logically collective on dm
455944a7f3ddSMatthew G. Knepley 
456044a7f3ddSMatthew G. Knepley   Input Parameter:
456144a7f3ddSMatthew G. Knepley . dm - The DM
456244a7f3ddSMatthew G. Knepley 
456344a7f3ddSMatthew G. Knepley   Level: intermediate
456444a7f3ddSMatthew G. Knepley 
456544a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
456644a7f3ddSMatthew G. Knepley @*/
456744a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
456844a7f3ddSMatthew G. Knepley {
456944a7f3ddSMatthew G. Knepley   PetscInt       f;
457044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
457144a7f3ddSMatthew G. Knepley 
457244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
457344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
457444a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
457544a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
457644a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
457744a7f3ddSMatthew G. Knepley   }
457844a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
457944a7f3ddSMatthew G. Knepley   dm->fields = NULL;
458044a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
458144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
458244a7f3ddSMatthew G. Knepley }
458344a7f3ddSMatthew G. Knepley 
4584689b5837SMatthew G. Knepley /*@
4585689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4586689b5837SMatthew G. Knepley 
4587689b5837SMatthew G. Knepley   Not collective
4588689b5837SMatthew G. Knepley 
4589689b5837SMatthew G. Knepley   Input Parameter:
4590689b5837SMatthew G. Knepley . dm - The DM
4591689b5837SMatthew G. Knepley 
4592689b5837SMatthew G. Knepley   Output Parameter:
4593689b5837SMatthew G. Knepley . Nf - The number of fields
4594689b5837SMatthew G. Knepley 
4595689b5837SMatthew G. Knepley   Level: intermediate
4596689b5837SMatthew G. Knepley 
4597689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4598689b5837SMatthew G. Knepley @*/
45990f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
46000f21e855SMatthew G. Knepley {
46010f21e855SMatthew G. Knepley   PetscFunctionBegin;
46020f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4603534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
460444a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4605af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4606af122d2aSMatthew G Knepley }
4607af122d2aSMatthew G Knepley 
4608689b5837SMatthew G. Knepley /*@
4609689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4610689b5837SMatthew G. Knepley 
4611d083f849SBarry Smith   Logically collective on dm
4612689b5837SMatthew G. Knepley 
4613689b5837SMatthew G. Knepley   Input Parameters:
4614689b5837SMatthew G. Knepley + dm - The DM
4615689b5837SMatthew G. Knepley - Nf - The number of fields
4616689b5837SMatthew G. Knepley 
4617689b5837SMatthew G. Knepley   Level: intermediate
4618689b5837SMatthew G. Knepley 
4619689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4620689b5837SMatthew G. Knepley @*/
4621af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4622af122d2aSMatthew G Knepley {
46230f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4624af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4625af122d2aSMatthew G Knepley 
4626af122d2aSMatthew G Knepley   PetscFunctionBegin;
4627af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
462844a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
46290f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
46300f21e855SMatthew G. Knepley     PetscContainer obj;
46310f21e855SMatthew G. Knepley 
46320f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
463344a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
46340f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4635af122d2aSMatthew G Knepley   }
4636af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4637af122d2aSMatthew G Knepley }
4638af122d2aSMatthew G Knepley 
4639c1929be8SMatthew G. Knepley /*@
4640c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4641c1929be8SMatthew G. Knepley 
4642c1929be8SMatthew G. Knepley   Not collective
4643c1929be8SMatthew G. Knepley 
4644c1929be8SMatthew G. Knepley   Input Parameters:
4645c1929be8SMatthew G. Knepley + dm - The DM
4646c1929be8SMatthew G. Knepley - f  - The field number
4647c1929be8SMatthew G. Knepley 
464844a7f3ddSMatthew G. Knepley   Output Parameters:
464944a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
465044a7f3ddSMatthew G. Knepley - field - The discretization object
4651c1929be8SMatthew G. Knepley 
465244a7f3ddSMatthew G. Knepley   Level: intermediate
4653c1929be8SMatthew G. Knepley 
465444a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4655c1929be8SMatthew G. Knepley @*/
465644a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4657af122d2aSMatthew G Knepley {
4658af122d2aSMatthew G Knepley   PetscFunctionBegin;
4659af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
466044a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
466144a7f3ddSMatthew 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);
466244a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
466344a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4664decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4665decb47aaSMatthew G. Knepley }
4666decb47aaSMatthew G. Knepley 
4667c1929be8SMatthew G. Knepley /*@
4668c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4669c1929be8SMatthew G. Knepley 
4670d083f849SBarry Smith   Logically collective on dm
4671c1929be8SMatthew G. Knepley 
4672c1929be8SMatthew G. Knepley   Input Parameters:
4673c1929be8SMatthew G. Knepley + dm    - The DM
4674c1929be8SMatthew G. Knepley . f     - The field number
467544a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4676c1929be8SMatthew G. Knepley - field - The discretization object
4677c1929be8SMatthew G. Knepley 
467844a7f3ddSMatthew G. Knepley   Level: intermediate
4679c1929be8SMatthew G. Knepley 
468044a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4681c1929be8SMatthew G. Knepley @*/
468244a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4683decb47aaSMatthew G. Knepley {
4684decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4685decb47aaSMatthew G. Knepley 
4686decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4687decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4688e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
468944a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4690e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
469144a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
469244a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
469344a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
469444a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
469544a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
469644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
469744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
469834aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
46992df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
470044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
470144a7f3ddSMatthew G. Knepley }
470244a7f3ddSMatthew G. Knepley 
470344a7f3ddSMatthew G. Knepley /*@
470444a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
470544a7f3ddSMatthew G. Knepley 
4706d083f849SBarry Smith   Logically collective on dm
470744a7f3ddSMatthew G. Knepley 
470844a7f3ddSMatthew G. Knepley   Input Parameters:
470944a7f3ddSMatthew G. Knepley + dm    - The DM
471044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
471144a7f3ddSMatthew G. Knepley - field - The discretization object
471244a7f3ddSMatthew G. Knepley 
471344a7f3ddSMatthew G. Knepley   Level: intermediate
471444a7f3ddSMatthew G. Knepley 
471544a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
471644a7f3ddSMatthew G. Knepley @*/
471744a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
471844a7f3ddSMatthew G. Knepley {
471944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
472044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
472144a7f3ddSMatthew G. Knepley 
472244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
472344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
472444a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
472544a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
472644a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
472744a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
472844a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
472944a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
473044a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
473134aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
47322df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4733af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4734af122d2aSMatthew G Knepley }
47356636e97aSMatthew G Knepley 
4736e5e52638SMatthew G. Knepley /*@
4737e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4738e5e52638SMatthew G. Knepley 
4739d083f849SBarry Smith   Collective on dm
4740e5e52638SMatthew G. Knepley 
4741e5e52638SMatthew G. Knepley   Input Parameter:
4742e5e52638SMatthew G. Knepley . dm - The DM
4743e5e52638SMatthew G. Knepley 
4744e5e52638SMatthew G. Knepley   Output Parameter:
4745e5e52638SMatthew G. Knepley . newdm - The DM
4746e5e52638SMatthew G. Knepley 
4747e5e52638SMatthew G. Knepley   Level: advanced
4748e5e52638SMatthew G. Knepley 
4749e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4750e5e52638SMatthew G. Knepley @*/
4751e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4752e5e52638SMatthew G. Knepley {
4753e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4754e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4755e5e52638SMatthew G. Knepley 
4756e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4757e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4758e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4759e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4760e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4761e5e52638SMatthew G. Knepley     DMLabel     label;
4762e5e52638SMatthew G. Knepley     PetscObject field;
476334aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4764e5e52638SMatthew G. Knepley 
4765e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4766e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
476734aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
476834aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
476934aa8a36SMatthew G. Knepley   }
477034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
477134aa8a36SMatthew G. Knepley }
477234aa8a36SMatthew G. Knepley 
477334aa8a36SMatthew G. Knepley /*@
477434aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
477534aa8a36SMatthew G. Knepley 
477634aa8a36SMatthew G. Knepley   Not collective
477734aa8a36SMatthew G. Knepley 
477834aa8a36SMatthew G. Knepley   Input Parameters:
477934aa8a36SMatthew G. Knepley + dm - The DM object
478034aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
478134aa8a36SMatthew G. Knepley 
478234aa8a36SMatthew G. Knepley   Output Parameter:
478334aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
478434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
478534aa8a36SMatthew G. Knepley 
478634aa8a36SMatthew G. Knepley   Notes:
478734aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
478834aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
478934aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4790979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
479134aa8a36SMatthew G. Knepley 
479234aa8a36SMatthew G. Knepley   Level: developer
479334aa8a36SMatthew G. Knepley 
479434aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
479534aa8a36SMatthew G. Knepley @*/
479634aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
479734aa8a36SMatthew G. Knepley {
479834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
479934aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4800534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4801534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
480234aa8a36SMatthew G. Knepley   if (f < 0) {
480334aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
480434aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
480534aa8a36SMatthew G. Knepley   } else {
480634aa8a36SMatthew G. Knepley     PetscInt       Nf;
480734aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
480834aa8a36SMatthew G. Knepley 
480934aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
481034aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
481134aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
481234aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
481334aa8a36SMatthew G. Knepley   }
481434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
481534aa8a36SMatthew G. Knepley }
481634aa8a36SMatthew G. Knepley 
481734aa8a36SMatthew G. Knepley /*@
481834aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
481934aa8a36SMatthew G. Knepley 
482034aa8a36SMatthew G. Knepley   Not collective
482134aa8a36SMatthew G. Knepley 
482234aa8a36SMatthew G. Knepley   Input Parameters:
482334aa8a36SMatthew G. Knepley + dm         - The DM object
482434aa8a36SMatthew G. Knepley . f          - The field number
482534aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
482634aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
482734aa8a36SMatthew G. Knepley 
482834aa8a36SMatthew G. Knepley   Notes:
482934aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
483034aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
483134aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4832979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
483334aa8a36SMatthew G. Knepley 
483434aa8a36SMatthew G. Knepley   Level: developer
483534aa8a36SMatthew G. Knepley 
483634aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
483734aa8a36SMatthew G. Knepley @*/
483834aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
483934aa8a36SMatthew G. Knepley {
484034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
484134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
484234aa8a36SMatthew G. Knepley   if (f < 0) {
484334aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
484434aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
484534aa8a36SMatthew G. Knepley   } else {
484634aa8a36SMatthew G. Knepley     PetscInt       Nf;
484734aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
484834aa8a36SMatthew G. Knepley 
484934aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
485034aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
485134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
485234aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4853e5e52638SMatthew G. Knepley   }
4854e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4855e5e52638SMatthew G. Knepley }
4856e5e52638SMatthew G. Knepley 
4857b0441da4SMatthew G. Knepley /*@
4858b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4859b0441da4SMatthew G. Knepley 
4860b0441da4SMatthew G. Knepley   Not collective
4861b0441da4SMatthew G. Knepley 
4862b0441da4SMatthew G. Knepley   Input Parameters:
4863b0441da4SMatthew G. Knepley . dm - The DM object
4864b0441da4SMatthew G. Knepley 
4865b0441da4SMatthew G. Knepley   Output Parameter:
4866b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4867b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4868b0441da4SMatthew G. Knepley 
4869b0441da4SMatthew G. Knepley   Notes:
4870b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4871b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4872b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4873b0441da4SMatthew G. Knepley 
4874b0441da4SMatthew G. Knepley   Level: developer
4875b0441da4SMatthew G. Knepley 
4876b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4877b0441da4SMatthew G. Knepley @*/
4878b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4879b0441da4SMatthew G. Knepley {
4880b0441da4SMatthew G. Knepley   PetscInt       Nf;
4881b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4882b0441da4SMatthew G. Knepley 
4883b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4884b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4885534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4886534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4887b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4888b0441da4SMatthew G. Knepley   if (!Nf) {
4889b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4890b0441da4SMatthew G. Knepley   } else {
4891b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4892b0441da4SMatthew G. Knepley   }
4893b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4894b0441da4SMatthew G. Knepley }
4895b0441da4SMatthew G. Knepley 
4896b0441da4SMatthew G. Knepley /*@
4897b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4898b0441da4SMatthew G. Knepley 
4899b0441da4SMatthew G. Knepley   Not collective
4900b0441da4SMatthew G. Knepley 
4901b0441da4SMatthew G. Knepley   Input Parameters:
4902b0441da4SMatthew G. Knepley + dm         - The DM object
4903b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4904b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4905b0441da4SMatthew G. Knepley 
4906b0441da4SMatthew G. Knepley   Notes:
4907b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4908b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4909b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4910b0441da4SMatthew G. Knepley 
4911b0441da4SMatthew G. Knepley   Level: developer
4912b0441da4SMatthew G. Knepley 
4913b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4914b0441da4SMatthew G. Knepley @*/
4915b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4916b0441da4SMatthew G. Knepley {
4917b0441da4SMatthew G. Knepley   PetscInt       Nf;
4918b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4919b0441da4SMatthew G. Knepley 
4920b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4921b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4922b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4923b0441da4SMatthew G. Knepley   if (!Nf) {
4924b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4925b0441da4SMatthew G. Knepley   } else {
4926b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4927e5e52638SMatthew G. Knepley   }
4928e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4929e5e52638SMatthew G. Knepley }
4930e5e52638SMatthew G. Knepley 
4931e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4932e5e52638SMatthew G. Knepley {
4933e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4934e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4935e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4936e5e52638SMatthew G. Knepley 
4937e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4938e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4939e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4940e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4941b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4942e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4943e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4944e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4945e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4946e5e52638SMatthew G. Knepley }
4947e5e52638SMatthew G. Knepley 
4948e5e52638SMatthew G. Knepley /*@
4949e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4950e5e52638SMatthew G. Knepley 
4951e5e52638SMatthew G. Knepley   Not collective
4952e5e52638SMatthew G. Knepley 
4953e5e52638SMatthew G. Knepley   Input Parameter:
4954e5e52638SMatthew G. Knepley . dm - The DM
4955e5e52638SMatthew G. Knepley 
4956e5e52638SMatthew G. Knepley   Output Parameter:
4957e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4958e5e52638SMatthew G. Knepley 
4959e5e52638SMatthew G. Knepley   Level: intermediate
4960e5e52638SMatthew G. Knepley 
4961e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4962e5e52638SMatthew G. Knepley @*/
4963e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4964e5e52638SMatthew G. Knepley {
4965e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4966e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4967534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
4968e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4969e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4970e5e52638SMatthew G. Knepley }
4971e5e52638SMatthew G. Knepley 
4972e5e52638SMatthew G. Knepley /*@
4973e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4974e5e52638SMatthew G. Knepley 
4975d083f849SBarry Smith   Logically collective on dm
4976e5e52638SMatthew G. Knepley 
4977e5e52638SMatthew G. Knepley   Input Parameter:
4978e5e52638SMatthew G. Knepley . dm - The DM
4979e5e52638SMatthew G. Knepley 
4980e5e52638SMatthew G. Knepley   Level: intermediate
4981e5e52638SMatthew G. Knepley 
4982e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4983e5e52638SMatthew G. Knepley @*/
4984e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4985e5e52638SMatthew G. Knepley {
4986e5e52638SMatthew G. Knepley   PetscInt       s;
4987e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4988e5e52638SMatthew G. Knepley 
4989e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4990e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4991e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4992e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4993e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4994b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4995e5e52638SMatthew G. Knepley   }
4996e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4997e5e52638SMatthew G. Knepley   dm->probs = NULL;
4998e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4999e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5000e5e52638SMatthew G. Knepley }
5001e5e52638SMatthew G. Knepley 
5002e5e52638SMatthew G. Knepley /*@
5003e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5004e5e52638SMatthew G. Knepley 
5005e5e52638SMatthew G. Knepley   Not collective
5006e5e52638SMatthew G. Knepley 
5007e5e52638SMatthew G. Knepley   Input Parameter:
5008e5e52638SMatthew G. Knepley . dm    - The DM
5009e5e52638SMatthew G. Knepley 
5010e5e52638SMatthew G. Knepley   Output Parameter:
5011e5e52638SMatthew G. Knepley . prob - The default PetscDS
5012e5e52638SMatthew G. Knepley 
5013e5e52638SMatthew G. Knepley   Level: intermediate
5014e5e52638SMatthew G. Knepley 
5015e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5016e5e52638SMatthew G. Knepley @*/
5017e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5018e5e52638SMatthew G. Knepley {
5019b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5020b0143b4dSMatthew G. Knepley 
5021e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5022e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5023e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5024b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5025b0143b4dSMatthew G. Knepley     PetscDS ds;
5026b0143b4dSMatthew G. Knepley 
5027b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5028b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5029b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5030b0143b4dSMatthew G. Knepley   }
5031b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5032e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5033e5e52638SMatthew G. Knepley }
5034e5e52638SMatthew G. Knepley 
5035e5e52638SMatthew G. Knepley /*@
5036e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5037e5e52638SMatthew G. Knepley 
5038e5e52638SMatthew G. Knepley   Not collective
5039e5e52638SMatthew G. Knepley 
5040e5e52638SMatthew G. Knepley   Input Parameters:
5041e5e52638SMatthew G. Knepley + dm    - The DM
5042e5e52638SMatthew G. Knepley - point - Cell for the DS
5043e5e52638SMatthew G. Knepley 
5044e5e52638SMatthew G. Knepley   Output Parameter:
5045e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5046e5e52638SMatthew G. Knepley 
5047e5e52638SMatthew G. Knepley   Level: developer
5048e5e52638SMatthew G. Knepley 
5049b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5050e5e52638SMatthew G. Knepley @*/
5051e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5052e5e52638SMatthew G. Knepley {
5053e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5054e5e52638SMatthew G. Knepley   PetscInt       s;
5055e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5056e5e52638SMatthew G. Knepley 
5057e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5058e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5059e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5060e5e52638SMatthew G. Knepley   *prob = NULL;
5061e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5062e5e52638SMatthew G. Knepley     PetscInt val;
5063e5e52638SMatthew G. Knepley 
5064e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5065e5e52638SMatthew G. Knepley     else {
5066e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5067e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5068e5e52638SMatthew G. Knepley     }
5069e5e52638SMatthew G. Knepley   }
5070e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5071e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5072e5e52638SMatthew G. Knepley }
5073e5e52638SMatthew G. Knepley 
5074e5e52638SMatthew G. Knepley /*@
5075e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5076e5e52638SMatthew G. Knepley 
5077e5e52638SMatthew G. Knepley   Not collective
5078e5e52638SMatthew G. Knepley 
5079e5e52638SMatthew G. Knepley   Input Parameters:
5080e5e52638SMatthew G. Knepley + dm    - The DM
5081e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5082e5e52638SMatthew G. Knepley 
5083b3cf3223SMatthew G. Knepley   Output Parameters:
5084b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5085b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5086e5e52638SMatthew G. Knepley 
5087e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5088e5e52638SMatthew G. Knepley 
5089e5e52638SMatthew G. Knepley   Level: advanced
5090e5e52638SMatthew G. Knepley 
5091e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5092e5e52638SMatthew G. Knepley @*/
5093b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5094e5e52638SMatthew G. Knepley {
5095e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5096e5e52638SMatthew G. Knepley 
5097e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5098e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5099e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5100b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5101b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5102e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5103b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5104b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5105b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5106b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5107b3cf3223SMatthew G. Knepley     }
5108e5e52638SMatthew G. Knepley   }
51092df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5110e5e52638SMatthew G. Knepley }
5111e5e52638SMatthew G. Knepley 
5112e5e52638SMatthew G. Knepley /*@
5113e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5114e5e52638SMatthew G. Knepley 
5115e5e52638SMatthew G. Knepley   Not collective
5116e5e52638SMatthew G. Knepley 
5117e5e52638SMatthew G. Knepley   Input Parameters:
5118e5e52638SMatthew G. Knepley + dm  - The DM
5119e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5120e5e52638SMatthew G. Knepley 
5121e5e52638SMatthew G. Knepley   Output Parameters:
5122b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5123b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5124b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
5125e5e52638SMatthew G. Knepley 
5126e5e52638SMatthew G. Knepley   Level: advanced
5127e5e52638SMatthew G. Knepley 
5128e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5129e5e52638SMatthew G. Knepley @*/
5130b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5131e5e52638SMatthew G. Knepley {
5132e5e52638SMatthew G. Knepley   PetscInt       Nds;
5133e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5134e5e52638SMatthew G. Knepley 
5135e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5136e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5137e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5138e5e52638SMatthew 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);
5139e5e52638SMatthew G. Knepley   if (label) {
5140e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5141e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5142e5e52638SMatthew G. Knepley   }
5143b3cf3223SMatthew G. Knepley   if (fields) {
5144b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5145b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5146b3cf3223SMatthew G. Knepley   }
5147e5e52638SMatthew G. Knepley   if (ds) {
5148b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5149e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5150e5e52638SMatthew G. Knepley   }
5151e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5152e5e52638SMatthew G. Knepley }
5153e5e52638SMatthew G. Knepley 
5154e5e52638SMatthew G. Knepley /*@
5155e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5156e5e52638SMatthew G. Knepley 
5157d083f849SBarry Smith   Collective on dm
5158e5e52638SMatthew G. Knepley 
5159e5e52638SMatthew G. Knepley   Input Parameters:
5160e5e52638SMatthew G. Knepley + dm     - The DM
5161e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5162b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5163e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5164e5e52638SMatthew G. Knepley 
5165b3cf3223SMatthew 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,
5166b3cf3223SMatthew G. Knepley   the fields argument is ignored.
5167e5e52638SMatthew G. Knepley 
5168e5e52638SMatthew G. Knepley   Level: advanced
5169e5e52638SMatthew G. Knepley 
5170e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
5171e5e52638SMatthew G. Knepley @*/
5172b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5173e5e52638SMatthew G. Knepley {
5174e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5175e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5176e5e52638SMatthew G. Knepley 
5177e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5178e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5179e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5180e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5181e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5182e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
5183b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5184e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
5185e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
5186e5e52638SMatthew G. Knepley     }
5187e5e52638SMatthew G. Knepley   }
51881b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5189e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5190b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5191e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5192e5e52638SMatthew G. Knepley   if (!label) {
5193e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5194e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5195e5e52638SMatthew G. Knepley     Nds = 0;
5196e5e52638SMatthew G. Knepley   }
5197e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
5198b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5199e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5200e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5201e5e52638SMatthew G. Knepley }
5202e5e52638SMatthew G. Knepley 
5203e5e52638SMatthew G. Knepley /*@
5204e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5205e5e52638SMatthew G. Knepley 
5206d083f849SBarry Smith   Collective on dm
5207e5e52638SMatthew G. Knepley 
5208e5e52638SMatthew G. Knepley   Input Parameter:
5209e5e52638SMatthew G. Knepley . dm - The DM
5210e5e52638SMatthew G. Knepley 
5211e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5212e5e52638SMatthew G. Knepley 
5213e5e52638SMatthew G. Knepley   Level: intermediate
5214e5e52638SMatthew G. Knepley 
5215e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5216e5e52638SMatthew G. Knepley @*/
5217e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5218e5e52638SMatthew G. Knepley {
5219e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5220e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5221b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5222e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5223e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5224e5e52638SMatthew G. Knepley 
5225e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5226e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5227e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5228e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5229e5e52638SMatthew G. Knepley    1) NULL
5230e5e52638SMatthew G. Knepley    2) Hybrid cells
5231e5e52638SMatthew G. Knepley   */
5232e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5233e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5234e5e52638SMatthew G. Knepley   /* Create default DS */
5235b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
52362df9ee95SMatthew G. Knepley   if (!prob) {
5237b3cf3223SMatthew G. Knepley     IS        fields;
5238b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5239b3cf3223SMatthew G. Knepley 
5240b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5241b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5242b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
524388f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
524488f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
524588f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
524688f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
524788f0c812SMatthew G. Knepley 
52482df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5249b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
52502df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5251b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5252b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
52532df9ee95SMatthew G. Knepley   }
5254e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5255e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5256b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5257e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5258e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5259e5e52638SMatthew G. Knepley 
5260e5e52638SMatthew G. Knepley     if (label) {
52610122748bSMatthew G. Knepley       DM        plex;
5262a2d47024SMatthew G. Knepley       IS        fields;
5263a2d47024SMatthew G. Knepley       PetscInt *fld;
52640122748bSMatthew G. Knepley       PetscInt  depth, pMax[4];
52650122748bSMatthew G. Knepley 
52660122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
52670122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
52680122748bSMatthew G. Knepley       ierr = DMPlexGetHybridBounds(plex, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
52690122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
52700122748bSMatthew G. Knepley 
5271e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5272e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
5273e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5274a2d47024SMatthew G. Knepley       ierr = PetscMalloc1(1, &fld);CHKERRQ(ierr);
5275a2d47024SMatthew G. Knepley       fld[0] = f;
5276a2d47024SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5277a2d47024SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5278a2d47024SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5279a2d47024SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, 1, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5280a2d47024SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, fields, probh);CHKERRQ(ierr);
5281a2d47024SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
5282e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5283e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5284e5e52638SMatthew G. Knepley       break;
5285e5e52638SMatthew G. Knepley     }
5286e5e52638SMatthew G. Knepley   }
5287e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5288b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5289e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5290e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5291e5e52638SMatthew G. Knepley 
5292e5e52638SMatthew G. Knepley     if (!label) {
5293e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5294e5e52638SMatthew G. Knepley       if (probh) {
5295e5e52638SMatthew G. Knepley         PetscFE subfe;
5296e5e52638SMatthew G. Knepley 
5297e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5298e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5299e5e52638SMatthew G. Knepley       }
5300e5e52638SMatthew G. Knepley     } else {
5301e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5302e5e52638SMatthew G. Knepley     }
5303e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5304e5e52638SMatthew G. Knepley     {
5305e5e52638SMatthew G. Knepley       PetscClassId id;
5306e5e52638SMatthew G. Knepley 
5307e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5308e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5309e5e52638SMatthew G. Knepley     }
5310e5e52638SMatthew G. Knepley   }
5311e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5312e5e52638SMatthew G. Knepley   /* Setup DSes */
5313e5e52638SMatthew G. Knepley   if (doSetup) {
5314e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5315e5e52638SMatthew G. Knepley   }
5316e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5317e5e52638SMatthew G. Knepley }
5318e5e52638SMatthew G. Knepley 
5319e5e52638SMatthew G. Knepley /*@
5320e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5321e5e52638SMatthew G. Knepley 
5322d083f849SBarry Smith   Collective on dm
5323e5e52638SMatthew G. Knepley 
5324e5e52638SMatthew G. Knepley   Input Parameter:
5325e5e52638SMatthew G. Knepley . dm - The DM
5326e5e52638SMatthew G. Knepley 
5327e5e52638SMatthew G. Knepley   Output Parameter:
5328e5e52638SMatthew G. Knepley . newdm - The DM
5329e5e52638SMatthew G. Knepley 
5330e5e52638SMatthew G. Knepley   Level: advanced
5331e5e52638SMatthew G. Knepley 
5332e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5333e5e52638SMatthew G. Knepley @*/
5334e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5335e5e52638SMatthew G. Knepley {
5336e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5337e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5338e5e52638SMatthew G. Knepley 
5339e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5340e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5341e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5342e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5343e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5344e5e52638SMatthew G. Knepley     DMLabel label;
5345b3cf3223SMatthew G. Knepley     IS      fields;
5346e5e52638SMatthew G. Knepley     PetscDS ds;
5347e5e52638SMatthew G. Knepley 
5348b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5349b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5350e5e52638SMatthew G. Knepley   }
5351e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5352e5e52638SMatthew G. Knepley }
5353e5e52638SMatthew G. Knepley 
5354e5e52638SMatthew G. Knepley /*@
5355e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5356e5e52638SMatthew G. Knepley 
5357d083f849SBarry Smith   Collective on dm
5358e5e52638SMatthew G. Knepley 
5359e5e52638SMatthew G. Knepley   Input Parameter:
5360e5e52638SMatthew G. Knepley . dm - The DM
5361e5e52638SMatthew G. Knepley 
5362e5e52638SMatthew G. Knepley   Output Parameter:
5363e5e52638SMatthew G. Knepley . newdm - The DM
5364e5e52638SMatthew G. Knepley 
5365e5e52638SMatthew G. Knepley   Level: advanced
5366e5e52638SMatthew G. Knepley 
5367e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5368e5e52638SMatthew G. Knepley @*/
5369e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5370e5e52638SMatthew G. Knepley {
5371e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5372e5e52638SMatthew G. Knepley 
5373e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5374e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5375e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5376e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5377e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5378e5e52638SMatthew G. Knepley }
5379e5e52638SMatthew G. Knepley 
5380b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5381b64e0483SPeter Brune {
5382b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5383b64e0483SPeter Brune   PetscErrorCode ierr;
5384b64e0483SPeter Brune   Vec coords,ccoords;
53856dbf9973SLawrence Mitchell   Mat inject;
5386b64e0483SPeter Brune   PetscFunctionBegin;
5387b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5388b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5389b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5390b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5391b64e0483SPeter Brune   if (coords && !ccoords) {
5392b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
53936668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
53946dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
53952adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
53966dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5397b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5398b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5399b64e0483SPeter Brune   }
5400b64e0483SPeter Brune   PetscFunctionReturn(0);
5401b64e0483SPeter Brune }
5402b64e0483SPeter Brune 
540303dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
540403dadc2fSPeter Brune {
540503dadc2fSPeter Brune   DM dm_coord,subdm_coord;
540603dadc2fSPeter Brune   PetscErrorCode ierr;
540703dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
540803dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
540903dadc2fSPeter Brune   PetscFunctionBegin;
541003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
541103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
541203dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
541303dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
541403dadc2fSPeter Brune   if (coords && !ccoords) {
541503dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
54166668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
541703dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
541824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
541903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
542003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
542103dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
54221ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
542303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
542403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
542503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
542603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
542703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
542803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
542903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
543003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
543103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
543203dadc2fSPeter Brune   }
543303dadc2fSPeter Brune   PetscFunctionReturn(0);
543403dadc2fSPeter Brune }
543503dadc2fSPeter Brune 
5436c73cfb54SMatthew G. Knepley /*@
5437c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5438c73cfb54SMatthew G. Knepley 
5439c73cfb54SMatthew G. Knepley   Not collective
5440c73cfb54SMatthew G. Knepley 
5441c73cfb54SMatthew G. Knepley   Input Parameter:
5442c73cfb54SMatthew G. Knepley . dm - The DM
5443c73cfb54SMatthew G. Knepley 
5444c73cfb54SMatthew G. Knepley   Output Parameter:
5445c73cfb54SMatthew G. Knepley . dim - The topological dimension
5446c73cfb54SMatthew G. Knepley 
5447c73cfb54SMatthew G. Knepley   Level: beginner
5448c73cfb54SMatthew G. Knepley 
5449c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5450c73cfb54SMatthew G. Knepley @*/
5451c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5452c73cfb54SMatthew G. Knepley {
5453c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5454c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5455534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5456c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5457c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5458c73cfb54SMatthew G. Knepley }
5459c73cfb54SMatthew G. Knepley 
5460c73cfb54SMatthew G. Knepley /*@
5461c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5462c73cfb54SMatthew G. Knepley 
5463c73cfb54SMatthew G. Knepley   Collective on dm
5464c73cfb54SMatthew G. Knepley 
5465c73cfb54SMatthew G. Knepley   Input Parameters:
5466c73cfb54SMatthew G. Knepley + dm - The DM
5467c73cfb54SMatthew G. Knepley - dim - The topological dimension
5468c73cfb54SMatthew G. Knepley 
5469c73cfb54SMatthew G. Knepley   Level: beginner
5470c73cfb54SMatthew G. Knepley 
5471c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5472c73cfb54SMatthew G. Knepley @*/
5473c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5474c73cfb54SMatthew G. Knepley {
5475e5e52638SMatthew G. Knepley   PetscDS        ds;
5476f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5477f17e8794SMatthew G. Knepley 
5478c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5479c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5480c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5481c73cfb54SMatthew G. Knepley   dm->dim = dim;
5482e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5483e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5484c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5485c73cfb54SMatthew G. Knepley }
5486c73cfb54SMatthew G. Knepley 
5487793f3fe5SMatthew G. Knepley /*@
5488793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5489793f3fe5SMatthew G. Knepley 
5490d083f849SBarry Smith   Collective on dm
5491793f3fe5SMatthew G. Knepley 
5492793f3fe5SMatthew G. Knepley   Input Parameters:
5493793f3fe5SMatthew G. Knepley + dm - the DM
5494793f3fe5SMatthew G. Knepley - dim - the dimension
5495793f3fe5SMatthew G. Knepley 
5496793f3fe5SMatthew G. Knepley   Output Parameters:
5497793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5498aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5499793f3fe5SMatthew G. Knepley 
5500793f3fe5SMatthew G. Knepley   Note:
5501793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5502a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5503793f3fe5SMatthew G. Knepley   then the interval is empty.
5504793f3fe5SMatthew G. Knepley 
5505793f3fe5SMatthew G. Knepley   Level: intermediate
5506793f3fe5SMatthew G. Knepley 
5507793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5508793f3fe5SMatthew G. Knepley @*/
5509793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5510793f3fe5SMatthew G. Knepley {
5511793f3fe5SMatthew G. Knepley   PetscInt       d;
5512793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5513793f3fe5SMatthew G. Knepley 
5514793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5515793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5516793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5517793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5518b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5519793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5520793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5521793f3fe5SMatthew G. Knepley }
5522793f3fe5SMatthew G. Knepley 
55236636e97aSMatthew G Knepley /*@
55246636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
55256636e97aSMatthew G Knepley 
5526d083f849SBarry Smith   Collective on dm
55276636e97aSMatthew G Knepley 
55286636e97aSMatthew G Knepley   Input Parameters:
55296636e97aSMatthew G Knepley + dm - the DM
55306636e97aSMatthew G Knepley - c - coordinate vector
55316636e97aSMatthew G Knepley 
55322dd40e9bSPatrick Sanan   Notes:
55332dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
55342dd40e9bSPatrick Sanan 
55352dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
55366636e97aSMatthew G Knepley 
55376636e97aSMatthew G Knepley   Level: intermediate
55386636e97aSMatthew G Knepley 
55392dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
55406636e97aSMatthew G Knepley @*/
55416636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
55426636e97aSMatthew G Knepley {
55436636e97aSMatthew G Knepley   PetscErrorCode ierr;
55446636e97aSMatthew G Knepley 
55456636e97aSMatthew G Knepley   PetscFunctionBegin;
55466636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55476636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55486636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55496636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55506636e97aSMatthew G Knepley   dm->coordinates = c;
55516636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5552b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
555303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
55546636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55556636e97aSMatthew G Knepley }
55566636e97aSMatthew G Knepley 
55576636e97aSMatthew G Knepley /*@
55586636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
55596636e97aSMatthew G Knepley 
55607058e716SVaclav Hapla   Not collective
55616636e97aSMatthew G Knepley 
55626636e97aSMatthew G Knepley    Input Parameters:
55636636e97aSMatthew G Knepley +  dm - the DM
55646636e97aSMatthew G Knepley -  c - coordinate vector
55656636e97aSMatthew G Knepley 
55662dd40e9bSPatrick Sanan   Notes:
55676636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
55686636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
55696636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
55706636e97aSMatthew G Knepley 
55712dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
55722dd40e9bSPatrick Sanan 
55736636e97aSMatthew G Knepley   Level: intermediate
55746636e97aSMatthew G Knepley 
55756636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
55766636e97aSMatthew G Knepley @*/
55776636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
55786636e97aSMatthew G Knepley {
55796636e97aSMatthew G Knepley   PetscErrorCode ierr;
55806636e97aSMatthew G Knepley 
55816636e97aSMatthew G Knepley   PetscFunctionBegin;
55826636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55836636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55846636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55856636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
55868865f1eaSKarl Rupp 
55876636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
55888865f1eaSKarl Rupp 
55896636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55906636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55916636e97aSMatthew G Knepley }
55926636e97aSMatthew G Knepley 
55936636e97aSMatthew G Knepley /*@
55946636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
55956636e97aSMatthew G Knepley 
5596d083f849SBarry Smith   Collective on dm
55976636e97aSMatthew G Knepley 
55986636e97aSMatthew G Knepley   Input Parameter:
55996636e97aSMatthew G Knepley . dm - the DM
56006636e97aSMatthew G Knepley 
56016636e97aSMatthew G Knepley   Output Parameter:
56026636e97aSMatthew G Knepley . c - global coordinate vector
56036636e97aSMatthew G Knepley 
56046636e97aSMatthew G Knepley   Note:
56056636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
56066636e97aSMatthew G Knepley 
56076636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
56086636e97aSMatthew G Knepley 
56096636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56106636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56116636e97aSMatthew G Knepley 
56126636e97aSMatthew G Knepley   Level: intermediate
56136636e97aSMatthew G Knepley 
56146636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
56156636e97aSMatthew G Knepley @*/
56166636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
56176636e97aSMatthew G Knepley {
56186636e97aSMatthew G Knepley   PetscErrorCode ierr;
56196636e97aSMatthew G Knepley 
56206636e97aSMatthew G Knepley   PetscFunctionBegin;
56216636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56226636e97aSMatthew G Knepley   PetscValidPointer(c,2);
56231f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
56240298fd71SBarry Smith     DM        cdm = NULL;
56251970a576SMatthew G. Knepley     PetscBool localized;
56266636e97aSMatthew G Knepley 
56276636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
56286636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
56291970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
56301970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
56311970a576SMatthew G. Knepley     if (localized) {
56321970a576SMatthew G. Knepley       PetscInt cdim;
56331970a576SMatthew G. Knepley 
56341970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
56351970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
56361970a576SMatthew G. Knepley     }
56376636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
56386636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
56396636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
56406636e97aSMatthew G Knepley   }
56416636e97aSMatthew G Knepley   *c = dm->coordinates;
56426636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56436636e97aSMatthew G Knepley }
56446636e97aSMatthew G Knepley 
56456636e97aSMatthew G Knepley /*@
564681e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
564781e9a530SVaclav Hapla 
5648d083f849SBarry Smith   Collective on dm
564981e9a530SVaclav Hapla 
565081e9a530SVaclav Hapla   Input Parameter:
565181e9a530SVaclav Hapla . dm - the DM
565281e9a530SVaclav Hapla 
565381e9a530SVaclav Hapla   Level: advanced
565481e9a530SVaclav Hapla 
565581e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
565681e9a530SVaclav Hapla @*/
565781e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
565881e9a530SVaclav Hapla {
565981e9a530SVaclav Hapla   PetscErrorCode ierr;
566081e9a530SVaclav Hapla 
566181e9a530SVaclav Hapla   PetscFunctionBegin;
566281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
566381e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
56641970a576SMatthew G. Knepley     DM        cdm = NULL;
56651970a576SMatthew G. Knepley     PetscBool localized;
56661970a576SMatthew G. Knepley 
566781e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
566881e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
56691970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
56701970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
56711970a576SMatthew G. Knepley     if (localized) {
56721970a576SMatthew G. Knepley       PetscInt cdim;
56731970a576SMatthew G. Knepley 
56741970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
56751970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
56761970a576SMatthew G. Knepley     }
567781e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
567881e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
567981e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
568081e9a530SVaclav Hapla   }
568181e9a530SVaclav Hapla   PetscFunctionReturn(0);
568281e9a530SVaclav Hapla }
568381e9a530SVaclav Hapla 
568481e9a530SVaclav Hapla /*@
56856636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
56866636e97aSMatthew G Knepley 
5687d083f849SBarry Smith   Collective on dm
56886636e97aSMatthew G Knepley 
56896636e97aSMatthew G Knepley   Input Parameter:
56906636e97aSMatthew G Knepley . dm - the DM
56916636e97aSMatthew G Knepley 
56926636e97aSMatthew G Knepley   Output Parameter:
56936636e97aSMatthew G Knepley . c - coordinate vector
56946636e97aSMatthew G Knepley 
56956636e97aSMatthew G Knepley   Note:
56966636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
56976636e97aSMatthew G Knepley 
56986636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
56996636e97aSMatthew G Knepley 
57006636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
57016636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
57026636e97aSMatthew G Knepley 
57036636e97aSMatthew G Knepley   Level: intermediate
57046636e97aSMatthew G Knepley 
570581e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
57066636e97aSMatthew G Knepley @*/
57076636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
57086636e97aSMatthew G Knepley {
57096636e97aSMatthew G Knepley   PetscErrorCode ierr;
57106636e97aSMatthew G Knepley 
57116636e97aSMatthew G Knepley   PetscFunctionBegin;
57126636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57136636e97aSMatthew G Knepley   PetscValidPointer(c,2);
571481e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
57156636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
57166636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57176636e97aSMatthew G Knepley }
57186636e97aSMatthew G Knepley 
571981e9a530SVaclav Hapla /*@
572081e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
572181e9a530SVaclav Hapla 
572281e9a530SVaclav Hapla   Not collective
572381e9a530SVaclav Hapla 
572481e9a530SVaclav Hapla   Input Parameter:
572581e9a530SVaclav Hapla . dm - the DM
572681e9a530SVaclav Hapla 
572781e9a530SVaclav Hapla   Output Parameter:
572881e9a530SVaclav Hapla . c - coordinate vector
572981e9a530SVaclav Hapla 
573081e9a530SVaclav Hapla   Level: advanced
573181e9a530SVaclav Hapla 
573281e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
573381e9a530SVaclav Hapla @*/
573481e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
573581e9a530SVaclav Hapla {
573681e9a530SVaclav Hapla   PetscFunctionBegin;
573781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
573881e9a530SVaclav Hapla   PetscValidPointer(c,2);
573981e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
57406636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
57416636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57426636e97aSMatthew G Knepley }
57436636e97aSMatthew G Knepley 
57442db98f8dSVaclav Hapla /*@
57452db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
57462db98f8dSVaclav Hapla 
57472db98f8dSVaclav Hapla   Not collective
57482db98f8dSVaclav Hapla 
57492db98f8dSVaclav Hapla   Input Parameter:
57502db98f8dSVaclav Hapla + dm - the DM
57512db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
57522db98f8dSVaclav Hapla 
57532db98f8dSVaclav Hapla   Output Parameter:
57542db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
57552db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
57562db98f8dSVaclav Hapla 
57572db98f8dSVaclav Hapla   Note:
57582db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
57592db98f8dSVaclav Hapla 
57602db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
57612db98f8dSVaclav Hapla 
57622db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
57632db98f8dSVaclav Hapla 
57642db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
57652db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
57662db98f8dSVaclav Hapla 
57672db98f8dSVaclav Hapla   Level: advanced
57682db98f8dSVaclav Hapla 
57692db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
57702db98f8dSVaclav Hapla @*/
57712db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
57722db98f8dSVaclav Hapla {
57732db98f8dSVaclav Hapla   PetscSection        cs, newcs;
57742db98f8dSVaclav Hapla   Vec                 coords;
57752db98f8dSVaclav Hapla   const PetscScalar   *arr;
57762db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
57772db98f8dSVaclav Hapla   PetscInt            n;
57782db98f8dSVaclav Hapla   PetscErrorCode      ierr;
57792db98f8dSVaclav Hapla 
57802db98f8dSVaclav Hapla   PetscFunctionBegin;
57812db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57822db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
57832db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
57842db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
57852db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
57861bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
57871bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
57882db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
57892db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
57902db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
57912db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
57922db98f8dSVaclav Hapla   if (pCoord) {
57932db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
57942db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
57952db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
57962db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5797ad9ac99dSVaclav Hapla   } else {
5798ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
57992db98f8dSVaclav Hapla   }
5800ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5801ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
58022db98f8dSVaclav Hapla   PetscFunctionReturn(0);
58032db98f8dSVaclav Hapla }
58042db98f8dSVaclav Hapla 
5805f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5806f19dbd58SToby Isaac {
5807f19dbd58SToby Isaac   PetscErrorCode ierr;
5808f19dbd58SToby Isaac 
5809f19dbd58SToby Isaac   PetscFunctionBegin;
5810f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5811f19dbd58SToby Isaac   PetscValidPointer(field,2);
5812f19dbd58SToby Isaac   if (!dm->coordinateField) {
5813f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5814f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5815f19dbd58SToby Isaac     }
5816f19dbd58SToby Isaac   }
5817f19dbd58SToby Isaac   *field = dm->coordinateField;
5818f19dbd58SToby Isaac   PetscFunctionReturn(0);
5819f19dbd58SToby Isaac }
5820f19dbd58SToby Isaac 
5821f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5822f19dbd58SToby Isaac {
5823f19dbd58SToby Isaac   PetscErrorCode ierr;
5824f19dbd58SToby Isaac 
5825f19dbd58SToby Isaac   PetscFunctionBegin;
5826f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5827f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5828c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5829f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5830f19dbd58SToby Isaac   dm->coordinateField = field;
5831f19dbd58SToby Isaac   PetscFunctionReturn(0);
5832f19dbd58SToby Isaac }
5833f19dbd58SToby Isaac 
58346636e97aSMatthew G Knepley /*@
58351cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
58366636e97aSMatthew G Knepley 
5837d083f849SBarry Smith   Collective on dm
58386636e97aSMatthew G Knepley 
58396636e97aSMatthew G Knepley   Input Parameter:
58406636e97aSMatthew G Knepley . dm - the DM
58416636e97aSMatthew G Knepley 
58426636e97aSMatthew G Knepley   Output Parameter:
58436636e97aSMatthew G Knepley . cdm - coordinate DM
58446636e97aSMatthew G Knepley 
58456636e97aSMatthew G Knepley   Level: intermediate
58466636e97aSMatthew G Knepley 
58471cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58486636e97aSMatthew G Knepley @*/
58496636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
58506636e97aSMatthew G Knepley {
58516636e97aSMatthew G Knepley   PetscErrorCode ierr;
58526636e97aSMatthew G Knepley 
58536636e97aSMatthew G Knepley   PetscFunctionBegin;
58546636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58556636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
58566636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5857308f8a94SToby Isaac     DM cdm;
5858308f8a94SToby Isaac 
585982f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5860308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5861308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5862308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5863308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5864308f8a94SToby Isaac     dm->coordinateDM = cdm;
58656636e97aSMatthew G Knepley   }
58666636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
58676636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58686636e97aSMatthew G Knepley }
5869e87bb0d3SMatthew G Knepley 
58701cfe2091SMatthew G. Knepley /*@
58711cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
58721cfe2091SMatthew G. Knepley 
5873d083f849SBarry Smith   Logically Collective on dm
58741cfe2091SMatthew G. Knepley 
58751cfe2091SMatthew G. Knepley   Input Parameters:
58761cfe2091SMatthew G. Knepley + dm - the DM
58771cfe2091SMatthew G. Knepley - cdm - coordinate DM
58781cfe2091SMatthew G. Knepley 
58791cfe2091SMatthew G. Knepley   Level: intermediate
58801cfe2091SMatthew G. Knepley 
58811cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58821cfe2091SMatthew G. Knepley @*/
58831cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
58841cfe2091SMatthew G. Knepley {
58851cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
58861cfe2091SMatthew G. Knepley 
58871cfe2091SMatthew G. Knepley   PetscFunctionBegin;
58881cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58891cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5890f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
58911cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
58921cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
58931cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
58941cfe2091SMatthew G. Knepley }
58951cfe2091SMatthew G. Knepley 
589646e270d4SMatthew G. Knepley /*@
589746e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
589846e270d4SMatthew G. Knepley 
589946e270d4SMatthew G. Knepley   Not Collective
590046e270d4SMatthew G. Knepley 
590146e270d4SMatthew G. Knepley   Input Parameter:
590246e270d4SMatthew G. Knepley . dm - The DM object
590346e270d4SMatthew G. Knepley 
590446e270d4SMatthew G. Knepley   Output Parameter:
590546e270d4SMatthew G. Knepley . dim - The embedding dimension
590646e270d4SMatthew G. Knepley 
590746e270d4SMatthew G. Knepley   Level: intermediate
590846e270d4SMatthew G. Knepley 
590992fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
591046e270d4SMatthew G. Knepley @*/
591146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
591246e270d4SMatthew G. Knepley {
591346e270d4SMatthew G. Knepley   PetscFunctionBegin;
591446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5915534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
59169a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
59179a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
59189a9a41abSToby Isaac   }
591946e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
592046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
592146e270d4SMatthew G. Knepley }
592246e270d4SMatthew G. Knepley 
592346e270d4SMatthew G. Knepley /*@
592446e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
592546e270d4SMatthew G. Knepley 
592646e270d4SMatthew G. Knepley   Not Collective
592746e270d4SMatthew G. Knepley 
592846e270d4SMatthew G. Knepley   Input Parameters:
592946e270d4SMatthew G. Knepley + dm  - The DM object
593046e270d4SMatthew G. Knepley - dim - The embedding dimension
593146e270d4SMatthew G. Knepley 
593246e270d4SMatthew G. Knepley   Level: intermediate
593346e270d4SMatthew G. Knepley 
593492fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
593546e270d4SMatthew G. Knepley @*/
593646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
593746e270d4SMatthew G. Knepley {
5938e5e52638SMatthew G. Knepley   PetscDS        ds;
5939f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5940f17e8794SMatthew G. Knepley 
594146e270d4SMatthew G. Knepley   PetscFunctionBegin;
594246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
594346e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5944e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5945e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
594646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
594746e270d4SMatthew G. Knepley }
594846e270d4SMatthew G. Knepley 
5949e8abe2deSMatthew G. Knepley /*@
5950e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5951e8abe2deSMatthew G. Knepley 
5952d083f849SBarry Smith   Collective on dm
5953e8abe2deSMatthew G. Knepley 
5954e8abe2deSMatthew G. Knepley   Input Parameter:
5955e8abe2deSMatthew G. Knepley . dm - The DM object
5956e8abe2deSMatthew G. Knepley 
5957e8abe2deSMatthew G. Knepley   Output Parameter:
5958e8abe2deSMatthew G. Knepley . section - The PetscSection object
5959e8abe2deSMatthew G. Knepley 
5960e8abe2deSMatthew G. Knepley   Level: intermediate
5961e8abe2deSMatthew G. Knepley 
596292fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
5963e8abe2deSMatthew G. Knepley @*/
5964e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5965e8abe2deSMatthew G. Knepley {
5966e8abe2deSMatthew G. Knepley   DM             cdm;
5967e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5968e8abe2deSMatthew G. Knepley 
5969e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5970e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5971e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5972e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
597392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
5974e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5975e8abe2deSMatthew G. Knepley }
5976e8abe2deSMatthew G. Knepley 
5977e8abe2deSMatthew G. Knepley /*@
5978e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5979e8abe2deSMatthew G. Knepley 
5980e8abe2deSMatthew G. Knepley   Not Collective
5981e8abe2deSMatthew G. Knepley 
5982e8abe2deSMatthew G. Knepley   Input Parameters:
5983e8abe2deSMatthew G. Knepley + dm      - The DM object
598446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5985e8abe2deSMatthew G. Knepley - section - The PetscSection object
5986e8abe2deSMatthew G. Knepley 
5987e8abe2deSMatthew G. Knepley   Level: intermediate
5988e8abe2deSMatthew G. Knepley 
598992fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
5990e8abe2deSMatthew G. Knepley @*/
599146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5992e8abe2deSMatthew G. Knepley {
5993e8abe2deSMatthew G. Knepley   DM             cdm;
5994e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5995e8abe2deSMatthew G. Knepley 
5996e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5997e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
599846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5999e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
600092fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
600146e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
60024c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
600346e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
600446e270d4SMatthew G. Knepley 
600546e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
600646e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
600746e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
600846e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
600946e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
601046e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
601146e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
601246e270d4SMatthew G. Knepley     }
6013ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
601446e270d4SMatthew G. Knepley   }
6015e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6016e8abe2deSMatthew G. Knepley }
6017e8abe2deSMatthew G. Knepley 
60185dc8c3f7SMatthew G. Knepley /*@C
601990b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
60205dc8c3f7SMatthew G. Knepley 
60215dc8c3f7SMatthew G. Knepley   Input Parameters:
602290b157c4SStefano Zampini . dm      - The DM object
602390b157c4SStefano Zampini 
602490b157c4SStefano Zampini   Output Parameters:
602590b157c4SStefano Zampini + per     - Whether the DM is periodic or not
60265dc8c3f7SMatthew 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
60275dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
60285dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
60295dc8c3f7SMatthew G. Knepley 
60305dc8c3f7SMatthew G. Knepley   Level: developer
60315dc8c3f7SMatthew G. Knepley 
60325dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
60335dc8c3f7SMatthew G. Knepley @*/
603490b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6035c6b900c6SMatthew G. Knepley {
6036c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6037c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
603890b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6039c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6040c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
60415dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6042c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6043c6b900c6SMatthew G. Knepley }
6044c6b900c6SMatthew G. Knepley 
60455dc8c3f7SMatthew G. Knepley /*@C
60465dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
60475dc8c3f7SMatthew G. Knepley 
60485dc8c3f7SMatthew G. Knepley   Input Parameters:
60495dc8c3f7SMatthew G. Knepley + dm      - The DM object
605090b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
60515dc8c3f7SMatthew 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
60525dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
60535dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
60545dc8c3f7SMatthew G. Knepley 
60555dc8c3f7SMatthew G. Knepley   Level: developer
60565dc8c3f7SMatthew G. Knepley 
60575dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
60585dc8c3f7SMatthew G. Knepley @*/
605990b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6060c6b900c6SMatthew G. Knepley {
6061c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6062c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6063c6b900c6SMatthew G. Knepley 
6064c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6065c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
606690b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
606790b157c4SStefano Zampini   if (maxCell) {
6068534a8f05SLisandro Dalcin     PetscValidRealPointer(maxCell,3);
6069534a8f05SLisandro Dalcin     PetscValidRealPointer(L,4);
607090b157c4SStefano Zampini     PetscValidPointer(bd,5);
607190b157c4SStefano Zampini   }
60725dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
60735dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
607490b157c4SStefano Zampini   if (maxCell) {
60755dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
60765dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
607790b157c4SStefano Zampini   }
6078072d7d67SStefano Zampini   dm->periodic = per;
6079c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6080c6b900c6SMatthew G. Knepley }
6081c6b900c6SMatthew G. Knepley 
60822e17dfb7SMatthew G. Knepley /*@
60832e17dfb7SMatthew 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.
60842e17dfb7SMatthew G. Knepley 
60852e17dfb7SMatthew G. Knepley   Input Parameters:
60862e17dfb7SMatthew G. Knepley + dm     - The DM
608765da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
608865da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
60892e17dfb7SMatthew G. Knepley 
60902e17dfb7SMatthew G. Knepley   Output Parameter:
60912e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60922e17dfb7SMatthew G. Knepley 
60932e17dfb7SMatthew G. Knepley   Level: developer
60942e17dfb7SMatthew G. Knepley 
60952e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60962e17dfb7SMatthew G. Knepley @*/
609765da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
60982e17dfb7SMatthew G. Knepley {
60992e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
61002e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
61012e17dfb7SMatthew G. Knepley 
61022e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61032e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
61042e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61052e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61062e17dfb7SMatthew G. Knepley   } else {
610765da65dcSMatthew G. Knepley     if (endpoint) {
610865da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6109da3333bfSMatthew 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)) {
6110da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
611165da65dcSMatthew G. Knepley         } else {
6112da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
611365da65dcSMatthew G. Knepley         }
611465da65dcSMatthew G. Knepley       }
611565da65dcSMatthew G. Knepley     } else {
61162e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
61171118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
61182e17dfb7SMatthew G. Knepley       }
61192e17dfb7SMatthew G. Knepley     }
612065da65dcSMatthew G. Knepley   }
61212e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61222e17dfb7SMatthew G. Knepley }
61232e17dfb7SMatthew G. Knepley 
61242e17dfb7SMatthew G. Knepley /*
61252e17dfb7SMatthew 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.
61262e17dfb7SMatthew G. Knepley 
61272e17dfb7SMatthew G. Knepley   Input Parameters:
61282e17dfb7SMatthew G. Knepley + dm     - The DM
61292e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
61302e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
61312e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
61322e17dfb7SMatthew G. Knepley 
61332e17dfb7SMatthew G. Knepley   Output Parameter:
61342e17dfb7SMatthew G. Knepley . out - The localized coordinate point
61352e17dfb7SMatthew G. Knepley 
61362e17dfb7SMatthew G. Knepley   Level: developer
61372e17dfb7SMatthew G. Knepley 
61382e17dfb7SMatthew 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
61392e17dfb7SMatthew G. Knepley 
61402e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
61412e17dfb7SMatthew G. Knepley */
61422e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
61432e17dfb7SMatthew G. Knepley {
61442e17dfb7SMatthew G. Knepley   PetscInt d;
61452e17dfb7SMatthew G. Knepley 
61462e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61472e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61482e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61492e17dfb7SMatthew G. Knepley   } else {
61502e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6151908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
61522e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
61532e17dfb7SMatthew G. Knepley       } else {
61542e17dfb7SMatthew G. Knepley         out[d] = in[d];
61552e17dfb7SMatthew G. Knepley       }
61562e17dfb7SMatthew G. Knepley     }
61572e17dfb7SMatthew G. Knepley   }
61582e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61592e17dfb7SMatthew G. Knepley }
61602e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
61612e17dfb7SMatthew G. Knepley {
61622e17dfb7SMatthew G. Knepley   PetscInt d;
61632e17dfb7SMatthew G. Knepley 
61642e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61652e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61662e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61672e17dfb7SMatthew G. Knepley   } else {
61682e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6169908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
61702e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
61712e17dfb7SMatthew G. Knepley       } else {
61722e17dfb7SMatthew G. Knepley         out[d] = in[d];
61732e17dfb7SMatthew G. Knepley       }
61742e17dfb7SMatthew G. Knepley     }
61752e17dfb7SMatthew G. Knepley   }
61762e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61772e17dfb7SMatthew G. Knepley }
61782e17dfb7SMatthew G. Knepley 
61792e17dfb7SMatthew G. Knepley /*
61802e17dfb7SMatthew 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.
61812e17dfb7SMatthew G. Knepley 
61822e17dfb7SMatthew G. Knepley   Input Parameters:
61832e17dfb7SMatthew G. Knepley + dm     - The DM
61842e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
61852e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
61862e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
61872e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
61882e17dfb7SMatthew G. Knepley 
61892e17dfb7SMatthew G. Knepley   Output Parameter:
61902e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
61912e17dfb7SMatthew G. Knepley 
61922e17dfb7SMatthew G. Knepley   Level: developer
61932e17dfb7SMatthew G. Knepley 
61942e17dfb7SMatthew 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
61952e17dfb7SMatthew G. Knepley 
61962e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
61972e17dfb7SMatthew G. Knepley */
61982e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
61992e17dfb7SMatthew G. Knepley {
62002e17dfb7SMatthew G. Knepley   PetscInt d;
62012e17dfb7SMatthew G. Knepley 
62022e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
62032e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
62042e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
62052e17dfb7SMatthew G. Knepley   } else {
62062e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6207908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
62082e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
62092e17dfb7SMatthew G. Knepley       } else {
62102e17dfb7SMatthew G. Knepley         out[d] += in[d];
62112e17dfb7SMatthew G. Knepley       }
62122e17dfb7SMatthew G. Knepley     }
62132e17dfb7SMatthew G. Knepley   }
62142e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
62152e17dfb7SMatthew G. Knepley }
62162e17dfb7SMatthew G. Knepley 
621736447a5eSToby Isaac /*@
62188f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
62198f700142SStefano Zampini 
62208f700142SStefano Zampini   Not collective
622136447a5eSToby Isaac 
622236447a5eSToby Isaac   Input Parameter:
622336447a5eSToby Isaac . dm - The DM
622436447a5eSToby Isaac 
622536447a5eSToby Isaac   Output Parameter:
622636447a5eSToby Isaac   areLocalized - True if localized
622736447a5eSToby Isaac 
622836447a5eSToby Isaac   Level: developer
622936447a5eSToby Isaac 
62308f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
623136447a5eSToby Isaac @*/
62328f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
623336447a5eSToby Isaac {
623436447a5eSToby Isaac   DM             cdm;
623536447a5eSToby Isaac   PetscSection   coordSection;
623646a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
623746a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
623836447a5eSToby Isaac   PetscErrorCode ierr;
623936447a5eSToby Isaac 
624036447a5eSToby Isaac   PetscFunctionBegin;
624136447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6242534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62438b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
624446a3a80fSLisandro Dalcin 
624536447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
624636447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
624746a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
62489f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
624946a3a80fSLisandro Dalcin 
62509f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
625146a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
625236447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
625336447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
625446a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
625546a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
625636447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
625746a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
625836447a5eSToby Isaac   }
62598f700142SStefano Zampini   *areLocalized = alreadyLocalized;
626036447a5eSToby Isaac   PetscFunctionReturn(0);
626136447a5eSToby Isaac }
626236447a5eSToby Isaac 
62638f700142SStefano Zampini /*@
62648f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
62658f700142SStefano Zampini 
62668f700142SStefano Zampini   Collective on dm
62678f700142SStefano Zampini 
62688f700142SStefano Zampini   Input Parameter:
62698f700142SStefano Zampini . dm - The DM
62708f700142SStefano Zampini 
62718f700142SStefano Zampini   Output Parameter:
62728f700142SStefano Zampini   areLocalized - True if localized
62738f700142SStefano Zampini 
62748f700142SStefano Zampini   Level: developer
62758f700142SStefano Zampini 
62768f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
62778f700142SStefano Zampini @*/
62788f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
62798f700142SStefano Zampini {
62808f700142SStefano Zampini   PetscBool      localized;
62818f700142SStefano Zampini   PetscErrorCode ierr;
62828f700142SStefano Zampini 
62838f700142SStefano Zampini   PetscFunctionBegin;
62848f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6285534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62868f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
62878f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
62888f700142SStefano Zampini   PetscFunctionReturn(0);
62898f700142SStefano Zampini }
629036447a5eSToby Isaac 
62912e17dfb7SMatthew G. Knepley /*@
6292492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
62932e17dfb7SMatthew G. Knepley 
62948f700142SStefano Zampini   Collective on dm
62958f700142SStefano Zampini 
62962e17dfb7SMatthew G. Knepley   Input Parameter:
62972e17dfb7SMatthew G. Knepley . dm - The DM
62982e17dfb7SMatthew G. Knepley 
62992e17dfb7SMatthew G. Knepley   Level: developer
63002e17dfb7SMatthew G. Knepley 
63018f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
63022e17dfb7SMatthew G. Knepley @*/
63032e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
63042e17dfb7SMatthew G. Knepley {
63052e17dfb7SMatthew G. Knepley   DM             cdm;
63062e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
63072e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
63083e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
63093e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6310e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
63113e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
63123e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
63132e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
63142e17dfb7SMatthew G. Knepley 
63152e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63162e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
631792c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6318f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6319f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6320f7cbd40bSStefano Zampini 
63212e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
63222e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
63232e17dfb7SMatthew G. Knepley   {
63242e17dfb7SMatthew G. Knepley     PetscBool isplex;
63252e17dfb7SMatthew G. Knepley 
63262e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
63272e17dfb7SMatthew G. Knepley     if (isplex) {
63282e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
63293e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
633069291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
63313e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
63323e922f36SToby Isaac       newStart = vStart;
63333e922f36SToby Isaac       newEnd   = vEnd;
63343e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
63353e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
63363e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
63373e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
63383e922f36SToby Isaac       }
63392e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
63402e17dfb7SMatthew G. Knepley   }
63412e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
634243eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
63432e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
63443e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6345e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
63463e922f36SToby Isaac 
63472e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
63482e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
63492e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
63502e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
63513e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
63523e922f36SToby Isaac 
635369291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63543e922f36SToby Isaac   localized = &anchor[bs];
63553e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
63563e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63573e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63583e922f36SToby Isaac 
63593e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
63603e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
63613e922f36SToby Isaac       PetscInt     b;
63623e922f36SToby Isaac 
63633e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
63643e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63653e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63663e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
63673e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
63683e922f36SToby Isaac         for (b = 0; b < bs; b++) {
63693e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
63703e922f36SToby Isaac         }
63713e922f36SToby Isaac         if (b < bs) break;
63723e922f36SToby Isaac       }
63733e922f36SToby Isaac       if (d < dof/bs) {
63743e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
63753e922f36SToby Isaac           PetscInt cdof;
63763e922f36SToby Isaac 
63773e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
63783e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
63793e922f36SToby Isaac         }
63803e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
63813e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
63823e922f36SToby Isaac       }
63833e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63843e922f36SToby Isaac     }
63853e922f36SToby Isaac   }
63863e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
63873e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
638869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63893e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
639069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
63913e922f36SToby Isaac     PetscFunctionReturn(0);
63923e922f36SToby Isaac   }
63932e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63942e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63952e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
63962e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
63972e17dfb7SMatthew G. Knepley   }
63982e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
63992e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6400c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
64012e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
64022e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
64032e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
64042e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6405c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
64062e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
64072e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
64082e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
64092e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
64102e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
64112e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
64122e17dfb7SMatthew G. Knepley   }
64133e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
64143e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
64153e922f36SToby Isaac 
64162e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
64172e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
64183e922f36SToby Isaac       PetscInt     b, cdof;
64192e17dfb7SMatthew G. Knepley 
64203e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
64213e922f36SToby Isaac       if (!cdof) continue;
64222e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
64232e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
64242e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
64252e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
64262e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
64272e17dfb7SMatthew G. Knepley     }
64283e922f36SToby Isaac   }
642969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
643069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6431c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
64322e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
64332e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
64342e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
64352e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
64362e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
64372e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
64382e17dfb7SMatthew G. Knepley }
64392e17dfb7SMatthew G. Knepley 
6440e87bb0d3SMatthew G Knepley /*@
64413a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6442e87bb0d3SMatthew G Knepley 
6443d083f849SBarry Smith   Collective on v (see explanation below)
6444e87bb0d3SMatthew G Knepley 
6445e87bb0d3SMatthew G Knepley   Input Parameters:
6446e87bb0d3SMatthew G Knepley + dm - The DM
64473a93e3b7SToby Isaac . v - The Vec of points
644862a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
64493a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6450e87bb0d3SMatthew G Knepley 
645161e3bb9bSMatthew G Knepley   Output Parameter:
645262a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
645362a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
64543a93e3b7SToby Isaac 
6455e87bb0d3SMatthew G Knepley 
6456e87bb0d3SMatthew G Knepley   Level: developer
645761e3bb9bSMatthew G Knepley 
645862a38674SMatthew G. Knepley   Notes:
64593a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
646062a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
64613a93e3b7SToby Isaac 
64623a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
646362a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
64643a93e3b7SToby Isaac 
64653a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
64663a93e3b7SToby Isaac 
646762a38674SMatthew G. Knepley $    const PetscSFNode *cells;
646862a38674SMatthew G. Knepley $    PetscInt           nFound;
6469a6216909SToby Isaac $    const PetscInt    *found;
647062a38674SMatthew G. Knepley $
6471a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
64723a93e3b7SToby Isaac 
64733a93e3b7SToby 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
64743a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
64753a93e3b7SToby Isaac 
647662a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
647761e3bb9bSMatthew G Knepley @*/
647862a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6479e87bb0d3SMatthew G Knepley {
6480735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6481735aa83eSMatthew G Knepley 
6482e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6483e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6484e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6485e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
64863a93e3b7SToby Isaac   if (*cellSF) {
64873a93e3b7SToby Isaac     PetscMPIInt result;
64883a93e3b7SToby Isaac 
6489e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6490a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
64913a93e3b7SToby 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");
6492e0fc9d1bSMatthew G. Knepley   } else {
64933a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
64943a93e3b7SToby Isaac   }
6495b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
649647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
649762a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
649847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6499e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6500e87bb0d3SMatthew G Knepley }
650114f150ffSMatthew G. Knepley 
6502f4d763aaSMatthew G. Knepley /*@
6503f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6504f4d763aaSMatthew G. Knepley 
65058f700142SStefano Zampini   Collective on dm
65068f700142SStefano Zampini 
6507f4d763aaSMatthew G. Knepley   Input Parameter:
6508f4d763aaSMatthew G. Knepley . dm - The original DM
6509f4d763aaSMatthew G. Knepley 
6510f4d763aaSMatthew G. Knepley   Output Parameter:
6511f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6512f4d763aaSMatthew G. Knepley 
6513f4d763aaSMatthew G. Knepley   Level: intermediate
6514f4d763aaSMatthew G. Knepley 
6515e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6516f4d763aaSMatthew G. Knepley @*/
651714f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
651814f150ffSMatthew G. Knepley {
6519c26acbdeSMatthew G. Knepley   PetscSection   section;
65202d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
652114f150ffSMatthew G. Knepley   PetscErrorCode ierr;
652214f150ffSMatthew G. Knepley 
652314f150ffSMatthew G. Knepley   PetscFunctionBegin;
652414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
652514f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
652692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6527c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6528127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
65292d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6530c26acbdeSMatthew G. Knepley     *odm = dm;
6531c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6532c26acbdeSMatthew G. Knepley   }
653314f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6534c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
653514f150ffSMatthew G. Knepley     PetscSF      sf;
653614f150ffSMatthew G. Knepley 
653714f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6538e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
653914f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
654092fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
654114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
654214f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
654315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6544e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
654514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
654614f150ffSMatthew G. Knepley   }
654714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
654814f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
654914f150ffSMatthew G. Knepley }
6550f4d763aaSMatthew G. Knepley 
6551f4d763aaSMatthew G. Knepley /*@
6552cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6553f4d763aaSMatthew G. Knepley 
6554f4d763aaSMatthew G. Knepley   Input Parameter:
6555f4d763aaSMatthew G. Knepley . dm - The original DM
6556f4d763aaSMatthew G. Knepley 
6557cdb7a50dSMatthew G. Knepley   Output Parameters:
6558cdb7a50dSMatthew G. Knepley + num - The output sequence number
6559cdb7a50dSMatthew G. Knepley - val - The output sequence value
6560f4d763aaSMatthew G. Knepley 
6561f4d763aaSMatthew G. Knepley   Level: intermediate
6562f4d763aaSMatthew G. Knepley 
6563f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6564f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6565f4d763aaSMatthew G. Knepley 
6566f4d763aaSMatthew G. Knepley .seealso: VecView()
6567f4d763aaSMatthew G. Knepley @*/
6568cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6569f4d763aaSMatthew G. Knepley {
6570f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6571f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6572534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6573534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6574f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6575f4d763aaSMatthew G. Knepley }
6576f4d763aaSMatthew G. Knepley 
6577f4d763aaSMatthew G. Knepley /*@
6578cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6579f4d763aaSMatthew G. Knepley 
6580f4d763aaSMatthew G. Knepley   Input Parameters:
6581f4d763aaSMatthew G. Knepley + dm - The original DM
6582cdb7a50dSMatthew G. Knepley . num - The output sequence number
6583cdb7a50dSMatthew G. Knepley - val - The output sequence value
6584f4d763aaSMatthew G. Knepley 
6585f4d763aaSMatthew G. Knepley   Level: intermediate
6586f4d763aaSMatthew G. Knepley 
6587f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6588f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6589f4d763aaSMatthew G. Knepley 
6590f4d763aaSMatthew G. Knepley .seealso: VecView()
6591f4d763aaSMatthew G. Knepley @*/
6592cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6593f4d763aaSMatthew G. Knepley {
6594f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6595f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6596f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6597cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6598cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6599cdb7a50dSMatthew G. Knepley }
6600cdb7a50dSMatthew G. Knepley 
6601cdb7a50dSMatthew G. Knepley /*@C
6602cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6603cdb7a50dSMatthew G. Knepley 
6604cdb7a50dSMatthew G. Knepley   Input Parameters:
6605cdb7a50dSMatthew G. Knepley + dm   - The original DM
6606cdb7a50dSMatthew G. Knepley . name - The sequence name
6607cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6608cdb7a50dSMatthew G. Knepley 
6609cdb7a50dSMatthew G. Knepley   Output Parameter:
6610cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6611cdb7a50dSMatthew G. Knepley 
6612cdb7a50dSMatthew G. Knepley   Level: intermediate
6613cdb7a50dSMatthew G. Knepley 
6614cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6615cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6616cdb7a50dSMatthew G. Knepley 
6617cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6618cdb7a50dSMatthew G. Knepley @*/
6619cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6620cdb7a50dSMatthew G. Knepley {
6621cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6622cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6623cdb7a50dSMatthew G. Knepley 
6624cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6625cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6626cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6627534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6628cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6629cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6630cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6631cdb7a50dSMatthew G. Knepley     PetscScalar value;
6632cdb7a50dSMatthew G. Knepley 
663339d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
66344aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6635cdb7a50dSMatthew G. Knepley #endif
6636cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6637f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6638f4d763aaSMatthew G. Knepley }
66398e4ac7eaSMatthew G. Knepley 
66408e4ac7eaSMatthew G. Knepley /*@
66418e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
66428e4ac7eaSMatthew G. Knepley 
66438e4ac7eaSMatthew G. Knepley   Not collective
66448e4ac7eaSMatthew G. Knepley 
66458e4ac7eaSMatthew G. Knepley   Input Parameter:
66468e4ac7eaSMatthew G. Knepley . dm - The DM
66478e4ac7eaSMatthew G. Knepley 
66488e4ac7eaSMatthew G. Knepley   Output Parameter:
66498e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
66508e4ac7eaSMatthew G. Knepley 
66518e4ac7eaSMatthew G. Knepley   Level: beginner
66528e4ac7eaSMatthew G. Knepley 
66538e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
66548e4ac7eaSMatthew G. Knepley @*/
66558e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
66568e4ac7eaSMatthew G. Knepley {
66578e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66588e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6659534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
66608e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66618e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66628e4ac7eaSMatthew G. Knepley }
66638e4ac7eaSMatthew G. Knepley 
66648e4ac7eaSMatthew G. Knepley /*@
66655d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
66668e4ac7eaSMatthew G. Knepley 
66678e4ac7eaSMatthew G. Knepley   Collective on dm
66688e4ac7eaSMatthew G. Knepley 
66698e4ac7eaSMatthew G. Knepley   Input Parameters:
66708e4ac7eaSMatthew G. Knepley + dm - The DM
66718e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
66728e4ac7eaSMatthew G. Knepley 
66735d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
66745d3b26e6SMatthew G. Knepley 
66758e4ac7eaSMatthew G. Knepley   Level: beginner
66768e4ac7eaSMatthew G. Knepley 
66775d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
66788e4ac7eaSMatthew G. Knepley @*/
66798e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
66808e4ac7eaSMatthew G. Knepley {
66818e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66828e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66838833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66848e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66858e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66868e4ac7eaSMatthew G. Knepley }
6687c58f1c22SToby Isaac 
6688c58f1c22SToby Isaac 
6689c58f1c22SToby Isaac /*@C
6690c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6691c58f1c22SToby Isaac 
6692c58f1c22SToby Isaac   Not Collective
6693c58f1c22SToby Isaac 
6694c58f1c22SToby Isaac   Input Parameters:
6695c58f1c22SToby Isaac + dm   - The DM object
6696c58f1c22SToby Isaac - name - The label name
6697c58f1c22SToby Isaac 
6698c58f1c22SToby Isaac   Level: intermediate
6699c58f1c22SToby Isaac 
6700c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6701c58f1c22SToby Isaac @*/
6702c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6703c58f1c22SToby Isaac {
67045d80c0bfSVaclav Hapla   PetscBool      flg;
67055d80c0bfSVaclav Hapla   DMLabel        label;
6706c58f1c22SToby Isaac   PetscErrorCode ierr;
6707c58f1c22SToby Isaac 
6708c58f1c22SToby Isaac   PetscFunctionBegin;
6709c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6710c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
67115d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
6712c58f1c22SToby Isaac   if (!flg) {
67135d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
67145d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
67155d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
6716c58f1c22SToby Isaac   }
6717c58f1c22SToby Isaac   PetscFunctionReturn(0);
6718c58f1c22SToby Isaac }
6719c58f1c22SToby Isaac 
6720c58f1c22SToby Isaac /*@C
6721c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac   Not Collective
6724c58f1c22SToby Isaac 
6725c58f1c22SToby Isaac   Input Parameters:
6726c58f1c22SToby Isaac + dm   - The DM object
6727c58f1c22SToby Isaac . name - The label name
6728c58f1c22SToby Isaac - point - The mesh point
6729c58f1c22SToby Isaac 
6730c58f1c22SToby Isaac   Output Parameter:
6731c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6732c58f1c22SToby Isaac 
6733c58f1c22SToby Isaac   Level: beginner
6734c58f1c22SToby Isaac 
6735c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6736c58f1c22SToby Isaac @*/
6737c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6738c58f1c22SToby Isaac {
6739c58f1c22SToby Isaac   DMLabel        label;
6740c58f1c22SToby Isaac   PetscErrorCode ierr;
6741c58f1c22SToby Isaac 
6742c58f1c22SToby Isaac   PetscFunctionBegin;
6743c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6744c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6745c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
674613903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6747c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6748c58f1c22SToby Isaac   PetscFunctionReturn(0);
6749c58f1c22SToby Isaac }
6750c58f1c22SToby Isaac 
6751c58f1c22SToby Isaac /*@C
6752c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6753c58f1c22SToby Isaac 
6754c58f1c22SToby Isaac   Not Collective
6755c58f1c22SToby Isaac 
6756c58f1c22SToby Isaac   Input Parameters:
6757c58f1c22SToby Isaac + dm   - The DM object
6758c58f1c22SToby Isaac . name - The label name
6759c58f1c22SToby Isaac . point - The mesh point
6760c58f1c22SToby Isaac - value - The label value for this point
6761c58f1c22SToby Isaac 
6762c58f1c22SToby Isaac   Output Parameter:
6763c58f1c22SToby Isaac 
6764c58f1c22SToby Isaac   Level: beginner
6765c58f1c22SToby Isaac 
6766c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6767c58f1c22SToby Isaac @*/
6768c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6769c58f1c22SToby Isaac {
6770c58f1c22SToby Isaac   DMLabel        label;
6771c58f1c22SToby Isaac   PetscErrorCode ierr;
6772c58f1c22SToby Isaac 
6773c58f1c22SToby Isaac   PetscFunctionBegin;
6774c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6775c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6776c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6777c58f1c22SToby Isaac   if (!label) {
6778c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6779c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6780c58f1c22SToby Isaac   }
6781c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6782c58f1c22SToby Isaac   PetscFunctionReturn(0);
6783c58f1c22SToby Isaac }
6784c58f1c22SToby Isaac 
6785c58f1c22SToby Isaac /*@C
6786c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6787c58f1c22SToby Isaac 
6788c58f1c22SToby Isaac   Not Collective
6789c58f1c22SToby Isaac 
6790c58f1c22SToby Isaac   Input Parameters:
6791c58f1c22SToby Isaac + dm   - The DM object
6792c58f1c22SToby Isaac . name - The label name
6793c58f1c22SToby Isaac . point - The mesh point
6794c58f1c22SToby Isaac - value - The label value for this point
6795c58f1c22SToby Isaac 
6796c58f1c22SToby Isaac   Output Parameter:
6797c58f1c22SToby Isaac 
6798c58f1c22SToby Isaac   Level: beginner
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6801c58f1c22SToby Isaac @*/
6802c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6803c58f1c22SToby Isaac {
6804c58f1c22SToby Isaac   DMLabel        label;
6805c58f1c22SToby Isaac   PetscErrorCode ierr;
6806c58f1c22SToby Isaac 
6807c58f1c22SToby Isaac   PetscFunctionBegin;
6808c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6809c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6810c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6811c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6812c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6813c58f1c22SToby Isaac   PetscFunctionReturn(0);
6814c58f1c22SToby Isaac }
6815c58f1c22SToby Isaac 
6816c58f1c22SToby Isaac /*@C
6817c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6818c58f1c22SToby Isaac 
6819c58f1c22SToby Isaac   Not Collective
6820c58f1c22SToby Isaac 
6821c58f1c22SToby Isaac   Input Parameters:
6822c58f1c22SToby Isaac + dm   - The DM object
6823c58f1c22SToby Isaac - name - The label name
6824c58f1c22SToby Isaac 
6825c58f1c22SToby Isaac   Output Parameter:
6826c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6827c58f1c22SToby Isaac 
6828c58f1c22SToby Isaac   Level: beginner
6829c58f1c22SToby Isaac 
6830df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6831c58f1c22SToby Isaac @*/
6832c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6833c58f1c22SToby Isaac {
6834c58f1c22SToby Isaac   DMLabel        label;
6835c58f1c22SToby Isaac   PetscErrorCode ierr;
6836c58f1c22SToby Isaac 
6837c58f1c22SToby Isaac   PetscFunctionBegin;
6838c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6839c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6840534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
6841c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6842c58f1c22SToby Isaac   *size = 0;
6843c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6844c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6845c58f1c22SToby Isaac   PetscFunctionReturn(0);
6846c58f1c22SToby Isaac }
6847c58f1c22SToby Isaac 
6848c58f1c22SToby Isaac /*@C
6849c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6850c58f1c22SToby Isaac 
6851c58f1c22SToby Isaac   Not Collective
6852c58f1c22SToby Isaac 
6853c58f1c22SToby Isaac   Input Parameters:
6854c58f1c22SToby Isaac + mesh - The DM object
6855c58f1c22SToby Isaac - name - The label name
6856c58f1c22SToby Isaac 
6857c58f1c22SToby Isaac   Output Parameter:
6858c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac   Level: beginner
6861c58f1c22SToby Isaac 
6862c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6863c58f1c22SToby Isaac @*/
6864c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6865c58f1c22SToby Isaac {
6866c58f1c22SToby Isaac   DMLabel        label;
6867c58f1c22SToby Isaac   PetscErrorCode ierr;
6868c58f1c22SToby Isaac 
6869c58f1c22SToby Isaac   PetscFunctionBegin;
6870c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6871c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6872c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6873c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6874c58f1c22SToby Isaac   *ids = NULL;
6875dab2e251SBlaise Bourdin  if (label) {
6876c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6877dab2e251SBlaise Bourdin   } else {
6878dab2e251SBlaise Bourdin     /* returning an empty IS */
6879dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6880dab2e251SBlaise Bourdin   }
6881c58f1c22SToby Isaac   PetscFunctionReturn(0);
6882c58f1c22SToby Isaac }
6883c58f1c22SToby Isaac 
6884c58f1c22SToby Isaac /*@C
6885c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6886c58f1c22SToby Isaac 
6887c58f1c22SToby Isaac   Not Collective
6888c58f1c22SToby Isaac 
6889c58f1c22SToby Isaac   Input Parameters:
6890c58f1c22SToby Isaac + dm - The DM object
6891c58f1c22SToby Isaac . name - The label name
6892c58f1c22SToby Isaac - value - The stratum value
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   Output Parameter:
6895c58f1c22SToby Isaac . size - The stratum size
6896c58f1c22SToby Isaac 
6897c58f1c22SToby Isaac   Level: beginner
6898c58f1c22SToby Isaac 
6899c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6900c58f1c22SToby Isaac @*/
6901c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6902c58f1c22SToby Isaac {
6903c58f1c22SToby Isaac   DMLabel        label;
6904c58f1c22SToby Isaac   PetscErrorCode ierr;
6905c58f1c22SToby Isaac 
6906c58f1c22SToby Isaac   PetscFunctionBegin;
6907c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6908c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6909534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
6910c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6911c58f1c22SToby Isaac   *size = 0;
6912c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6913c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6914c58f1c22SToby Isaac   PetscFunctionReturn(0);
6915c58f1c22SToby Isaac }
6916c58f1c22SToby Isaac 
6917c58f1c22SToby Isaac /*@C
6918c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6919c58f1c22SToby Isaac 
6920c58f1c22SToby Isaac   Not Collective
6921c58f1c22SToby Isaac 
6922c58f1c22SToby Isaac   Input Parameters:
6923c58f1c22SToby Isaac + dm - The DM object
6924c58f1c22SToby Isaac . name - The label name
6925c58f1c22SToby Isaac - value - The stratum value
6926c58f1c22SToby Isaac 
6927c58f1c22SToby Isaac   Output Parameter:
6928c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6929c58f1c22SToby Isaac 
6930c58f1c22SToby Isaac   Level: beginner
6931c58f1c22SToby Isaac 
6932c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6933c58f1c22SToby Isaac @*/
6934c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6935c58f1c22SToby Isaac {
6936c58f1c22SToby Isaac   DMLabel        label;
6937c58f1c22SToby Isaac   PetscErrorCode ierr;
6938c58f1c22SToby Isaac 
6939c58f1c22SToby Isaac   PetscFunctionBegin;
6940c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6941c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6942c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6943c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6944c58f1c22SToby Isaac   *points = NULL;
6945c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6946c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6947c58f1c22SToby Isaac   PetscFunctionReturn(0);
6948c58f1c22SToby Isaac }
6949c58f1c22SToby Isaac 
69504de306b1SToby Isaac /*@C
69519044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69524de306b1SToby Isaac 
69534de306b1SToby Isaac   Not Collective
69544de306b1SToby Isaac 
69554de306b1SToby Isaac   Input Parameters:
69564de306b1SToby Isaac + dm - The DM object
69574de306b1SToby Isaac . name - The label name
69584de306b1SToby Isaac . value - The stratum value
69594de306b1SToby Isaac - points - The stratum points
69604de306b1SToby Isaac 
69614de306b1SToby Isaac   Level: beginner
69624de306b1SToby Isaac 
69634de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
69644de306b1SToby Isaac @*/
69654de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
69664de306b1SToby Isaac {
69674de306b1SToby Isaac   DMLabel        label;
69684de306b1SToby Isaac   PetscErrorCode ierr;
69694de306b1SToby Isaac 
69704de306b1SToby Isaac   PetscFunctionBegin;
69714de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69724de306b1SToby Isaac   PetscValidCharPointer(name, 2);
69734de306b1SToby Isaac   PetscValidPointer(points, 4);
69744de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
69754de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
69764de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
69774de306b1SToby Isaac   PetscFunctionReturn(0);
69784de306b1SToby Isaac }
69794de306b1SToby Isaac 
6980c58f1c22SToby Isaac /*@C
6981c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac   Not Collective
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac   Input Parameters:
6986c58f1c22SToby Isaac + dm   - The DM object
6987c58f1c22SToby Isaac . name - The label name
6988c58f1c22SToby Isaac - value - The label value for this point
6989c58f1c22SToby Isaac 
6990c58f1c22SToby Isaac   Output Parameter:
6991c58f1c22SToby Isaac 
6992c58f1c22SToby Isaac   Level: beginner
6993c58f1c22SToby Isaac 
6994c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6995c58f1c22SToby Isaac @*/
6996c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6997c58f1c22SToby Isaac {
6998c58f1c22SToby Isaac   DMLabel        label;
6999c58f1c22SToby Isaac   PetscErrorCode ierr;
7000c58f1c22SToby Isaac 
7001c58f1c22SToby Isaac   PetscFunctionBegin;
7002c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7003c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7004c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7005c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7006c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7007c58f1c22SToby Isaac   PetscFunctionReturn(0);
7008c58f1c22SToby Isaac }
7009c58f1c22SToby Isaac 
7010c58f1c22SToby Isaac /*@
7011c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7012c58f1c22SToby Isaac 
7013c58f1c22SToby Isaac   Not Collective
7014c58f1c22SToby Isaac 
7015c58f1c22SToby Isaac   Input Parameter:
7016c58f1c22SToby Isaac . dm   - The DM object
7017c58f1c22SToby Isaac 
7018c58f1c22SToby Isaac   Output Parameter:
7019c58f1c22SToby Isaac . numLabels - the number of Labels
7020c58f1c22SToby Isaac 
7021c58f1c22SToby Isaac   Level: intermediate
7022c58f1c22SToby Isaac 
7023c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7024c58f1c22SToby Isaac @*/
7025c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7026c58f1c22SToby Isaac {
70275d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7028c58f1c22SToby Isaac   PetscInt  n    = 0;
7029c58f1c22SToby Isaac 
7030c58f1c22SToby Isaac   PetscFunctionBegin;
7031c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7032534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7033c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7034c58f1c22SToby Isaac   *numLabels = n;
7035c58f1c22SToby Isaac   PetscFunctionReturn(0);
7036c58f1c22SToby Isaac }
7037c58f1c22SToby Isaac 
7038c58f1c22SToby Isaac /*@C
7039c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7040c58f1c22SToby Isaac 
7041c58f1c22SToby Isaac   Not Collective
7042c58f1c22SToby Isaac 
7043c58f1c22SToby Isaac   Input Parameters:
7044c58f1c22SToby Isaac + dm - The DM object
7045c58f1c22SToby Isaac - n  - the label number
7046c58f1c22SToby Isaac 
7047c58f1c22SToby Isaac   Output Parameter:
7048c58f1c22SToby Isaac . name - the label name
7049c58f1c22SToby Isaac 
7050c58f1c22SToby Isaac   Level: intermediate
7051c58f1c22SToby Isaac 
7052c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7053c58f1c22SToby Isaac @*/
7054c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7055c58f1c22SToby Isaac {
70565d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7057c58f1c22SToby Isaac   PetscInt       l    = 0;
7058d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7059c58f1c22SToby Isaac 
7060c58f1c22SToby Isaac   PetscFunctionBegin;
7061c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7062c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7063c58f1c22SToby Isaac   while (next) {
7064c58f1c22SToby Isaac     if (l == n) {
7065d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7066c58f1c22SToby Isaac       PetscFunctionReturn(0);
7067c58f1c22SToby Isaac     }
7068c58f1c22SToby Isaac     ++l;
7069c58f1c22SToby Isaac     next = next->next;
7070c58f1c22SToby Isaac   }
7071c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7072c58f1c22SToby Isaac }
7073c58f1c22SToby Isaac 
7074c58f1c22SToby Isaac /*@C
7075c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7076c58f1c22SToby Isaac 
7077c58f1c22SToby Isaac   Not Collective
7078c58f1c22SToby Isaac 
7079c58f1c22SToby Isaac   Input Parameters:
7080c58f1c22SToby Isaac + dm   - The DM object
7081c58f1c22SToby Isaac - name - The label name
7082c58f1c22SToby Isaac 
7083c58f1c22SToby Isaac   Output Parameter:
7084c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac   Level: intermediate
7087c58f1c22SToby Isaac 
7088c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7089c58f1c22SToby Isaac @*/
7090c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7091c58f1c22SToby Isaac {
70925d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7093d67d17b1SMatthew G. Knepley   const char    *lname;
7094c58f1c22SToby Isaac   PetscErrorCode ierr;
7095c58f1c22SToby Isaac 
7096c58f1c22SToby Isaac   PetscFunctionBegin;
7097c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7098c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7099534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7100c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7101c58f1c22SToby Isaac   while (next) {
7102d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7103d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7104c58f1c22SToby Isaac     if (*hasLabel) break;
7105c58f1c22SToby Isaac     next = next->next;
7106c58f1c22SToby Isaac   }
7107c58f1c22SToby Isaac   PetscFunctionReturn(0);
7108c58f1c22SToby Isaac }
7109c58f1c22SToby Isaac 
7110c58f1c22SToby Isaac /*@C
7111c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7112c58f1c22SToby Isaac 
7113c58f1c22SToby Isaac   Not Collective
7114c58f1c22SToby Isaac 
7115c58f1c22SToby Isaac   Input Parameters:
7116c58f1c22SToby Isaac + dm   - The DM object
7117c58f1c22SToby Isaac - name - The label name
7118c58f1c22SToby Isaac 
7119c58f1c22SToby Isaac   Output Parameter:
7120c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7121c58f1c22SToby Isaac 
7122c58f1c22SToby Isaac   Level: intermediate
7123c58f1c22SToby Isaac 
7124c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7125c58f1c22SToby Isaac @*/
7126c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7127c58f1c22SToby Isaac {
71285d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7129c58f1c22SToby Isaac   PetscBool      hasLabel;
7130d67d17b1SMatthew G. Knepley   const char    *lname;
7131c58f1c22SToby Isaac   PetscErrorCode ierr;
7132c58f1c22SToby Isaac 
7133c58f1c22SToby Isaac   PetscFunctionBegin;
7134c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7135c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7136c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7137c58f1c22SToby Isaac   *label = NULL;
7138c58f1c22SToby Isaac   while (next) {
7139d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7140d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7141c58f1c22SToby Isaac     if (hasLabel) {
7142c58f1c22SToby Isaac       *label = next->label;
7143c58f1c22SToby Isaac       break;
7144c58f1c22SToby Isaac     }
7145c58f1c22SToby Isaac     next = next->next;
7146c58f1c22SToby Isaac   }
7147c58f1c22SToby Isaac   PetscFunctionReturn(0);
7148c58f1c22SToby Isaac }
7149c58f1c22SToby Isaac 
7150c58f1c22SToby Isaac /*@C
7151c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7152c58f1c22SToby Isaac 
7153c58f1c22SToby Isaac   Not Collective
7154c58f1c22SToby Isaac 
7155c58f1c22SToby Isaac   Input Parameters:
7156c58f1c22SToby Isaac + dm - The DM object
7157c58f1c22SToby Isaac - n  - the label number
7158c58f1c22SToby Isaac 
7159c58f1c22SToby Isaac   Output Parameter:
7160c58f1c22SToby Isaac . label - the label
7161c58f1c22SToby Isaac 
7162c58f1c22SToby Isaac   Level: intermediate
7163c58f1c22SToby Isaac 
7164c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7165c58f1c22SToby Isaac @*/
7166c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7167c58f1c22SToby Isaac {
71685d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7169c58f1c22SToby Isaac   PetscInt    l    = 0;
7170c58f1c22SToby Isaac 
7171c58f1c22SToby Isaac   PetscFunctionBegin;
7172c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7173c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7174c58f1c22SToby Isaac   while (next) {
7175c58f1c22SToby Isaac     if (l == n) {
7176c58f1c22SToby Isaac       *label = next->label;
7177c58f1c22SToby Isaac       PetscFunctionReturn(0);
7178c58f1c22SToby Isaac     }
7179c58f1c22SToby Isaac     ++l;
7180c58f1c22SToby Isaac     next = next->next;
7181c58f1c22SToby Isaac   }
7182c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7183c58f1c22SToby Isaac }
7184c58f1c22SToby Isaac 
7185c58f1c22SToby Isaac /*@C
7186c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7187c58f1c22SToby Isaac 
7188c58f1c22SToby Isaac   Not Collective
7189c58f1c22SToby Isaac 
7190c58f1c22SToby Isaac   Input Parameters:
7191c58f1c22SToby Isaac + dm   - The DM object
7192c58f1c22SToby Isaac - label - The DMLabel
7193c58f1c22SToby Isaac 
7194c58f1c22SToby Isaac   Level: developer
7195c58f1c22SToby Isaac 
7196c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7197c58f1c22SToby Isaac @*/
7198c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7199c58f1c22SToby Isaac {
72005d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7201c58f1c22SToby Isaac   PetscBool      hasLabel;
7202d67d17b1SMatthew G. Knepley   const char    *lname;
72035d80c0bfSVaclav Hapla   PetscBool      flg;
7204c58f1c22SToby Isaac   PetscErrorCode ierr;
7205c58f1c22SToby Isaac 
7206c58f1c22SToby Isaac   PetscFunctionBegin;
7207c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7208d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7209d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7210d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7211c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7212c58f1c22SToby Isaac   tmpLabel->label  = label;
7213c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72145d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
72155d80c0bfSVaclav Hapla   *p = tmpLabel;
721608f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
72175d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
72185d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7219ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7220ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7221c58f1c22SToby Isaac   PetscFunctionReturn(0);
7222c58f1c22SToby Isaac }
7223c58f1c22SToby Isaac 
7224c58f1c22SToby Isaac /*@C
7225e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7226c58f1c22SToby Isaac 
7227c58f1c22SToby Isaac   Not Collective
7228c58f1c22SToby Isaac 
7229c58f1c22SToby Isaac   Input Parameters:
7230c58f1c22SToby Isaac + dm   - The DM object
7231c58f1c22SToby Isaac - name - The label name
7232c58f1c22SToby Isaac 
7233c58f1c22SToby Isaac   Output Parameter:
7234c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7235c58f1c22SToby Isaac 
7236c58f1c22SToby Isaac   Level: developer
7237c58f1c22SToby Isaac 
7238e5472504SVaclav Hapla   Notes:
7239e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7240e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7241e5472504SVaclav Hapla 
7242e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7243e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7244e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7245e5472504SVaclav Hapla 
7246e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7247c58f1c22SToby Isaac @*/
7248c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7249c58f1c22SToby Isaac {
725095d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7251c58f1c22SToby Isaac   PetscBool      hasLabel;
7252d67d17b1SMatthew G. Knepley   const char    *lname;
7253c58f1c22SToby Isaac   PetscErrorCode ierr;
7254c58f1c22SToby Isaac 
7255c58f1c22SToby Isaac   PetscFunctionBegin;
7256c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7257e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7258e5472504SVaclav Hapla   if (label) {
7259e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7260c58f1c22SToby Isaac     *label = NULL;
7261e5472504SVaclav Hapla   }
72625d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
726395d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7264d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7265c58f1c22SToby Isaac     if (hasLabel) {
726695d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7267c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
726895d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7269ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7270ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
727195d578d6SVaclav Hapla       if (label) *label = link->label;
727295d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
727395d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7274c58f1c22SToby Isaac       break;
7275c58f1c22SToby Isaac     }
7276c58f1c22SToby Isaac   }
7277c58f1c22SToby Isaac   PetscFunctionReturn(0);
7278c58f1c22SToby Isaac }
7279c58f1c22SToby Isaac 
7280306894acSVaclav Hapla /*@
7281306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7282306894acSVaclav Hapla 
7283306894acSVaclav Hapla   Not Collective
7284306894acSVaclav Hapla 
7285306894acSVaclav Hapla   Input Parameters:
7286306894acSVaclav Hapla + dm   - The DM object
7287306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7288306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7289306894acSVaclav Hapla 
7290306894acSVaclav Hapla   Level: developer
7291306894acSVaclav Hapla 
7292306894acSVaclav Hapla   Notes:
7293306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7294306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7295306894acSVaclav Hapla   *label nullified.
7296306894acSVaclav Hapla 
7297306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7298306894acSVaclav Hapla @*/
7299306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7300306894acSVaclav Hapla {
730143e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7302306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7303306894acSVaclav Hapla   PetscErrorCode ierr;
7304306894acSVaclav Hapla 
7305306894acSVaclav Hapla   PetscFunctionBegin;
7306306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7307306894acSVaclav Hapla   PetscValidPointer(label, 2);
7308f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7309306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7310306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
73115d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
731243e45a93SVaclav Hapla     if (*label == link->label) {
7313306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
731443e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7315306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7316ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
731743e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
731843e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
731943e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7320306894acSVaclav Hapla       break;
7321306894acSVaclav Hapla     }
7322306894acSVaclav Hapla   }
7323306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7324306894acSVaclav Hapla   PetscFunctionReturn(0);
7325306894acSVaclav Hapla }
7326306894acSVaclav Hapla 
7327c58f1c22SToby Isaac /*@C
7328c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7329c58f1c22SToby Isaac 
7330c58f1c22SToby Isaac   Not Collective
7331c58f1c22SToby Isaac 
7332c58f1c22SToby Isaac   Input Parameters:
7333c58f1c22SToby Isaac + dm   - The DM object
7334c58f1c22SToby Isaac - name - The label name
7335c58f1c22SToby Isaac 
7336c58f1c22SToby Isaac   Output Parameter:
7337c58f1c22SToby Isaac . output - The flag for output
7338c58f1c22SToby Isaac 
7339c58f1c22SToby Isaac   Level: developer
7340c58f1c22SToby Isaac 
7341c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7342c58f1c22SToby Isaac @*/
7343c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7344c58f1c22SToby Isaac {
73455d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7346d67d17b1SMatthew G. Knepley   const char    *lname;
7347c58f1c22SToby Isaac   PetscErrorCode ierr;
7348c58f1c22SToby Isaac 
7349c58f1c22SToby Isaac   PetscFunctionBegin;
7350c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7351c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7352c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7353c58f1c22SToby Isaac   while (next) {
7354c58f1c22SToby Isaac     PetscBool flg;
7355c58f1c22SToby Isaac 
7356d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7357d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7358c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7359c58f1c22SToby Isaac     next = next->next;
7360c58f1c22SToby Isaac   }
7361c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7362c58f1c22SToby Isaac }
7363c58f1c22SToby Isaac 
7364c58f1c22SToby Isaac /*@C
7365c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7366c58f1c22SToby Isaac 
7367c58f1c22SToby Isaac   Not Collective
7368c58f1c22SToby Isaac 
7369c58f1c22SToby Isaac   Input Parameters:
7370c58f1c22SToby Isaac + dm     - The DM object
7371c58f1c22SToby Isaac . name   - The label name
7372c58f1c22SToby Isaac - output - The flag for output
7373c58f1c22SToby Isaac 
7374c58f1c22SToby Isaac   Level: developer
7375c58f1c22SToby Isaac 
7376c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7377c58f1c22SToby Isaac @*/
7378c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7379c58f1c22SToby Isaac {
73805d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7381d67d17b1SMatthew G. Knepley   const char    *lname;
7382c58f1c22SToby Isaac   PetscErrorCode ierr;
7383c58f1c22SToby Isaac 
7384c58f1c22SToby Isaac   PetscFunctionBegin;
7385c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7386534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7387c58f1c22SToby Isaac   while (next) {
7388c58f1c22SToby Isaac     PetscBool flg;
7389c58f1c22SToby Isaac 
7390d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7391d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7392c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7393c58f1c22SToby Isaac     next = next->next;
7394c58f1c22SToby Isaac   }
7395c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7396c58f1c22SToby Isaac }
7397c58f1c22SToby Isaac 
7398c58f1c22SToby Isaac /*@
7399c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7400c58f1c22SToby Isaac 
7401d083f849SBarry Smith   Collective on dmA
7402c58f1c22SToby Isaac 
7403c58f1c22SToby Isaac   Input Parameter:
74045d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
74052e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
74065d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7407ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7408c58f1c22SToby Isaac 
7409c58f1c22SToby Isaac   Level: intermediate
7410c58f1c22SToby Isaac 
7411c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7412c58f1c22SToby Isaac 
74135d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7414c58f1c22SToby Isaac @*/
74155d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7416c58f1c22SToby Isaac {
7417c58f1c22SToby Isaac   DMLabel        label, labelNew;
7418c58f1c22SToby Isaac   const char    *name;
7419c58f1c22SToby Isaac   PetscBool      flg;
74205d80c0bfSVaclav Hapla   DMLabelLink    link;
74215d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7422c58f1c22SToby Isaac 
74235d80c0bfSVaclav Hapla   PetscFunctionBegin;
74245d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
74255d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
74265d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
74275d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
74285d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
74295d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
74305d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
74315d80c0bfSVaclav Hapla     label=link->label;
74325d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
74335d80c0bfSVaclav Hapla     if (!all) {
7434c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7435c58f1c22SToby Isaac       if (flg) continue;
74367d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
74377d5acc75SStefano Zampini       if (flg) continue;
7438ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7439ba2698f1SMatthew G. Knepley       if (flg) continue;
74405d80c0bfSVaclav Hapla     } else {
74415d80c0bfSVaclav Hapla       dmB->depthLabel    = dmA->depthLabel;
7442ba2698f1SMatthew G. Knepley       dmB->celltypeLabel = dmA->celltypeLabel;
74435d80c0bfSVaclav Hapla     }
74445d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7445c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
74465d80c0bfSVaclav Hapla     } else {
74475d80c0bfSVaclav Hapla       labelNew = label;
74485d80c0bfSVaclav Hapla     }
7449c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
74505d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7451c58f1c22SToby Isaac   }
7452c58f1c22SToby Isaac   PetscFunctionReturn(0);
7453c58f1c22SToby Isaac }
7454a8fb8f29SToby Isaac 
7455a8fb8f29SToby Isaac /*@
7456a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7457a8fb8f29SToby Isaac 
7458a8fb8f29SToby Isaac   Input Parameter:
7459a8fb8f29SToby Isaac . dm - The DM object
7460a8fb8f29SToby Isaac 
7461a8fb8f29SToby Isaac   Output Parameter:
7462a8fb8f29SToby Isaac . cdm - The coarse DM
7463a8fb8f29SToby Isaac 
7464a8fb8f29SToby Isaac   Level: intermediate
7465a8fb8f29SToby Isaac 
7466a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7467a8fb8f29SToby Isaac @*/
7468a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7469a8fb8f29SToby Isaac {
7470a8fb8f29SToby Isaac   PetscFunctionBegin;
7471a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7472a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7473a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7474a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7475a8fb8f29SToby Isaac }
7476a8fb8f29SToby Isaac 
7477a8fb8f29SToby Isaac /*@
7478a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7479a8fb8f29SToby Isaac 
7480a8fb8f29SToby Isaac   Input Parameters:
7481a8fb8f29SToby Isaac + dm - The DM object
7482a8fb8f29SToby Isaac - cdm - The coarse DM
7483a8fb8f29SToby Isaac 
7484a8fb8f29SToby Isaac   Level: intermediate
7485a8fb8f29SToby Isaac 
7486a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7487a8fb8f29SToby Isaac @*/
7488a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7489a8fb8f29SToby Isaac {
7490a8fb8f29SToby Isaac   PetscErrorCode ierr;
7491a8fb8f29SToby Isaac 
7492a8fb8f29SToby Isaac   PetscFunctionBegin;
7493a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7494a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7495a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7496a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7497a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7498a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7499a8fb8f29SToby Isaac }
7500a8fb8f29SToby Isaac 
750188bdff64SToby Isaac /*@
750288bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
750388bdff64SToby Isaac 
750488bdff64SToby Isaac   Input Parameter:
750588bdff64SToby Isaac . dm - The DM object
750688bdff64SToby Isaac 
750788bdff64SToby Isaac   Output Parameter:
750888bdff64SToby Isaac . fdm - The fine DM
750988bdff64SToby Isaac 
751088bdff64SToby Isaac   Level: intermediate
751188bdff64SToby Isaac 
751288bdff64SToby Isaac .seealso: DMSetFineDM()
751388bdff64SToby Isaac @*/
751488bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
751588bdff64SToby Isaac {
751688bdff64SToby Isaac   PetscFunctionBegin;
751788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
751888bdff64SToby Isaac   PetscValidPointer(fdm, 2);
751988bdff64SToby Isaac   *fdm = dm->fineMesh;
752088bdff64SToby Isaac   PetscFunctionReturn(0);
752188bdff64SToby Isaac }
752288bdff64SToby Isaac 
752388bdff64SToby Isaac /*@
752488bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
752588bdff64SToby Isaac 
752688bdff64SToby Isaac   Input Parameters:
752788bdff64SToby Isaac + dm - The DM object
752888bdff64SToby Isaac - fdm - The fine DM
752988bdff64SToby Isaac 
753088bdff64SToby Isaac   Level: intermediate
753188bdff64SToby Isaac 
753288bdff64SToby Isaac .seealso: DMGetFineDM()
753388bdff64SToby Isaac @*/
753488bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
753588bdff64SToby Isaac {
753688bdff64SToby Isaac   PetscErrorCode ierr;
753788bdff64SToby Isaac 
753888bdff64SToby Isaac   PetscFunctionBegin;
753988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
754088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
754188bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
754288bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
754388bdff64SToby Isaac   dm->fineMesh = fdm;
754488bdff64SToby Isaac   PetscFunctionReturn(0);
754588bdff64SToby Isaac }
754688bdff64SToby Isaac 
7547a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7548a6ba4734SToby Isaac 
7549a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7550a6ba4734SToby Isaac {
7551e5e52638SMatthew G. Knepley   PetscInt       d;
7552a6ba4734SToby Isaac   PetscErrorCode ierr;
7553a6ba4734SToby Isaac 
7554a6ba4734SToby Isaac   PetscFunctionBegin;
7555e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7556e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7557e5e52638SMatthew G. Knepley   }
7558a6ba4734SToby Isaac   PetscFunctionReturn(0);
7559a6ba4734SToby Isaac }
7560a6ba4734SToby Isaac 
7561a6ba4734SToby Isaac /*@C
7562a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7563a6ba4734SToby Isaac 
7564a6ba4734SToby Isaac   Input Parameters:
75654c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7566f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7567a6ba4734SToby Isaac . name        - The BC name
7568a6ba4734SToby Isaac . labelname   - The label defining constrained points
7569a6ba4734SToby Isaac . field       - The field to constrain
7570e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7571a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7572a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7573a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7574a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7575a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7576a6ba4734SToby Isaac 
7577a6ba4734SToby Isaac   Options Database Keys:
7578a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7579a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7580a6ba4734SToby Isaac 
7581a6ba4734SToby Isaac   Level: developer
7582a6ba4734SToby Isaac 
7583a6ba4734SToby Isaac .seealso: DMGetBoundary()
7584a6ba4734SToby Isaac @*/
7585a30ec4eaSSatish 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)
7586a6ba4734SToby Isaac {
7587e5e52638SMatthew G. Knepley   PetscDS        ds;
7588a6ba4734SToby Isaac   PetscErrorCode ierr;
7589a6ba4734SToby Isaac 
7590a6ba4734SToby Isaac   PetscFunctionBegin;
7591a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7592e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7593e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7594a6ba4734SToby Isaac   PetscFunctionReturn(0);
7595a6ba4734SToby Isaac }
7596a6ba4734SToby Isaac 
7597a6ba4734SToby Isaac /*@
7598a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7599a6ba4734SToby Isaac 
7600a6ba4734SToby Isaac   Input Parameters:
7601a6ba4734SToby Isaac . dm - The mesh object
7602a6ba4734SToby Isaac 
7603a6ba4734SToby Isaac   Output Parameters:
7604a6ba4734SToby Isaac . numBd - The number of BC
7605a6ba4734SToby Isaac 
7606a6ba4734SToby Isaac   Level: intermediate
7607a6ba4734SToby Isaac 
7608a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7609a6ba4734SToby Isaac @*/
7610a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7611a6ba4734SToby Isaac {
7612e5e52638SMatthew G. Knepley   PetscDS        ds;
761358ebd649SToby Isaac   PetscErrorCode ierr;
7614a6ba4734SToby Isaac 
7615a6ba4734SToby Isaac   PetscFunctionBegin;
7616a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7617e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7618e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7619a6ba4734SToby Isaac   PetscFunctionReturn(0);
7620a6ba4734SToby Isaac }
7621a6ba4734SToby Isaac 
7622a6ba4734SToby Isaac /*@C
76231c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7624a6ba4734SToby Isaac 
7625a6ba4734SToby Isaac   Input Parameters:
7626a6ba4734SToby Isaac + dm          - The mesh object
7627a6ba4734SToby Isaac - bd          - The BC number
7628a6ba4734SToby Isaac 
7629a6ba4734SToby Isaac   Output Parameters:
7630f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7631a6ba4734SToby Isaac . name        - The BC name
7632a6ba4734SToby Isaac . labelname   - The label defining constrained points
7633a6ba4734SToby Isaac . field       - The field to constrain
7634a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7635a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7636a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7637a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7638a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7639a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7640a6ba4734SToby Isaac 
7641a6ba4734SToby Isaac   Options Database Keys:
7642a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7643a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7644a6ba4734SToby Isaac 
7645a6ba4734SToby Isaac   Level: developer
7646a6ba4734SToby Isaac 
7647a6ba4734SToby Isaac .seealso: DMAddBoundary()
7648a6ba4734SToby Isaac @*/
7649a30ec4eaSSatish 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)
7650a6ba4734SToby Isaac {
7651e5e52638SMatthew G. Knepley   PetscDS        ds;
765258ebd649SToby Isaac   PetscErrorCode ierr;
7653a6ba4734SToby Isaac 
7654a6ba4734SToby Isaac   PetscFunctionBegin;
7655a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7656e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7657e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7658a6ba4734SToby Isaac   PetscFunctionReturn(0);
7659a6ba4734SToby Isaac }
7660a6ba4734SToby Isaac 
7661e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7662e6f8dbb6SToby Isaac {
7663e5e52638SMatthew G. Knepley   PetscDS        ds;
7664dff059c6SToby Isaac   DMBoundary    *lastnext;
7665e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7666e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7667e6f8dbb6SToby Isaac 
7668e6f8dbb6SToby Isaac   PetscFunctionBegin;
7669e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7670e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
767147a1f5adSToby Isaac   if (dm->boundary) {
767247a1f5adSToby Isaac     DMBoundary next = dm->boundary;
767347a1f5adSToby Isaac 
767447a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
767547a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
767647a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
767747a1f5adSToby Isaac     while (next) {
767847a1f5adSToby Isaac       DMBoundary b = next;
767947a1f5adSToby Isaac 
768047a1f5adSToby Isaac       next = b->next;
768147a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7682a6ba4734SToby Isaac     }
768347a1f5adSToby Isaac     dm->boundary = NULL;
7684a6ba4734SToby Isaac   }
768547a1f5adSToby Isaac 
7686dff059c6SToby Isaac   lastnext = &(dm->boundary);
7687e6f8dbb6SToby Isaac   while (dsbound) {
7688e6f8dbb6SToby Isaac     DMBoundary dmbound;
7689e6f8dbb6SToby Isaac 
7690e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7691e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7692e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7693994fe344SLisandro 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);}
769447a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7695dff059c6SToby Isaac     *lastnext = dmbound;
7696dff059c6SToby Isaac     lastnext = &(dmbound->next);
7697dff059c6SToby Isaac     dsbound = dsbound->next;
7698a6ba4734SToby Isaac   }
7699a6ba4734SToby Isaac   PetscFunctionReturn(0);
7700a6ba4734SToby Isaac }
7701a6ba4734SToby Isaac 
7702a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7703a6ba4734SToby Isaac {
7704b95f2879SToby Isaac   DMBoundary     b;
7705a6ba4734SToby Isaac   PetscErrorCode ierr;
7706a6ba4734SToby Isaac 
7707a6ba4734SToby Isaac   PetscFunctionBegin;
7708a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7709534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7710a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7711e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7712b95f2879SToby Isaac   b = dm->boundary;
7713a6ba4734SToby Isaac   while (b && !(*isBd)) {
7714e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7715e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
77163424c85cSToby Isaac 
77173424c85cSToby Isaac     if (label) {
7718a6ba4734SToby Isaac       PetscInt i;
7719a6ba4734SToby Isaac 
7720e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7721e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7722a6ba4734SToby Isaac       }
7723a6ba4734SToby Isaac     }
7724a6ba4734SToby Isaac     b = b->next;
7725a6ba4734SToby Isaac   }
7726a6ba4734SToby Isaac   PetscFunctionReturn(0);
7727a6ba4734SToby Isaac }
77284d6f44ffSToby Isaac 
77294d6f44ffSToby Isaac /*@C
7730a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
7731a6e0b375SMatthew G. Knepley 
7732a6e0b375SMatthew G. Knepley   Collective on DM
77334d6f44ffSToby Isaac 
77344d6f44ffSToby Isaac   Input Parameters:
77354d6f44ffSToby Isaac + dm      - The DM
77360709b2feSToby Isaac . time    - The time
77374d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
77384d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
77394d6f44ffSToby Isaac - mode    - The insertion mode for values
77404d6f44ffSToby Isaac 
77414d6f44ffSToby Isaac   Output Parameter:
77424d6f44ffSToby Isaac . X - vector
77434d6f44ffSToby Isaac 
77444d6f44ffSToby Isaac    Calling sequence of func:
77450709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
77464d6f44ffSToby Isaac 
77474d6f44ffSToby Isaac +  dim - The spatial dimension
77484d6f44ffSToby Isaac .  x   - The coordinates
77494d6f44ffSToby Isaac .  Nf  - The number of fields
77504d6f44ffSToby Isaac .  u   - The output field values
77514d6f44ffSToby Isaac -  ctx - optional user-defined function context
77524d6f44ffSToby Isaac 
77534d6f44ffSToby Isaac   Level: developer
77544d6f44ffSToby Isaac 
7755a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
77564d6f44ffSToby Isaac @*/
77570709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
77584d6f44ffSToby Isaac {
77594d6f44ffSToby Isaac   Vec            localX;
77604d6f44ffSToby Isaac   PetscErrorCode ierr;
77614d6f44ffSToby Isaac 
77624d6f44ffSToby Isaac   PetscFunctionBegin;
77634d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77644d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
77650709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77664d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
77674d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
77684d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
77694d6f44ffSToby Isaac   PetscFunctionReturn(0);
77704d6f44ffSToby Isaac }
77714d6f44ffSToby Isaac 
7772a6e0b375SMatthew G. Knepley /*@C
7773a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
7774a6e0b375SMatthew G. Knepley 
7775a6e0b375SMatthew G. Knepley   Not collective
7776a6e0b375SMatthew G. Knepley 
7777a6e0b375SMatthew G. Knepley   Input Parameters:
7778a6e0b375SMatthew G. Knepley + dm      - The DM
7779a6e0b375SMatthew G. Knepley . time    - The time
7780a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7781a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7782a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7783a6e0b375SMatthew G. Knepley 
7784a6e0b375SMatthew G. Knepley   Output Parameter:
7785a6e0b375SMatthew G. Knepley . localX - vector
7786a6e0b375SMatthew G. Knepley 
7787a6e0b375SMatthew G. Knepley    Calling sequence of func:
7788a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7789a6e0b375SMatthew G. Knepley 
7790a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7791a6e0b375SMatthew G. Knepley .  x   - The coordinates
7792a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7793a6e0b375SMatthew G. Knepley .  u   - The output field values
7794a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7795a6e0b375SMatthew G. Knepley 
7796a6e0b375SMatthew G. Knepley   Level: developer
7797a6e0b375SMatthew G. Knepley 
7798a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
7799a6e0b375SMatthew G. Knepley @*/
78000709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
78014d6f44ffSToby Isaac {
78024d6f44ffSToby Isaac   PetscErrorCode ierr;
78034d6f44ffSToby Isaac 
78044d6f44ffSToby Isaac   PetscFunctionBegin;
78054d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78064d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
78070918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
78080709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
78094d6f44ffSToby Isaac   PetscFunctionReturn(0);
78104d6f44ffSToby Isaac }
78114d6f44ffSToby Isaac 
7812a6e0b375SMatthew G. Knepley /*@C
7813a6e0b375SMatthew 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.
7814a6e0b375SMatthew G. Knepley 
7815a6e0b375SMatthew G. Knepley   Collective on DM
7816a6e0b375SMatthew G. Knepley 
7817a6e0b375SMatthew G. Knepley   Input Parameters:
7818a6e0b375SMatthew G. Knepley + dm      - The DM
7819a6e0b375SMatthew G. Knepley . time    - The time
7820a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7821a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7822a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7823a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7824a6e0b375SMatthew G. Knepley 
7825a6e0b375SMatthew G. Knepley   Output Parameter:
7826a6e0b375SMatthew G. Knepley . X - vector
7827a6e0b375SMatthew G. Knepley 
7828a6e0b375SMatthew G. Knepley    Calling sequence of func:
7829a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7830a6e0b375SMatthew G. Knepley 
7831a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7832a6e0b375SMatthew G. Knepley .  x   - The coordinates
7833a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7834a6e0b375SMatthew G. Knepley .  u   - The output field values
7835a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7836a6e0b375SMatthew G. Knepley 
7837a6e0b375SMatthew G. Knepley   Level: developer
7838a6e0b375SMatthew G. Knepley 
7839a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
7840a6e0b375SMatthew G. Knepley @*/
78412c53366bSMatthew 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)
78422c53366bSMatthew G. Knepley {
78432c53366bSMatthew G. Knepley   Vec            localX;
78442c53366bSMatthew G. Knepley   PetscErrorCode ierr;
78452c53366bSMatthew G. Knepley 
78462c53366bSMatthew G. Knepley   PetscFunctionBegin;
78472c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78482c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
78492c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
78502c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
78512c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
78522c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
78532c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
78542c53366bSMatthew G. Knepley }
78552c53366bSMatthew G. Knepley 
7856a6e0b375SMatthew G. Knepley /*@C
7857a6e0b375SMatthew 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.
7858a6e0b375SMatthew G. Knepley 
7859a6e0b375SMatthew G. Knepley   Not collective
7860a6e0b375SMatthew G. Knepley 
7861a6e0b375SMatthew G. Knepley   Input Parameters:
7862a6e0b375SMatthew G. Knepley + dm      - The DM
7863a6e0b375SMatthew G. Knepley . time    - The time
7864a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7865a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7866a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7867a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7868a6e0b375SMatthew G. Knepley 
7869a6e0b375SMatthew G. Knepley   Output Parameter:
7870a6e0b375SMatthew G. Knepley . localX - vector
7871a6e0b375SMatthew G. Knepley 
7872a6e0b375SMatthew G. Knepley    Calling sequence of func:
7873a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7874a6e0b375SMatthew G. Knepley 
7875a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7876a6e0b375SMatthew G. Knepley .  x   - The coordinates
7877a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7878a6e0b375SMatthew G. Knepley .  u   - The output field values
7879a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7880a6e0b375SMatthew G. Knepley 
7881a6e0b375SMatthew G. Knepley   Level: developer
7882a6e0b375SMatthew G. Knepley 
7883a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
7884a6e0b375SMatthew G. Knepley @*/
78851c531cf8SMatthew 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)
78864d6f44ffSToby Isaac {
78874d6f44ffSToby Isaac   PetscErrorCode ierr;
78884d6f44ffSToby Isaac 
78894d6f44ffSToby Isaac   PetscFunctionBegin;
78904d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78914d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
78920918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
78931c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
78944d6f44ffSToby Isaac   PetscFunctionReturn(0);
78954d6f44ffSToby Isaac }
78962716604bSToby Isaac 
7897a6e0b375SMatthew G. Knepley /*@C
7898a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
7899a6e0b375SMatthew G. Knepley 
7900a6e0b375SMatthew G. Knepley   Not collective
7901a6e0b375SMatthew G. Knepley 
7902a6e0b375SMatthew G. Knepley   Input Parameters:
7903a6e0b375SMatthew G. Knepley + dm      - The DM
7904a6e0b375SMatthew G. Knepley . time    - The time
7905a6e0b375SMatthew G. Knepley . localU  - The input field vector
7906a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7907a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7908a6e0b375SMatthew G. Knepley 
7909a6e0b375SMatthew G. Knepley   Output Parameter:
7910a6e0b375SMatthew G. Knepley . localX  - The output vector
7911a6e0b375SMatthew G. Knepley 
7912a6e0b375SMatthew G. Knepley    Calling sequence of func:
7913a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7914a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7915a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7916a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7917a6e0b375SMatthew G. Knepley 
7918a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7919a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7920a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7921a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7922a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7923a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7924a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7925a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7926a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7927a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7928a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7929a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7930a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7931a6e0b375SMatthew G. Knepley .  t            - The current time
7932a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7933a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7934a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7935a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7936a6e0b375SMatthew G. Knepley 
7937a6e0b375SMatthew 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.
7938a6e0b375SMatthew 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
7939a6e0b375SMatthew 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
7940a6e0b375SMatthew 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.
7941a6e0b375SMatthew G. Knepley 
7942a6e0b375SMatthew G. Knepley   Level: intermediate
7943a6e0b375SMatthew G. Knepley 
7944a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
7945a6e0b375SMatthew G. Knepley @*/
79468c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
79478c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
79488c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79498c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7950191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
79518c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
79528c6c5593SMatthew G. Knepley {
79538c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
79548c6c5593SMatthew G. Knepley 
79558c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79568c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
79588c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
79590918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
79608c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
79618c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
79628c6c5593SMatthew G. Knepley }
79638c6c5593SMatthew G. Knepley 
7964a6e0b375SMatthew G. Knepley /*@C
7965a6e0b375SMatthew 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.
7966a6e0b375SMatthew G. Knepley 
7967a6e0b375SMatthew G. Knepley   Not collective
7968a6e0b375SMatthew G. Knepley 
7969a6e0b375SMatthew G. Knepley   Input Parameters:
7970a6e0b375SMatthew G. Knepley + dm      - The DM
7971a6e0b375SMatthew G. Knepley . time    - The time
7972a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
7973a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
7974a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
7975a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
7976a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7977a6e0b375SMatthew G. Knepley . localU  - The input field vector
7978a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7979a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7980a6e0b375SMatthew G. Knepley 
7981a6e0b375SMatthew G. Knepley   Output Parameter:
7982a6e0b375SMatthew G. Knepley . localX  - The output vector
7983a6e0b375SMatthew G. Knepley 
7984a6e0b375SMatthew G. Knepley    Calling sequence of func:
7985a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7986a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7987a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7988a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7989a6e0b375SMatthew G. Knepley 
7990a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7991a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7992a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7993a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7994a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7995a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7996a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7997a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7998a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7999a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8000a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8001a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8002a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8003a6e0b375SMatthew G. Knepley .  t            - The current time
8004a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8005a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8006a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8007a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8008a6e0b375SMatthew G. Knepley 
8009a6e0b375SMatthew 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.
8010a6e0b375SMatthew 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
8011a6e0b375SMatthew 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
8012a6e0b375SMatthew 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.
8013a6e0b375SMatthew G. Knepley 
8014a6e0b375SMatthew G. Knepley   Level: intermediate
8015a6e0b375SMatthew G. Knepley 
8016a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8017a6e0b375SMatthew G. Knepley @*/
80181c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
80198c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
80208c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
80218c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8022191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
80238c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
80248c6c5593SMatthew G. Knepley {
80258c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
80268c6c5593SMatthew G. Knepley 
80278c6c5593SMatthew G. Knepley   PetscFunctionBegin;
80288c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80298c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
80308c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
80310918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
80321c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
80338c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
80348c6c5593SMatthew G. Knepley }
80358c6c5593SMatthew G. Knepley 
80362716604bSToby Isaac /*@C
80372716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
80382716604bSToby Isaac 
80392716604bSToby Isaac   Input Parameters:
80402716604bSToby Isaac + dm    - The DM
80410709b2feSToby Isaac . time  - The time
80422716604bSToby Isaac . funcs - The functions to evaluate for each field component
80432716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8044574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
80452716604bSToby Isaac 
80462716604bSToby Isaac   Output Parameter:
80472716604bSToby Isaac . diff - The diff ||u - u_h||_2
80482716604bSToby Isaac 
80492716604bSToby Isaac   Level: developer
80502716604bSToby Isaac 
80511189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
80522716604bSToby Isaac @*/
80530709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
80542716604bSToby Isaac {
80552716604bSToby Isaac   PetscErrorCode ierr;
80562716604bSToby Isaac 
80572716604bSToby Isaac   PetscFunctionBegin;
80582716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8059b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80600918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
80610709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
80622716604bSToby Isaac   PetscFunctionReturn(0);
80632716604bSToby Isaac }
8064b698f381SToby Isaac 
8065b698f381SToby Isaac /*@C
8066b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8067b698f381SToby Isaac 
8068d083f849SBarry Smith   Collective on dm
8069d083f849SBarry Smith 
8070b698f381SToby Isaac   Input Parameters:
8071b698f381SToby Isaac + dm    - The DM
8072b698f381SToby Isaac , time  - The time
8073b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8074b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8075574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8076b698f381SToby Isaac - n     - The vector to project along
8077b698f381SToby Isaac 
8078b698f381SToby Isaac   Output Parameter:
8079b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8080b698f381SToby Isaac 
8081b698f381SToby Isaac   Level: developer
8082b698f381SToby Isaac 
8083b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8084b698f381SToby Isaac @*/
8085b698f381SToby 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)
8086b698f381SToby Isaac {
8087b698f381SToby Isaac   PetscErrorCode ierr;
8088b698f381SToby Isaac 
8089b698f381SToby Isaac   PetscFunctionBegin;
8090b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8091b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8092b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8093b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8094b698f381SToby Isaac   PetscFunctionReturn(0);
8095b698f381SToby Isaac }
8096b698f381SToby Isaac 
80972a16baeaSToby Isaac /*@C
80982a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
80992a16baeaSToby Isaac 
8100d083f849SBarry Smith   Collective on dm
8101d083f849SBarry Smith 
81022a16baeaSToby Isaac   Input Parameters:
81032a16baeaSToby Isaac + dm    - The DM
81042a16baeaSToby Isaac . time  - The time
81052a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
81062a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8107574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
81082a16baeaSToby Isaac 
81092a16baeaSToby Isaac   Output Parameter:
81102a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
81112a16baeaSToby Isaac 
81122a16baeaSToby Isaac   Level: developer
81132a16baeaSToby Isaac 
81141189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
81152a16baeaSToby Isaac @*/
81161189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
81172a16baeaSToby Isaac {
81182a16baeaSToby Isaac   PetscErrorCode ierr;
81192a16baeaSToby Isaac 
81202a16baeaSToby Isaac   PetscFunctionBegin;
81212a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81222a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
81230918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
81242a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
81252a16baeaSToby Isaac   PetscFunctionReturn(0);
81262a16baeaSToby Isaac }
81272a16baeaSToby Isaac 
8128df0b854cSToby Isaac /*@C
8129df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8130cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8131df0b854cSToby Isaac 
8132df0b854cSToby Isaac   Collective on dm
8133df0b854cSToby Isaac 
8134df0b854cSToby Isaac   Input parameters:
8135df0b854cSToby Isaac + dm - the pre-adaptation DM object
8136a1b0c543SToby Isaac - label - label with the flags
8137df0b854cSToby Isaac 
8138df0b854cSToby Isaac   Output parameters:
81390d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8140df0b854cSToby Isaac 
8141df0b854cSToby Isaac   Level: intermediate
81420d1cd5e0SMatthew G. Knepley 
81430d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8144df0b854cSToby Isaac @*/
81450d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8146df0b854cSToby Isaac {
8147df0b854cSToby Isaac   PetscErrorCode ierr;
8148df0b854cSToby Isaac 
8149df0b854cSToby Isaac   PetscFunctionBegin;
8150df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8151a1b0c543SToby Isaac   PetscValidPointer(label,2);
81520d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
81530d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
81546f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
81550d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
81560d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
81570d1cd5e0SMatthew G. Knepley }
81580d1cd5e0SMatthew G. Knepley 
81590d1cd5e0SMatthew G. Knepley /*@C
81600d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
81610d1cd5e0SMatthew G. Knepley 
81620d1cd5e0SMatthew G. Knepley   Input Parameters:
81630d1cd5e0SMatthew G. Knepley + dm - The DM object
81640d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
81656f25b0d8SLisandro 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_".
81660d1cd5e0SMatthew G. Knepley 
81670d1cd5e0SMatthew G. Knepley   Output Parameter:
81680d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
81690d1cd5e0SMatthew G. Knepley 
81700d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
81710d1cd5e0SMatthew G. Knepley 
81720d1cd5e0SMatthew G. Knepley   Level: advanced
81730d1cd5e0SMatthew G. Knepley 
81740d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
81750d1cd5e0SMatthew G. Knepley @*/
81760d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
81770d1cd5e0SMatthew G. Knepley {
81780d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
81790d1cd5e0SMatthew G. Knepley 
81800d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
81810d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81820d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
81830d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
81840d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
81856f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
81866f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
81870d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8188df0b854cSToby Isaac   PetscFunctionReturn(0);
8189df0b854cSToby Isaac }
8190c4088d22SMatthew G. Knepley 
8191502a2867SDave May /*@C
8192502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8193502a2867SDave May 
8194502a2867SDave May  Not Collective
8195502a2867SDave May 
8196502a2867SDave May  Input Parameter:
8197502a2867SDave May  . dm    - The DM
8198502a2867SDave May 
8199502a2867SDave May  Output Parameter:
8200502a2867SDave May  . nranks - the number of neighbours
8201502a2867SDave May  . ranks - the neighbors ranks
8202502a2867SDave May 
8203502a2867SDave May  Notes:
8204502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8205502a2867SDave May 
8206502a2867SDave May  Level: beginner
8207502a2867SDave May 
8208dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8209502a2867SDave May @*/
8210502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8211502a2867SDave May {
8212502a2867SDave May   PetscErrorCode ierr;
8213502a2867SDave May 
8214502a2867SDave May   PetscFunctionBegin;
8215502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82160918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8217502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8218502a2867SDave May   PetscFunctionReturn(0);
8219502a2867SDave May }
8220502a2867SDave May 
8221531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8222531c7667SBarry Smith 
8223531c7667SBarry Smith /*
8224531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8225531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8226531c7667SBarry Smith */
8227531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8228531c7667SBarry Smith {
8229531c7667SBarry Smith   PetscErrorCode ierr;
8230531c7667SBarry Smith 
8231531c7667SBarry Smith   PetscFunctionBegin;
8232531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8233531c7667SBarry Smith     Vec x1local;
8234531c7667SBarry Smith     DM  dm;
8235531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8236531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8237531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8238531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8239531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8240531c7667SBarry Smith     x1   = x1local;
8241531c7667SBarry Smith   }
8242531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8243531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8244531c7667SBarry Smith     DM  dm;
8245531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8246531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8247531c7667SBarry Smith   }
8248531c7667SBarry Smith   PetscFunctionReturn(0);
8249531c7667SBarry Smith }
8250531c7667SBarry Smith 
8251531c7667SBarry Smith /*@
8252531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8253531c7667SBarry Smith 
8254531c7667SBarry Smith     Input Parameter:
8255531c7667SBarry Smith .    coloring - the MatFDColoring object
8256531c7667SBarry Smith 
825795452b02SPatrick Sanan     Developer Notes:
825895452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8259531c7667SBarry Smith 
82601b266c99SBarry Smith     Level: advanced
82611b266c99SBarry Smith 
8262531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8263531c7667SBarry Smith @*/
8264531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8265531c7667SBarry Smith {
8266531c7667SBarry Smith   PetscFunctionBegin;
8267531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8268531c7667SBarry Smith   PetscFunctionReturn(0);
8269531c7667SBarry Smith }
82708320bc6fSPatrick Sanan 
82718320bc6fSPatrick Sanan /*@
82728320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
82738320bc6fSPatrick Sanan 
82748320bc6fSPatrick Sanan     Collective
82758320bc6fSPatrick Sanan 
82768320bc6fSPatrick Sanan     Input Parameters:
82778320bc6fSPatrick Sanan +    dm - the first DM
82788320bc6fSPatrick Sanan -    dm2 - the second DM
82798320bc6fSPatrick Sanan 
82808320bc6fSPatrick Sanan     Output Parameters:
82818320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
82828320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
82838320bc6fSPatrick Sanan 
82848320bc6fSPatrick Sanan     Notes:
82858320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
82863d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
82878320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
82883d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
82893d862458SPatrick Sanan     decomposition, but hold different data.
82908320bc6fSPatrick Sanan 
82918320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
82928320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
82938320bc6fSPatrick Sanan 
82948320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
82958320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
82968320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
82978320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
82988320bc6fSPatrick Sanan 
82998320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
83008320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
83018320bc6fSPatrick Sanan .vb
83028320bc6fSPatrick Sanan   ...
83038320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
83048320bc6fSPatrick Sanan   if (set && compatible)  {
83058320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
83068320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
83073d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
83088320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
83098320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
83108320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
83118320bc6fSPatrick Sanan       }
83128320bc6fSPatrick Sanan     }
83138320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
83148320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
83158320bc6fSPatrick Sanan   } else {
83168320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
83178320bc6fSPatrick Sanan   }
83188320bc6fSPatrick Sanan   ...
83198320bc6fSPatrick Sanan .ve
83208320bc6fSPatrick Sanan 
83218320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
83228320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
83238320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
83248320bc6fSPatrick Sanan     always check the "set" output parameter.
83258320bc6fSPatrick Sanan 
83268320bc6fSPatrick Sanan     A DM is always compatible with itself.
83278320bc6fSPatrick Sanan 
83288320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
83298320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
83308320bc6fSPatrick Sanan     incompatible.
83318320bc6fSPatrick Sanan 
83328320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
83338320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
83348320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
83358320bc6fSPatrick Sanan 
83368320bc6fSPatrick Sanan     Developer Notes:
83373d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
83383d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
83398320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
83408320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
83418320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
83423d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
83433d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
83448320bc6fSPatrick Sanan 
83458320bc6fSPatrick Sanan     Level: advanced
83468320bc6fSPatrick Sanan 
83473d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
83488320bc6fSPatrick Sanan @*/
83498320bc6fSPatrick Sanan 
83508320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
83518320bc6fSPatrick Sanan {
83528320bc6fSPatrick Sanan   PetscErrorCode ierr;
83538320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
83548320bc6fSPatrick Sanan   DMType         type,type2;
83558320bc6fSPatrick Sanan   PetscBool      sameType;
83568320bc6fSPatrick Sanan 
83578320bc6fSPatrick Sanan   PetscFunctionBegin;
83588320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83598320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83608320bc6fSPatrick Sanan 
83618320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
83628320bc6fSPatrick Sanan   if (dm == dm2) {
83638320bc6fSPatrick Sanan     *set = PETSC_TRUE;
83648320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
83658320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83668320bc6fSPatrick Sanan   }
83678320bc6fSPatrick Sanan 
83688320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
83698320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
83708320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
83718320bc6fSPatrick Sanan      determined by the implementation-specific logic */
83728320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
83738320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
83748320bc6fSPatrick Sanan     *set = PETSC_TRUE;
83758320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
83768320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83778320bc6fSPatrick Sanan   }
83788320bc6fSPatrick Sanan 
83798320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
83808320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
83818320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
8382b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
83838320bc6fSPatrick Sanan   }
83848320bc6fSPatrick Sanan 
83858320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
83868320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
83878320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
83888320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
83898320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
83908320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
83918320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
83928320bc6fSPatrick Sanan   } else {
83938320bc6fSPatrick Sanan     *set = PETSC_FALSE;
83948320bc6fSPatrick Sanan   }
83958320bc6fSPatrick Sanan   PetscFunctionReturn(0);
83968320bc6fSPatrick Sanan }
8397c0f0dcc3SMatthew G. Knepley 
8398c0f0dcc3SMatthew G. Knepley /*@C
8399c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8400c0f0dcc3SMatthew G. Knepley 
8401c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8402c0f0dcc3SMatthew G. Knepley 
8403c0f0dcc3SMatthew G. Knepley   Input Parameters:
8404c0f0dcc3SMatthew G. Knepley + DM - the DM
8405c0f0dcc3SMatthew G. Knepley . f - the monitor function
8406c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8407c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8408c0f0dcc3SMatthew G. Knepley 
8409c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8410c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8411c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8412c0f0dcc3SMatthew G. Knepley 
8413c0f0dcc3SMatthew G. Knepley   Notes:
8414c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8415c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8416c0f0dcc3SMatthew G. Knepley   order in which they were set.
8417c0f0dcc3SMatthew G. Knepley 
8418c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8419c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8420c0f0dcc3SMatthew G. Knepley 
8421c0f0dcc3SMatthew G. Knepley   Level: intermediate
8422c0f0dcc3SMatthew G. Knepley 
8423c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8424c0f0dcc3SMatthew G. Knepley @*/
8425c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8426c0f0dcc3SMatthew G. Knepley {
8427c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8428c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8429c0f0dcc3SMatthew G. Knepley 
8430c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8431c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8432c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8433c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8434c0f0dcc3SMatthew G. Knepley 
8435c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8436c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8437c0f0dcc3SMatthew G. Knepley   }
8438c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8439c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8440c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8441c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8442c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8443c0f0dcc3SMatthew G. Knepley }
8444c0f0dcc3SMatthew G. Knepley 
8445c0f0dcc3SMatthew G. Knepley /*@
8446c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8447c0f0dcc3SMatthew G. Knepley 
8448c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8449c0f0dcc3SMatthew G. Knepley 
8450c0f0dcc3SMatthew G. Knepley   Input Parameter:
8451c0f0dcc3SMatthew G. Knepley . dm - the DM
8452c0f0dcc3SMatthew G. Knepley 
8453c0f0dcc3SMatthew G. Knepley   Options Database Key:
8454c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8455c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8456c0f0dcc3SMatthew G. Knepley   set via the options database
8457c0f0dcc3SMatthew G. Knepley 
8458c0f0dcc3SMatthew G. Knepley   Notes:
8459c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8460c0f0dcc3SMatthew G. Knepley 
8461c0f0dcc3SMatthew G. Knepley   Level: intermediate
8462c0f0dcc3SMatthew G. Knepley 
8463c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8464c0f0dcc3SMatthew G. Knepley @*/
8465c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8466c0f0dcc3SMatthew G. Knepley {
8467c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8468c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8469c0f0dcc3SMatthew G. Knepley 
8470c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8471c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8472c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8473c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
8474c0f0dcc3SMatthew G. Knepley   }
8475c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8476c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8477c0f0dcc3SMatthew G. Knepley }
8478c0f0dcc3SMatthew G. Knepley 
8479c0f0dcc3SMatthew G. Knepley /*@C
8480c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8481c0f0dcc3SMatthew G. Knepley 
8482c0f0dcc3SMatthew G. Knepley   Collective on DM
8483c0f0dcc3SMatthew G. Knepley 
8484c0f0dcc3SMatthew G. Knepley   Input Parameters:
8485c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8486c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8487c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8488c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8489c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8490c0f0dcc3SMatthew 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
8491c0f0dcc3SMatthew G. Knepley 
8492c0f0dcc3SMatthew G. Knepley   Output Parameter:
8493c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8494c0f0dcc3SMatthew G. Knepley 
8495c0f0dcc3SMatthew G. Knepley   Level: developer
8496c0f0dcc3SMatthew G. Knepley 
8497c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
8498c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
8499c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
8500c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
8501c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
8502c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
8503c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
8504c0f0dcc3SMatthew G. Knepley @*/
8505c0f0dcc3SMatthew 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)
8506c0f0dcc3SMatthew G. Knepley {
8507c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8508c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8509c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
8510c0f0dcc3SMatthew G. Knepley 
8511c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8512c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8513c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
8514c0f0dcc3SMatthew G. Knepley   if (*flg) {
8515c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8516c0f0dcc3SMatthew G. Knepley 
8517c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
8518c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
8519c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
8520c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
8521c0f0dcc3SMatthew G. Knepley   }
8522c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8523c0f0dcc3SMatthew G. Knepley }
8524c0f0dcc3SMatthew G. Knepley 
8525c0f0dcc3SMatthew G. Knepley /*@
8526c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8527c0f0dcc3SMatthew G. Knepley 
8528c0f0dcc3SMatthew G. Knepley    Collective on DM
8529c0f0dcc3SMatthew G. Knepley 
8530c0f0dcc3SMatthew G. Knepley    Input Parameters:
8531c0f0dcc3SMatthew G. Knepley .  dm - The DM
8532c0f0dcc3SMatthew G. Knepley 
8533c0f0dcc3SMatthew G. Knepley    Level: developer
8534c0f0dcc3SMatthew G. Knepley 
8535c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8536c0f0dcc3SMatthew G. Knepley @*/
8537c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8538c0f0dcc3SMatthew G. Knepley {
8539c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8540c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8541c0f0dcc3SMatthew G. Knepley 
8542c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8543c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8544c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8545c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8546c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
8547c0f0dcc3SMatthew G. Knepley   }
8548c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8549c0f0dcc3SMatthew G. Knepley }
8550