xref: /petsc/src/dm/interface/dm.c (revision f2cacb80554376ba60c1c183c00cd40ea8a0a80d)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
900d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
1000d952a4SJed Brown #  include <valgrind/memcheck.h>
1100d952a4SJed Brown #endif
1200d952a4SJed Brown 
13732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
14d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
1558cd63d5SVaclav 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;
1667a56275SMatthew G Knepley 
17e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
1840967b3bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_",0};
19412e9a14SMatthew G. Knepley const char *const DMPolytopeTypes[] = {"vertex", "segment", "tensor_segment", "triangle", "quadrilateral", "tensor_quad", "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "FV_ghost_cell", "unknown", "DMPolytopeType", "DM_POLYTOPE_", 0};
20a4121054SBarry Smith /*@
21de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
22a4121054SBarry Smith 
23a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
24a4121054SBarry Smith    error when you try to use the vector.
25a4121054SBarry Smith 
26d083f849SBarry Smith   Collective
27a4121054SBarry Smith 
28a4121054SBarry Smith   Input Parameter:
29a4121054SBarry Smith . comm - The communicator for the DM object
30a4121054SBarry Smith 
31a4121054SBarry Smith   Output Parameter:
32a4121054SBarry Smith . dm - The DM object
33a4121054SBarry Smith 
34a4121054SBarry Smith   Level: beginner
35a4121054SBarry Smith 
368472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
37a4121054SBarry Smith @*/
387087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
39a4121054SBarry Smith {
40a4121054SBarry Smith   DM             v;
41e5e52638SMatthew G. Knepley   PetscDS        ds;
42a4121054SBarry Smith   PetscErrorCode ierr;
43a4121054SBarry Smith 
44a4121054SBarry Smith   PetscFunctionBegin;
451411c6eeSJed Brown   PetscValidPointer(dm,2);
460298fd71SBarry Smith   *dm = NULL;
47607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
48a4121054SBarry Smith 
4973107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
50e7c4fc90SDmitry Karpeev 
5149be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5249be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
530298fd71SBarry Smith   v->ltogmap                  = NULL;
541411c6eeSJed Brown   v->bs                       = 1;
55171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5688ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
571bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
58c58f1c22SToby Isaac   v->labels                   = NULL;
5934aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6034aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
61c58f1c22SToby Isaac   v->depthLabel               = NULL;
62ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
631bb6d2a8SBarry Smith   v->localSection             = NULL;
641bb6d2a8SBarry Smith   v->globalSection            = NULL;
65fba222abSToby Isaac   v->defaultConstraintSection = NULL;
66fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
67c6b900c6SMatthew G. Knepley   v->L                        = NULL;
68c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
695dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
709a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7196173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
72435a35e8SMatthew G Knepley   {
73435a35e8SMatthew G Knepley     PetscInt i;
74435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
750298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
76f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
77435a35e8SMatthew G Knepley     }
78435a35e8SMatthew G Knepley   }
79e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
80b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
81e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
8214f150ffSMatthew G. Knepley   v->dmBC = NULL;
83a8fb8f29SToby Isaac   v->coarseMesh = NULL;
84f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
85cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
86c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
87b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
884a7a4c06SLawrence Mitchell 
891411c6eeSJed Brown   *dm = v;
90a4121054SBarry Smith   PetscFunctionReturn(0);
91a4121054SBarry Smith }
92a4121054SBarry Smith 
9338221697SMatthew G. Knepley /*@
9438221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9538221697SMatthew G. Knepley 
96d083f849SBarry Smith   Collective
9738221697SMatthew G. Knepley 
9838221697SMatthew G. Knepley   Input Parameter:
9938221697SMatthew G. Knepley . dm - The original DM object
10038221697SMatthew G. Knepley 
10138221697SMatthew G. Knepley   Output Parameter:
10238221697SMatthew G. Knepley . newdm  - The new DM object
10338221697SMatthew G. Knepley 
10438221697SMatthew G. Knepley   Level: beginner
10538221697SMatthew G. Knepley 
1061cb8cacdSPatrick Sanan   Notes:
1071cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1081cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1091cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1101bb6d2a8SBarry Smith 
11189706ed2SPatrick Sanan   The clone is considered set up iff the original is.
11289706ed2SPatrick Sanan 
11392cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1141bb6d2a8SBarry Smith 
11538221697SMatthew G. Knepley @*/
11638221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11738221697SMatthew G. Knepley {
11838221697SMatthew G. Knepley   PetscSF        sf;
11938221697SMatthew G. Knepley   Vec            coords;
12038221697SMatthew G. Knepley   void          *ctx;
121a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12238221697SMatthew G. Knepley   PetscErrorCode ierr;
12338221697SMatthew G. Knepley 
12438221697SMatthew G. Knepley   PetscFunctionBegin;
12538221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12638221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12738221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1285d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
129ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
130ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1311de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1321de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13338221697SMatthew G. Knepley   if (dm->ops->clone) {
13438221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   }
1363f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13738221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13838221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
141be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
142be4c1c3eSMatthew G. Knepley     DM           ncdm;
143be4c1c3eSMatthew G. Knepley     PetscSection cs;
1445a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
145be4c1c3eSMatthew G. Knepley 
14692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
147be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1485a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1495a0206caSToby Isaac     if (pEndMax >= 0) {
150be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
15183bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
15292fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
153a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
154be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
155be4c1c3eSMatthew G. Knepley     }
156be4c1c3eSMatthew G. Knepley   }
157a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
158a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15938221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16038221697SMatthew G. Knepley   if (coords) {
16138221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
16238221697SMatthew G. Knepley   } else {
16338221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16438221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16538221697SMatthew G. Knepley   }
16690b157c4SStefano Zampini   {
16790b157c4SStefano Zampini     PetscBool             isper;
168c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1695dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17090b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17190b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
172c6b900c6SMatthew G. Knepley   }
17334aa8a36SMatthew G. Knepley   {
17434aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17534aa8a36SMatthew G. Knepley 
17634aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17834aa8a36SMatthew G. Knepley   }
17938221697SMatthew G. Knepley   PetscFunctionReturn(0);
18038221697SMatthew G. Knepley }
18138221697SMatthew G. Knepley 
1829a42bb27SBarry Smith /*@C
183564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1849a42bb27SBarry Smith 
185d083f849SBarry Smith    Logically Collective on da
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith    Input Parameter:
1889a42bb27SBarry Smith +  da - initial distributed array
189e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1909a42bb27SBarry Smith 
1919a42bb27SBarry Smith    Options Database:
192dd85299cSBarry Smith .   -dm_vec_type ctype
1939a42bb27SBarry Smith 
1949a42bb27SBarry Smith    Level: intermediate
1959a42bb27SBarry Smith 
196a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1979a42bb27SBarry Smith @*/
19819fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1999a42bb27SBarry Smith {
2009a42bb27SBarry Smith   PetscErrorCode ierr;
2019a42bb27SBarry Smith 
2029a42bb27SBarry Smith   PetscFunctionBegin;
2039a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2049a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2069a42bb27SBarry Smith   PetscFunctionReturn(0);
2079a42bb27SBarry Smith }
2089a42bb27SBarry Smith 
209c0dedaeaSBarry Smith /*@C
210c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
211c0dedaeaSBarry Smith 
212d083f849SBarry Smith    Logically Collective on da
213c0dedaeaSBarry Smith 
214c0dedaeaSBarry Smith    Input Parameter:
215c0dedaeaSBarry Smith .  da - initial distributed array
216c0dedaeaSBarry Smith 
217c0dedaeaSBarry Smith    Output Parameter:
218c0dedaeaSBarry Smith .  ctype - the vector type
219c0dedaeaSBarry Smith 
220c0dedaeaSBarry Smith    Level: intermediate
221c0dedaeaSBarry Smith 
222a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
223c0dedaeaSBarry Smith @*/
224c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
225c0dedaeaSBarry Smith {
226c0dedaeaSBarry Smith   PetscFunctionBegin;
227c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
228c0dedaeaSBarry Smith   *ctype = da->vectype;
229c0dedaeaSBarry Smith   PetscFunctionReturn(0);
230c0dedaeaSBarry Smith }
231c0dedaeaSBarry Smith 
2325f1ad066SMatthew G Knepley /*@
23334f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2345f1ad066SMatthew G Knepley 
2355f1ad066SMatthew G Knepley   Not collective
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley   Input Parameter:
2385f1ad066SMatthew G Knepley . v - The Vec
2395f1ad066SMatthew G Knepley 
2405f1ad066SMatthew G Knepley   Output Parameter:
2415f1ad066SMatthew G Knepley . dm - The DM
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley   Level: intermediate
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2465f1ad066SMatthew G Knepley @*/
2475f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2485f1ad066SMatthew G Knepley {
2495f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley   PetscFunctionBegin;
2525f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2535f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2545f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2555f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2565f1ad066SMatthew G Knepley }
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley /*@
259d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2605f1ad066SMatthew G Knepley 
2615f1ad066SMatthew G Knepley   Not collective
2625f1ad066SMatthew G Knepley 
2635f1ad066SMatthew G Knepley   Input Parameters:
2645f1ad066SMatthew G Knepley + v - The Vec
2655f1ad066SMatthew G Knepley - dm - The DM
2665f1ad066SMatthew G Knepley 
267d9805387SMatthew G. Knepley   Note: This is NOT the same as DMCreateGlobalVector() since it does not change the view methods or perform other customization, but merely sets the DM member.
268d9805387SMatthew G. Knepley 
2695f1ad066SMatthew G Knepley   Level: intermediate
2705f1ad066SMatthew G Knepley 
2715f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2725f1ad066SMatthew G Knepley @*/
2735f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2745f1ad066SMatthew G Knepley {
2755f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2765f1ad066SMatthew G Knepley 
2775f1ad066SMatthew G Knepley   PetscFunctionBegin;
2785f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
279d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2805f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2815f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2825f1ad066SMatthew G Knepley }
2835f1ad066SMatthew G Knepley 
284521d9a4cSLisandro Dalcin /*@C
2858f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2868f1509bcSBarry Smith 
287d083f849SBarry Smith    Logically Collective on dm
2888f1509bcSBarry Smith 
2898f1509bcSBarry Smith    Input Parameters:
2908f1509bcSBarry Smith +  dm - the DM context
2918f1509bcSBarry Smith -  ctype - the matrix type
2928f1509bcSBarry Smith 
2938f1509bcSBarry Smith    Options Database:
2948f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2958f1509bcSBarry Smith 
2968f1509bcSBarry Smith    Level: intermediate
2978f1509bcSBarry Smith 
2988f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2998f1509bcSBarry Smith           DMGetISColoringType()
3008f1509bcSBarry Smith @*/
3018f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3028f1509bcSBarry Smith {
3038f1509bcSBarry Smith   PetscFunctionBegin;
3048f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3058f1509bcSBarry Smith   dm->coloringtype = ctype;
3068f1509bcSBarry Smith   PetscFunctionReturn(0);
3078f1509bcSBarry Smith }
3088f1509bcSBarry Smith 
3098f1509bcSBarry Smith /*@C
3108f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
311521d9a4cSLisandro Dalcin 
312d083f849SBarry Smith    Logically Collective on dm
313521d9a4cSLisandro Dalcin 
314521d9a4cSLisandro Dalcin    Input Parameter:
3158f1509bcSBarry Smith .  dm - the DM context
3168f1509bcSBarry Smith 
3178f1509bcSBarry Smith    Output Parameter:
3188f1509bcSBarry Smith .  ctype - the matrix type
3198f1509bcSBarry Smith 
3208f1509bcSBarry Smith    Options Database:
3218f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3228f1509bcSBarry Smith 
3238f1509bcSBarry Smith    Level: intermediate
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3268f1509bcSBarry Smith           DMGetISColoringType()
3278f1509bcSBarry Smith @*/
3288f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3298f1509bcSBarry Smith {
3308f1509bcSBarry Smith   PetscFunctionBegin;
3318f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3328f1509bcSBarry Smith   *ctype = dm->coloringtype;
3338f1509bcSBarry Smith   PetscFunctionReturn(0);
3348f1509bcSBarry Smith }
3358f1509bcSBarry Smith 
3368f1509bcSBarry Smith /*@C
3378f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3388f1509bcSBarry Smith 
339d083f849SBarry Smith    Logically Collective on dm
3408f1509bcSBarry Smith 
3418f1509bcSBarry Smith    Input Parameters:
342521d9a4cSLisandro Dalcin +  dm - the DM context
343a2b5a043SBarry Smith -  ctype - the matrix type
344521d9a4cSLisandro Dalcin 
345521d9a4cSLisandro Dalcin    Options Database:
346521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
347521d9a4cSLisandro Dalcin 
348521d9a4cSLisandro Dalcin    Level: intermediate
349521d9a4cSLisandro Dalcin 
350a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
351521d9a4cSLisandro Dalcin @*/
35219fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
353521d9a4cSLisandro Dalcin {
354521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35588f0584fSBarry Smith 
356521d9a4cSLisandro Dalcin   PetscFunctionBegin;
357521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
358521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35919fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
360521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
361521d9a4cSLisandro Dalcin }
362521d9a4cSLisandro Dalcin 
363c0dedaeaSBarry Smith /*@C
364c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
365c0dedaeaSBarry Smith 
366d083f849SBarry Smith    Logically Collective on dm
367c0dedaeaSBarry Smith 
368c0dedaeaSBarry Smith    Input Parameter:
369c0dedaeaSBarry Smith .  dm - the DM context
370c0dedaeaSBarry Smith 
371c0dedaeaSBarry Smith    Output Parameter:
372c0dedaeaSBarry Smith .  ctype - the matrix type
373c0dedaeaSBarry Smith 
374c0dedaeaSBarry Smith    Options Database:
375c0dedaeaSBarry Smith .   -dm_mat_type ctype
376c0dedaeaSBarry Smith 
377c0dedaeaSBarry Smith    Level: intermediate
378c0dedaeaSBarry Smith 
379a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
380c0dedaeaSBarry Smith @*/
381c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
382c0dedaeaSBarry Smith {
383c0dedaeaSBarry Smith   PetscFunctionBegin;
384c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
385c0dedaeaSBarry Smith   *ctype = dm->mattype;
386c0dedaeaSBarry Smith   PetscFunctionReturn(0);
387c0dedaeaSBarry Smith }
388c0dedaeaSBarry Smith 
389c688c046SMatthew G Knepley /*@
39034f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
391c688c046SMatthew G Knepley 
392c688c046SMatthew G Knepley   Not collective
393c688c046SMatthew G Knepley 
394c688c046SMatthew G Knepley   Input Parameter:
395c688c046SMatthew G Knepley . A - The Mat
396c688c046SMatthew G Knepley 
397c688c046SMatthew G Knepley   Output Parameter:
398c688c046SMatthew G Knepley . dm - The DM
399c688c046SMatthew G Knepley 
400c688c046SMatthew G Knepley   Level: intermediate
401c688c046SMatthew G Knepley 
4028f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4038f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4048f1509bcSBarry Smith 
405c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
406c688c046SMatthew G Knepley @*/
407c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
408c688c046SMatthew G Knepley {
409c688c046SMatthew G Knepley   PetscErrorCode ierr;
410c688c046SMatthew G Knepley 
411c688c046SMatthew G Knepley   PetscFunctionBegin;
412c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
413c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
414c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
415c688c046SMatthew G Knepley   PetscFunctionReturn(0);
416c688c046SMatthew G Knepley }
417c688c046SMatthew G Knepley 
418c688c046SMatthew G Knepley /*@
419c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley   Not collective
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   Input Parameters:
424c688c046SMatthew G Knepley + A - The Mat
425c688c046SMatthew G Knepley - dm - The DM
426c688c046SMatthew G Knepley 
427c688c046SMatthew G Knepley   Level: intermediate
428c688c046SMatthew G Knepley 
4298f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4308f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4318f1509bcSBarry Smith 
4328f1509bcSBarry Smith 
433c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
434c688c046SMatthew G Knepley @*/
435c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
436c688c046SMatthew G Knepley {
437c688c046SMatthew G Knepley   PetscErrorCode ierr;
438c688c046SMatthew G Knepley 
439c688c046SMatthew G Knepley   PetscFunctionBegin;
440c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4418865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
442c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
443c688c046SMatthew G Knepley   PetscFunctionReturn(0);
444c688c046SMatthew G Knepley }
445c688c046SMatthew G Knepley 
4469a42bb27SBarry Smith /*@C
4479a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4486757b960SDave May    DM options in the database.
4499a42bb27SBarry Smith 
450d083f849SBarry Smith    Logically Collective on dm
4519a42bb27SBarry Smith 
4529a42bb27SBarry Smith    Input Parameter:
4538353ddbbSDave May +  da - the DM context
4549a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4559a42bb27SBarry Smith 
4569a42bb27SBarry Smith    Notes:
4579a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4589a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4599a42bb27SBarry Smith 
4609a42bb27SBarry Smith    Level: advanced
4619a42bb27SBarry Smith 
4629a42bb27SBarry Smith .seealso: DMSetFromOptions()
4639a42bb27SBarry Smith @*/
4647087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4659a42bb27SBarry Smith {
4669a42bb27SBarry Smith   PetscErrorCode ierr;
4679a42bb27SBarry Smith 
4689a42bb27SBarry Smith   PetscFunctionBegin;
4699a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4709a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
471691be533SLawrence Mitchell   if (dm->sf) {
472691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
473691be533SLawrence Mitchell   }
4741bb6d2a8SBarry Smith   if (dm->sectionSF) {
4751bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
476691be533SLawrence Mitchell   }
4779a42bb27SBarry Smith   PetscFunctionReturn(0);
4789a42bb27SBarry Smith }
4799a42bb27SBarry Smith 
48031697293SDave May /*@C
48131697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48231697293SDave May    DM options in the database.
48331697293SDave May 
484d083f849SBarry Smith    Logically Collective on dm
48531697293SDave May 
48631697293SDave May    Input Parameters:
48731697293SDave May +  dm - the DM context
48831697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48931697293SDave May 
49031697293SDave May    Notes:
49131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49331697293SDave May 
49431697293SDave May    Level: advanced
49531697293SDave May 
49631697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49731697293SDave May @*/
49831697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49931697293SDave May {
50031697293SDave May   PetscErrorCode ierr;
50131697293SDave May 
50231697293SDave May   PetscFunctionBegin;
50331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50431697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50531697293SDave May   PetscFunctionReturn(0);
50631697293SDave May }
50731697293SDave May 
50831697293SDave May /*@C
50931697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
51031697293SDave May    DM options in the database.
51131697293SDave May 
51231697293SDave May    Not Collective
51331697293SDave May 
51431697293SDave May    Input Parameters:
51531697293SDave May .  dm - the DM context
51631697293SDave May 
51731697293SDave May    Output Parameters:
51831697293SDave May .  prefix - pointer to the prefix string used is returned
51931697293SDave May 
52095452b02SPatrick Sanan    Notes:
52195452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52231697293SDave May    sufficient length to hold the prefix.
52331697293SDave May 
52431697293SDave May    Level: advanced
52531697293SDave May 
52631697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52731697293SDave May @*/
52831697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52931697293SDave May {
53031697293SDave May   PetscErrorCode ierr;
53131697293SDave May 
53231697293SDave May   PetscFunctionBegin;
53331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53431697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53531697293SDave May   PetscFunctionReturn(0);
53631697293SDave May }
53731697293SDave May 
53888bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53988bdff64SToby Isaac {
5406eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
54188bdff64SToby Isaac   PetscErrorCode ierr;
54288bdff64SToby Isaac 
54388bdff64SToby Isaac   PetscFunctionBegin;
544aab5bcd8SJed Brown   *ncrefct = 0;
54588bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54688bdff64SToby Isaac     refct--;
54788bdff64SToby Isaac     if (recurseCoarse) {
54888bdff64SToby Isaac       PetscInt coarseCount;
54988bdff64SToby Isaac 
55088bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55188bdff64SToby Isaac       refct += coarseCount;
55288bdff64SToby Isaac     }
55388bdff64SToby Isaac   }
55488bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55588bdff64SToby Isaac     refct--;
55688bdff64SToby Isaac     if (recurseFine) {
55788bdff64SToby Isaac       PetscInt fineCount;
55888bdff64SToby Isaac 
55988bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56088bdff64SToby Isaac       refct += fineCount;
56188bdff64SToby Isaac     }
56288bdff64SToby Isaac   }
56388bdff64SToby Isaac   *ncrefct = refct;
56488bdff64SToby Isaac   PetscFunctionReturn(0);
56588bdff64SToby Isaac }
56688bdff64SToby Isaac 
567f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
568354557abSToby Isaac {
5695d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
570354557abSToby Isaac   PetscErrorCode ierr;
571354557abSToby Isaac 
572354557abSToby Isaac   PetscFunctionBegin;
573354557abSToby Isaac   /* destroy the labels */
574354557abSToby Isaac   while (next) {
575354557abSToby Isaac     DMLabelLink tmp = next->next;
576354557abSToby Isaac 
5775d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
578ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
579354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
580354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
581354557abSToby Isaac     next = tmp;
582354557abSToby Isaac   }
5835d80c0bfSVaclav Hapla   dm->labels = NULL;
584354557abSToby Isaac   PetscFunctionReturn(0);
585354557abSToby Isaac }
586354557abSToby Isaac 
58747c6ae99SBarry Smith /*@
5888472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
58947c6ae99SBarry Smith 
590d083f849SBarry Smith     Collective on dm
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith     Input Parameter:
59347c6ae99SBarry Smith .   dm - the DM object to destroy
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith     Level: developer
59647c6ae99SBarry Smith 
597e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith @*/
600fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60147c6ae99SBarry Smith {
6026eb26441SStefano Zampini   PetscInt       cnt;
603dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60447c6ae99SBarry Smith   PetscErrorCode ierr;
60547c6ae99SBarry Smith 
60647c6ae99SBarry Smith   PetscFunctionBegin;
6076bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6086bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
60987e657c6SBarry Smith 
61088bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61188bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61288bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
61388bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
6146bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6156bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6166eb26441SStefano Zampini 
6176eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6186eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6196eb26441SStefano Zampini 
620f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6210298fd71SBarry Smith   (*dm)->namedglobal = NULL;
622f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6232348bcf4SPeter Brune     nnext = nlink->next;
6242348bcf4SPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6252348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6262348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6272348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6282348bcf4SPeter Brune   }
629f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6300298fd71SBarry Smith   (*dm)->namedlocal = NULL;
631f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
632f490541aSPeter Brune     nnext = nlink->next;
633f490541aSPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
634f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
635f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
636f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
637f490541aSPeter Brune   }
6382348bcf4SPeter Brune 
639b17ce1afSJed Brown   /* Destroy the list of hooks */
640c833c3b5SJed Brown   {
641c833c3b5SJed Brown     DMCoarsenHookLink link,next;
642b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
643b17ce1afSJed Brown       next = link->next;
644b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
645b17ce1afSJed Brown     }
6460298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
647c833c3b5SJed Brown   }
648c833c3b5SJed Brown   {
649c833c3b5SJed Brown     DMRefineHookLink link,next;
650c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
651c833c3b5SJed Brown       next = link->next;
652c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
653c833c3b5SJed Brown     }
6540298fd71SBarry Smith     (*dm)->refinehook = NULL;
655c833c3b5SJed Brown   }
656be081cd6SPeter Brune   {
657be081cd6SPeter Brune     DMSubDomainHookLink link,next;
658be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
659be081cd6SPeter Brune       next = link->next;
660be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
661be081cd6SPeter Brune     }
6620298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
663be081cd6SPeter Brune   }
664baf369e7SPeter Brune   {
665baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
666baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
667baf369e7SPeter Brune       next = link->next;
668baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
669baf369e7SPeter Brune     }
6700298fd71SBarry Smith     (*dm)->gtolhook = NULL;
671baf369e7SPeter Brune   }
672d4d07f1eSToby Isaac   {
673d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
674d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
675d4d07f1eSToby Isaac       next = link->next;
676d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
677d4d07f1eSToby Isaac     }
678d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
679d4d07f1eSToby Isaac   }
680aa1993deSMatthew G Knepley   /* Destroy the work arrays */
681aa1993deSMatthew G Knepley   {
682aa1993deSMatthew G Knepley     DMWorkLink link,next;
683aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
684aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
685aa1993deSMatthew G Knepley       next = link->next;
686aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
687aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
688aa1993deSMatthew G Knepley     }
6890298fd71SBarry Smith     (*dm)->workin = NULL;
690aa1993deSMatthew G Knepley   }
691c58f1c22SToby Isaac   /* destroy the labels */
692f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
693f4cdcedcSVaclav Hapla   /* destroy the fields */
694e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
695f4cdcedcSVaclav Hapla   /* destroy the boundaries */
696e6f8dbb6SToby Isaac   {
697e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
698e6f8dbb6SToby Isaac     while (next) {
699e6f8dbb6SToby Isaac       DMBoundary b = next;
700e6f8dbb6SToby Isaac 
701e6f8dbb6SToby Isaac       next = b->next;
702e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
703e6f8dbb6SToby Isaac     }
704e6f8dbb6SToby Isaac   }
705b17ce1afSJed Brown 
70652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
70752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
70852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
70952536dc3SBarry Smith 
7101a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7111a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7121a266240SBarry Smith   }
71371cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7146bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7156bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
716073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
71788ed4aceSMatthew G Knepley 
7181bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7191bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7208b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
721fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
722fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
72388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7241bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
725736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
726736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7278e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
728736995cdSBlaise Bourdin     }
729736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
730736995cdSBlaise Bourdin   }
73188bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
73288bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
73388bdff64SToby Isaac   }
7346eb26441SStefano Zampini 
735a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
73688bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
73788bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
73888bdff64SToby Isaac   }
73988bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
740f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7416636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7426636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7436636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
744412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
745412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
746412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
747ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
748ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
749ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7506636e97aSMatthew G Knepley 
751e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
75214f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
753e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
754e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
755732e2eb9SMatthew G Knepley 
756ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7576bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
758ed3c66a1SDave May   }
759c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
760435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
761732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
76247c6ae99SBarry Smith   PetscFunctionReturn(0);
76347c6ae99SBarry Smith }
76447c6ae99SBarry Smith 
765d7bf68aeSBarry Smith /*@
766d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
767d7bf68aeSBarry Smith 
768d083f849SBarry Smith     Collective on dm
769d7bf68aeSBarry Smith 
770d7bf68aeSBarry Smith     Input Parameter:
771d7bf68aeSBarry Smith .   dm - the DM object to setup
772d7bf68aeSBarry Smith 
773d7bf68aeSBarry Smith     Level: developer
774d7bf68aeSBarry Smith 
775e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
776d7bf68aeSBarry Smith 
777d7bf68aeSBarry Smith @*/
7787087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
779d7bf68aeSBarry Smith {
780d7bf68aeSBarry Smith   PetscErrorCode ierr;
781d7bf68aeSBarry Smith 
782d7bf68aeSBarry Smith   PetscFunctionBegin;
783171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7848387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
785d7bf68aeSBarry Smith   if (dm->ops->setup) {
786d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
787d7bf68aeSBarry Smith   }
7888387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
789d7bf68aeSBarry Smith   PetscFunctionReturn(0);
790d7bf68aeSBarry Smith }
791d7bf68aeSBarry Smith 
792d7bf68aeSBarry Smith /*@
793d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
794d7bf68aeSBarry Smith 
795d083f849SBarry Smith     Collective on dm
796d7bf68aeSBarry Smith 
797d7bf68aeSBarry Smith     Input Parameter:
798d7bf68aeSBarry Smith .   dm - the DM object to set options for
799d7bf68aeSBarry Smith 
800732e2eb9SMatthew G Knepley     Options Database:
8014164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8024164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8034164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8048f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
805732e2eb9SMatthew G Knepley 
806384a6580SVaclav Hapla     DMPLEX Specific Checks
807384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
808384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
809384a6580SVaclav Hapla .   -dm_plex_check_faces           - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type - DMPlexCheckFaces()
810384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
811384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
812384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
813384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
814d7bf68aeSBarry Smith 
81595eb5ee5SVaclav Hapla     Level: intermediate
81695eb5ee5SVaclav Hapla 
81795eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
81895eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
819d7bf68aeSBarry Smith 
820d7bf68aeSBarry Smith @*/
8217087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
822d7bf68aeSBarry Smith {
8237781c08eSBarry Smith   char           typeName[256];
824ca266f36SBarry Smith   PetscBool      flg;
825d7bf68aeSBarry Smith   PetscErrorCode ierr;
826d7bf68aeSBarry Smith 
827d7bf68aeSBarry Smith   PetscFunctionBegin;
828171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82949be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
83049be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8311bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8323194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8330298fd71SBarry Smith   ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL);CHKERRQ(ierr);
834a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
835f9ba7244SBarry Smith   if (flg) {
836f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
837f9ba7244SBarry Smith   }
838a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
839073dac72SJed Brown   if (flg) {
840521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
841073dac72SJed Brown   }
8428f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
843f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
844e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
845f9ba7244SBarry Smith   }
846f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8470633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
84882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
849d7bf68aeSBarry Smith   PetscFunctionReturn(0);
850d7bf68aeSBarry Smith }
851d7bf68aeSBarry Smith 
852fc9bc008SSatish Balay /*@C
853fe2efc57SMark    DMViewFromOptions - View from Options
854fe2efc57SMark 
855fe2efc57SMark    Collective on DM
856fe2efc57SMark 
857fe2efc57SMark    Input Parameters:
858fe2efc57SMark +  dm - the DM object
859736c3998SJose E. Roman .  obj - Optional object
860736c3998SJose E. Roman -  name - command line option
861fe2efc57SMark 
862fe2efc57SMark    Level: intermediate
863fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
864fe2efc57SMark @*/
865fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
866fe2efc57SMark {
867fe2efc57SMark   PetscErrorCode ierr;
868fe2efc57SMark 
869fe2efc57SMark   PetscFunctionBegin;
870fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
871fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
872fe2efc57SMark   PetscFunctionReturn(0);
873fe2efc57SMark }
874fe2efc57SMark 
875fe2efc57SMark /*@C
876224748a4SBarry Smith     DMView - Views a DM
87747c6ae99SBarry Smith 
878d083f849SBarry Smith     Collective on dm
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Input Parameter:
88147c6ae99SBarry Smith +   dm - the DM object to view
88247c6ae99SBarry Smith -   v - the viewer
88347c6ae99SBarry Smith 
884224748a4SBarry Smith     Level: beginner
88547c6ae99SBarry Smith 
886e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88747c6ae99SBarry Smith 
88847c6ae99SBarry Smith @*/
8897087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
89047c6ae99SBarry Smith {
89147c6ae99SBarry Smith   PetscErrorCode    ierr;
89232c0f0efSBarry Smith   PetscBool         isbinary;
89376a8abe0SBarry Smith   PetscMPIInt       size;
89476a8abe0SBarry Smith   PetscViewerFormat format;
89547c6ae99SBarry Smith 
89647c6ae99SBarry Smith   PetscFunctionBegin;
897171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8983014e516SBarry Smith   if (!v) {
899ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9003014e516SBarry Smith   }
901b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
90274903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
90374903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
90474903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
90574903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
90674903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
90774903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
90874903a4fSStefano Zampini      in an error here */
90974903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9108bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
911b1b135c8SBarry Smith 
91276a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
91376a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
91476a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
91598c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
91632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
91732c0f0efSBarry Smith   if (isbinary) {
91855849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
91932c0f0efSBarry Smith     char     type[256];
92032c0f0efSBarry Smith 
921f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
92232c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
923f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
92432c0f0efSBarry Smith   }
9250c010503SBarry Smith   if (dm->ops->view) {
9260c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
92747c6ae99SBarry Smith   }
92847c6ae99SBarry Smith   PetscFunctionReturn(0);
92947c6ae99SBarry Smith }
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith /*@
9328472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
93347c6ae99SBarry Smith 
934d083f849SBarry Smith     Collective on dm
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith     Input Parameter:
93747c6ae99SBarry Smith .   dm - the DM object
93847c6ae99SBarry Smith 
93947c6ae99SBarry Smith     Output Parameter:
94047c6ae99SBarry Smith .   vec - the global vector
94147c6ae99SBarry Smith 
942073dac72SJed Brown     Level: beginner
94347c6ae99SBarry Smith 
944e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith @*/
9477087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
94847c6ae99SBarry Smith {
94947c6ae99SBarry Smith   PetscErrorCode ierr;
95047c6ae99SBarry Smith 
95147c6ae99SBarry Smith   PetscFunctionBegin;
952171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
953b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
954b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
95547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
95676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
957c6b011d8SStefano Zampini     DM vdm;
958c6b011d8SStefano Zampini 
959c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
960c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
961c6b011d8SStefano Zampini   }
96247c6ae99SBarry Smith   PetscFunctionReturn(0);
96347c6ae99SBarry Smith }
96447c6ae99SBarry Smith 
96547c6ae99SBarry Smith /*@
9668472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
96747c6ae99SBarry Smith 
96847c6ae99SBarry Smith     Not Collective
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith     Input Parameter:
97147c6ae99SBarry Smith .   dm - the DM object
97247c6ae99SBarry Smith 
97347c6ae99SBarry Smith     Output Parameter:
97447c6ae99SBarry Smith .   vec - the local vector
97547c6ae99SBarry Smith 
976073dac72SJed Brown     Level: beginner
97747c6ae99SBarry Smith 
978e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith @*/
9817087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
98247c6ae99SBarry Smith {
98347c6ae99SBarry Smith   PetscErrorCode ierr;
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith   PetscFunctionBegin;
986171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
987b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
988b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
98947c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
99076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
991c6b011d8SStefano Zampini     DM vdm;
992c6b011d8SStefano Zampini 
993c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
994c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
995c6b011d8SStefano Zampini   }
99647c6ae99SBarry Smith   PetscFunctionReturn(0);
99747c6ae99SBarry Smith }
99847c6ae99SBarry Smith 
9991411c6eeSJed Brown /*@
10001411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10011411c6eeSJed Brown 
1002d083f849SBarry Smith    Collective on dm
10031411c6eeSJed Brown 
10041411c6eeSJed Brown    Input Parameter:
10051411c6eeSJed Brown .  dm - the DM that provides the mapping
10061411c6eeSJed Brown 
10071411c6eeSJed Brown    Output Parameter:
10081411c6eeSJed Brown .  ltog - the mapping
10091411c6eeSJed Brown 
10101411c6eeSJed Brown    Level: intermediate
10111411c6eeSJed Brown 
10121411c6eeSJed Brown    Notes:
10131411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10141411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10151411c6eeSJed Brown 
1016fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10171411c6eeSJed Brown @*/
10187087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10191411c6eeSJed Brown {
10200be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10211411c6eeSJed Brown   PetscErrorCode ierr;
10221411c6eeSJed Brown 
10231411c6eeSJed Brown   PetscFunctionBegin;
10241411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10251411c6eeSJed Brown   PetscValidPointer(ltog,2);
10261411c6eeSJed Brown   if (!dm->ltogmap) {
102737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
102837d0c07bSMatthew G Knepley 
102992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
103037d0c07bSMatthew G Knepley     if (section) {
1031a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
103237d0c07bSMatthew G Knepley       PetscInt       *ltog;
1033ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
103437d0c07bSMatthew G Knepley 
1035e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1037ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1038ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
103937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1040a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
104137d0c07bSMatthew G Knepley 
104237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
104337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1044a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1045a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
104637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10471a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10481a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10491a7dc684SMatthew G. Knepley         if (dof) {
1050b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1051b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10521a7dc684SMatthew G. Knepley         }
105337d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1054a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1055a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
105637d0c07bSMatthew G Knepley         }
105737d0c07bSMatthew G Knepley       }
1058bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10590be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10600be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10610be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10620be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10637591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10647591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1065ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1066ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1067ccf3bd66SMatthew G. Knepley         n /= bs;
1068ccf3bd66SMatthew G. Knepley       }
1069ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10703bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
107137d0c07bSMatthew G Knepley     } else {
1072b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1073184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10741411c6eeSJed Brown     }
107537d0c07bSMatthew G Knepley   }
10761411c6eeSJed Brown   *ltog = dm->ltogmap;
10771411c6eeSJed Brown   PetscFunctionReturn(0);
10781411c6eeSJed Brown }
10791411c6eeSJed Brown 
10801411c6eeSJed Brown /*@
10811411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10821411c6eeSJed Brown 
10831411c6eeSJed Brown    Not Collective
10841411c6eeSJed Brown 
10851411c6eeSJed Brown    Input Parameter:
10861411c6eeSJed Brown .  dm - the DM with block structure
10871411c6eeSJed Brown 
10881411c6eeSJed Brown    Output Parameter:
10891411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10901411c6eeSJed Brown 
10911411c6eeSJed Brown    Level: intermediate
10921411c6eeSJed Brown 
1093fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10941411c6eeSJed Brown @*/
10957087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10961411c6eeSJed Brown {
10971411c6eeSJed Brown   PetscFunctionBegin;
10981411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1099534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11001411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11011411c6eeSJed Brown   *bs = dm->bs;
11021411c6eeSJed Brown   PetscFunctionReturn(0);
11031411c6eeSJed Brown }
11041411c6eeSJed Brown 
110547c6ae99SBarry Smith /*@
11068472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
110747c6ae99SBarry Smith 
1108a5bc1bf3SBarry Smith     Collective on dmc
110947c6ae99SBarry Smith 
111047c6ae99SBarry Smith     Input Parameter:
1111a5bc1bf3SBarry Smith +   dmc - the DM object
1112a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
111347c6ae99SBarry Smith 
111447c6ae99SBarry Smith     Output Parameter:
111547c6ae99SBarry Smith +  mat - the interpolation
111647c6ae99SBarry Smith -  vec - the scaling (optional)
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith     Level: developer
111947c6ae99SBarry Smith 
112095452b02SPatrick Sanan     Notes:
112195452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
112285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1123d52bd9f3SBarry Smith 
11241f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1125d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
112685afcc9aSBarry Smith 
112785afcc9aSBarry Smith 
11282ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
112947c6ae99SBarry Smith 
113047c6ae99SBarry Smith @*/
1131a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
113247c6ae99SBarry Smith {
113347c6ae99SBarry Smith   PetscErrorCode ierr;
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith   PetscFunctionBegin;
1136a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1137a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1138c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1139a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1140a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1141a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1142a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
114347c6ae99SBarry Smith   PetscFunctionReturn(0);
114447c6ae99SBarry Smith }
114547c6ae99SBarry Smith 
11463ad4599aSBarry Smith /*@
1147d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11482ed6491fSPatrick Sanan 
11492ed6491fSPatrick Sanan   Input Parameters:
11502ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11512ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11522ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11532ed6491fSPatrick Sanan 
11542ed6491fSPatrick Sanan   Output Parameter:
11552ed6491fSPatrick Sanan .    scale - the scaled vector
11562ed6491fSPatrick Sanan 
11572ed6491fSPatrick Sanan   Level: developer
11582ed6491fSPatrick Sanan 
11592ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
11602ed6491fSPatrick Sanan 
11612ed6491fSPatrick Sanan @*/
11622ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11632ed6491fSPatrick Sanan {
11642ed6491fSPatrick Sanan   PetscErrorCode ierr;
11652ed6491fSPatrick Sanan   Vec            fine;
11662ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11672ed6491fSPatrick Sanan 
11682ed6491fSPatrick Sanan   PetscFunctionBegin;
11692ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
11702ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
11712ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
11722ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
11732ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
11742ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
11752ed6491fSPatrick Sanan   PetscFunctionReturn(0);
11762ed6491fSPatrick Sanan }
11772ed6491fSPatrick Sanan 
11782ed6491fSPatrick Sanan /*@
11793ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11803ad4599aSBarry Smith 
1181a5bc1bf3SBarry Smith     Collective on dmc
11823ad4599aSBarry Smith 
11833ad4599aSBarry Smith     Input Parameter:
1184a5bc1bf3SBarry Smith +   dmc - the DM object
1185a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
11863ad4599aSBarry Smith 
11873ad4599aSBarry Smith     Output Parameter:
11883ad4599aSBarry Smith .  mat - the restriction
11893ad4599aSBarry Smith 
11903ad4599aSBarry Smith 
11913ad4599aSBarry Smith     Level: developer
11923ad4599aSBarry Smith 
119395452b02SPatrick Sanan     Notes:
119495452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
11953ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11963ad4599aSBarry Smith 
11973ad4599aSBarry Smith 
11983ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11993ad4599aSBarry Smith 
12003ad4599aSBarry Smith @*/
1201a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12023ad4599aSBarry Smith {
12033ad4599aSBarry Smith   PetscErrorCode ierr;
12043ad4599aSBarry Smith 
12053ad4599aSBarry Smith   PetscFunctionBegin;
1206a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1207a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12085a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1209a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1210a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1211a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1212a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12133ad4599aSBarry Smith   PetscFunctionReturn(0);
12143ad4599aSBarry Smith }
12153ad4599aSBarry Smith 
121647c6ae99SBarry Smith /*@
12178472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
121847c6ae99SBarry Smith 
1219a5bc1bf3SBarry Smith     Collective on dac
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith     Input Parameter:
1222a5bc1bf3SBarry Smith +   dac - the DM object
1223a5bc1bf3SBarry Smith -   daf - the second, finer DM object
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith     Output Parameter:
12266dbf9973SLawrence Mitchell .   mat - the injection
122747c6ae99SBarry Smith 
122847c6ae99SBarry Smith     Level: developer
122947c6ae99SBarry Smith 
123095452b02SPatrick Sanan    Notes:
123195452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
123285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
123385afcc9aSBarry Smith 
1234e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith @*/
1237a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
123847c6ae99SBarry Smith {
123947c6ae99SBarry Smith   PetscErrorCode ierr;
124047c6ae99SBarry Smith 
124147c6ae99SBarry Smith   PetscFunctionBegin;
1242a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1243a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12445a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1245a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1246a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1247a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1248a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
124947c6ae99SBarry Smith   PetscFunctionReturn(0);
125047c6ae99SBarry Smith }
125147c6ae99SBarry Smith 
1252b412c318SBarry Smith /*@
1253bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1254bd041c0cSMatthew G. Knepley 
1255a5bc1bf3SBarry Smith   Collective on dac
1256bd041c0cSMatthew G. Knepley 
1257bd041c0cSMatthew G. Knepley   Input Parameter:
1258a5bc1bf3SBarry Smith + dac - the DM object
1259a5bc1bf3SBarry Smith - daf - the second, finer DM object
1260bd041c0cSMatthew G. Knepley 
1261bd041c0cSMatthew G. Knepley   Output Parameter:
1262bd041c0cSMatthew G. Knepley . mat - the interpolation
1263bd041c0cSMatthew G. Knepley 
1264bd041c0cSMatthew G. Knepley   Level: developer
1265bd041c0cSMatthew G. Knepley 
1266bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1267bd041c0cSMatthew G. Knepley @*/
1268a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1269bd041c0cSMatthew G. Knepley {
1270bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1271bd041c0cSMatthew G. Knepley 
1272bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1273a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1274a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
12755a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1276a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1277a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1278bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1279bd041c0cSMatthew G. Knepley }
1280bd041c0cSMatthew G. Knepley 
1281bd041c0cSMatthew G. Knepley /*@
1282b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
128347c6ae99SBarry Smith 
1284d083f849SBarry Smith     Collective on dm
128547c6ae99SBarry Smith 
128647c6ae99SBarry Smith     Input Parameter:
128747c6ae99SBarry Smith +   dm - the DM object
12885bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
128947c6ae99SBarry Smith 
129047c6ae99SBarry Smith     Output Parameter:
129147c6ae99SBarry Smith .   coloring - the coloring
129247c6ae99SBarry Smith 
1293ec5066bdSBarry Smith     Notes:
1294ec5066bdSBarry Smith        Coloring of matrices can be computed directly from the sparse matrix nonzero structure via the MatColoring object or from the mesh from which the
1295ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1296ec5066bdSBarry Smith 
1297ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1298ec5066bdSBarry Smith 
129947c6ae99SBarry Smith     Level: developer
130047c6ae99SBarry Smith 
1301ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
130247c6ae99SBarry Smith 
1303aab9d709SJed Brown @*/
1304b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
130547c6ae99SBarry Smith {
130647c6ae99SBarry Smith   PetscErrorCode ierr;
130747c6ae99SBarry Smith 
130847c6ae99SBarry Smith   PetscFunctionBegin;
1309171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13105a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1311b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1312b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
131347c6ae99SBarry Smith   PetscFunctionReturn(0);
131447c6ae99SBarry Smith }
131547c6ae99SBarry Smith 
1316b412c318SBarry Smith /*@
13178472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
131847c6ae99SBarry Smith 
1319d083f849SBarry Smith     Collective on dm
132047c6ae99SBarry Smith 
132147c6ae99SBarry Smith     Input Parameter:
1322b412c318SBarry Smith .   dm - the DM object
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith     Output Parameter:
132547c6ae99SBarry Smith .   mat - the empty Jacobian
132647c6ae99SBarry Smith 
1327073dac72SJed Brown     Level: beginner
132847c6ae99SBarry Smith 
132995452b02SPatrick Sanan     Notes:
133095452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
133194013140SBarry Smith        do not need to do it yourself.
133294013140SBarry Smith 
133394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13347889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
133594013140SBarry Smith 
133694013140SBarry Smith        For structured grid problems, when you call MatView() on this matrix it is displayed using the global natural ordering, NOT in the ordering used
133794013140SBarry Smith        internally by PETSc.
133894013140SBarry Smith 
133994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1340aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
134194013140SBarry Smith 
1342b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
134347c6ae99SBarry Smith 
1344aab9d709SJed Brown @*/
1345b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
134647c6ae99SBarry Smith {
134747c6ae99SBarry Smith   PetscErrorCode ierr;
134847c6ae99SBarry Smith 
134947c6ae99SBarry Smith   PetscFunctionBegin;
1350171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1351c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1352b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13535a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1354fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1355b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
135676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1357c6b011d8SStefano Zampini     DM mdm;
1358c6b011d8SStefano Zampini 
1359c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1360c6b011d8SStefano Zampini     if (!mdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix\n",((PetscObject)dm)->type_name);
1361c6b011d8SStefano Zampini   }
1362e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1363e5e52638SMatthew G. Knepley   if (dm->Nf) {
1364e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1365e571a35bSMatthew G. Knepley     PetscInt     Nf;
1366e571a35bSMatthew G. Knepley 
1367e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1368e571a35bSMatthew G. Knepley     if (Nf == 1) {
1369e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1370e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1371e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1372e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1373e571a35bSMatthew G. Knepley       }
1374e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1375e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1376e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1377e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1378e571a35bSMatthew G. Knepley       }
1379e571a35bSMatthew G. Knepley     }
1380e571a35bSMatthew G. Knepley   }
1381fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
138247c6ae99SBarry Smith   PetscFunctionReturn(0);
138347c6ae99SBarry Smith }
138447c6ae99SBarry Smith 
1385732e2eb9SMatthew G Knepley /*@
1386950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1387732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1388732e2eb9SMatthew G Knepley 
1389d083f849SBarry Smith   Logically Collective on dm
1390732e2eb9SMatthew G Knepley 
1391732e2eb9SMatthew G Knepley   Input Parameter:
1392732e2eb9SMatthew G Knepley + dm - the DM
1393732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1394732e2eb9SMatthew G Knepley 
1395732e2eb9SMatthew G Knepley   Level: developer
1396b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1397732e2eb9SMatthew G Knepley @*/
1398732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1399732e2eb9SMatthew G Knepley {
1400732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1401732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1402732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1403732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1404732e2eb9SMatthew G Knepley }
1405732e2eb9SMatthew G Knepley 
1406b06ff27eSHong Zhang /*@
1407b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1408b06ff27eSHong Zhang     but the array for values will not be allocated.
1409b06ff27eSHong Zhang 
1410d083f849SBarry Smith   Logically Collective on dm
1411b06ff27eSHong Zhang 
1412b06ff27eSHong Zhang   Input Parameter:
1413b06ff27eSHong Zhang + dm - the DM
1414b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1415b06ff27eSHong Zhang 
1416b06ff27eSHong Zhang   Level: developer
1417b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1418b06ff27eSHong Zhang @*/
1419b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1420b06ff27eSHong Zhang {
1421b06ff27eSHong Zhang   PetscFunctionBegin;
1422b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1423b06ff27eSHong Zhang   dm->structure_only = only;
1424b06ff27eSHong Zhang   PetscFunctionReturn(0);
1425b06ff27eSHong Zhang }
1426b06ff27eSHong Zhang 
1427a89ea682SMatthew G Knepley /*@C
1428aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1429a89ea682SMatthew G Knepley 
1430a89ea682SMatthew G Knepley   Not Collective
1431a89ea682SMatthew G Knepley 
1432a89ea682SMatthew G Knepley   Input Parameters:
1433a89ea682SMatthew G Knepley + dm - the DM object
1434aa1993deSMatthew G Knepley . count - The minium size
143569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1436a89ea682SMatthew G Knepley 
1437a89ea682SMatthew G Knepley   Output Parameter:
1438a89ea682SMatthew G Knepley . array - the work array
1439a89ea682SMatthew G Knepley 
1440a89ea682SMatthew G Knepley   Level: developer
1441a89ea682SMatthew G Knepley 
1442a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1443a89ea682SMatthew G Knepley @*/
144469291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1445a89ea682SMatthew G Knepley {
1446a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1447aa1993deSMatthew G Knepley   DMWorkLink     link;
144869291d52SBarry Smith   PetscMPIInt    dsize;
1449a89ea682SMatthew G Knepley 
1450a89ea682SMatthew G Knepley   PetscFunctionBegin;
1451a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1452aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1453aa1993deSMatthew G Knepley   if (dm->workin) {
1454aa1993deSMatthew G Knepley     link       = dm->workin;
1455aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1456aa1993deSMatthew G Knepley   } else {
1457b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1458a89ea682SMatthew G Knepley   }
145969291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14605056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1461aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1462854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1463854ce69bSBarry Smith     link->bytes = dsize*count;
1464aa1993deSMatthew G Knepley   }
1465aa1993deSMatthew G Knepley   link->next   = dm->workout;
1466aa1993deSMatthew G Knepley   dm->workout  = link;
146700d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
146800d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
146900d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
147000d952a4SJed Brown #endif
1471aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1472a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1473a89ea682SMatthew G Knepley }
1474a89ea682SMatthew G Knepley 
1475aa1993deSMatthew G Knepley /*@C
1476aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1477aa1993deSMatthew G Knepley 
1478aa1993deSMatthew G Knepley   Not Collective
1479aa1993deSMatthew G Knepley 
1480aa1993deSMatthew G Knepley   Input Parameters:
1481aa1993deSMatthew G Knepley + dm - the DM object
1482aa1993deSMatthew G Knepley . count - The minium size
148369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1484aa1993deSMatthew G Knepley 
1485aa1993deSMatthew G Knepley   Output Parameter:
1486aa1993deSMatthew G Knepley . array - the work array
1487aa1993deSMatthew G Knepley 
1488aa1993deSMatthew G Knepley   Level: developer
1489aa1993deSMatthew G Knepley 
149095452b02SPatrick Sanan   Developer Notes:
149195452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1492aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1493aa1993deSMatthew G Knepley @*/
149469291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1495aa1993deSMatthew G Knepley {
1496aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1497aa1993deSMatthew G Knepley 
1498aa1993deSMatthew G Knepley   PetscFunctionBegin;
1499aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1500aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1501aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1502aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1503aa1993deSMatthew G Knepley       *p           = link->next;
1504aa1993deSMatthew G Knepley       link->next   = dm->workin;
1505aa1993deSMatthew G Knepley       dm->workin   = link;
15060298fd71SBarry Smith       *(void**)mem = NULL;
1507aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1508aa1993deSMatthew G Knepley     }
1509aa1993deSMatthew G Knepley   }
1510aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1511aa1993deSMatthew G Knepley }
1512e7c4fc90SDmitry Karpeev 
1513435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1514435a35e8SMatthew G Knepley {
1515435a35e8SMatthew G Knepley   PetscFunctionBegin;
1516435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
151782f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1518435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1519435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1520435a35e8SMatthew G Knepley }
1521435a35e8SMatthew G Knepley 
15220a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
15230a50eb56SMatthew G. Knepley {
15240a50eb56SMatthew G. Knepley   PetscFunctionBegin;
15250a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1526f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
15270a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15280a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15290a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15300a50eb56SMatthew G. Knepley }
15310a50eb56SMatthew G. Knepley 
1532f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1533f9d4088aSMatthew G. Knepley {
1534f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1535f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1536f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1537f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1538f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1539f9d4088aSMatthew G. Knepley }
1540f9d4088aSMatthew G. Knepley 
1541f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1542f9d4088aSMatthew G. Knepley {
1543f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1544f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1545f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1546f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1547f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1548f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1549f9d4088aSMatthew G. Knepley }
1550f9d4088aSMatthew G. Knepley 
15514f3b5142SJed Brown /*@C
15524d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
15534d343eeaSMatthew G Knepley 
15544d343eeaSMatthew G Knepley   Not collective
15554d343eeaSMatthew G Knepley 
15564d343eeaSMatthew G Knepley   Input Parameter:
15574d343eeaSMatthew G Knepley . dm - the DM object
15584d343eeaSMatthew G Knepley 
15594d343eeaSMatthew G Knepley   Output Parameters:
15600298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
15610298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
15620298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
15634d343eeaSMatthew G Knepley 
15644d343eeaSMatthew G Knepley   Level: intermediate
15654d343eeaSMatthew G Knepley 
156621c9b008SJed Brown   Notes:
156721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
156821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
156921c9b008SJed Brown   PetscFree().
157021c9b008SJed Brown 
15714d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
15724d343eeaSMatthew G Knepley @*/
157337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
15744d343eeaSMatthew G Knepley {
157537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
15764d343eeaSMatthew G Knepley   PetscErrorCode ierr;
15774d343eeaSMatthew G Knepley 
15784d343eeaSMatthew G Knepley   PetscFunctionBegin;
15794d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
158069ca1f37SDmitry Karpeev   if (numFields) {
1581534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
158269ca1f37SDmitry Karpeev     *numFields = 0;
158369ca1f37SDmitry Karpeev   }
158437d0c07bSMatthew G Knepley   if (fieldNames) {
158537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15860298fd71SBarry Smith     *fieldNames = NULL;
158769ca1f37SDmitry Karpeev   }
158869ca1f37SDmitry Karpeev   if (fields) {
158969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15900298fd71SBarry Smith     *fields = NULL;
159169ca1f37SDmitry Karpeev   }
159292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
159337d0c07bSMatthew G Knepley   if (section) {
15943a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
159537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
159637d0c07bSMatthew G Knepley 
1597e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
159837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15993a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
160037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
160137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
160237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
16033a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
160437d0c07bSMatthew G Knepley     }
160537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
160637d0c07bSMatthew G Knepley       PetscInt gdof;
160737d0c07bSMatthew G Knepley 
160837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
160937d0c07bSMatthew G Knepley       if (gdof > 0) {
161037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
16113a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
161237d0c07bSMatthew G Knepley 
161337d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
161437d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
16153a544194SStefano Zampini           fpdof = fdof-fcdof;
16163a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
16173a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
16183a544194SStefano Zampini             fieldNc[f] = 1;
16193a544194SStefano Zampini           }
16203a544194SStefano Zampini           fieldSizes[f] += fpdof;
162137d0c07bSMatthew G Knepley         }
162237d0c07bSMatthew G Knepley       }
162337d0c07bSMatthew G Knepley     }
162437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1625785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
162637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
162737d0c07bSMatthew G Knepley     }
162837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
162937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
163037d0c07bSMatthew G Knepley 
163137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
163237d0c07bSMatthew G Knepley       if (gdof > 0) {
163337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
163437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
163537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
163637d0c07bSMatthew G Knepley 
163737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
163837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
163937d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
164037d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
164137d0c07bSMatthew G Knepley           }
164237d0c07bSMatthew G Knepley         }
164337d0c07bSMatthew G Knepley       }
164437d0c07bSMatthew G Knepley     }
16458865f1eaSKarl Rupp     if (numFields) *numFields = nF;
164637d0c07bSMatthew G Knepley     if (fieldNames) {
1647785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
164837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
164937d0c07bSMatthew G Knepley         const char *fieldName;
165037d0c07bSMatthew G Knepley 
165137d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
165237d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
165337d0c07bSMatthew G Knepley       }
165437d0c07bSMatthew G Knepley     }
165537d0c07bSMatthew G Knepley     if (fields) {
1656785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
165737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
16583a544194SStefano Zampini         PetscInt bs, in[2], out[2];
16593a544194SStefano Zampini 
166082f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
16613a544194SStefano Zampini         in[0] = -fieldNc[f];
16623a544194SStefano Zampini         in[1] = fieldNc[f];
16633a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
16643a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
16653a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
166637d0c07bSMatthew G Knepley       }
166737d0c07bSMatthew G Knepley     }
16683a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
16698865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
16708865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
167169ca1f37SDmitry Karpeev   }
16724d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
16734d343eeaSMatthew G Knepley }
16744d343eeaSMatthew G Knepley 
167516621825SDmitry Karpeev 
167616621825SDmitry Karpeev /*@C
167716621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
167816621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
167916621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1680e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1681e7c4fc90SDmitry Karpeev 
1682e7c4fc90SDmitry Karpeev   Not collective
1683e7c4fc90SDmitry Karpeev 
1684e7c4fc90SDmitry Karpeev   Input Parameter:
1685e7c4fc90SDmitry Karpeev . dm - the DM object
1686e7c4fc90SDmitry Karpeev 
1687e7c4fc90SDmitry Karpeev   Output Parameters:
16880298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16890298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16900298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16910298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1692e7c4fc90SDmitry Karpeev 
1693e7c4fc90SDmitry Karpeev   Level: intermediate
1694e7c4fc90SDmitry Karpeev 
1695e7c4fc90SDmitry Karpeev   Notes:
1696e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1697e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1698e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1699e7c4fc90SDmitry Karpeev 
1700e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1701e7c4fc90SDmitry Karpeev @*/
170216621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1703e7c4fc90SDmitry Karpeev {
1704e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1705e7c4fc90SDmitry Karpeev 
1706e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1707e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17088865f1eaSKarl Rupp   if (len) {
1709534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
17108865f1eaSKarl Rupp     *len = 0;
17118865f1eaSKarl Rupp   }
17128865f1eaSKarl Rupp   if (namelist) {
17138865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
17148865f1eaSKarl Rupp     *namelist = 0;
17158865f1eaSKarl Rupp   }
17168865f1eaSKarl Rupp   if (islist) {
17178865f1eaSKarl Rupp     PetscValidPointer(islist,4);
17188865f1eaSKarl Rupp     *islist = 0;
17198865f1eaSKarl Rupp   }
17208865f1eaSKarl Rupp   if (dmlist) {
17218865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
17228865f1eaSKarl Rupp     *dmlist = 0;
17238865f1eaSKarl Rupp   }
1724f3f0edfdSDmitry Karpeev   /*
1725f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1726f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1727f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1728f3f0edfdSDmitry Karpeev    */
1729ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
173016621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1731435a35e8SMatthew G Knepley     PetscSection section;
1732435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1733435a35e8SMatthew G Knepley 
173492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1735435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1736435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1737f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
173803dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
173903dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
174003dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1741435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1742435a35e8SMatthew G Knepley         const char *fieldName;
1743435a35e8SMatthew G Knepley 
174403dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
174503dc3394SMatthew G. Knepley         if (namelist) {
1746435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1747435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1748435a35e8SMatthew G Knepley         }
174903dc3394SMatthew G. Knepley       }
1750435a35e8SMatthew G Knepley     } else {
175169ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1752e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
17530298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1754e7c4fc90SDmitry Karpeev     }
17558865f1eaSKarl Rupp   } else {
175616621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
175716621825SDmitry Karpeev   }
175816621825SDmitry Karpeev   PetscFunctionReturn(0);
175916621825SDmitry Karpeev }
176016621825SDmitry Karpeev 
1761564cec59SMatthew G. Knepley /*@
1762435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1763435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1764435a35e8SMatthew G Knepley 
1765435a35e8SMatthew G Knepley   Not collective
1766435a35e8SMatthew G Knepley 
1767435a35e8SMatthew G Knepley   Input Parameters:
17682adcc780SMatthew G. Knepley + dm        - The DM object
17692adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
17702adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1771435a35e8SMatthew G Knepley 
1772435a35e8SMatthew G Knepley   Output Parameters:
17732adcc780SMatthew G. Knepley + is - The global indices for the subproblem
17742adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1775435a35e8SMatthew G Knepley 
17765d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17775d3b26e6SMatthew G. Knepley 
1778435a35e8SMatthew G Knepley   Level: intermediate
1779435a35e8SMatthew G Knepley 
17805d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1781435a35e8SMatthew G Knepley @*/
178237bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1783435a35e8SMatthew G Knepley {
1784435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1785435a35e8SMatthew G Knepley 
1786435a35e8SMatthew G Knepley   PetscFunctionBegin;
1787435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1788435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17898865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17908865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1791b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1792435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1793435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1794435a35e8SMatthew G Knepley }
1795435a35e8SMatthew G Knepley 
17962adcc780SMatthew G. Knepley /*@C
17972adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17982adcc780SMatthew G. Knepley 
17992adcc780SMatthew G. Knepley   Not collective
18002adcc780SMatthew G. Knepley 
18012adcc780SMatthew G. Knepley   Input Parameter:
18022adcc780SMatthew G. Knepley + dms - The DM objects
18032adcc780SMatthew G. Knepley - len - The number of DMs
18042adcc780SMatthew G. Knepley 
18052adcc780SMatthew G. Knepley   Output Parameters:
1806a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
18072adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
18082adcc780SMatthew G. Knepley 
18095d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
18105d3b26e6SMatthew G. Knepley 
18112adcc780SMatthew G. Knepley   Level: intermediate
18122adcc780SMatthew G. Knepley 
18135d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
18142adcc780SMatthew G. Knepley @*/
18152adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
18162adcc780SMatthew G. Knepley {
18172adcc780SMatthew G. Knepley   PetscInt       i;
18182adcc780SMatthew G. Knepley   PetscErrorCode ierr;
18192adcc780SMatthew G. Knepley 
18202adcc780SMatthew G. Knepley   PetscFunctionBegin;
18212adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
18222adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
18232adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1824a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
18252adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
18262adcc780SMatthew G. Knepley   if (len) {
1827b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1828b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1829b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
18302adcc780SMatthew G. Knepley   }
18312adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
18322adcc780SMatthew G. Knepley }
18332adcc780SMatthew G. Knepley 
183416621825SDmitry Karpeev 
183516621825SDmitry Karpeev /*@C
18368d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
18378d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
18388d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
18398d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
18408d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
184116621825SDmitry Karpeev 
184216621825SDmitry Karpeev   Not collective
184316621825SDmitry Karpeev 
184416621825SDmitry Karpeev   Input Parameter:
184516621825SDmitry Karpeev . dm - the DM object
184616621825SDmitry Karpeev 
184716621825SDmitry Karpeev   Output Parameters:
18480298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
18490298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
18500298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
18510298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
18520298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
185316621825SDmitry Karpeev 
185416621825SDmitry Karpeev   Level: intermediate
185516621825SDmitry Karpeev 
185616621825SDmitry Karpeev   Notes:
185716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
185816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
185916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
186016621825SDmitry Karpeev 
1861245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
186216621825SDmitry Karpeev @*/
18638d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
186416621825SDmitry Karpeev {
186516621825SDmitry Karpeev   PetscErrorCode      ierr;
1866be081cd6SPeter Brune   DMSubDomainHookLink link;
1867be081cd6SPeter Brune   PetscInt            i,l;
186816621825SDmitry Karpeev 
186916621825SDmitry Karpeev   PetscFunctionBegin;
187016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
187114a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
18720298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
18730298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
18740298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
18750298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1876f3f0edfdSDmitry Karpeev   /*
1877f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1878f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1879f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1880f3f0edfdSDmitry Karpeev    */
1881ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
188216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1883be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
188414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1885f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1886be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1887be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1888be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1889be081cd6SPeter Brune         }
1890648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1891e7c4fc90SDmitry Karpeev       }
189214a18fd3SPeter Brune     }
189314a18fd3SPeter Brune     if (len) *len = l;
189414a18fd3SPeter Brune   }
1895e30e807fSPeter Brune   PetscFunctionReturn(0);
1896e30e807fSPeter Brune }
1897e30e807fSPeter Brune 
1898e30e807fSPeter Brune 
1899e30e807fSPeter Brune /*@C
1900e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1901e30e807fSPeter Brune 
1902e30e807fSPeter Brune   Not collective
1903e30e807fSPeter Brune 
1904e30e807fSPeter Brune   Input Parameters:
1905e30e807fSPeter Brune + dm - the DM object
1906e30e807fSPeter Brune . n  - the number of subdomain scatters
1907e30e807fSPeter Brune - subdms - the local subdomains
1908e30e807fSPeter Brune 
1909e30e807fSPeter Brune   Output Parameters:
1910e30e807fSPeter Brune + n     - the number of scatters returned
1911e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1912e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1913e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1914e30e807fSPeter Brune 
191595452b02SPatrick Sanan   Notes:
191695452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1917e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1918e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1919e30e807fSPeter Brune   solution and residual data.
1920e30e807fSPeter Brune 
1921e30e807fSPeter Brune   Level: developer
1922e30e807fSPeter Brune 
1923e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1924e30e807fSPeter Brune @*/
1925e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1926e30e807fSPeter Brune {
1927e30e807fSPeter Brune   PetscErrorCode ierr;
1928e30e807fSPeter Brune 
1929e30e807fSPeter Brune   PetscFunctionBegin;
1930e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1931e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1932b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1933e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1934e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1935e7c4fc90SDmitry Karpeev }
1936e7c4fc90SDmitry Karpeev 
193747c6ae99SBarry Smith /*@
193847c6ae99SBarry Smith   DMRefine - Refines a DM object
193947c6ae99SBarry Smith 
1940d083f849SBarry Smith   Collective on dm
194147c6ae99SBarry Smith 
194247c6ae99SBarry Smith   Input Parameter:
194347c6ae99SBarry Smith + dm   - the DM object
194491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
194547c6ae99SBarry Smith 
194647c6ae99SBarry Smith   Output Parameter:
19470298fd71SBarry Smith . dmf - the refined DM, or NULL
1948ae0a1c52SMatthew G Knepley 
1949412e9a14SMatthew G. Knepley   Options Dtabase Keys:
1950412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
1951412e9a14SMatthew G. Knepley 
19520298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
195347c6ae99SBarry Smith 
195447c6ae99SBarry Smith   Level: developer
195547c6ae99SBarry Smith 
1956e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
195747c6ae99SBarry Smith @*/
19587087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
195947c6ae99SBarry Smith {
196047c6ae99SBarry Smith   PetscErrorCode   ierr;
1961c833c3b5SJed Brown   DMRefineHookLink link;
196247c6ae99SBarry Smith 
196347c6ae99SBarry Smith   PetscFunctionBegin;
1964732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1965b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
19661ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
196747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
19684057135bSMatthew G Knepley   if (*dmf) {
196943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
19708865f1eaSKarl Rupp 
19718cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
19728865f1eaSKarl Rupp 
1973644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
19740598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1975656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
19768865f1eaSKarl Rupp 
1977e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1978c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
19798865f1eaSKarl Rupp       if (link->refinehook) {
19808865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19818865f1eaSKarl Rupp       }
1982c833c3b5SJed Brown     }
1983c833c3b5SJed Brown   }
19841ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1985c833c3b5SJed Brown   PetscFunctionReturn(0);
1986c833c3b5SJed Brown }
1987c833c3b5SJed Brown 
1988bb9467b5SJed Brown /*@C
1989c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1990c833c3b5SJed Brown 
1991c833c3b5SJed Brown    Logically Collective
1992c833c3b5SJed Brown 
1993c833c3b5SJed Brown    Input Arguments:
1994c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1995c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1996c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19970298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1998c833c3b5SJed Brown 
1999c833c3b5SJed Brown    Calling sequence of refinehook:
2000c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2001c833c3b5SJed Brown 
2002c833c3b5SJed Brown +  coarse - coarse level DM
2003c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2004c833c3b5SJed Brown -  ctx - optional user-defined function context
2005c833c3b5SJed Brown 
2006c833c3b5SJed Brown    Calling sequence for interphook:
2007c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2008c833c3b5SJed Brown 
2009c833c3b5SJed Brown +  coarse - coarse level DM
2010c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2011c833c3b5SJed Brown .  fine - fine level DM to update
2012c833c3b5SJed Brown -  ctx - optional user-defined function context
2013c833c3b5SJed Brown 
2014c833c3b5SJed Brown    Level: advanced
2015c833c3b5SJed Brown 
2016c833c3b5SJed Brown    Notes:
2017c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2018c833c3b5SJed Brown 
2019c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2020c833c3b5SJed Brown 
2021bb9467b5SJed Brown    This function is currently not available from Fortran.
2022bb9467b5SJed Brown 
2023c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2024c833c3b5SJed Brown @*/
2025c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2026c833c3b5SJed Brown {
2027c833c3b5SJed Brown   PetscErrorCode   ierr;
2028c833c3b5SJed Brown   DMRefineHookLink link,*p;
2029c833c3b5SJed Brown 
2030c833c3b5SJed Brown   PetscFunctionBegin;
2031c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20323d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
20333d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
20343d8e3701SJed Brown   }
203595dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2036c833c3b5SJed Brown   link->refinehook = refinehook;
2037c833c3b5SJed Brown   link->interphook = interphook;
2038c833c3b5SJed Brown   link->ctx        = ctx;
20390298fd71SBarry Smith   link->next       = NULL;
2040c833c3b5SJed Brown   *p               = link;
2041c833c3b5SJed Brown   PetscFunctionReturn(0);
2042c833c3b5SJed Brown }
2043c833c3b5SJed Brown 
20443d8e3701SJed Brown /*@C
20453d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
20463d8e3701SJed Brown 
20473d8e3701SJed Brown    Logically Collective
20483d8e3701SJed Brown 
20493d8e3701SJed Brown    Input Arguments:
20503d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
20513d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
20523d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20533d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20543d8e3701SJed Brown 
20553d8e3701SJed Brown    Level: advanced
20563d8e3701SJed Brown 
20573d8e3701SJed Brown    Notes:
20583d8e3701SJed Brown    This function does nothing if the hook is not in the list.
20593d8e3701SJed Brown 
20603d8e3701SJed Brown    This function is currently not available from Fortran.
20613d8e3701SJed Brown 
20623d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20633d8e3701SJed Brown @*/
20643d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
20653d8e3701SJed Brown {
20663d8e3701SJed Brown   PetscErrorCode   ierr;
20673d8e3701SJed Brown   DMRefineHookLink link,*p;
20683d8e3701SJed Brown 
20693d8e3701SJed Brown   PetscFunctionBegin;
20703d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20713d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
20723d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
20733d8e3701SJed Brown       link = *p;
20743d8e3701SJed Brown       *p = link->next;
20753d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
20763d8e3701SJed Brown       break;
20773d8e3701SJed Brown     }
20783d8e3701SJed Brown   }
20793d8e3701SJed Brown   PetscFunctionReturn(0);
20803d8e3701SJed Brown }
20813d8e3701SJed Brown 
2082c833c3b5SJed Brown /*@
2083c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2084c833c3b5SJed Brown 
2085c833c3b5SJed Brown    Collective if any hooks are
2086c833c3b5SJed Brown 
2087c833c3b5SJed Brown    Input Arguments:
2088c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2089e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2090c833c3b5SJed Brown -  fine - finer DM to update
2091c833c3b5SJed Brown 
2092c833c3b5SJed Brown    Level: developer
2093c833c3b5SJed Brown 
2094c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2095c833c3b5SJed Brown @*/
2096c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2097c833c3b5SJed Brown {
2098c833c3b5SJed Brown   PetscErrorCode   ierr;
2099c833c3b5SJed Brown   DMRefineHookLink link;
2100c833c3b5SJed Brown 
2101c833c3b5SJed Brown   PetscFunctionBegin;
2102c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
21038865f1eaSKarl Rupp     if (link->interphook) {
21048865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
21058865f1eaSKarl Rupp     }
21064057135bSMatthew G Knepley   }
210747c6ae99SBarry Smith   PetscFunctionReturn(0);
210847c6ae99SBarry Smith }
210947c6ae99SBarry Smith 
2110eb3f98d2SBarry Smith /*@
2111eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2112eb3f98d2SBarry Smith 
2113eb3f98d2SBarry Smith     Not Collective
2114eb3f98d2SBarry Smith 
2115eb3f98d2SBarry Smith     Input Parameter:
2116eb3f98d2SBarry Smith .   dm - the DM object
2117eb3f98d2SBarry Smith 
2118eb3f98d2SBarry Smith     Output Parameter:
2119eb3f98d2SBarry Smith .   level - number of refinements
2120eb3f98d2SBarry Smith 
2121eb3f98d2SBarry Smith     Level: developer
2122eb3f98d2SBarry Smith 
21236a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2124eb3f98d2SBarry Smith 
2125eb3f98d2SBarry Smith @*/
2126eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2127eb3f98d2SBarry Smith {
2128eb3f98d2SBarry Smith   PetscFunctionBegin;
2129eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2130eb3f98d2SBarry Smith   *level = dm->levelup;
2131eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2132eb3f98d2SBarry Smith }
2133eb3f98d2SBarry Smith 
2134fef3a512SBarry Smith /*@
2135fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2136fef3a512SBarry Smith 
2137fef3a512SBarry Smith     Not Collective
2138fef3a512SBarry Smith 
2139fef3a512SBarry Smith     Input Parameter:
2140fef3a512SBarry Smith +   dm - the DM object
2141fef3a512SBarry Smith -   level - number of refinements
2142fef3a512SBarry Smith 
2143fef3a512SBarry Smith     Level: advanced
2144fef3a512SBarry Smith 
214595452b02SPatrick Sanan     Notes:
214695452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2147fef3a512SBarry Smith 
2148fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2149fef3a512SBarry Smith 
2150fef3a512SBarry Smith @*/
2151fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2152fef3a512SBarry Smith {
2153fef3a512SBarry Smith   PetscFunctionBegin;
2154fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2155fef3a512SBarry Smith   dm->levelup = level;
2156fef3a512SBarry Smith   PetscFunctionReturn(0);
2157fef3a512SBarry Smith }
2158fef3a512SBarry Smith 
2159ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2160ca3d3a14SMatthew G. Knepley {
2161ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2162ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2163ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2164ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2165ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2166ca3d3a14SMatthew G. Knepley }
2167ca3d3a14SMatthew G. Knepley 
2168ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2169ca3d3a14SMatthew G. Knepley {
2170ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2171ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2172ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2173ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2174ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2175ca3d3a14SMatthew G. Knepley }
2176ca3d3a14SMatthew G. Knepley 
2177ca3d3a14SMatthew G. Knepley /*@
2178c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2179ca3d3a14SMatthew G. Knepley 
2180ca3d3a14SMatthew G. Knepley   Input Parameter:
2181ca3d3a14SMatthew G. Knepley . dm - The DM
2182ca3d3a14SMatthew G. Knepley 
2183ca3d3a14SMatthew G. Knepley   Output Parameter:
2184ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2185ca3d3a14SMatthew G. Knepley 
2186ca3d3a14SMatthew G. Knepley   Level: developer
2187ca3d3a14SMatthew G. Knepley 
2188436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2189ca3d3a14SMatthew G. Knepley @*/
2190ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2191ca3d3a14SMatthew G. Knepley {
2192ca3d3a14SMatthew G. Knepley   Vec            tv;
2193ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2194ca3d3a14SMatthew G. Knepley 
2195ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2196ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2197534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2198ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2199ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2200ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2201ca3d3a14SMatthew G. Knepley }
2202ca3d3a14SMatthew G. Knepley 
2203ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2204ca3d3a14SMatthew G. Knepley {
2205ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2206ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2207ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2208ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2209ca3d3a14SMatthew G. Knepley 
2210ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2211ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
221292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2213ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2214ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2215ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
221692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2217ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2218ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2219ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2220ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2221ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2222ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2223ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2224ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2225ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2226ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2227ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2228ca3d3a14SMatthew G. Knepley     }
2229ca3d3a14SMatthew G. Knepley   }
2230ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2231ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2232ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2233ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2234ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2235ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2236ca3d3a14SMatthew G. Knepley       if (dof) {
2237ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2238ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2239ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2240ca3d3a14SMatthew G. Knepley 
2241ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2242ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2243ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2244580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2245ca3d3a14SMatthew G. Knepley       }
2246ca3d3a14SMatthew G. Knepley     }
2247ca3d3a14SMatthew G. Knepley   }
2248ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2249ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2250ca3d3a14SMatthew G. Knepley }
2251ca3d3a14SMatthew G. Knepley 
2252ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2253ca3d3a14SMatthew G. Knepley {
2254ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2255ca3d3a14SMatthew G. Knepley 
2256ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2257ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2258ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2259ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2260ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2261ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2262ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2263ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2264ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2265ca3d3a14SMatthew G. Knepley }
2266ca3d3a14SMatthew G. Knepley 
2267bb9467b5SJed Brown /*@C
2268baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2269baf369e7SPeter Brune 
2270baf369e7SPeter Brune    Logically Collective
2271baf369e7SPeter Brune 
2272baf369e7SPeter Brune    Input Arguments:
2273baf369e7SPeter Brune +  dm - the DM
2274baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2275baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
22760298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2277baf369e7SPeter Brune 
2278baf369e7SPeter Brune    Calling sequence for beginhook:
2279baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2280baf369e7SPeter Brune 
2281baf369e7SPeter Brune +  dm - global DM
2282baf369e7SPeter Brune .  g - global vector
2283baf369e7SPeter Brune .  mode - mode
2284baf369e7SPeter Brune .  l - local vector
2285baf369e7SPeter Brune -  ctx - optional user-defined function context
2286baf369e7SPeter Brune 
2287baf369e7SPeter Brune 
2288baf369e7SPeter Brune    Calling sequence for endhook:
2289ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2290baf369e7SPeter Brune 
2291baf369e7SPeter Brune +  global - global DM
2292baf369e7SPeter Brune -  ctx - optional user-defined function context
2293baf369e7SPeter Brune 
2294baf369e7SPeter Brune    Level: advanced
2295baf369e7SPeter Brune 
2296baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2297baf369e7SPeter Brune @*/
2298baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2299baf369e7SPeter Brune {
2300baf369e7SPeter Brune   PetscErrorCode          ierr;
2301baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2302baf369e7SPeter Brune 
2303baf369e7SPeter Brune   PetscFunctionBegin;
2304baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2305baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
230695dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2307baf369e7SPeter Brune   link->beginhook = beginhook;
2308baf369e7SPeter Brune   link->endhook   = endhook;
2309baf369e7SPeter Brune   link->ctx       = ctx;
23100298fd71SBarry Smith   link->next      = NULL;
2311baf369e7SPeter Brune   *p              = link;
2312baf369e7SPeter Brune   PetscFunctionReturn(0);
2313baf369e7SPeter Brune }
2314baf369e7SPeter Brune 
23154c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
23164c274da1SToby Isaac {
23174c274da1SToby Isaac   Mat cMat;
23184c274da1SToby Isaac   Vec cVec;
23194c274da1SToby Isaac   PetscSection section, cSec;
23204c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
23214c274da1SToby Isaac   PetscErrorCode ierr;
23224c274da1SToby Isaac 
23234c274da1SToby Isaac   PetscFunctionBegin;
23244c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23254c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23264c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
23275db9a05bSToby Isaac     PetscInt nRows;
23285db9a05bSToby Isaac 
23295db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23305db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
233192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
23327711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23334c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
23344c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23354c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23364c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23374c274da1SToby Isaac       if (dof) {
23384c274da1SToby Isaac         PetscScalar *vals;
23394c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
23404c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
23414c274da1SToby Isaac       }
23424c274da1SToby Isaac     }
23434c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23444c274da1SToby Isaac   }
23454c274da1SToby Isaac   PetscFunctionReturn(0);
23464c274da1SToby Isaac }
23474c274da1SToby Isaac 
234847c6ae99SBarry Smith /*@
234901729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
235001729b5cSPatrick Sanan 
2351d083f849SBarry Smith     Neighbor-wise Collective on dm
235201729b5cSPatrick Sanan 
235301729b5cSPatrick Sanan     Input Parameters:
235401729b5cSPatrick Sanan +   dm - the DM object
235501729b5cSPatrick Sanan .   g - the global vector
235601729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
235701729b5cSPatrick Sanan -   l - the local vector
235801729b5cSPatrick Sanan 
235901729b5cSPatrick Sanan     Notes:
236001729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
236101729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
236201729b5cSPatrick Sanan 
236301729b5cSPatrick Sanan     Level: beginner
236401729b5cSPatrick Sanan 
236501729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
236601729b5cSPatrick Sanan 
236701729b5cSPatrick Sanan @*/
236801729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
236901729b5cSPatrick Sanan {
237001729b5cSPatrick Sanan   PetscErrorCode ierr;
237101729b5cSPatrick Sanan 
237201729b5cSPatrick Sanan   PetscFunctionBegin;
237301729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
237401729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
237501729b5cSPatrick Sanan   PetscFunctionReturn(0);
237601729b5cSPatrick Sanan }
237701729b5cSPatrick Sanan 
237801729b5cSPatrick Sanan /*@
237947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
238047c6ae99SBarry Smith 
2381d083f849SBarry Smith     Neighbor-wise Collective on dm
238247c6ae99SBarry Smith 
238347c6ae99SBarry Smith     Input Parameters:
238447c6ae99SBarry Smith +   dm - the DM object
238547c6ae99SBarry Smith .   g - the global vector
238647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
238747c6ae99SBarry Smith -   l - the local vector
238847c6ae99SBarry Smith 
238901729b5cSPatrick Sanan     Level: intermediate
239047c6ae99SBarry Smith 
239101729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
239247c6ae99SBarry Smith 
239347c6ae99SBarry Smith @*/
23947087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
239547c6ae99SBarry Smith {
23967128ae9fSMatthew G Knepley   PetscSF                 sf;
239747c6ae99SBarry Smith   PetscErrorCode          ierr;
2398baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
239947c6ae99SBarry Smith 
240047c6ae99SBarry Smith   PetscFunctionBegin;
2401171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2402baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
24038865f1eaSKarl Rupp     if (link->beginhook) {
24048865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
24058865f1eaSKarl Rupp     }
2406baf369e7SPeter Brune   }
24071bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
24087128ae9fSMatthew G Knepley   if (sf) {
2409ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2410ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
24117128ae9fSMatthew G Knepley 
241282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2413fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2414fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24157128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2416fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2417fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24187128ae9fSMatthew G Knepley   } else {
241933907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2420843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24217128ae9fSMatthew G Knepley   }
242247c6ae99SBarry Smith   PetscFunctionReturn(0);
242347c6ae99SBarry Smith }
242447c6ae99SBarry Smith 
242547c6ae99SBarry Smith /*@
242647c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
242747c6ae99SBarry Smith 
2428d083f849SBarry Smith     Neighbor-wise Collective on dm
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith     Input Parameters:
243147c6ae99SBarry Smith +   dm - the DM object
243247c6ae99SBarry Smith .   g - the global vector
243347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
243447c6ae99SBarry Smith -   l - the local vector
243547c6ae99SBarry Smith 
243601729b5cSPatrick Sanan     Level: intermediate
243747c6ae99SBarry Smith 
243801729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
243947c6ae99SBarry Smith 
244047c6ae99SBarry Smith @*/
24417087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
244247c6ae99SBarry Smith {
24437128ae9fSMatthew G Knepley   PetscSF                 sf;
244447c6ae99SBarry Smith   PetscErrorCode          ierr;
2445ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2446ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2447ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2448baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
244947c6ae99SBarry Smith 
245047c6ae99SBarry Smith   PetscFunctionBegin;
2451171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24521bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2453ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
24547128ae9fSMatthew G Knepley   if (sf) {
245582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24567128ae9fSMatthew G Knepley 
2457fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2458fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24597128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2460fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2461fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
2462ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
24637128ae9fSMatthew G Knepley   } else {
246433907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2465843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24667128ae9fSMatthew G Knepley   }
24674c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2468baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2469baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2470baf369e7SPeter Brune   }
247147c6ae99SBarry Smith   PetscFunctionReturn(0);
247247c6ae99SBarry Smith }
247347c6ae99SBarry Smith 
2474d4d07f1eSToby Isaac /*@C
2475d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2476d4d07f1eSToby Isaac 
2477d4d07f1eSToby Isaac    Logically Collective
2478d4d07f1eSToby Isaac 
2479d4d07f1eSToby Isaac    Input Arguments:
2480d4d07f1eSToby Isaac +  dm - the DM
2481d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2482d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2483d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2484d4d07f1eSToby Isaac 
2485d4d07f1eSToby Isaac    Calling sequence for beginhook:
2486d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2487d4d07f1eSToby Isaac 
2488d4d07f1eSToby Isaac +  dm - global DM
2489d4d07f1eSToby Isaac .  l - local vector
2490d4d07f1eSToby Isaac .  mode - mode
2491d4d07f1eSToby Isaac .  g - global vector
2492d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2493d4d07f1eSToby Isaac 
2494d4d07f1eSToby Isaac 
2495d4d07f1eSToby Isaac    Calling sequence for endhook:
2496d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2497d4d07f1eSToby Isaac 
2498d4d07f1eSToby Isaac +  global - global DM
2499d4d07f1eSToby Isaac .  l - local vector
2500d4d07f1eSToby Isaac .  mode - mode
2501d4d07f1eSToby Isaac .  g - global vector
2502d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2503d4d07f1eSToby Isaac 
2504d4d07f1eSToby Isaac    Level: advanced
2505d4d07f1eSToby Isaac 
2506d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2507d4d07f1eSToby Isaac @*/
2508d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2509d4d07f1eSToby Isaac {
2510d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2511d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2512d4d07f1eSToby Isaac 
2513d4d07f1eSToby Isaac   PetscFunctionBegin;
2514d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2515d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
251695dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2517d4d07f1eSToby Isaac   link->beginhook = beginhook;
2518d4d07f1eSToby Isaac   link->endhook   = endhook;
2519d4d07f1eSToby Isaac   link->ctx       = ctx;
2520d4d07f1eSToby Isaac   link->next      = NULL;
2521d4d07f1eSToby Isaac   *p              = link;
2522d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2523d4d07f1eSToby Isaac }
2524d4d07f1eSToby Isaac 
25254c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
25264c274da1SToby Isaac {
25274c274da1SToby Isaac   Mat cMat;
25284c274da1SToby Isaac   Vec cVec;
25294c274da1SToby Isaac   PetscSection section, cSec;
25304c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25314c274da1SToby Isaac   PetscErrorCode ierr;
25324c274da1SToby Isaac 
25334c274da1SToby Isaac   PetscFunctionBegin;
25344c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25354c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25364c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
25375db9a05bSToby Isaac     PetscInt nRows;
25385db9a05bSToby Isaac 
25395db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25405db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
254192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25427711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25434c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25444c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25454c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25464c274da1SToby Isaac       if (dof) {
25474c274da1SToby Isaac         PetscInt d;
25484c274da1SToby Isaac         PetscScalar *vals;
25494c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
25504c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
25514c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
25524c274da1SToby Isaac          * we just extracted */
25534c274da1SToby Isaac         for (d = 0; d < dof; d++) {
25544c274da1SToby Isaac           vals[d] = 0.;
25554c274da1SToby Isaac         }
25564c274da1SToby Isaac       }
25574c274da1SToby Isaac     }
25584c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
25594c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25604c274da1SToby Isaac   }
25614c274da1SToby Isaac   PetscFunctionReturn(0);
25624c274da1SToby Isaac }
256301729b5cSPatrick Sanan /*@
256401729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
256501729b5cSPatrick Sanan 
2566d083f849SBarry Smith     Neighbor-wise Collective on dm
256701729b5cSPatrick Sanan 
256801729b5cSPatrick Sanan     Input Parameters:
256901729b5cSPatrick Sanan +   dm - the DM object
257001729b5cSPatrick Sanan .   l - the local vector
257101729b5cSPatrick 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.
257201729b5cSPatrick Sanan -   g - the global vector
257301729b5cSPatrick Sanan 
257401729b5cSPatrick Sanan     Notes:
257501729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
257601729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
257701729b5cSPatrick Sanan 
257801729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
257901729b5cSPatrick 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.
258001729b5cSPatrick Sanan 
258101729b5cSPatrick Sanan     Level: beginner
258201729b5cSPatrick Sanan 
258301729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
258401729b5cSPatrick Sanan 
258501729b5cSPatrick Sanan @*/
258601729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
258701729b5cSPatrick Sanan {
258801729b5cSPatrick Sanan   PetscErrorCode ierr;
258901729b5cSPatrick Sanan 
259001729b5cSPatrick Sanan   PetscFunctionBegin;
259101729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
259201729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
259301729b5cSPatrick Sanan   PetscFunctionReturn(0);
259401729b5cSPatrick Sanan }
25954c274da1SToby Isaac 
259647c6ae99SBarry Smith /*@
259701729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25989a42bb27SBarry Smith 
2599d083f849SBarry Smith     Neighbor-wise Collective on dm
26009a42bb27SBarry Smith 
26019a42bb27SBarry Smith     Input Parameters:
26029a42bb27SBarry Smith +   dm - the DM object
2603f6813fd5SJed Brown .   l - the local vector
26041eb28f2eSBarry 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.
26051eb28f2eSBarry Smith -   g - the global vector
26069a42bb27SBarry Smith 
260795452b02SPatrick Sanan     Notes:
260895452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
260984330215SMatthew 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.
26109a42bb27SBarry Smith 
261101729b5cSPatrick Sanan     Level: intermediate
26129a42bb27SBarry Smith 
261301729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
26149a42bb27SBarry Smith 
26159a42bb27SBarry Smith @*/
26167087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
26179a42bb27SBarry Smith {
26187128ae9fSMatthew G Knepley   PetscSF                 sf;
261984330215SMatthew G. Knepley   PetscSection            s, gs;
2620d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2621ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2622ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2623ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2624fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
262584330215SMatthew G. Knepley   PetscErrorCode          ierr;
26269a42bb27SBarry Smith 
26279a42bb27SBarry Smith   PetscFunctionBegin;
2628171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2629d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2630d4d07f1eSToby Isaac     if (link->beginhook) {
2631d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2632d4d07f1eSToby Isaac     }
2633d4d07f1eSToby Isaac   }
26344c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
26351bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
263692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26377128ae9fSMatthew G Knepley   switch (mode) {
26387128ae9fSMatthew G Knepley   case INSERT_VALUES:
26397128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2640304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
264184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26427128ae9fSMatthew G Knepley   case ADD_VALUES:
26437128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2644304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
264584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26467128ae9fSMatthew G Knepley   default:
264782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26487128ae9fSMatthew G Knepley   }
2649ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2650ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2651ca3d3a14SMatthew G. Knepley     if (transform) {
2652ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2653ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2654ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2655ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2656fa88e482SJed Brown     } else if (isInsert) {
2657ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2658fa88e482SJed Brown     } else {
2659fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2660fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2661ca3d3a14SMatthew G. Knepley     }
2662fa88e482SJed Brown     if (s && isInsert) {
26637128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2664fa88e482SJed Brown     } else {
2665fa88e482SJed Brown       ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2666fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2667fa88e482SJed Brown     }
2668ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2669a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
267084330215SMatthew G. Knepley     } else if (s && isInsert) {
267184330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
267284330215SMatthew G. Knepley 
2673e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
267484330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
267584330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
267684330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2677b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
267884330215SMatthew G. Knepley 
267984330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
268003442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
268184330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2682b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
268384330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
268484330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2685b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
268603442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2687b3b16f48SMatthew 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);
2688b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2689b3b16f48SMatthew G. Knepley         if (!gcdof) {
269084330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2691b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2692b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
269384330215SMatthew G. Knepley           const PetscInt *cdofs;
269484330215SMatthew G. Knepley           PetscInt        cind = 0;
269584330215SMatthew G. Knepley 
269684330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2697b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
269884330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2699b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
270084330215SMatthew G. Knepley           }
2701b3b16f48SMatthew 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);
270284330215SMatthew G. Knepley       }
2703ca3d3a14SMatthew G. Knepley     }
2704fa88e482SJed Brown     if (g_inplace) {
2705fa88e482SJed Brown       ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
2706fa88e482SJed Brown     } else {
27077128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2708fa88e482SJed Brown     }
2709ca3d3a14SMatthew G. Knepley     if (transform) {
2710ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2711ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2712fa88e482SJed Brown     } else if (l_inplace) {
2713fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2714ca3d3a14SMatthew G. Knepley     } else {
2715ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2716ca3d3a14SMatthew G. Knepley     }
27177128ae9fSMatthew G Knepley   } else {
2718b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2719843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27207128ae9fSMatthew G Knepley   }
27219a42bb27SBarry Smith   PetscFunctionReturn(0);
27229a42bb27SBarry Smith }
27239a42bb27SBarry Smith 
27249a42bb27SBarry Smith /*@
27259a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
272647c6ae99SBarry Smith 
2727d083f849SBarry Smith     Neighbor-wise Collective on dm
272847c6ae99SBarry Smith 
272947c6ae99SBarry Smith     Input Parameters:
273047c6ae99SBarry Smith +   dm - the DM object
2731f6813fd5SJed Brown .   l - the local vector
273247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2733f6813fd5SJed Brown -   g - the global vector
273447c6ae99SBarry Smith 
273501729b5cSPatrick Sanan     Level: intermediate
273647c6ae99SBarry Smith 
2737e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
273847c6ae99SBarry Smith 
273947c6ae99SBarry Smith @*/
27407087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
274147c6ae99SBarry Smith {
27427128ae9fSMatthew G Knepley   PetscSF                 sf;
274384330215SMatthew G. Knepley   PetscSection            s;
2744d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2745ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
274684330215SMatthew G. Knepley   PetscErrorCode          ierr;
274747c6ae99SBarry Smith 
274847c6ae99SBarry Smith   PetscFunctionBegin;
2749171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27501bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
275192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27527128ae9fSMatthew G Knepley   switch (mode) {
27537128ae9fSMatthew G Knepley   case INSERT_VALUES:
27547128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
275584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27567128ae9fSMatthew G Knepley   case ADD_VALUES:
27577128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
275884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27597128ae9fSMatthew G Knepley   default:
276082f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27617128ae9fSMatthew G Knepley   }
276284330215SMatthew G. Knepley   if (sf && !isInsert) {
2763ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2764ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2765ca3d3a14SMatthew G. Knepley     Vec                tmpl;
276684330215SMatthew G. Knepley 
2767ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2768ca3d3a14SMatthew G. Knepley     if (transform) {
2769ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2770ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2771ca3d3a14SMatthew G. Knepley     } else {
2772fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2773ca3d3a14SMatthew G. Knepley     }
2774fa88e482SJed Brown     ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2775a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2776ca3d3a14SMatthew G. Knepley     if (transform) {
2777ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2778ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2779ca3d3a14SMatthew G. Knepley     } else {
2780fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2781ca3d3a14SMatthew G. Knepley     }
2782fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
278384330215SMatthew G. Knepley   } else if (s && isInsert) {
27847128ae9fSMatthew G Knepley   } else {
2785b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2786843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27877128ae9fSMatthew G Knepley   }
2788d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2789d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2790d4d07f1eSToby Isaac   }
279147c6ae99SBarry Smith   PetscFunctionReturn(0);
279247c6ae99SBarry Smith }
279347c6ae99SBarry Smith 
2794f089877aSRichard Tran Mills /*@
2795bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2796bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2797d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2798f089877aSRichard Tran Mills 
2799d083f849SBarry Smith    Neighbor-wise Collective on dm
2800f089877aSRichard Tran Mills 
2801f089877aSRichard Tran Mills    Input Parameters:
2802f089877aSRichard Tran Mills +  dm - the DM object
2803bc0a1609SRichard Tran Mills .  g - the original local vector
2804bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2805f089877aSRichard Tran Mills 
2806bc0a1609SRichard Tran Mills    Output Parameter:
2807bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2808f089877aSRichard Tran Mills 
2809f089877aSRichard Tran Mills    Level: intermediate
2810f089877aSRichard Tran Mills 
2811bc0a1609SRichard Tran Mills    Notes:
2812bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2813bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2814bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2815bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2816bc0a1609SRichard Tran Mills 
2817bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2818f089877aSRichard Tran Mills 
2819f089877aSRichard Tran Mills @*/
2820f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2821f089877aSRichard Tran Mills {
2822f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2823f089877aSRichard Tran Mills 
2824f089877aSRichard Tran Mills   PetscFunctionBegin;
2825f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2826bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2827f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2828f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2829f089877aSRichard Tran Mills }
2830f089877aSRichard Tran Mills 
2831f089877aSRichard Tran Mills /*@
2832bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2833bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2834d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2835f089877aSRichard Tran Mills 
2836d083f849SBarry Smith    Neighbor-wise Collective on dm
2837f089877aSRichard Tran Mills 
2838f089877aSRichard Tran Mills    Input Parameters:
2839bc0a1609SRichard Tran Mills +  da - the DM object
2840bc0a1609SRichard Tran Mills .  g - the original local vector
2841bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2842f089877aSRichard Tran Mills 
2843bc0a1609SRichard Tran Mills    Output Parameter:
2844bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2845f089877aSRichard Tran Mills 
2846f089877aSRichard Tran Mills    Level: intermediate
2847f089877aSRichard Tran Mills 
2848bc0a1609SRichard Tran Mills    Notes:
2849bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2850bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2851bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2852bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2853bc0a1609SRichard Tran Mills 
2854bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2855f089877aSRichard Tran Mills 
2856f089877aSRichard Tran Mills @*/
2857f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2858f089877aSRichard Tran Mills {
2859f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2860f089877aSRichard Tran Mills 
2861f089877aSRichard Tran Mills   PetscFunctionBegin;
2862f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2863bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2864f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2865f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2866f089877aSRichard Tran Mills }
2867f089877aSRichard Tran Mills 
2868f089877aSRichard Tran Mills 
286947c6ae99SBarry Smith /*@
287047c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
287147c6ae99SBarry Smith 
2872d083f849SBarry Smith     Collective on dm
287347c6ae99SBarry Smith 
287447c6ae99SBarry Smith     Input Parameter:
287547c6ae99SBarry Smith +   dm - the DM object
287691d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
287747c6ae99SBarry Smith 
287847c6ae99SBarry Smith     Output Parameter:
287947c6ae99SBarry Smith .   dmc - the coarsened DM
288047c6ae99SBarry Smith 
288147c6ae99SBarry Smith     Level: developer
288247c6ae99SBarry Smith 
2883e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
288447c6ae99SBarry Smith 
288547c6ae99SBarry Smith @*/
28867087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
288747c6ae99SBarry Smith {
288847c6ae99SBarry Smith   PetscErrorCode    ierr;
2889b17ce1afSJed Brown   DMCoarsenHookLink link;
289047c6ae99SBarry Smith 
289147c6ae99SBarry Smith   PetscFunctionBegin;
2892171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2893b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
289447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
289547c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2896b9d85ea2SLisandro Dalcin   if (*dmc) {
2897a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
289843842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28998cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2900644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
29010598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2902656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2903e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2904b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2905b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2906b17ce1afSJed Brown     }
2907b9d85ea2SLisandro Dalcin   }
290847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2909b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2910b17ce1afSJed Brown   PetscFunctionReturn(0);
2911b17ce1afSJed Brown }
2912b17ce1afSJed Brown 
2913bb9467b5SJed Brown /*@C
2914b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2915b17ce1afSJed Brown 
2916b17ce1afSJed Brown    Logically Collective
2917b17ce1afSJed Brown 
2918b17ce1afSJed Brown    Input Arguments:
2919b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2920b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2921b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
29220298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2923b17ce1afSJed Brown 
2924b17ce1afSJed Brown    Calling sequence of coarsenhook:
2925b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2926b17ce1afSJed Brown 
2927b17ce1afSJed Brown +  fine - fine level DM
2928b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2929b17ce1afSJed Brown -  ctx - optional user-defined function context
2930b17ce1afSJed Brown 
2931b17ce1afSJed Brown    Calling sequence for restricthook:
2932c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2933b17ce1afSJed Brown 
2934b17ce1afSJed Brown +  fine - fine level DM
2935b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2936c833c3b5SJed Brown .  rscale - scaling vector for restriction
2937c833c3b5SJed Brown .  inject - matrix restricting by injection
2938b17ce1afSJed Brown .  coarse - coarse level DM to update
2939b17ce1afSJed Brown -  ctx - optional user-defined function context
2940b17ce1afSJed Brown 
2941b17ce1afSJed Brown    Level: advanced
2942b17ce1afSJed Brown 
2943b17ce1afSJed Brown    Notes:
2944b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2945b17ce1afSJed Brown 
2946b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2947b17ce1afSJed Brown 
2948b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2949b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2950b17ce1afSJed Brown 
2951bb9467b5SJed Brown    This function is currently not available from Fortran.
2952bb9467b5SJed Brown 
2953dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2954b17ce1afSJed Brown @*/
2955b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2956b17ce1afSJed Brown {
2957b17ce1afSJed Brown   PetscErrorCode    ierr;
2958b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2959b17ce1afSJed Brown 
2960b17ce1afSJed Brown   PetscFunctionBegin;
2961b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
29621e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
29631e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
29641e3d8eccSJed Brown   }
296595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2966b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2967b17ce1afSJed Brown   link->restricthook = restricthook;
2968b17ce1afSJed Brown   link->ctx          = ctx;
29690298fd71SBarry Smith   link->next         = NULL;
2970b17ce1afSJed Brown   *p                 = link;
2971b17ce1afSJed Brown   PetscFunctionReturn(0);
2972b17ce1afSJed Brown }
2973b17ce1afSJed Brown 
2974dc822a44SJed Brown /*@C
2975dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2976dc822a44SJed Brown 
2977dc822a44SJed Brown    Logically Collective
2978dc822a44SJed Brown 
2979dc822a44SJed Brown    Input Arguments:
2980dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2981dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2982dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2983dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2984dc822a44SJed Brown 
2985dc822a44SJed Brown    Level: advanced
2986dc822a44SJed Brown 
2987dc822a44SJed Brown    Notes:
2988dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2989dc822a44SJed Brown 
2990dc822a44SJed Brown    This function is currently not available from Fortran.
2991dc822a44SJed Brown 
2992dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2993dc822a44SJed Brown @*/
2994dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2995dc822a44SJed Brown {
2996dc822a44SJed Brown   PetscErrorCode    ierr;
2997dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2998dc822a44SJed Brown 
2999dc822a44SJed Brown   PetscFunctionBegin;
3000dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3001dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3002dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3003dc822a44SJed Brown       link = *p;
3004dc822a44SJed Brown       *p = link->next;
3005dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3006dc822a44SJed Brown       break;
3007dc822a44SJed Brown     }
3008dc822a44SJed Brown   }
3009dc822a44SJed Brown   PetscFunctionReturn(0);
3010dc822a44SJed Brown }
3011dc822a44SJed Brown 
3012dc822a44SJed Brown 
3013b17ce1afSJed Brown /*@
3014b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3015b17ce1afSJed Brown 
3016b17ce1afSJed Brown    Collective if any hooks are
3017b17ce1afSJed Brown 
3018b17ce1afSJed Brown    Input Arguments:
3019b17ce1afSJed Brown +  fine - finer DM to use as a base
3020b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3021e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3022b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3023e91eccc2SStefano Zampini -  coarse - coarser DM to update
3024b17ce1afSJed Brown 
3025b17ce1afSJed Brown    Level: developer
3026b17ce1afSJed Brown 
3027b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3028b17ce1afSJed Brown @*/
3029b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3030b17ce1afSJed Brown {
3031b17ce1afSJed Brown   PetscErrorCode    ierr;
3032b17ce1afSJed Brown   DMCoarsenHookLink link;
3033b17ce1afSJed Brown 
3034b17ce1afSJed Brown   PetscFunctionBegin;
3035b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
30368865f1eaSKarl Rupp     if (link->restricthook) {
30378865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
30388865f1eaSKarl Rupp     }
3039b17ce1afSJed Brown   }
304047c6ae99SBarry Smith   PetscFunctionReturn(0);
304147c6ae99SBarry Smith }
304247c6ae99SBarry Smith 
3043bb9467b5SJed Brown /*@C
3044be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
30455dbd56e3SPeter Brune 
3046d083f849SBarry Smith    Logically Collective on global
30475dbd56e3SPeter Brune 
30485dbd56e3SPeter Brune    Input Arguments:
30495dbd56e3SPeter Brune +  global - global DM
3050ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
30515dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
30520298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
30535dbd56e3SPeter Brune 
3054ec4806b8SPeter Brune 
3055ec4806b8SPeter Brune    Calling sequence for ddhook:
3056ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3057ec4806b8SPeter Brune 
3058ec4806b8SPeter Brune +  global - global DM
3059ec4806b8SPeter Brune .  block  - block DM
3060ec4806b8SPeter Brune -  ctx - optional user-defined function context
3061ec4806b8SPeter Brune 
30625dbd56e3SPeter Brune    Calling sequence for restricthook:
3063ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
30645dbd56e3SPeter Brune 
30655dbd56e3SPeter Brune +  global - global DM
30665dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
30675dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3068ec4806b8SPeter Brune .  block  - block DM
30695dbd56e3SPeter Brune -  ctx - optional user-defined function context
30705dbd56e3SPeter Brune 
30715dbd56e3SPeter Brune    Level: advanced
30725dbd56e3SPeter Brune 
30735dbd56e3SPeter Brune    Notes:
3074ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
30755dbd56e3SPeter Brune 
30765dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
30775dbd56e3SPeter Brune 
30785dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3079ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
30805dbd56e3SPeter Brune 
3081bb9467b5SJed Brown    This function is currently not available from Fortran.
3082bb9467b5SJed Brown 
30835dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
30845dbd56e3SPeter Brune @*/
3085be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
30865dbd56e3SPeter Brune {
30875dbd56e3SPeter Brune   PetscErrorCode      ierr;
3088be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
30895dbd56e3SPeter Brune 
30905dbd56e3SPeter Brune   PetscFunctionBegin;
30915dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3092b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3093b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3094b3a6b972SJed Brown   }
309595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30965dbd56e3SPeter Brune   link->restricthook = restricthook;
3097be081cd6SPeter Brune   link->ddhook       = ddhook;
30985dbd56e3SPeter Brune   link->ctx          = ctx;
30990298fd71SBarry Smith   link->next         = NULL;
31005dbd56e3SPeter Brune   *p                 = link;
31015dbd56e3SPeter Brune   PetscFunctionReturn(0);
31025dbd56e3SPeter Brune }
31035dbd56e3SPeter Brune 
3104b3a6b972SJed Brown /*@C
3105b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3106b3a6b972SJed Brown 
3107b3a6b972SJed Brown    Logically Collective
3108b3a6b972SJed Brown 
3109b3a6b972SJed Brown    Input Arguments:
3110b3a6b972SJed Brown +  global - global DM
3111b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3112b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3113b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3114b3a6b972SJed Brown 
3115b3a6b972SJed Brown    Level: advanced
3116b3a6b972SJed Brown 
3117b3a6b972SJed Brown    Notes:
3118b3a6b972SJed Brown 
3119b3a6b972SJed Brown    This function is currently not available from Fortran.
3120b3a6b972SJed Brown 
3121b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3122b3a6b972SJed Brown @*/
3123b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3124b3a6b972SJed Brown {
3125b3a6b972SJed Brown   PetscErrorCode      ierr;
3126b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3127b3a6b972SJed Brown 
3128b3a6b972SJed Brown   PetscFunctionBegin;
3129b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3130b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3131b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3132b3a6b972SJed Brown       link = *p;
3133b3a6b972SJed Brown       *p = link->next;
3134b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3135b3a6b972SJed Brown       break;
3136b3a6b972SJed Brown     }
3137b3a6b972SJed Brown   }
3138b3a6b972SJed Brown   PetscFunctionReturn(0);
3139b3a6b972SJed Brown }
3140b3a6b972SJed Brown 
31415dbd56e3SPeter Brune /*@
3142be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
31435dbd56e3SPeter Brune 
31445dbd56e3SPeter Brune    Collective if any hooks are
31455dbd56e3SPeter Brune 
31465dbd56e3SPeter Brune    Input Arguments:
31475dbd56e3SPeter Brune +  fine - finer DM to use as a base
3148be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3149be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
31505dbd56e3SPeter Brune -  coarse - coarer DM to update
31515dbd56e3SPeter Brune 
31525dbd56e3SPeter Brune    Level: developer
31535dbd56e3SPeter Brune 
31545dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
31555dbd56e3SPeter Brune @*/
3156be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
31575dbd56e3SPeter Brune {
31585dbd56e3SPeter Brune   PetscErrorCode      ierr;
3159be081cd6SPeter Brune   DMSubDomainHookLink link;
31605dbd56e3SPeter Brune 
31615dbd56e3SPeter Brune   PetscFunctionBegin;
3162be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
31638865f1eaSKarl Rupp     if (link->restricthook) {
31648865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
31658865f1eaSKarl Rupp     }
31665dbd56e3SPeter Brune   }
31675dbd56e3SPeter Brune   PetscFunctionReturn(0);
31685dbd56e3SPeter Brune }
31695dbd56e3SPeter Brune 
31705fe1f584SPeter Brune /*@
31716a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
31725fe1f584SPeter Brune 
31735fe1f584SPeter Brune     Not Collective
31745fe1f584SPeter Brune 
31755fe1f584SPeter Brune     Input Parameter:
31765fe1f584SPeter Brune .   dm - the DM object
31775fe1f584SPeter Brune 
31785fe1f584SPeter Brune     Output Parameter:
31796a7d9d85SPeter Brune .   level - number of coarsenings
31805fe1f584SPeter Brune 
31815fe1f584SPeter Brune     Level: developer
31825fe1f584SPeter Brune 
31836a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31845fe1f584SPeter Brune 
31855fe1f584SPeter Brune @*/
31865fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
31875fe1f584SPeter Brune {
31885fe1f584SPeter Brune   PetscFunctionBegin;
31895fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3190b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
31915fe1f584SPeter Brune   *level = dm->leveldown;
31925fe1f584SPeter Brune   PetscFunctionReturn(0);
31935fe1f584SPeter Brune }
31945fe1f584SPeter Brune 
31959a64c4a8SMatthew G. Knepley /*@
31969a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31979a64c4a8SMatthew G. Knepley 
31989a64c4a8SMatthew G. Knepley     Not Collective
31999a64c4a8SMatthew G. Knepley 
32009a64c4a8SMatthew G. Knepley     Input Parameters:
32019a64c4a8SMatthew G. Knepley +   dm - the DM object
32029a64c4a8SMatthew G. Knepley -   level - number of coarsenings
32039a64c4a8SMatthew G. Knepley 
32049a64c4a8SMatthew G. Knepley     Level: developer
32059a64c4a8SMatthew G. Knepley 
32069a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
32079a64c4a8SMatthew G. Knepley @*/
32089a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
32099a64c4a8SMatthew G. Knepley {
32109a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
32119a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32129a64c4a8SMatthew G. Knepley   dm->leveldown = level;
32139a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
32149a64c4a8SMatthew G. Knepley }
32159a64c4a8SMatthew G. Knepley 
32165fe1f584SPeter Brune 
32175fe1f584SPeter Brune 
321847c6ae99SBarry Smith /*@C
321947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
322047c6ae99SBarry Smith 
3221d083f849SBarry Smith     Collective on dm
322247c6ae99SBarry Smith 
322347c6ae99SBarry Smith     Input Parameter:
322447c6ae99SBarry Smith +   dm - the DM object
322547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
322647c6ae99SBarry Smith 
322747c6ae99SBarry Smith     Output Parameter:
322847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
322947c6ae99SBarry Smith 
323047c6ae99SBarry Smith     Level: developer
323147c6ae99SBarry Smith 
3232e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
323347c6ae99SBarry Smith 
323447c6ae99SBarry Smith @*/
32357087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
323647c6ae99SBarry Smith {
323747c6ae99SBarry Smith   PetscErrorCode ierr;
323847c6ae99SBarry Smith 
323947c6ae99SBarry Smith   PetscFunctionBegin;
3240171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3241ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
324247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3243b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
324447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
324547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
324647c6ae99SBarry Smith   } else if (dm->ops->refine) {
324747c6ae99SBarry Smith     PetscInt i;
324847c6ae99SBarry Smith 
3249ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
325047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3251ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
325247c6ae99SBarry Smith     }
3253ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
325447c6ae99SBarry Smith   PetscFunctionReturn(0);
325547c6ae99SBarry Smith }
325647c6ae99SBarry Smith 
325747c6ae99SBarry Smith /*@C
325847c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
325947c6ae99SBarry Smith 
3260d083f849SBarry Smith     Collective on dm
326147c6ae99SBarry Smith 
326247c6ae99SBarry Smith     Input Parameter:
326347c6ae99SBarry Smith +   dm - the DM object
326447c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
326547c6ae99SBarry Smith 
326647c6ae99SBarry Smith     Output Parameter:
326747c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
326847c6ae99SBarry Smith 
326947c6ae99SBarry Smith     Level: developer
327047c6ae99SBarry Smith 
3271e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
327247c6ae99SBarry Smith 
327347c6ae99SBarry Smith @*/
32747087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
327547c6ae99SBarry Smith {
327647c6ae99SBarry Smith   PetscErrorCode ierr;
327747c6ae99SBarry Smith 
327847c6ae99SBarry Smith   PetscFunctionBegin;
3279171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3280ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
328147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
328247c6ae99SBarry Smith   PetscValidPointer(dmc,3);
328347c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
328447c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
328547c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
328647c6ae99SBarry Smith     PetscInt i;
328747c6ae99SBarry Smith 
3288ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
328947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3290ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
329147c6ae99SBarry Smith     }
3292ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
329347c6ae99SBarry Smith   PetscFunctionReturn(0);
329447c6ae99SBarry Smith }
329547c6ae99SBarry Smith 
32961a266240SBarry Smith /*@C
32971a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32981a266240SBarry Smith 
32991a266240SBarry Smith     Not Collective
33001a266240SBarry Smith 
33011a266240SBarry Smith     Input Parameters:
33021a266240SBarry Smith +   dm - the DM object
33031a266240SBarry Smith -   destroy - the destroy function
33041a266240SBarry Smith 
33051a266240SBarry Smith     Level: intermediate
33061a266240SBarry Smith 
3307e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
33081a266240SBarry Smith 
3309f07f9ceaSJed Brown @*/
33101a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
33111a266240SBarry Smith {
33121a266240SBarry Smith   PetscFunctionBegin;
3313171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33141a266240SBarry Smith   dm->ctxdestroy = destroy;
33151a266240SBarry Smith   PetscFunctionReturn(0);
33161a266240SBarry Smith }
33171a266240SBarry Smith 
3318b07ff414SBarry Smith /*@
33191b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
332047c6ae99SBarry Smith 
332147c6ae99SBarry Smith     Not Collective
332247c6ae99SBarry Smith 
332347c6ae99SBarry Smith     Input Parameters:
332447c6ae99SBarry Smith +   dm - the DM object
332547c6ae99SBarry Smith -   ctx - the user context
332647c6ae99SBarry Smith 
332747c6ae99SBarry Smith     Level: intermediate
332847c6ae99SBarry Smith 
3329e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
333047c6ae99SBarry Smith 
333147c6ae99SBarry Smith @*/
33321b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
333347c6ae99SBarry Smith {
333447c6ae99SBarry Smith   PetscFunctionBegin;
3335171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
333647c6ae99SBarry Smith   dm->ctx = ctx;
333747c6ae99SBarry Smith   PetscFunctionReturn(0);
333847c6ae99SBarry Smith }
333947c6ae99SBarry Smith 
334047c6ae99SBarry Smith /*@
33411b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
334247c6ae99SBarry Smith 
334347c6ae99SBarry Smith     Not Collective
334447c6ae99SBarry Smith 
334547c6ae99SBarry Smith     Input Parameter:
334647c6ae99SBarry Smith .   dm - the DM object
334747c6ae99SBarry Smith 
334847c6ae99SBarry Smith     Output Parameter:
334947c6ae99SBarry Smith .   ctx - the user context
335047c6ae99SBarry Smith 
335147c6ae99SBarry Smith     Level: intermediate
335247c6ae99SBarry Smith 
3353e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
335447c6ae99SBarry Smith 
335547c6ae99SBarry Smith @*/
33561b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
335747c6ae99SBarry Smith {
335847c6ae99SBarry Smith   PetscFunctionBegin;
3359171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33601b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
336147c6ae99SBarry Smith   PetscFunctionReturn(0);
336247c6ae99SBarry Smith }
336347c6ae99SBarry Smith 
336408da532bSDmitry Karpeev /*@C
3365df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
336608da532bSDmitry Karpeev 
3367d083f849SBarry Smith     Logically Collective on dm
336808da532bSDmitry Karpeev 
336908da532bSDmitry Karpeev     Input Parameter:
337008da532bSDmitry Karpeev +   dm - the DM object
33710298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
337208da532bSDmitry Karpeev 
337308da532bSDmitry Karpeev     Level: intermediate
337408da532bSDmitry Karpeev 
3375835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
337608da532bSDmitry Karpeev          DMSetJacobian()
337708da532bSDmitry Karpeev 
337808da532bSDmitry Karpeev @*/
337908da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
338008da532bSDmitry Karpeev {
338108da532bSDmitry Karpeev   PetscFunctionBegin;
33825a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
338308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
338408da532bSDmitry Karpeev   PetscFunctionReturn(0);
338508da532bSDmitry Karpeev }
338608da532bSDmitry Karpeev 
338708da532bSDmitry Karpeev /*@
338808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
338908da532bSDmitry Karpeev 
339008da532bSDmitry Karpeev     Not Collective
339108da532bSDmitry Karpeev 
339208da532bSDmitry Karpeev     Input Parameter:
339308da532bSDmitry Karpeev .   dm - the DM object to destroy
339408da532bSDmitry Karpeev 
339508da532bSDmitry Karpeev     Output Parameter:
339608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
339708da532bSDmitry Karpeev 
339808da532bSDmitry Karpeev     Level: developer
339908da532bSDmitry Karpeev 
340074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
340108da532bSDmitry Karpeev 
340208da532bSDmitry Karpeev @*/
340308da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
340408da532bSDmitry Karpeev {
340508da532bSDmitry Karpeev   PetscFunctionBegin;
34065a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3407534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
340808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
340908da532bSDmitry Karpeev   PetscFunctionReturn(0);
341008da532bSDmitry Karpeev }
341108da532bSDmitry Karpeev 
341208da532bSDmitry Karpeev /*@C
341308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
341408da532bSDmitry Karpeev 
3415d083f849SBarry Smith     Logically Collective on dm
341608da532bSDmitry Karpeev 
341708da532bSDmitry Karpeev     Input Parameters:
3418907376e6SBarry Smith .   dm - the DM object
341908da532bSDmitry Karpeev 
342008da532bSDmitry Karpeev     Output parameters:
342108da532bSDmitry Karpeev +   xl - lower bound
342208da532bSDmitry Karpeev -   xu - upper bound
342308da532bSDmitry Karpeev 
3424907376e6SBarry Smith     Level: advanced
3425907376e6SBarry Smith 
342695452b02SPatrick Sanan     Notes:
342795452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
342808da532bSDmitry Karpeev 
342974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
343008da532bSDmitry Karpeev 
343108da532bSDmitry Karpeev @*/
343208da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
343308da532bSDmitry Karpeev {
343408da532bSDmitry Karpeev   PetscErrorCode ierr;
34355fd66863SKarl Rupp 
343608da532bSDmitry Karpeev   PetscFunctionBegin;
34375a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
343808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
34395a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3440b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
344108da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
344208da532bSDmitry Karpeev   PetscFunctionReturn(0);
344308da532bSDmitry Karpeev }
344408da532bSDmitry Karpeev 
3445b0ae01b7SPeter Brune /*@
3446b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3447b0ae01b7SPeter Brune 
3448b0ae01b7SPeter Brune     Not Collective
3449b0ae01b7SPeter Brune 
3450b0ae01b7SPeter Brune     Input Parameter:
3451b0ae01b7SPeter Brune .   dm - the DM object
3452b0ae01b7SPeter Brune 
3453b0ae01b7SPeter Brune     Output Parameter:
3454b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3455b0ae01b7SPeter Brune 
3456b0ae01b7SPeter Brune     Level: developer
3457b0ae01b7SPeter Brune 
34581565f0a7SPatrick Sanan .seealso DMCreateColoring()
3459b0ae01b7SPeter Brune 
3460b0ae01b7SPeter Brune @*/
3461b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3462b0ae01b7SPeter Brune {
3463b0ae01b7SPeter Brune   PetscFunctionBegin;
34645a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3465534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3466b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3467b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3468b0ae01b7SPeter Brune }
3469b0ae01b7SPeter Brune 
34703ad4599aSBarry Smith /*@
34713ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
34723ad4599aSBarry Smith 
34733ad4599aSBarry Smith     Not Collective
34743ad4599aSBarry Smith 
34753ad4599aSBarry Smith     Input Parameter:
34763ad4599aSBarry Smith .   dm - the DM object
34773ad4599aSBarry Smith 
34783ad4599aSBarry Smith     Output Parameter:
34793ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
34803ad4599aSBarry Smith 
34813ad4599aSBarry Smith     Level: developer
34823ad4599aSBarry Smith 
34831565f0a7SPatrick Sanan .seealso DMCreateRestriction()
34843ad4599aSBarry Smith 
34853ad4599aSBarry Smith @*/
34863ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
34873ad4599aSBarry Smith {
34883ad4599aSBarry Smith   PetscFunctionBegin;
34895a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3490534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34913ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34923ad4599aSBarry Smith   PetscFunctionReturn(0);
34933ad4599aSBarry Smith }
34943ad4599aSBarry Smith 
3495a7058e45SLawrence Mitchell 
3496a7058e45SLawrence Mitchell /*@
3497a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3498a7058e45SLawrence Mitchell 
3499a7058e45SLawrence Mitchell     Not Collective
3500a7058e45SLawrence Mitchell 
3501a7058e45SLawrence Mitchell     Input Parameter:
3502a7058e45SLawrence Mitchell .   dm - the DM object
3503a7058e45SLawrence Mitchell 
3504a7058e45SLawrence Mitchell     Output Parameter:
3505a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3506a7058e45SLawrence Mitchell 
3507a7058e45SLawrence Mitchell     Level: developer
3508a7058e45SLawrence Mitchell 
35091565f0a7SPatrick Sanan .seealso DMCreateInjection()
3510a7058e45SLawrence Mitchell 
3511a7058e45SLawrence Mitchell @*/
3512a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3513a7058e45SLawrence Mitchell {
35144a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
35155a84ad33SLisandro Dalcin 
3516a7058e45SLawrence Mitchell   PetscFunctionBegin;
35175a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3518534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
35195a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
35204a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
35215a84ad33SLisandro Dalcin   } else {
35225a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
35235a84ad33SLisandro Dalcin   }
3524a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3525a7058e45SLawrence Mitchell }
3526a7058e45SLawrence Mitchell 
35270298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3528264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3529264ace61SBarry Smith 
3530264ace61SBarry Smith /*@C
3531264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3532264ace61SBarry Smith 
3533d083f849SBarry Smith   Collective on dm
3534264ace61SBarry Smith 
3535264ace61SBarry Smith   Input Parameters:
3536264ace61SBarry Smith + dm     - The DM object
3537264ace61SBarry Smith - method - The name of the DM type
3538264ace61SBarry Smith 
3539264ace61SBarry Smith   Options Database Key:
3540264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3541264ace61SBarry Smith 
3542264ace61SBarry Smith   Notes:
3543e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3544264ace61SBarry Smith 
3545264ace61SBarry Smith   Level: intermediate
3546264ace61SBarry Smith 
3547264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3548264ace61SBarry Smith @*/
354919fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3550264ace61SBarry Smith {
3551264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3552264ace61SBarry Smith   PetscBool      match;
3553264ace61SBarry Smith   PetscErrorCode ierr;
3554264ace61SBarry Smith 
3555264ace61SBarry Smith   PetscFunctionBegin;
3556264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3557251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3558264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3559264ace61SBarry Smith 
35600f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35611c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3562ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3563264ace61SBarry Smith 
3564264ace61SBarry Smith   if (dm->ops->destroy) {
3565264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3566264ace61SBarry Smith   }
3567d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3568264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3569d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3570264ace61SBarry Smith   PetscFunctionReturn(0);
3571264ace61SBarry Smith }
3572264ace61SBarry Smith 
3573264ace61SBarry Smith /*@C
3574264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3575264ace61SBarry Smith 
3576264ace61SBarry Smith   Not Collective
3577264ace61SBarry Smith 
3578264ace61SBarry Smith   Input Parameter:
3579264ace61SBarry Smith . dm  - The DM
3580264ace61SBarry Smith 
3581264ace61SBarry Smith   Output Parameter:
3582264ace61SBarry Smith . type - The DM type name
3583264ace61SBarry Smith 
3584264ace61SBarry Smith   Level: intermediate
3585264ace61SBarry Smith 
3586264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3587264ace61SBarry Smith @*/
358819fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3589264ace61SBarry Smith {
3590264ace61SBarry Smith   PetscErrorCode ierr;
3591264ace61SBarry Smith 
3592264ace61SBarry Smith   PetscFunctionBegin;
3593264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3594c959eef4SJed Brown   PetscValidPointer(type,2);
3595607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3596264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3597264ace61SBarry Smith   PetscFunctionReturn(0);
3598264ace61SBarry Smith }
3599264ace61SBarry Smith 
360067a56275SMatthew G Knepley /*@C
360167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
360267a56275SMatthew G Knepley 
3603d083f849SBarry Smith   Collective on dm
360467a56275SMatthew G Knepley 
360567a56275SMatthew G Knepley   Input Parameters:
360667a56275SMatthew G Knepley + dm - the DM
360767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
360867a56275SMatthew G Knepley 
360967a56275SMatthew G Knepley   Output Parameter:
361067a56275SMatthew G Knepley . M - pointer to new DM
361167a56275SMatthew G Knepley 
361267a56275SMatthew G Knepley   Notes:
361367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
361467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
361567a56275SMatthew G Knepley   of the input DM.
361667a56275SMatthew G Knepley 
361767a56275SMatthew G Knepley   Level: intermediate
361867a56275SMatthew G Knepley 
361967a56275SMatthew G Knepley .seealso: DMCreate()
362067a56275SMatthew G Knepley @*/
362119fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
362267a56275SMatthew G Knepley {
362367a56275SMatthew G Knepley   DM             B;
362467a56275SMatthew G Knepley   char           convname[256];
3625c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
362667a56275SMatthew G Knepley   PetscErrorCode ierr;
362767a56275SMatthew G Knepley 
362867a56275SMatthew G Knepley   PetscFunctionBegin;
362967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
363067a56275SMatthew G Knepley   PetscValidType(dm,1);
363167a56275SMatthew G Knepley   PetscValidPointer(M,3);
3632251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3633c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3634c067b6caSMatthew G. Knepley   if (sametype) {
3635c067b6caSMatthew G. Knepley     *M   = dm;
3636c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3637c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3638c067b6caSMatthew G. Knepley   } else {
36390298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
364067a56275SMatthew G Knepley 
364167a56275SMatthew G Knepley     /*
364267a56275SMatthew G Knepley        Order of precedence:
364367a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
364467a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
364567a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
364667a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
364767a56275SMatthew G Knepley        5) Use a really basic converter.
364867a56275SMatthew G Knepley     */
364967a56275SMatthew G Knepley 
365067a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3651a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3652a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3653a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3654a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3655a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36560005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
365767a56275SMatthew G Knepley     if (conv) goto foundconv;
365867a56275SMatthew G Knepley 
365967a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
366082f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
366167a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3662a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3663a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3664a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3665a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3666a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36670005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
366867a56275SMatthew G Knepley     if (conv) {
3669fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
367067a56275SMatthew G Knepley       goto foundconv;
367167a56275SMatthew G Knepley     }
367267a56275SMatthew G Knepley 
367367a56275SMatthew G Knepley #if 0
367467a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
367567a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3676fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
367767a56275SMatthew G Knepley     if (conv) goto foundconv;
367867a56275SMatthew G Knepley 
367967a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
368067a56275SMatthew G Knepley     if (dm->ops->convert) {
368167a56275SMatthew G Knepley       conv = dm->ops->convert;
368267a56275SMatthew G Knepley     }
368367a56275SMatthew G Knepley     if (conv) goto foundconv;
368467a56275SMatthew G Knepley #endif
368567a56275SMatthew G Knepley 
368667a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
368782f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
368867a56275SMatthew G Knepley 
368967a56275SMatthew G Knepley foundconv:
369067a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
369167a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
369212fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
369390b157c4SStefano Zampini     {
369490b157c4SStefano Zampini       PetscBool             isper;
369512fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
369612fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
369790b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
369890b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
369912fa691eSMatthew G. Knepley     }
370067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
370167a56275SMatthew G Knepley   }
370267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
370367a56275SMatthew G Knepley   PetscFunctionReturn(0);
370467a56275SMatthew G Knepley }
3705264ace61SBarry Smith 
3706264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3707264ace61SBarry Smith 
3708264ace61SBarry Smith /*@C
37091c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
37101c84c290SBarry Smith 
37111c84c290SBarry Smith   Not Collective
37121c84c290SBarry Smith 
37131c84c290SBarry Smith   Input Parameters:
37141c84c290SBarry Smith + name        - The name of a new user-defined creation routine
37151c84c290SBarry Smith - create_func - The creation routine itself
37161c84c290SBarry Smith 
37171c84c290SBarry Smith   Notes:
37181c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
37191c84c290SBarry Smith 
37201c84c290SBarry Smith 
37211c84c290SBarry Smith   Sample usage:
37221c84c290SBarry Smith .vb
3723bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
37241c84c290SBarry Smith .ve
37251c84c290SBarry Smith 
37261c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
37271c84c290SBarry Smith .vb
37281c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
37291c84c290SBarry Smith     DMSetType(DM,"my_da");
37301c84c290SBarry Smith .ve
37311c84c290SBarry Smith    or at runtime via the option
37321c84c290SBarry Smith .vb
37331c84c290SBarry Smith     -da_type my_da
37341c84c290SBarry Smith .ve
3735264ace61SBarry Smith 
3736264ace61SBarry Smith   Level: advanced
37371c84c290SBarry Smith 
3738bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
37391c84c290SBarry Smith 
3740264ace61SBarry Smith @*/
3741bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3742264ace61SBarry Smith {
3743264ace61SBarry Smith   PetscErrorCode ierr;
3744264ace61SBarry Smith 
3745264ace61SBarry Smith   PetscFunctionBegin;
37461d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3747a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3748264ace61SBarry Smith   PetscFunctionReturn(0);
3749264ace61SBarry Smith }
3750264ace61SBarry Smith 
3751b859378eSBarry Smith /*@C
375255849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3753b859378eSBarry Smith 
3754d083f849SBarry Smith   Collective on viewer
3755b859378eSBarry Smith 
3756b859378eSBarry Smith   Input Parameters:
3757b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3758b859378eSBarry Smith            some related function before a call to DMLoad().
3759b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3760b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3761b859378eSBarry Smith 
3762b859378eSBarry Smith    Level: intermediate
3763b859378eSBarry Smith 
3764b859378eSBarry Smith   Notes:
376555849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3766b859378eSBarry Smith 
3767b859378eSBarry Smith   Notes for advanced users:
3768b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3769b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3770b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3771b859378eSBarry Smith   format is
3772b859378eSBarry Smith .vb
3773b859378eSBarry Smith      has not yet been determined
3774b859378eSBarry Smith .ve
3775b859378eSBarry Smith 
3776b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3777b859378eSBarry Smith @*/
3778b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3779b859378eSBarry Smith {
37809331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3781b859378eSBarry Smith   PetscErrorCode ierr;
3782b859378eSBarry Smith 
3783b859378eSBarry Smith   PetscFunctionBegin;
3784b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3785b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3786fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
378732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37889331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
378958cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
37909331c7a4SMatthew G. Knepley   if (isbinary) {
37919331c7a4SMatthew G. Knepley     PetscInt classid;
37929331c7a4SMatthew G. Knepley     char     type[256];
3793b859378eSBarry Smith 
3794060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37959200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3796060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
379732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37989331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37999331c7a4SMatthew G. Knepley   } else if (ishdf5) {
38009331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
38019331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
380258cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3803b859378eSBarry Smith   PetscFunctionReturn(0);
3804b859378eSBarry Smith }
3805b859378eSBarry Smith 
3806b2e4378dSMatthew G. Knepley /*@
3807b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3808b2e4378dSMatthew G. Knepley 
3809b2e4378dSMatthew G. Knepley   Not collective
3810b2e4378dSMatthew G. Knepley 
3811b2e4378dSMatthew G. Knepley   Input Parameter:
3812b2e4378dSMatthew G. Knepley . dm - the DM
3813b2e4378dSMatthew G. Knepley 
3814b2e4378dSMatthew G. Knepley   Output Parameters:
3815b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3816b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3817b2e4378dSMatthew G. Knepley 
3818b2e4378dSMatthew G. Knepley   Level: beginner
3819b2e4378dSMatthew G. Knepley 
3820b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3821b2e4378dSMatthew G. Knepley 
3822b2e4378dSMatthew G. Knepley 
3823b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3824b2e4378dSMatthew G. Knepley @*/
3825b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3826b2e4378dSMatthew G. Knepley {
3827b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3828b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3829b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3830b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3831b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3832b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3833b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3834b2e4378dSMatthew G. Knepley 
3835b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3836b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3837b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3838b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3839b2e4378dSMatthew G. Knepley   if (coords) {
3840b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3841b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3842b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3843b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3844b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3845b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3846b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3847b2e4378dSMatthew G. Knepley       }
3848b2e4378dSMatthew G. Knepley     }
3849b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3850b2e4378dSMatthew G. Knepley   } else {
3851b2e4378dSMatthew G. Knepley     PetscBool isda;
3852b2e4378dSMatthew G. Knepley 
3853b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3854b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3855b2e4378dSMatthew G. Knepley   }
3856b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3857b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3858b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3859b2e4378dSMatthew G. Knepley }
3860b2e4378dSMatthew G. Knepley 
3861b2e4378dSMatthew G. Knepley /*@
3862b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3863b2e4378dSMatthew G. Knepley 
3864b2e4378dSMatthew G. Knepley   Collective
3865b2e4378dSMatthew G. Knepley 
3866b2e4378dSMatthew G. Knepley   Input Parameter:
3867b2e4378dSMatthew G. Knepley . dm - the DM
3868b2e4378dSMatthew G. Knepley 
3869b2e4378dSMatthew G. Knepley   Output Parameters:
3870b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3871b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3872b2e4378dSMatthew G. Knepley 
3873b2e4378dSMatthew G. Knepley   Level: beginner
3874b2e4378dSMatthew G. Knepley 
3875b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3876b2e4378dSMatthew G. Knepley @*/
3877b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3878b2e4378dSMatthew G. Knepley {
3879b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
38806ce308c4SMatthew G. Knepley   PetscInt       cdim;
38816ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3882b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3883b2e4378dSMatthew G. Knepley 
3884b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3885b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3886b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3887b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3888b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3889b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3890b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3891b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3892b2e4378dSMatthew G. Knepley }
3893b2e4378dSMatthew G. Knepley 
38947da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38957da65231SMatthew G Knepley 
3896a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3897a6dfd86eSKarl Rupp {
38981d47ebbbSSatish Balay   PetscInt       f;
38991b30c384SMatthew G Knepley   PetscErrorCode ierr;
39001b30c384SMatthew G Knepley 
39017da65231SMatthew G Knepley   PetscFunctionBegin;
390274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39031d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
390457622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
39057da65231SMatthew G Knepley   }
39067da65231SMatthew G Knepley   PetscFunctionReturn(0);
39077da65231SMatthew G Knepley }
39087da65231SMatthew G Knepley 
3909a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3910a6dfd86eSKarl Rupp {
39111b30c384SMatthew G Knepley   PetscInt       f, g;
39127da65231SMatthew G Knepley   PetscErrorCode ierr;
39137da65231SMatthew G Knepley 
39147da65231SMatthew G Knepley   PetscFunctionBegin;
391574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39161d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
391774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
39181d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3919e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
39207da65231SMatthew G Knepley     }
392174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
39227da65231SMatthew G Knepley   }
39237da65231SMatthew G Knepley   PetscFunctionReturn(0);
39247da65231SMatthew G Knepley }
3925e7c4fc90SDmitry Karpeev 
39266113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3927e759306cSMatthew G. Knepley {
39280c5b8624SToby Isaac   PetscInt          localSize, bs;
39290c5b8624SToby Isaac   PetscMPIInt       size;
39300c5b8624SToby Isaac   Vec               x, xglob;
39310c5b8624SToby Isaac   const PetscScalar *xarray;
3932e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3933e759306cSMatthew G. Knepley 
3934e759306cSMatthew G. Knepley   PetscFunctionBegin;
39359852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3936e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3937e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
39386113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
39390c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
39400c5b8624SToby Isaac   if (size > 1) {
39410c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
39420c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
39430c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
39440c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
39450c5b8624SToby Isaac   } else {
39460c5b8624SToby Isaac     xglob = x;
39470c5b8624SToby Isaac   }
39480c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
39490c5b8624SToby Isaac   if (size > 1) {
39500c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
39510c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
39520c5b8624SToby Isaac   }
3953e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3954e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3955e759306cSMatthew G. Knepley }
3956e759306cSMatthew G. Knepley 
395788ed4aceSMatthew G Knepley /*@
39581bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3959061576a5SJed Brown 
3960061576a5SJed Brown   Input Parameter:
3961061576a5SJed Brown . dm - The DM
3962061576a5SJed Brown 
3963061576a5SJed Brown   Output Parameter:
3964061576a5SJed Brown . section - The PetscSection
3965061576a5SJed Brown 
3966061576a5SJed Brown   Options Database Keys:
3967061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3968061576a5SJed Brown 
3969061576a5SJed Brown   Level: advanced
3970061576a5SJed Brown 
3971061576a5SJed Brown   Notes:
3972061576a5SJed Brown   Use DMGetLocalSection() in new code.
3973061576a5SJed Brown 
3974061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3975061576a5SJed Brown 
3976061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3977061576a5SJed Brown @*/
3978061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3979061576a5SJed Brown {
3980061576a5SJed Brown   PetscErrorCode ierr;
3981061576a5SJed Brown 
3982061576a5SJed Brown   PetscFunctionBegin;
3983061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3984061576a5SJed Brown   PetscFunctionReturn(0);
3985061576a5SJed Brown }
3986061576a5SJed Brown 
3987061576a5SJed Brown /*@
3988061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
398988ed4aceSMatthew G Knepley 
399088ed4aceSMatthew G Knepley   Input Parameter:
399188ed4aceSMatthew G Knepley . dm - The DM
399288ed4aceSMatthew G Knepley 
399388ed4aceSMatthew G Knepley   Output Parameter:
399488ed4aceSMatthew G Knepley . section - The PetscSection
399588ed4aceSMatthew G Knepley 
3996e5893cccSMatthew G. Knepley   Options Database Keys:
3997e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3998e5893cccSMatthew G. Knepley 
399988ed4aceSMatthew G Knepley   Level: intermediate
400088ed4aceSMatthew G Knepley 
400188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
400288ed4aceSMatthew G Knepley 
4003061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
400488ed4aceSMatthew G Knepley @*/
4005061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
40060adebc6cSBarry Smith {
4007fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4008fd59a867SMatthew G. Knepley 
400988ed4aceSMatthew G Knepley   PetscFunctionBegin;
401088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
401188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
40121bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4013e5e52638SMatthew G. Knepley     PetscInt d;
4014e5e52638SMatthew G. Knepley 
4015e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
40161bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
40171bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
40182f0f8703SMatthew G. Knepley   }
40191bb6d2a8SBarry Smith   *section = dm->localSection;
402088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
402188ed4aceSMatthew G Knepley }
402288ed4aceSMatthew G Knepley 
402388ed4aceSMatthew G Knepley /*@
40241bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4025061576a5SJed Brown 
4026061576a5SJed Brown   Input Parameters:
4027061576a5SJed Brown + dm - The DM
4028061576a5SJed Brown - section - The PetscSection
4029061576a5SJed Brown 
4030061576a5SJed Brown   Level: advanced
4031061576a5SJed Brown 
4032061576a5SJed Brown   Notes:
4033061576a5SJed Brown   Use DMSetLocalSection() in new code.
4034061576a5SJed Brown 
4035061576a5SJed Brown   Any existing Section will be destroyed
4036061576a5SJed Brown 
4037061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4038061576a5SJed Brown @*/
4039061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4040061576a5SJed Brown {
4041061576a5SJed Brown   PetscErrorCode ierr;
4042061576a5SJed Brown 
4043061576a5SJed Brown   PetscFunctionBegin;
4044061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4045061576a5SJed Brown   PetscFunctionReturn(0);
4046061576a5SJed Brown }
4047061576a5SJed Brown 
4048061576a5SJed Brown /*@
4049061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
405088ed4aceSMatthew G Knepley 
405188ed4aceSMatthew G Knepley   Input Parameters:
405288ed4aceSMatthew G Knepley + dm - The DM
405388ed4aceSMatthew G Knepley - section - The PetscSection
405488ed4aceSMatthew G Knepley 
405588ed4aceSMatthew G Knepley   Level: intermediate
405688ed4aceSMatthew G Knepley 
405788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
405888ed4aceSMatthew G Knepley 
4059061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
406088ed4aceSMatthew G Knepley @*/
4061061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
40620adebc6cSBarry Smith {
4063c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4064af122d2aSMatthew G Knepley   PetscInt       f;
406588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
406688ed4aceSMatthew G Knepley 
406788ed4aceSMatthew G Knepley   PetscFunctionBegin;
406888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4069b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40701d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40711bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
40721bb6d2a8SBarry Smith   dm->localSection = section;
40731bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4074af122d2aSMatthew G Knepley   if (numFields) {
4075af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4076af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40770f21e855SMatthew G. Knepley       PetscObject disc;
4078af122d2aSMatthew G Knepley       const char *name;
4079af122d2aSMatthew G Knepley 
40801bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
408144a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40820f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4083af122d2aSMatthew G Knepley     }
4084af122d2aSMatthew G Knepley   }
4085e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40861bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
408788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
408888ed4aceSMatthew G Knepley }
408988ed4aceSMatthew G Knepley 
40909435951eSToby Isaac /*@
4091b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40929435951eSToby Isaac 
4093e228b242SToby Isaac   not collective
4094e228b242SToby Isaac 
40959435951eSToby Isaac   Input Parameter:
40969435951eSToby Isaac . dm - The DM
40979435951eSToby Isaac 
40989435951eSToby Isaac   Output Parameter:
40999435951eSToby 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.
41009435951eSToby 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.
41019435951eSToby Isaac 
41029435951eSToby Isaac   Level: advanced
41039435951eSToby Isaac 
41049435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
41059435951eSToby Isaac 
41069435951eSToby Isaac .seealso: DMSetDefaultConstraints()
41079435951eSToby Isaac @*/
41089435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
41099435951eSToby Isaac {
41109435951eSToby Isaac   PetscErrorCode ierr;
41119435951eSToby Isaac 
41129435951eSToby Isaac   PetscFunctionBegin;
41139435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41149435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
411545a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
411645a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
41179435951eSToby Isaac   PetscFunctionReturn(0);
41189435951eSToby Isaac }
41199435951eSToby Isaac 
41209435951eSToby Isaac /*@
4121b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
41229435951eSToby Isaac 
41239435951eSToby 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().
41249435951eSToby Isaac 
41259435951eSToby 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.
41269435951eSToby Isaac 
4127e228b242SToby Isaac   collective on dm
4128e228b242SToby Isaac 
41299435951eSToby Isaac   Input Parameters:
41309435951eSToby Isaac + dm - The DM
4131e228b242SToby 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).
4132e228b242SToby 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).
41339435951eSToby Isaac 
41349435951eSToby Isaac   Level: advanced
41359435951eSToby Isaac 
41369435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
41379435951eSToby Isaac 
41389435951eSToby Isaac .seealso: DMGetDefaultConstraints()
41399435951eSToby Isaac @*/
41409435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
41419435951eSToby Isaac {
4142e228b242SToby Isaac   PetscMPIInt result;
41439435951eSToby Isaac   PetscErrorCode ierr;
41449435951eSToby Isaac 
41459435951eSToby Isaac   PetscFunctionBegin;
41469435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4147e228b242SToby Isaac   if (section) {
4148e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4149e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4150f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4151e228b242SToby Isaac   }
4152e228b242SToby Isaac   if (mat) {
4153e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4154e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4155f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4156e228b242SToby Isaac   }
41579435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41589435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
41599435951eSToby Isaac   dm->defaultConstraintSection = section;
41609435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
41619435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
41629435951eSToby Isaac   dm->defaultConstraintMat = mat;
41639435951eSToby Isaac   PetscFunctionReturn(0);
41649435951eSToby Isaac }
41659435951eSToby Isaac 
4166497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4167507e4973SMatthew G. Knepley /*
4168507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4169507e4973SMatthew G. Knepley 
4170507e4973SMatthew G. Knepley   Input Parameters:
4171507e4973SMatthew G. Knepley + dm - The DM
4172507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4173507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4174507e4973SMatthew G. Knepley 
4175507e4973SMatthew G. Knepley   Level: intermediate
4176507e4973SMatthew G. Knepley 
41771bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4178507e4973SMatthew G. Knepley */
4179f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4180507e4973SMatthew G. Knepley {
4181507e4973SMatthew G. Knepley   MPI_Comm        comm;
4182507e4973SMatthew G. Knepley   PetscLayout     layout;
4183507e4973SMatthew G. Knepley   const PetscInt *ranges;
4184507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4185507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4186507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4187507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4188507e4973SMatthew G. Knepley 
4189507e4973SMatthew G. Knepley   PetscFunctionBegin;
4190507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4191507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4192507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4193507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4194507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4195507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4196507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4197507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4198507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4199507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4200507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4201507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4202f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4203507e4973SMatthew G. Knepley 
4204507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4205507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4206507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4207507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4208507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4209507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4210507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4211507e4973SMatthew 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;}
4212507e4973SMatthew 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;}
4213507e4973SMatthew G. Knepley     if (gdof < 0) {
4214507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4215507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4216507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4217507e4973SMatthew G. Knepley 
4218507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4219507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4220507e4973SMatthew 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;}
4221507e4973SMatthew G. Knepley       }
4222507e4973SMatthew G. Knepley     }
4223507e4973SMatthew G. Knepley   }
4224507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4225507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4226b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4227507e4973SMatthew G. Knepley   if (!gvalid) {
4228507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4229507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4230507e4973SMatthew G. Knepley   }
4231507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4232507e4973SMatthew G. Knepley }
4233f741bcd2SMatthew G. Knepley #endif
4234507e4973SMatthew G. Knepley 
423588ed4aceSMatthew G Knepley /*@
4236e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
423788ed4aceSMatthew G Knepley 
4238d083f849SBarry Smith   Collective on dm
42398b1ab98fSJed Brown 
424088ed4aceSMatthew G Knepley   Input Parameter:
424188ed4aceSMatthew G Knepley . dm - The DM
424288ed4aceSMatthew G Knepley 
424388ed4aceSMatthew G Knepley   Output Parameter:
424488ed4aceSMatthew G Knepley . section - The PetscSection
424588ed4aceSMatthew G Knepley 
424688ed4aceSMatthew G Knepley   Level: intermediate
424788ed4aceSMatthew G Knepley 
424888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
424988ed4aceSMatthew G Knepley 
425092fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
425188ed4aceSMatthew G Knepley @*/
4252e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
42530adebc6cSBarry Smith {
425488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
425588ed4aceSMatthew G Knepley 
425688ed4aceSMatthew G Knepley   PetscFunctionBegin;
425788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
425888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42591bb6d2a8SBarry Smith   if (!dm->globalSection) {
4260fd59a867SMatthew G. Knepley     PetscSection s;
4261fd59a867SMatthew G. Knepley 
426292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4263fd59a867SMatthew 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");
426433907cc2SStefano 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");
42651bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4266cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
42671bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
42681bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
426988ed4aceSMatthew G Knepley   }
42701bb6d2a8SBarry Smith   *section = dm->globalSection;
427188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
427288ed4aceSMatthew G Knepley }
427388ed4aceSMatthew G Knepley 
4274b21d0597SMatthew G Knepley /*@
4275e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4276b21d0597SMatthew G Knepley 
4277b21d0597SMatthew G Knepley   Input Parameters:
4278b21d0597SMatthew G Knepley + dm - The DM
42795080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4280b21d0597SMatthew G Knepley 
4281b21d0597SMatthew G Knepley   Level: intermediate
4282b21d0597SMatthew G Knepley 
4283b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4284b21d0597SMatthew G Knepley 
428592fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4286b21d0597SMatthew G Knepley @*/
4287e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42880adebc6cSBarry Smith {
4289b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4290b21d0597SMatthew G Knepley 
4291b21d0597SMatthew G Knepley   PetscFunctionBegin;
4292b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42935080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42941d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42951bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
42961bb6d2a8SBarry Smith   dm->globalSection = section;
4297497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
42981bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4299507e4973SMatthew G. Knepley #endif
4300b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4301b21d0597SMatthew G Knepley }
4302b21d0597SMatthew G Knepley 
430388ed4aceSMatthew G Knepley /*@
43041bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
430588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
430688ed4aceSMatthew G Knepley 
430788ed4aceSMatthew G Knepley   Input Parameter:
430888ed4aceSMatthew G Knepley . dm - The DM
430988ed4aceSMatthew G Knepley 
431088ed4aceSMatthew G Knepley   Output Parameter:
431188ed4aceSMatthew G Knepley . sf - The PetscSF
431288ed4aceSMatthew G Knepley 
431388ed4aceSMatthew G Knepley   Level: intermediate
431488ed4aceSMatthew G Knepley 
431588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
431688ed4aceSMatthew G Knepley 
43171bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
431888ed4aceSMatthew G Knepley @*/
43191bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
43200adebc6cSBarry Smith {
432188ed4aceSMatthew G Knepley   PetscInt       nroots;
432288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
432388ed4aceSMatthew G Knepley 
432488ed4aceSMatthew G Knepley   PetscFunctionBegin;
432588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
432688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
43271bb6d2a8SBarry Smith   if (!dm->sectionSF) {
43281bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
432933907cc2SStefano Zampini   }
43301bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
433188ed4aceSMatthew G Knepley   if (nroots < 0) {
433288ed4aceSMatthew G Knepley     PetscSection section, gSection;
433388ed4aceSMatthew G Knepley 
433492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
433531ea6d37SMatthew G Knepley     if (section) {
4336e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
43371bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
433831ea6d37SMatthew G Knepley     } else {
43390298fd71SBarry Smith       *sf = NULL;
434031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
434131ea6d37SMatthew G Knepley     }
434288ed4aceSMatthew G Knepley   }
43431bb6d2a8SBarry Smith   *sf = dm->sectionSF;
434488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
434588ed4aceSMatthew G Knepley }
434688ed4aceSMatthew G Knepley 
434788ed4aceSMatthew G Knepley /*@
43481bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
434988ed4aceSMatthew G Knepley 
435088ed4aceSMatthew G Knepley   Input Parameters:
435188ed4aceSMatthew G Knepley + dm - The DM
435288ed4aceSMatthew G Knepley - sf - The PetscSF
435388ed4aceSMatthew G Knepley 
435488ed4aceSMatthew G Knepley   Level: intermediate
435588ed4aceSMatthew G Knepley 
435688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
435788ed4aceSMatthew G Knepley 
43581bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
435988ed4aceSMatthew G Knepley @*/
43601bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
43610adebc6cSBarry Smith {
436288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
436388ed4aceSMatthew G Knepley 
436488ed4aceSMatthew G Knepley   PetscFunctionBegin;
436588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4366b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
436733907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
43681bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
43691bb6d2a8SBarry Smith   dm->sectionSF = sf;
437088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
437188ed4aceSMatthew G Knepley }
437288ed4aceSMatthew G Knepley 
437388ed4aceSMatthew G Knepley /*@C
43741bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
437588ed4aceSMatthew G Knepley   describing the data layout.
437688ed4aceSMatthew G Knepley 
437788ed4aceSMatthew G Knepley   Input Parameters:
437888ed4aceSMatthew G Knepley + dm - The DM
437988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
438088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
438188ed4aceSMatthew G Knepley 
43821bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
438388ed4aceSMatthew G Knepley 
43841bb6d2a8SBarry Smith   Level: developer
43851bb6d2a8SBarry Smith 
43861bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43871bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43881bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43891bb6d2a8SBarry Smith 
43901bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
439188ed4aceSMatthew G Knepley @*/
43921bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
439388ed4aceSMatthew G Knepley {
439482f516ccSBarry Smith   MPI_Comm       comm;
439588ed4aceSMatthew G Knepley   PetscLayout    layout;
439688ed4aceSMatthew G Knepley   const PetscInt *ranges;
439788ed4aceSMatthew G Knepley   PetscInt       *local;
439888ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4399ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
440088ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
440188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
440288ed4aceSMatthew G Knepley 
440388ed4aceSMatthew G Knepley   PetscFunctionBegin;
440488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4405367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
440688ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
440788ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
440888ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
440988ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
441088ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
441188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
441288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
441388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
441488ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4415ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
44166636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
441788ed4aceSMatthew G Knepley 
44186636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44196636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4420235fbf56SMatthew 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));
44216636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
442288ed4aceSMatthew G Knepley   }
4423785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4424785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
442588ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
44261f588964SMatthew G Knepley     const PetscInt *cind;
44276636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
442888ed4aceSMatthew G Knepley 
442988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
443088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
443188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
443288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
443388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44346636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
443588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
44366636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
44376636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
44386636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4439057b4bcdSMatthew 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);
44406636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
44416636e97aSMatthew G Knepley     }
444288ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
444388ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
444488ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
444588ed4aceSMatthew G Knepley     }
444688ed4aceSMatthew G Knepley     if (gdof < 0) {
44476636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
444888ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
444988ed4aceSMatthew G Knepley 
445005376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
445131d3f06eSJed Brown         if (r < 0) r = -(r+2);
445205376888SMatthew 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);
445388ed4aceSMatthew G Knepley         remote[l].rank  = r;
445488ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
445588ed4aceSMatthew G Knepley       }
445688ed4aceSMatthew G Knepley     } else {
44576636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
445888ed4aceSMatthew G Knepley         remote[l].rank  = rank;
445988ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
446088ed4aceSMatthew G Knepley       }
446188ed4aceSMatthew G Knepley     }
446288ed4aceSMatthew G Knepley   }
44636636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
446488ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
44651bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
446688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
446788ed4aceSMatthew G Knepley }
4468af122d2aSMatthew G Knepley 
4469b21d0597SMatthew G Knepley /*@
4470b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4471b21d0597SMatthew G Knepley 
4472b21d0597SMatthew G Knepley   Input Parameter:
4473b21d0597SMatthew G Knepley . dm - The DM
4474b21d0597SMatthew G Knepley 
4475b21d0597SMatthew G Knepley   Output Parameter:
4476b21d0597SMatthew G Knepley . sf - The PetscSF
4477b21d0597SMatthew G Knepley 
4478b21d0597SMatthew G Knepley   Level: intermediate
4479b21d0597SMatthew G Knepley 
4480b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4481b21d0597SMatthew G Knepley 
44821bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4483b21d0597SMatthew G Knepley @*/
44840adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44850adebc6cSBarry Smith {
4486b21d0597SMatthew G Knepley   PetscFunctionBegin;
4487b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4488b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4489b21d0597SMatthew G Knepley   *sf = dm->sf;
4490b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4491b21d0597SMatthew G Knepley }
4492b21d0597SMatthew G Knepley 
4493057b4bcdSMatthew G Knepley /*@
4494057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4495057b4bcdSMatthew G Knepley 
4496057b4bcdSMatthew G Knepley   Input Parameters:
4497057b4bcdSMatthew G Knepley + dm - The DM
4498057b4bcdSMatthew G Knepley - sf - The PetscSF
4499057b4bcdSMatthew G Knepley 
4500057b4bcdSMatthew G Knepley   Level: intermediate
4501057b4bcdSMatthew G Knepley 
45021bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4503057b4bcdSMatthew G Knepley @*/
45040adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
45050adebc6cSBarry Smith {
4506057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4507057b4bcdSMatthew G Knepley 
4508057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4509057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4510b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4511057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
451233907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4513057b4bcdSMatthew G Knepley   dm->sf = sf;
4514057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4515057b4bcdSMatthew G Knepley }
4516057b4bcdSMatthew G Knepley 
451734aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
451834aa8a36SMatthew G. Knepley {
451934aa8a36SMatthew G. Knepley   PetscClassId   id;
452034aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
452134aa8a36SMatthew G. Knepley 
452234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
452334aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
452434aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
452534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
452634aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
452734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
452817c1d62eSMatthew G. Knepley   } else {
452917c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
453034aa8a36SMatthew G. Knepley   }
453134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
453234aa8a36SMatthew G. Knepley }
453334aa8a36SMatthew G. Knepley 
453444a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
453544a7f3ddSMatthew G. Knepley {
453644a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
453744a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
453844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
453944a7f3ddSMatthew G. Knepley 
454044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
454144a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
454244a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
454344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
454444a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
454544a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
454644a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
454744a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
454844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
454944a7f3ddSMatthew G. Knepley }
455044a7f3ddSMatthew G. Knepley 
455144a7f3ddSMatthew G. Knepley /*@
455244a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
455344a7f3ddSMatthew G. Knepley 
4554d083f849SBarry Smith   Logically collective on dm
455544a7f3ddSMatthew G. Knepley 
455644a7f3ddSMatthew G. Knepley   Input Parameter:
455744a7f3ddSMatthew G. Knepley . dm - The DM
455844a7f3ddSMatthew G. Knepley 
455944a7f3ddSMatthew G. Knepley   Level: intermediate
456044a7f3ddSMatthew G. Knepley 
456144a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
456244a7f3ddSMatthew G. Knepley @*/
456344a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
456444a7f3ddSMatthew G. Knepley {
456544a7f3ddSMatthew G. Knepley   PetscInt       f;
456644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
456744a7f3ddSMatthew G. Knepley 
456844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
456944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
457044a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
457144a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
457244a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
457344a7f3ddSMatthew G. Knepley   }
457444a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
457544a7f3ddSMatthew G. Knepley   dm->fields = NULL;
457644a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
457744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
457844a7f3ddSMatthew G. Knepley }
457944a7f3ddSMatthew G. Knepley 
4580689b5837SMatthew G. Knepley /*@
4581689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4582689b5837SMatthew G. Knepley 
4583689b5837SMatthew G. Knepley   Not collective
4584689b5837SMatthew G. Knepley 
4585689b5837SMatthew G. Knepley   Input Parameter:
4586689b5837SMatthew G. Knepley . dm - The DM
4587689b5837SMatthew G. Knepley 
4588689b5837SMatthew G. Knepley   Output Parameter:
4589689b5837SMatthew G. Knepley . Nf - The number of fields
4590689b5837SMatthew G. Knepley 
4591689b5837SMatthew G. Knepley   Level: intermediate
4592689b5837SMatthew G. Knepley 
4593689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4594689b5837SMatthew G. Knepley @*/
45950f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
45960f21e855SMatthew G. Knepley {
45970f21e855SMatthew G. Knepley   PetscFunctionBegin;
45980f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4599534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
460044a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4601af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4602af122d2aSMatthew G Knepley }
4603af122d2aSMatthew G Knepley 
4604689b5837SMatthew G. Knepley /*@
4605689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4606689b5837SMatthew G. Knepley 
4607d083f849SBarry Smith   Logically collective on dm
4608689b5837SMatthew G. Knepley 
4609689b5837SMatthew G. Knepley   Input Parameters:
4610689b5837SMatthew G. Knepley + dm - The DM
4611689b5837SMatthew G. Knepley - Nf - The number of fields
4612689b5837SMatthew G. Knepley 
4613689b5837SMatthew G. Knepley   Level: intermediate
4614689b5837SMatthew G. Knepley 
4615689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4616689b5837SMatthew G. Knepley @*/
4617af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4618af122d2aSMatthew G Knepley {
46190f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4620af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4621af122d2aSMatthew G Knepley 
4622af122d2aSMatthew G Knepley   PetscFunctionBegin;
4623af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
462444a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
46250f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
46260f21e855SMatthew G. Knepley     PetscContainer obj;
46270f21e855SMatthew G. Knepley 
46280f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
462944a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
46300f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4631af122d2aSMatthew G Knepley   }
4632af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4633af122d2aSMatthew G Knepley }
4634af122d2aSMatthew G Knepley 
4635c1929be8SMatthew G. Knepley /*@
4636c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4637c1929be8SMatthew G. Knepley 
4638c1929be8SMatthew G. Knepley   Not collective
4639c1929be8SMatthew G. Knepley 
4640c1929be8SMatthew G. Knepley   Input Parameters:
4641c1929be8SMatthew G. Knepley + dm - The DM
4642c1929be8SMatthew G. Knepley - f  - The field number
4643c1929be8SMatthew G. Knepley 
464444a7f3ddSMatthew G. Knepley   Output Parameters:
464544a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
464644a7f3ddSMatthew G. Knepley - field - The discretization object
4647c1929be8SMatthew G. Knepley 
464844a7f3ddSMatthew G. Knepley   Level: intermediate
4649c1929be8SMatthew G. Knepley 
465044a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4651c1929be8SMatthew G. Knepley @*/
465244a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4653af122d2aSMatthew G Knepley {
4654af122d2aSMatthew G Knepley   PetscFunctionBegin;
4655af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
465644a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
465744a7f3ddSMatthew 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);
465844a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
465944a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4660decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4661decb47aaSMatthew G. Knepley }
4662decb47aaSMatthew G. Knepley 
4663083401c6SMatthew G. Knepley /* Does not clear the DS */
4664083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4665083401c6SMatthew G. Knepley {
4666083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4667083401c6SMatthew G. Knepley 
4668083401c6SMatthew G. Knepley   PetscFunctionBegin;
4669083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4670083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4671083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4672083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4673083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4674083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4675083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4676083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4677083401c6SMatthew G. Knepley }
4678083401c6SMatthew G. Knepley 
4679c1929be8SMatthew G. Knepley /*@
4680c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4681c1929be8SMatthew G. Knepley 
4682d083f849SBarry Smith   Logically collective on dm
4683c1929be8SMatthew G. Knepley 
4684c1929be8SMatthew G. Knepley   Input Parameters:
4685c1929be8SMatthew G. Knepley + dm    - The DM
4686c1929be8SMatthew G. Knepley . f     - The field number
468744a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4688c1929be8SMatthew G. Knepley - field - The discretization object
4689c1929be8SMatthew G. Knepley 
469044a7f3ddSMatthew G. Knepley   Level: intermediate
4691c1929be8SMatthew G. Knepley 
469244a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4693c1929be8SMatthew G. Knepley @*/
469444a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4695decb47aaSMatthew G. Knepley {
4696decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4697decb47aaSMatthew G. Knepley 
4698decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4699decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4700e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
470144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4702e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4703083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
470434aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
47052df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
470644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
470744a7f3ddSMatthew G. Knepley }
470844a7f3ddSMatthew G. Knepley 
470944a7f3ddSMatthew G. Knepley /*@
471044a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
471144a7f3ddSMatthew G. Knepley 
4712d083f849SBarry Smith   Logically collective on dm
471344a7f3ddSMatthew G. Knepley 
471444a7f3ddSMatthew G. Knepley   Input Parameters:
471544a7f3ddSMatthew G. Knepley + dm    - The DM
471644a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
471744a7f3ddSMatthew G. Knepley - field - The discretization object
471844a7f3ddSMatthew G. Knepley 
471944a7f3ddSMatthew G. Knepley   Level: intermediate
472044a7f3ddSMatthew G. Knepley 
472144a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
472244a7f3ddSMatthew G. Knepley @*/
472344a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
472444a7f3ddSMatthew G. Knepley {
472544a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
472644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
472744a7f3ddSMatthew G. Knepley 
472844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
472944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
473044a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
473144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
473244a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
473344a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
473444a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
473544a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
473644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
473734aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
47382df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4739af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4740af122d2aSMatthew G Knepley }
47416636e97aSMatthew G Knepley 
4742e5e52638SMatthew G. Knepley /*@
4743e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4744e5e52638SMatthew G. Knepley 
4745d083f849SBarry Smith   Collective on dm
4746e5e52638SMatthew G. Knepley 
4747e5e52638SMatthew G. Knepley   Input Parameter:
4748e5e52638SMatthew G. Knepley . dm - The DM
4749e5e52638SMatthew G. Knepley 
4750e5e52638SMatthew G. Knepley   Output Parameter:
4751e5e52638SMatthew G. Knepley . newdm - The DM
4752e5e52638SMatthew G. Knepley 
4753e5e52638SMatthew G. Knepley   Level: advanced
4754e5e52638SMatthew G. Knepley 
4755e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4756e5e52638SMatthew G. Knepley @*/
4757e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4758e5e52638SMatthew G. Knepley {
4759e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4760e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4761e5e52638SMatthew G. Knepley 
4762e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4763e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4764e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4765e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4766e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4767e5e52638SMatthew G. Knepley     DMLabel     label;
4768e5e52638SMatthew G. Knepley     PetscObject field;
476934aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4770e5e52638SMatthew G. Knepley 
4771e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4772e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
477334aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
477434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
477534aa8a36SMatthew G. Knepley   }
477634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
477734aa8a36SMatthew G. Knepley }
477834aa8a36SMatthew G. Knepley 
477934aa8a36SMatthew G. Knepley /*@
478034aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
478134aa8a36SMatthew G. Knepley 
478234aa8a36SMatthew G. Knepley   Not collective
478334aa8a36SMatthew G. Knepley 
478434aa8a36SMatthew G. Knepley   Input Parameters:
478534aa8a36SMatthew G. Knepley + dm - The DM object
478634aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
478734aa8a36SMatthew G. Knepley 
478834aa8a36SMatthew G. Knepley   Output Parameter:
478934aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
479034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
479134aa8a36SMatthew G. Knepley 
479234aa8a36SMatthew G. Knepley   Notes:
479334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
479434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
479534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4796979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
479734aa8a36SMatthew G. Knepley 
479834aa8a36SMatthew G. Knepley   Level: developer
479934aa8a36SMatthew G. Knepley 
480034aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
480134aa8a36SMatthew G. Knepley @*/
480234aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
480334aa8a36SMatthew G. Knepley {
480434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
480534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4806534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4807534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
480834aa8a36SMatthew G. Knepley   if (f < 0) {
480934aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
481034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
481134aa8a36SMatthew G. Knepley   } else {
481234aa8a36SMatthew G. Knepley     PetscInt       Nf;
481334aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
481434aa8a36SMatthew G. Knepley 
481534aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
481634aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
481734aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
481834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
481934aa8a36SMatthew G. Knepley   }
482034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
482134aa8a36SMatthew G. Knepley }
482234aa8a36SMatthew G. Knepley 
482334aa8a36SMatthew G. Knepley /*@
482434aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
482534aa8a36SMatthew G. Knepley 
482634aa8a36SMatthew G. Knepley   Not collective
482734aa8a36SMatthew G. Knepley 
482834aa8a36SMatthew G. Knepley   Input Parameters:
482934aa8a36SMatthew G. Knepley + dm         - The DM object
483034aa8a36SMatthew G. Knepley . f          - The field number
483134aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
483234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
483334aa8a36SMatthew G. Knepley 
483434aa8a36SMatthew G. Knepley   Notes:
483534aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
483634aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
483734aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4838979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
483934aa8a36SMatthew G. Knepley 
484034aa8a36SMatthew G. Knepley   Level: developer
484134aa8a36SMatthew G. Knepley 
484234aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
484334aa8a36SMatthew G. Knepley @*/
484434aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
484534aa8a36SMatthew G. Knepley {
484634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
484734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
484834aa8a36SMatthew G. Knepley   if (f < 0) {
484934aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
485034aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
485134aa8a36SMatthew G. Knepley   } else {
485234aa8a36SMatthew G. Knepley     PetscInt       Nf;
485334aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
485434aa8a36SMatthew G. Knepley 
485534aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
485634aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
485734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
485834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4859e5e52638SMatthew G. Knepley   }
4860e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4861e5e52638SMatthew G. Knepley }
4862e5e52638SMatthew G. Knepley 
4863b0441da4SMatthew G. Knepley /*@
4864b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4865b0441da4SMatthew G. Knepley 
4866b0441da4SMatthew G. Knepley   Not collective
4867b0441da4SMatthew G. Knepley 
4868b0441da4SMatthew G. Knepley   Input Parameters:
4869b0441da4SMatthew G. Knepley . dm - The DM object
4870b0441da4SMatthew G. Knepley 
4871b0441da4SMatthew G. Knepley   Output Parameter:
4872b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4873b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4874b0441da4SMatthew G. Knepley 
4875b0441da4SMatthew G. Knepley   Notes:
4876b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4877b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4878b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4879b0441da4SMatthew G. Knepley 
4880b0441da4SMatthew G. Knepley   Level: developer
4881b0441da4SMatthew G. Knepley 
4882b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4883b0441da4SMatthew G. Knepley @*/
4884b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4885b0441da4SMatthew G. Knepley {
4886b0441da4SMatthew G. Knepley   PetscInt       Nf;
4887b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4888b0441da4SMatthew G. Knepley 
4889b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4890b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4891534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4892534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4893b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4894b0441da4SMatthew G. Knepley   if (!Nf) {
4895b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4896b0441da4SMatthew G. Knepley   } else {
4897b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4898b0441da4SMatthew G. Knepley   }
4899b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4900b0441da4SMatthew G. Knepley }
4901b0441da4SMatthew G. Knepley 
4902b0441da4SMatthew G. Knepley /*@
4903b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4904b0441da4SMatthew G. Knepley 
4905b0441da4SMatthew G. Knepley   Not collective
4906b0441da4SMatthew G. Knepley 
4907b0441da4SMatthew G. Knepley   Input Parameters:
4908b0441da4SMatthew G. Knepley + dm         - The DM object
4909b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4910b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4911b0441da4SMatthew G. Knepley 
4912b0441da4SMatthew G. Knepley   Notes:
4913b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4914b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4915b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4916b0441da4SMatthew G. Knepley 
4917b0441da4SMatthew G. Knepley   Level: developer
4918b0441da4SMatthew G. Knepley 
4919b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4920b0441da4SMatthew G. Knepley @*/
4921b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4922b0441da4SMatthew G. Knepley {
4923b0441da4SMatthew G. Knepley   PetscInt       Nf;
4924b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4925b0441da4SMatthew G. Knepley 
4926b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4927b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4928b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4929b0441da4SMatthew G. Knepley   if (!Nf) {
4930b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4931b0441da4SMatthew G. Knepley   } else {
4932b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4933e5e52638SMatthew G. Knepley   }
4934e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4935e5e52638SMatthew G. Knepley }
4936e5e52638SMatthew G. Knepley 
4937783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
4938783e2ec8SMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, const char labelname[])
4939783e2ec8SMatthew G. Knepley {
4940783e2ec8SMatthew G. Knepley   DMLabel        label;
4941783e2ec8SMatthew G. Knepley   PetscObject    obj;
4942783e2ec8SMatthew G. Knepley   PetscClassId   id;
4943783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
4944783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
4945783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
4946783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
4947783e2ec8SMatthew G. Knepley 
4948783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
4949783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
4950783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
4951783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
4952783e2ec8SMatthew G. Knepley   ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
4953783e2ec8SMatthew G. Knepley   if (isFE && label) {
4954783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
4955783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
4956783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
4957783e2ec8SMatthew G. Knepley       const char *lname;
4958783e2ec8SMatthew G. Knepley 
4959783e2ec8SMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, &lname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
4960783e2ec8SMatthew G. Knepley       ierr = PetscStrcmp(lname, labelname, &duplicate);CHKERRQ(ierr);
4961783e2ec8SMatthew G. Knepley       if (duplicate) break;
4962783e2ec8SMatthew G. Knepley     }
4963783e2ec8SMatthew G. Knepley     if (!duplicate) {
4964783e2ec8SMatthew G. Knepley       DM plex;
4965783e2ec8SMatthew G. Knepley 
4966783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
4967783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
4968783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
4969783e2ec8SMatthew G. Knepley     }
4970783e2ec8SMatthew G. Knepley   }
4971783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
4972783e2ec8SMatthew G. Knepley }
4973783e2ec8SMatthew G. Knepley 
4974e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4975e5e52638SMatthew G. Knepley {
4976e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4977e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4978e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4979e5e52638SMatthew G. Knepley 
4980e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4981e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4982e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4983e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4984b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4985e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4986e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4987e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4988e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4989e5e52638SMatthew G. Knepley }
4990e5e52638SMatthew G. Knepley 
4991e5e52638SMatthew G. Knepley /*@
4992e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4993e5e52638SMatthew G. Knepley 
4994e5e52638SMatthew G. Knepley   Not collective
4995e5e52638SMatthew G. Knepley 
4996e5e52638SMatthew G. Knepley   Input Parameter:
4997e5e52638SMatthew G. Knepley . dm - The DM
4998e5e52638SMatthew G. Knepley 
4999e5e52638SMatthew G. Knepley   Output Parameter:
5000e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5001e5e52638SMatthew G. Knepley 
5002e5e52638SMatthew G. Knepley   Level: intermediate
5003e5e52638SMatthew G. Knepley 
5004e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5005e5e52638SMatthew G. Knepley @*/
5006e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5007e5e52638SMatthew G. Knepley {
5008e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5009e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5010534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5011e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5012e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5013e5e52638SMatthew G. Knepley }
5014e5e52638SMatthew G. Knepley 
5015e5e52638SMatthew G. Knepley /*@
5016e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5017e5e52638SMatthew G. Knepley 
5018d083f849SBarry Smith   Logically collective on dm
5019e5e52638SMatthew G. Knepley 
5020e5e52638SMatthew G. Knepley   Input Parameter:
5021e5e52638SMatthew G. Knepley . dm - The DM
5022e5e52638SMatthew G. Knepley 
5023e5e52638SMatthew G. Knepley   Level: intermediate
5024e5e52638SMatthew G. Knepley 
5025e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5026e5e52638SMatthew G. Knepley @*/
5027e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5028e5e52638SMatthew G. Knepley {
5029e5e52638SMatthew G. Knepley   PetscInt       s;
5030e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5031e5e52638SMatthew G. Knepley 
5032e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5033e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5034e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5035e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5036e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5037b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5038e5e52638SMatthew G. Knepley   }
5039e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5040e5e52638SMatthew G. Knepley   dm->probs = NULL;
5041e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5042e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5043e5e52638SMatthew G. Knepley }
5044e5e52638SMatthew G. Knepley 
5045e5e52638SMatthew G. Knepley /*@
5046e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5047e5e52638SMatthew G. Knepley 
5048e5e52638SMatthew G. Knepley   Not collective
5049e5e52638SMatthew G. Knepley 
5050e5e52638SMatthew G. Knepley   Input Parameter:
5051e5e52638SMatthew G. Knepley . dm    - The DM
5052e5e52638SMatthew G. Knepley 
5053e5e52638SMatthew G. Knepley   Output Parameter:
5054e5e52638SMatthew G. Knepley . prob - The default PetscDS
5055e5e52638SMatthew G. Knepley 
5056e5e52638SMatthew G. Knepley   Level: intermediate
5057e5e52638SMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5059e5e52638SMatthew G. Knepley @*/
5060e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5061e5e52638SMatthew G. Knepley {
5062b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5063b0143b4dSMatthew G. Knepley 
5064e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5065e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5066e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5067b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5068b0143b4dSMatthew G. Knepley     PetscDS ds;
5069b0143b4dSMatthew G. Knepley 
5070b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5071b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5072b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5073b0143b4dSMatthew G. Knepley   }
5074b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5075e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5076e5e52638SMatthew G. Knepley }
5077e5e52638SMatthew G. Knepley 
5078e5e52638SMatthew G. Knepley /*@
5079e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5080e5e52638SMatthew G. Knepley 
5081e5e52638SMatthew G. Knepley   Not collective
5082e5e52638SMatthew G. Knepley 
5083e5e52638SMatthew G. Knepley   Input Parameters:
5084e5e52638SMatthew G. Knepley + dm    - The DM
5085e5e52638SMatthew G. Knepley - point - Cell for the DS
5086e5e52638SMatthew G. Knepley 
5087e5e52638SMatthew G. Knepley   Output Parameter:
5088e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5089e5e52638SMatthew G. Knepley 
5090e5e52638SMatthew G. Knepley   Level: developer
5091e5e52638SMatthew G. Knepley 
5092b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5093e5e52638SMatthew G. Knepley @*/
5094e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5095e5e52638SMatthew G. Knepley {
5096e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5097e5e52638SMatthew G. Knepley   PetscInt       s;
5098e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5099e5e52638SMatthew G. Knepley 
5100e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5101e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5102e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5103360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5104e5e52638SMatthew G. Knepley   *prob = NULL;
5105e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5106e5e52638SMatthew G. Knepley     PetscInt val;
5107e5e52638SMatthew G. Knepley 
5108e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5109e5e52638SMatthew G. Knepley     else {
5110e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5111e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5112e5e52638SMatthew G. Knepley     }
5113e5e52638SMatthew G. Knepley   }
5114e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5115e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5116e5e52638SMatthew G. Knepley }
5117e5e52638SMatthew G. Knepley 
5118e5e52638SMatthew G. Knepley /*@
5119e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5120e5e52638SMatthew G. Knepley 
5121e5e52638SMatthew G. Knepley   Not collective
5122e5e52638SMatthew G. Knepley 
5123e5e52638SMatthew G. Knepley   Input Parameters:
5124e5e52638SMatthew G. Knepley + dm    - The DM
5125e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5126e5e52638SMatthew G. Knepley 
5127b3cf3223SMatthew G. Knepley   Output Parameters:
5128b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5129b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5130e5e52638SMatthew G. Knepley 
5131e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5132e5e52638SMatthew G. Knepley 
5133e5e52638SMatthew G. Knepley   Level: advanced
5134e5e52638SMatthew G. Knepley 
5135e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5136e5e52638SMatthew G. Knepley @*/
5137b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5138e5e52638SMatthew G. Knepley {
5139e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5140e5e52638SMatthew G. Knepley 
5141e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5142e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5143e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5144b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5145b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5146e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5147b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5148b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5149b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5150b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5151b3cf3223SMatthew G. Knepley     }
5152e5e52638SMatthew G. Knepley   }
51532df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5154e5e52638SMatthew G. Knepley }
5155e5e52638SMatthew G. Knepley 
5156e5e52638SMatthew G. Knepley /*@
5157083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5158083401c6SMatthew G. Knepley 
5159083401c6SMatthew G. Knepley   Collective on dm
5160083401c6SMatthew G. Knepley 
5161083401c6SMatthew G. Knepley   Input Parameters:
5162083401c6SMatthew G. Knepley + dm     - The DM
5163083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5164083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5165083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5166083401c6SMatthew G. Knepley 
5167083401c6SMatthew 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,
5168083401c6SMatthew G. Knepley   the fields argument is ignored.
5169083401c6SMatthew G. Knepley 
5170083401c6SMatthew G. Knepley   Level: advanced
5171083401c6SMatthew G. Knepley 
5172083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5173083401c6SMatthew G. Knepley @*/
5174083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5175083401c6SMatthew G. Knepley {
5176083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5177083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5178083401c6SMatthew G. Knepley 
5179083401c6SMatthew G. Knepley   PetscFunctionBegin;
5180083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5181083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5182083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5183083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5184083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5185083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5186083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5187083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5188083401c6SMatthew G. Knepley     }
5189083401c6SMatthew G. Knepley   }
5190083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5191083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5192083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5193083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5194083401c6SMatthew G. Knepley   if (!label) {
5195083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5196083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5197083401c6SMatthew G. Knepley     Nds = 0;
5198083401c6SMatthew G. Knepley   }
5199083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5200083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5201083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5202083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5203083401c6SMatthew G. Knepley }
5204083401c6SMatthew G. Knepley 
5205083401c6SMatthew G. Knepley /*@
5206e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5207e5e52638SMatthew G. Knepley 
5208e5e52638SMatthew G. Knepley   Not collective
5209e5e52638SMatthew G. Knepley 
5210e5e52638SMatthew G. Knepley   Input Parameters:
5211e5e52638SMatthew G. Knepley + dm  - The DM
5212e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5213e5e52638SMatthew G. Knepley 
5214e5e52638SMatthew G. Knepley   Output Parameters:
5215b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5216b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5217083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5218e5e52638SMatthew G. Knepley 
5219e5e52638SMatthew G. Knepley   Level: advanced
5220e5e52638SMatthew G. Knepley 
5221e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5222e5e52638SMatthew G. Knepley @*/
5223b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5224e5e52638SMatthew G. Knepley {
5225e5e52638SMatthew G. Knepley   PetscInt       Nds;
5226e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5227e5e52638SMatthew G. Knepley 
5228e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5229e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5230e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5231e5e52638SMatthew 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);
5232e5e52638SMatthew G. Knepley   if (label) {
5233e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5234e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5235e5e52638SMatthew G. Knepley   }
5236b3cf3223SMatthew G. Knepley   if (fields) {
5237b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5238b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5239b3cf3223SMatthew G. Knepley   }
5240e5e52638SMatthew G. Knepley   if (ds) {
5241b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5242e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5243e5e52638SMatthew G. Knepley   }
5244e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5245e5e52638SMatthew G. Knepley }
5246e5e52638SMatthew G. Knepley 
5247e5e52638SMatthew G. Knepley /*@
5248083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5249e5e52638SMatthew G. Knepley 
5250083401c6SMatthew G. Knepley   Not collective
5251e5e52638SMatthew G. Knepley 
5252e5e52638SMatthew G. Knepley   Input Parameters:
5253e5e52638SMatthew G. Knepley + dm     - The DM
5254083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5255083401c6SMatthew G. Knepley . label  - The region label, or NULL
5256083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5257083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5258e5e52638SMatthew G. Knepley 
5259e5e52638SMatthew G. Knepley   Level: advanced
5260e5e52638SMatthew G. Knepley 
5261083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5262e5e52638SMatthew G. Knepley @*/
5263083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5264e5e52638SMatthew G. Knepley {
5265083401c6SMatthew G. Knepley   PetscInt       Nds;
5266e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5267e5e52638SMatthew G. Knepley 
5268e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5269e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5270083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5271083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5272083401c6SMatthew 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);
5273e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5274083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5275083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5276083401c6SMatthew G. Knepley   if (fields) {
5277083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5278b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5279083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5280083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5281e5e52638SMatthew G. Knepley   }
5282083401c6SMatthew G. Knepley   if (ds) {
5283083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5284083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5285083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5286083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5287083401c6SMatthew G. Knepley   }
5288e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5289e5e52638SMatthew G. Knepley }
5290e5e52638SMatthew G. Knepley 
5291e5e52638SMatthew G. Knepley /*@
52921d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
52931d3af9e0SMatthew G. Knepley 
52941d3af9e0SMatthew G. Knepley   Not collective
52951d3af9e0SMatthew G. Knepley 
52961d3af9e0SMatthew G. Knepley   Input Parameters:
52971d3af9e0SMatthew G. Knepley + dm  - The DM
52981d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
52991d3af9e0SMatthew G. Knepley 
53001d3af9e0SMatthew G. Knepley   Output Parameter:
53011d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
53021d3af9e0SMatthew G. Knepley 
53031d3af9e0SMatthew G. Knepley   Level: advanced
53041d3af9e0SMatthew G. Knepley 
53051d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
53061d3af9e0SMatthew G. Knepley @*/
53071d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
53081d3af9e0SMatthew G. Knepley {
53091d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
53101d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
53111d3af9e0SMatthew G. Knepley 
53121d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
53131d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53141d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
53151d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
53161d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
53171d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
53181d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
53191d3af9e0SMatthew G. Knepley   else          *num = n;
53201d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
53211d3af9e0SMatthew G. Knepley }
53221d3af9e0SMatthew G. Knepley 
53231d3af9e0SMatthew G. Knepley /*@
5324e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5325e5e52638SMatthew G. Knepley 
5326d083f849SBarry Smith   Collective on dm
5327e5e52638SMatthew G. Knepley 
5328e5e52638SMatthew G. Knepley   Input Parameter:
5329e5e52638SMatthew G. Knepley . dm - The DM
5330e5e52638SMatthew G. Knepley 
5331e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5332e5e52638SMatthew G. Knepley 
5333e5e52638SMatthew G. Knepley   Level: intermediate
5334e5e52638SMatthew G. Knepley 
5335e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5336e5e52638SMatthew G. Knepley @*/
5337e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5338e5e52638SMatthew G. Knepley {
5339e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5340083401c6SMatthew G. Knepley   PetscDS        dsDef;
5341083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5342083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5343e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5344e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5345e5e52638SMatthew G. Knepley 
5346e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5347e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5348e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5349e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5350083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5351083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5352083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5353083401c6SMatthew G. Knepley   Nl   = 0;
5354083401c6SMatthew G. Knepley   Ndef = 0;
5355083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5356083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5357083401c6SMatthew G. Knepley     PetscInt l;
5358083401c6SMatthew G. Knepley 
5359083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5360083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5361083401c6SMatthew G. Knepley     if (l < Nl) continue;
5362083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5363083401c6SMatthew G. Knepley   }
5364083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5365083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5366083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5367b3cf3223SMatthew G. Knepley     IS        fields;
5368b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5369b3cf3223SMatthew G. Knepley 
5370b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5371083401c6SMatthew G. Knepley     if (nf) {
5372b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5373b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
537488f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
537588f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
537688f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
537788f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
537888f0c812SMatthew G. Knepley 
5379083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5380083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5381083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5382b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
53832df9ee95SMatthew G. Knepley     }
5384083401c6SMatthew G. Knepley   }
5385083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5386083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5387083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5388083401c6SMatthew G. Knepley   if (Ndef && Nl) {
53890122748bSMatthew G. Knepley     DM              plex;
5390083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5391083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5392083401c6SMatthew G. Knepley     PetscInt       *fields;
5393083401c6SMatthew G. Knepley     const PetscInt *cells;
5394083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
53950122748bSMatthew G. Knepley 
53960122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
53970122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5398083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5399083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5400083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5401083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5402083401c6SMatthew G. Knepley       IS      pointIS;
5403083401c6SMatthew G. Knepley 
5404083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5405083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5406083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5407083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5408083401c6SMatthew G. Knepley     }
5409083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5410083401c6SMatthew G. Knepley 
5411083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5412083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5413083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5414083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5415083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5416083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5417083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5418083401c6SMatthew G. Knepley 
5419083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5420083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5421083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5422083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5423083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5424083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5425083401c6SMatthew G. Knepley 
5426083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5427083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5428083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5429083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5430083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5431083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
54320122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5433083401c6SMatthew G. Knepley   }
5434083401c6SMatthew G. Knepley   /* Create label DSes
5435083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5436083401c6SMatthew G. Knepley   */
5437083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5438083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5439083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5440083401c6SMatthew G. Knepley     PetscDS   ds;
5441083401c6SMatthew G. Knepley     IS        fields;
5442083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5443083401c6SMatthew G. Knepley 
5444083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5445083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5446083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5447083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5448083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5449083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5450083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5451083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5452083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5453083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5454083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5455083401c6SMatthew G. Knepley     {
5456083401c6SMatthew G. Knepley       DMPolytopeType ct;
5457083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5458083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
54590122748bSMatthew G. Knepley 
5460e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5461665f567fSMatthew G. Knepley       if (lStart >= 0) {
5462412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5463412e9a14SMatthew G. Knepley         switch (ct) {
5464412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5465412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5466412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5467412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5468083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5469083401c6SMatthew G. Knepley           default: break;
5470412e9a14SMatthew G. Knepley         }
5471665f567fSMatthew G. Knepley       }
5472083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5473083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5474e5e52638SMatthew G. Knepley     }
5475083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5476e5e52638SMatthew G. Knepley   }
5477083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5478e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5479083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5480083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5481083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5482083401c6SMatthew G. Knepley     const PetscInt *fld;
5483083401c6SMatthew G. Knepley     PetscInt        nf;
5484e5e52638SMatthew G. Knepley 
5485083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5486083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5487083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5488083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5489083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5490e5e52638SMatthew G. Knepley       PetscClassId id;
5491e5e52638SMatthew G. Knepley 
5492083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5493083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5494083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5495083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5496083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5497e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5498e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5499e5e52638SMatthew G. Knepley     }
5500083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5501e5e52638SMatthew G. Knepley   }
5502e5e52638SMatthew G. Knepley   /* Setup DSes */
5503e5e52638SMatthew G. Knepley   if (doSetup) {
5504e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5505e5e52638SMatthew G. Knepley   }
5506e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5507e5e52638SMatthew G. Knepley }
5508e5e52638SMatthew G. Knepley 
5509e5e52638SMatthew G. Knepley /*@
55107f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
55117f96f943SMatthew G. Knepley 
5512*f2cacb80SMatthew G. Knepley   Collective on DM
5513*f2cacb80SMatthew G. Knepley 
55147f96f943SMatthew G. Knepley   Input Parameters:
55157f96f943SMatthew G. Knepley + dm   - The DM
55167f96f943SMatthew G. Knepley - time - The time
55177f96f943SMatthew G. Knepley 
55187f96f943SMatthew G. Knepley   Output Parameters:
5519*f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5520*f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
55217f96f943SMatthew G. Knepley 
55227f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
55237f96f943SMatthew G. Knepley 
55247f96f943SMatthew G. Knepley   Level: developer
55257f96f943SMatthew G. Knepley 
55267f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
55277f96f943SMatthew G. Knepley @*/
5528*f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
55297f96f943SMatthew G. Knepley {
55307f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
55317f96f943SMatthew G. Knepley   void            **ectxs;
55327f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
55337f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
55347f96f943SMatthew G. Knepley 
55357f96f943SMatthew G. Knepley   PetscFunctionBegin;
5536*f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5537*f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5538*f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
55397f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
55407f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
55417f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
55427f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
55437f96f943SMatthew G. Knepley     PetscDS         ds;
55447f96f943SMatthew G. Knepley     DMLabel         label;
55457f96f943SMatthew G. Knepley     IS              fieldIS;
55467f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
55477f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
55487f96f943SMatthew G. Knepley 
55497f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
55507f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
55517f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
55527f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
55537f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5554*f2cacb80SMatthew G. Knepley     if (u) {
55557f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
55567f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
55577f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
55587f96f943SMatthew G. Knepley       }
55597f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
55607f96f943SMatthew G. Knepley       if (label) {
55617f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
55627f96f943SMatthew G. Knepley       } else {
55637f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
55647f96f943SMatthew G. Knepley       }
55657f96f943SMatthew G. Knepley     }
5566*f2cacb80SMatthew G. Knepley     if (u_t) {
5567*f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5568*f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5569*f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5570*f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5571*f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5572*f2cacb80SMatthew G. Knepley       }
5573*f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5574*f2cacb80SMatthew G. Knepley       if (label) {
5575*f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5576*f2cacb80SMatthew G. Knepley       } else {
5577*f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5578*f2cacb80SMatthew G. Knepley       }
5579*f2cacb80SMatthew G. Knepley     }
5580*f2cacb80SMatthew G. Knepley   }
5581*f2cacb80SMatthew G. Knepley   if (u) {
55827f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
55837f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5584*f2cacb80SMatthew G. Knepley   }
5585*f2cacb80SMatthew G. Knepley   if (u_t) {
5586*f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5587*f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5588*f2cacb80SMatthew G. Knepley   }
55897f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
55907f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
55917f96f943SMatthew G. Knepley }
55927f96f943SMatthew G. Knepley 
55937f96f943SMatthew G. Knepley /*@
5594e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5595e5e52638SMatthew G. Knepley 
5596d083f849SBarry Smith   Collective on dm
5597e5e52638SMatthew G. Knepley 
5598e5e52638SMatthew G. Knepley   Input Parameter:
5599e5e52638SMatthew G. Knepley . dm - The DM
5600e5e52638SMatthew G. Knepley 
5601e5e52638SMatthew G. Knepley   Output Parameter:
5602e5e52638SMatthew G. Knepley . newdm - The DM
5603e5e52638SMatthew G. Knepley 
5604e5e52638SMatthew G. Knepley   Level: advanced
5605e5e52638SMatthew G. Knepley 
5606e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5607e5e52638SMatthew G. Knepley @*/
5608e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5609e5e52638SMatthew G. Knepley {
5610e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5611e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5612e5e52638SMatthew G. Knepley 
5613e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5614e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5615e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5616e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5617e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5618e5e52638SMatthew G. Knepley     DMLabel  label;
5619b3cf3223SMatthew G. Knepley     IS       fields;
5620e5e52638SMatthew G. Knepley     PetscDS  ds;
5621783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5622e5e52638SMatthew G. Knepley 
5623b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5624b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5625783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5626783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5627783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5628783e2ec8SMatthew G. Knepley       PetscInt    field;
5629783e2ec8SMatthew G. Knepley 
5630783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
5631783e2ec8SMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5632783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5633783e2ec8SMatthew G. Knepley     }
5634e5e52638SMatthew G. Knepley   }
5635e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5636e5e52638SMatthew G. Knepley }
5637e5e52638SMatthew G. Knepley 
5638e5e52638SMatthew G. Knepley /*@
5639e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5640e5e52638SMatthew G. Knepley 
5641d083f849SBarry Smith   Collective on dm
5642e5e52638SMatthew G. Knepley 
5643e5e52638SMatthew G. Knepley   Input Parameter:
5644e5e52638SMatthew G. Knepley . dm - The DM
5645e5e52638SMatthew G. Knepley 
5646e5e52638SMatthew G. Knepley   Output Parameter:
5647e5e52638SMatthew G. Knepley . newdm - The DM
5648e5e52638SMatthew G. Knepley 
5649e5e52638SMatthew G. Knepley   Level: advanced
5650e5e52638SMatthew G. Knepley 
5651e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5652e5e52638SMatthew G. Knepley @*/
5653e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5654e5e52638SMatthew G. Knepley {
5655e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5656e5e52638SMatthew G. Knepley 
5657e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5658e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5659e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5660e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5661e5e52638SMatthew G. Knepley }
5662e5e52638SMatthew G. Knepley 
5663b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5664b64e0483SPeter Brune {
5665b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5666b64e0483SPeter Brune   PetscErrorCode ierr;
5667b64e0483SPeter Brune   Vec coords,ccoords;
56686dbf9973SLawrence Mitchell   Mat inject;
5669b64e0483SPeter Brune   PetscFunctionBegin;
5670b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5671b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5672b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5673b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5674b64e0483SPeter Brune   if (coords && !ccoords) {
5675b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
56766668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
56776dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
56782adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
56796dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5680b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5681b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5682b64e0483SPeter Brune   }
5683b64e0483SPeter Brune   PetscFunctionReturn(0);
5684b64e0483SPeter Brune }
5685b64e0483SPeter Brune 
568603dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
568703dadc2fSPeter Brune {
568803dadc2fSPeter Brune   DM dm_coord,subdm_coord;
568903dadc2fSPeter Brune   PetscErrorCode ierr;
569003dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
569103dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
569203dadc2fSPeter Brune   PetscFunctionBegin;
569303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
569403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
569503dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
569603dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
569703dadc2fSPeter Brune   if (coords && !ccoords) {
569803dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
56996668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
570003dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
570124640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
570203dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
570303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
570403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
57051ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
570603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
570703dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
570803dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
570903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
571003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
571103dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
571203dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
571303dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
571403dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
571503dadc2fSPeter Brune   }
571603dadc2fSPeter Brune   PetscFunctionReturn(0);
571703dadc2fSPeter Brune }
571803dadc2fSPeter Brune 
5719c73cfb54SMatthew G. Knepley /*@
5720c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5721c73cfb54SMatthew G. Knepley 
5722c73cfb54SMatthew G. Knepley   Not collective
5723c73cfb54SMatthew G. Knepley 
5724c73cfb54SMatthew G. Knepley   Input Parameter:
5725c73cfb54SMatthew G. Knepley . dm - The DM
5726c73cfb54SMatthew G. Knepley 
5727c73cfb54SMatthew G. Knepley   Output Parameter:
5728c73cfb54SMatthew G. Knepley . dim - The topological dimension
5729c73cfb54SMatthew G. Knepley 
5730c73cfb54SMatthew G. Knepley   Level: beginner
5731c73cfb54SMatthew G. Knepley 
5732c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5733c73cfb54SMatthew G. Knepley @*/
5734c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5735c73cfb54SMatthew G. Knepley {
5736c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5737c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5738534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5739c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5740c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5741c73cfb54SMatthew G. Knepley }
5742c73cfb54SMatthew G. Knepley 
5743c73cfb54SMatthew G. Knepley /*@
5744c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5745c73cfb54SMatthew G. Knepley 
5746c73cfb54SMatthew G. Knepley   Collective on dm
5747c73cfb54SMatthew G. Knepley 
5748c73cfb54SMatthew G. Knepley   Input Parameters:
5749c73cfb54SMatthew G. Knepley + dm - The DM
5750c73cfb54SMatthew G. Knepley - dim - The topological dimension
5751c73cfb54SMatthew G. Knepley 
5752c73cfb54SMatthew G. Knepley   Level: beginner
5753c73cfb54SMatthew G. Knepley 
5754c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5755c73cfb54SMatthew G. Knepley @*/
5756c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5757c73cfb54SMatthew G. Knepley {
5758e5e52638SMatthew G. Knepley   PetscDS        ds;
5759f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5760f17e8794SMatthew G. Knepley 
5761c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5762c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5763c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5764c73cfb54SMatthew G. Knepley   dm->dim = dim;
5765e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5766e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5767c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5768c73cfb54SMatthew G. Knepley }
5769c73cfb54SMatthew G. Knepley 
5770793f3fe5SMatthew G. Knepley /*@
5771793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5772793f3fe5SMatthew G. Knepley 
5773d083f849SBarry Smith   Collective on dm
5774793f3fe5SMatthew G. Knepley 
5775793f3fe5SMatthew G. Knepley   Input Parameters:
5776793f3fe5SMatthew G. Knepley + dm - the DM
5777793f3fe5SMatthew G. Knepley - dim - the dimension
5778793f3fe5SMatthew G. Knepley 
5779793f3fe5SMatthew G. Knepley   Output Parameters:
5780793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5781aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5782793f3fe5SMatthew G. Knepley 
5783793f3fe5SMatthew G. Knepley   Note:
5784793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5785a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5786793f3fe5SMatthew G. Knepley   then the interval is empty.
5787793f3fe5SMatthew G. Knepley 
5788793f3fe5SMatthew G. Knepley   Level: intermediate
5789793f3fe5SMatthew G. Knepley 
5790793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5791793f3fe5SMatthew G. Knepley @*/
5792793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5793793f3fe5SMatthew G. Knepley {
5794793f3fe5SMatthew G. Knepley   PetscInt       d;
5795793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5796793f3fe5SMatthew G. Knepley 
5797793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5798793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5799793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5800793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5801b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5802793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5803793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5804793f3fe5SMatthew G. Knepley }
5805793f3fe5SMatthew G. Knepley 
58066636e97aSMatthew G Knepley /*@
58076636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
58086636e97aSMatthew G Knepley 
5809d083f849SBarry Smith   Collective on dm
58106636e97aSMatthew G Knepley 
58116636e97aSMatthew G Knepley   Input Parameters:
58126636e97aSMatthew G Knepley + dm - the DM
58136636e97aSMatthew G Knepley - c - coordinate vector
58146636e97aSMatthew G Knepley 
58152dd40e9bSPatrick Sanan   Notes:
58162dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
58172dd40e9bSPatrick Sanan 
58182dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
58196636e97aSMatthew G Knepley 
58206636e97aSMatthew G Knepley   Level: intermediate
58216636e97aSMatthew G Knepley 
58222dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
58236636e97aSMatthew G Knepley @*/
58246636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
58256636e97aSMatthew G Knepley {
58266636e97aSMatthew G Knepley   PetscErrorCode ierr;
58276636e97aSMatthew G Knepley 
58286636e97aSMatthew G Knepley   PetscFunctionBegin;
58296636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58306636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
58316636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
58326636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
58336636e97aSMatthew G Knepley   dm->coordinates = c;
58346636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5835b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
583603dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
58376636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58386636e97aSMatthew G Knepley }
58396636e97aSMatthew G Knepley 
58406636e97aSMatthew G Knepley /*@
58416636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
58426636e97aSMatthew G Knepley 
58437058e716SVaclav Hapla   Not collective
58446636e97aSMatthew G Knepley 
58456636e97aSMatthew G Knepley    Input Parameters:
58466636e97aSMatthew G Knepley +  dm - the DM
58476636e97aSMatthew G Knepley -  c - coordinate vector
58486636e97aSMatthew G Knepley 
58492dd40e9bSPatrick Sanan   Notes:
58506636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
58516636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
58526636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
58536636e97aSMatthew G Knepley 
58542dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
58552dd40e9bSPatrick Sanan 
58566636e97aSMatthew G Knepley   Level: intermediate
58576636e97aSMatthew G Knepley 
58586636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
58596636e97aSMatthew G Knepley @*/
58606636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
58616636e97aSMatthew G Knepley {
58626636e97aSMatthew G Knepley   PetscErrorCode ierr;
58636636e97aSMatthew G Knepley 
58646636e97aSMatthew G Knepley   PetscFunctionBegin;
58656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
58676636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
58686636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
58698865f1eaSKarl Rupp 
58706636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
58718865f1eaSKarl Rupp 
58726636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
58736636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58746636e97aSMatthew G Knepley }
58756636e97aSMatthew G Knepley 
58766636e97aSMatthew G Knepley /*@
58776636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
58786636e97aSMatthew G Knepley 
5879d083f849SBarry Smith   Collective on dm
58806636e97aSMatthew G Knepley 
58816636e97aSMatthew G Knepley   Input Parameter:
58826636e97aSMatthew G Knepley . dm - the DM
58836636e97aSMatthew G Knepley 
58846636e97aSMatthew G Knepley   Output Parameter:
58856636e97aSMatthew G Knepley . c - global coordinate vector
58866636e97aSMatthew G Knepley 
58876636e97aSMatthew G Knepley   Note:
58886636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
58896636e97aSMatthew G Knepley 
58906636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
58916636e97aSMatthew G Knepley 
58926636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
58936636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
58946636e97aSMatthew G Knepley 
58956636e97aSMatthew G Knepley   Level: intermediate
58966636e97aSMatthew G Knepley 
58976636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
58986636e97aSMatthew G Knepley @*/
58996636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
59006636e97aSMatthew G Knepley {
59016636e97aSMatthew G Knepley   PetscErrorCode ierr;
59026636e97aSMatthew G Knepley 
59036636e97aSMatthew G Knepley   PetscFunctionBegin;
59046636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59056636e97aSMatthew G Knepley   PetscValidPointer(c,2);
59061f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
59070298fd71SBarry Smith     DM        cdm = NULL;
59081970a576SMatthew G. Knepley     PetscBool localized;
59096636e97aSMatthew G Knepley 
59106636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
59116636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
59121970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
59131970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
59141970a576SMatthew G. Knepley     if (localized) {
59151970a576SMatthew G. Knepley       PetscInt cdim;
59161970a576SMatthew G. Knepley 
59171970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
59181970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
59191970a576SMatthew G. Knepley     }
59206636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
59216636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
59226636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
59236636e97aSMatthew G Knepley   }
59246636e97aSMatthew G Knepley   *c = dm->coordinates;
59256636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59266636e97aSMatthew G Knepley }
59276636e97aSMatthew G Knepley 
59286636e97aSMatthew G Knepley /*@
592981e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
593081e9a530SVaclav Hapla 
5931d083f849SBarry Smith   Collective on dm
593281e9a530SVaclav Hapla 
593381e9a530SVaclav Hapla   Input Parameter:
593481e9a530SVaclav Hapla . dm - the DM
593581e9a530SVaclav Hapla 
593681e9a530SVaclav Hapla   Level: advanced
593781e9a530SVaclav Hapla 
593881e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
593981e9a530SVaclav Hapla @*/
594081e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
594181e9a530SVaclav Hapla {
594281e9a530SVaclav Hapla   PetscErrorCode ierr;
594381e9a530SVaclav Hapla 
594481e9a530SVaclav Hapla   PetscFunctionBegin;
594581e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
594681e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
59471970a576SMatthew G. Knepley     DM        cdm = NULL;
59481970a576SMatthew G. Knepley     PetscBool localized;
59491970a576SMatthew G. Knepley 
595081e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
595181e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
59521970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
59531970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
59541970a576SMatthew G. Knepley     if (localized) {
59551970a576SMatthew G. Knepley       PetscInt cdim;
59561970a576SMatthew G. Knepley 
59571970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
59581970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
59591970a576SMatthew G. Knepley     }
596081e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
596181e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
596281e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
596381e9a530SVaclav Hapla   }
596481e9a530SVaclav Hapla   PetscFunctionReturn(0);
596581e9a530SVaclav Hapla }
596681e9a530SVaclav Hapla 
596781e9a530SVaclav Hapla /*@
59686636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
59696636e97aSMatthew G Knepley 
5970d083f849SBarry Smith   Collective on dm
59716636e97aSMatthew G Knepley 
59726636e97aSMatthew G Knepley   Input Parameter:
59736636e97aSMatthew G Knepley . dm - the DM
59746636e97aSMatthew G Knepley 
59756636e97aSMatthew G Knepley   Output Parameter:
59766636e97aSMatthew G Knepley . c - coordinate vector
59776636e97aSMatthew G Knepley 
59786636e97aSMatthew G Knepley   Note:
59796636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
59806636e97aSMatthew G Knepley 
59816636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
59826636e97aSMatthew G Knepley 
59836636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59846636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59856636e97aSMatthew G Knepley 
59866636e97aSMatthew G Knepley   Level: intermediate
59876636e97aSMatthew G Knepley 
598881e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
59896636e97aSMatthew G Knepley @*/
59906636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
59916636e97aSMatthew G Knepley {
59926636e97aSMatthew G Knepley   PetscErrorCode ierr;
59936636e97aSMatthew G Knepley 
59946636e97aSMatthew G Knepley   PetscFunctionBegin;
59956636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59966636e97aSMatthew G Knepley   PetscValidPointer(c,2);
599781e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
59986636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
59996636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60006636e97aSMatthew G Knepley }
60016636e97aSMatthew G Knepley 
600281e9a530SVaclav Hapla /*@
600381e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
600481e9a530SVaclav Hapla 
600581e9a530SVaclav Hapla   Not collective
600681e9a530SVaclav Hapla 
600781e9a530SVaclav Hapla   Input Parameter:
600881e9a530SVaclav Hapla . dm - the DM
600981e9a530SVaclav Hapla 
601081e9a530SVaclav Hapla   Output Parameter:
601181e9a530SVaclav Hapla . c - coordinate vector
601281e9a530SVaclav Hapla 
601381e9a530SVaclav Hapla   Level: advanced
601481e9a530SVaclav Hapla 
601581e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
601681e9a530SVaclav Hapla @*/
601781e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
601881e9a530SVaclav Hapla {
601981e9a530SVaclav Hapla   PetscFunctionBegin;
602081e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
602181e9a530SVaclav Hapla   PetscValidPointer(c,2);
602281e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
60236636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
60246636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60256636e97aSMatthew G Knepley }
60266636e97aSMatthew G Knepley 
60272db98f8dSVaclav Hapla /*@
60282db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
60292db98f8dSVaclav Hapla 
60302db98f8dSVaclav Hapla   Not collective
60312db98f8dSVaclav Hapla 
60322db98f8dSVaclav Hapla   Input Parameter:
60332db98f8dSVaclav Hapla + dm - the DM
60342db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
60352db98f8dSVaclav Hapla 
60362db98f8dSVaclav Hapla   Output Parameter:
60372db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
60382db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
60392db98f8dSVaclav Hapla 
60402db98f8dSVaclav Hapla   Note:
60412db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
60422db98f8dSVaclav Hapla 
60432db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
60442db98f8dSVaclav Hapla 
60452db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
60462db98f8dSVaclav Hapla 
60472db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
60482db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
60492db98f8dSVaclav Hapla 
60502db98f8dSVaclav Hapla   Level: advanced
60512db98f8dSVaclav Hapla 
60522db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
60532db98f8dSVaclav Hapla @*/
60542db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
60552db98f8dSVaclav Hapla {
60562db98f8dSVaclav Hapla   PetscSection        cs, newcs;
60572db98f8dSVaclav Hapla   Vec                 coords;
60582db98f8dSVaclav Hapla   const PetscScalar   *arr;
60592db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
60602db98f8dSVaclav Hapla   PetscInt            n;
60612db98f8dSVaclav Hapla   PetscErrorCode      ierr;
60622db98f8dSVaclav Hapla 
60632db98f8dSVaclav Hapla   PetscFunctionBegin;
60642db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60652db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
60662db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
60672db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
60682db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
60691bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
60701bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
60712db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
60722db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
60732db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
60742db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
60752db98f8dSVaclav Hapla   if (pCoord) {
60762db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
60772db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
60782db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
60792db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6080ad9ac99dSVaclav Hapla   } else {
6081ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
60822db98f8dSVaclav Hapla   }
6083ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6084ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
60852db98f8dSVaclav Hapla   PetscFunctionReturn(0);
60862db98f8dSVaclav Hapla }
60872db98f8dSVaclav Hapla 
6088f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6089f19dbd58SToby Isaac {
6090f19dbd58SToby Isaac   PetscErrorCode ierr;
6091f19dbd58SToby Isaac 
6092f19dbd58SToby Isaac   PetscFunctionBegin;
6093f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6094f19dbd58SToby Isaac   PetscValidPointer(field,2);
6095f19dbd58SToby Isaac   if (!dm->coordinateField) {
6096f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6097f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6098f19dbd58SToby Isaac     }
6099f19dbd58SToby Isaac   }
6100f19dbd58SToby Isaac   *field = dm->coordinateField;
6101f19dbd58SToby Isaac   PetscFunctionReturn(0);
6102f19dbd58SToby Isaac }
6103f19dbd58SToby Isaac 
6104f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6105f19dbd58SToby Isaac {
6106f19dbd58SToby Isaac   PetscErrorCode ierr;
6107f19dbd58SToby Isaac 
6108f19dbd58SToby Isaac   PetscFunctionBegin;
6109f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6110f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6111c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6112f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6113f19dbd58SToby Isaac   dm->coordinateField = field;
6114f19dbd58SToby Isaac   PetscFunctionReturn(0);
6115f19dbd58SToby Isaac }
6116f19dbd58SToby Isaac 
61176636e97aSMatthew G Knepley /*@
61181cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
61196636e97aSMatthew G Knepley 
6120d083f849SBarry Smith   Collective on dm
61216636e97aSMatthew G Knepley 
61226636e97aSMatthew G Knepley   Input Parameter:
61236636e97aSMatthew G Knepley . dm - the DM
61246636e97aSMatthew G Knepley 
61256636e97aSMatthew G Knepley   Output Parameter:
61266636e97aSMatthew G Knepley . cdm - coordinate DM
61276636e97aSMatthew G Knepley 
61286636e97aSMatthew G Knepley   Level: intermediate
61296636e97aSMatthew G Knepley 
61301cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
61316636e97aSMatthew G Knepley @*/
61326636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
61336636e97aSMatthew G Knepley {
61346636e97aSMatthew G Knepley   PetscErrorCode ierr;
61356636e97aSMatthew G Knepley 
61366636e97aSMatthew G Knepley   PetscFunctionBegin;
61376636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61386636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
61396636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6140308f8a94SToby Isaac     DM cdm;
6141308f8a94SToby Isaac 
614282f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6143308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6144308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6145308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6146308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6147308f8a94SToby Isaac     dm->coordinateDM = cdm;
61486636e97aSMatthew G Knepley   }
61496636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
61506636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61516636e97aSMatthew G Knepley }
6152e87bb0d3SMatthew G Knepley 
61531cfe2091SMatthew G. Knepley /*@
61541cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
61551cfe2091SMatthew G. Knepley 
6156d083f849SBarry Smith   Logically Collective on dm
61571cfe2091SMatthew G. Knepley 
61581cfe2091SMatthew G. Knepley   Input Parameters:
61591cfe2091SMatthew G. Knepley + dm - the DM
61601cfe2091SMatthew G. Knepley - cdm - coordinate DM
61611cfe2091SMatthew G. Knepley 
61621cfe2091SMatthew G. Knepley   Level: intermediate
61631cfe2091SMatthew G. Knepley 
61641cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
61651cfe2091SMatthew G. Knepley @*/
61661cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
61671cfe2091SMatthew G. Knepley {
61681cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
61691cfe2091SMatthew G. Knepley 
61701cfe2091SMatthew G. Knepley   PetscFunctionBegin;
61711cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61721cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6173f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
61741cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
61751cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
61761cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
61771cfe2091SMatthew G. Knepley }
61781cfe2091SMatthew G. Knepley 
617946e270d4SMatthew G. Knepley /*@
618046e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
618146e270d4SMatthew G. Knepley 
618246e270d4SMatthew G. Knepley   Not Collective
618346e270d4SMatthew G. Knepley 
618446e270d4SMatthew G. Knepley   Input Parameter:
618546e270d4SMatthew G. Knepley . dm - The DM object
618646e270d4SMatthew G. Knepley 
618746e270d4SMatthew G. Knepley   Output Parameter:
618846e270d4SMatthew G. Knepley . dim - The embedding dimension
618946e270d4SMatthew G. Knepley 
619046e270d4SMatthew G. Knepley   Level: intermediate
619146e270d4SMatthew G. Knepley 
619292fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
619346e270d4SMatthew G. Knepley @*/
619446e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
619546e270d4SMatthew G. Knepley {
619646e270d4SMatthew G. Knepley   PetscFunctionBegin;
619746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6198534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
61999a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
62009a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
62019a9a41abSToby Isaac   }
620246e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
620346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
620446e270d4SMatthew G. Knepley }
620546e270d4SMatthew G. Knepley 
620646e270d4SMatthew G. Knepley /*@
620746e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
620846e270d4SMatthew G. Knepley 
620946e270d4SMatthew G. Knepley   Not Collective
621046e270d4SMatthew G. Knepley 
621146e270d4SMatthew G. Knepley   Input Parameters:
621246e270d4SMatthew G. Knepley + dm  - The DM object
621346e270d4SMatthew G. Knepley - dim - The embedding dimension
621446e270d4SMatthew G. Knepley 
621546e270d4SMatthew G. Knepley   Level: intermediate
621646e270d4SMatthew G. Knepley 
621792fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
621846e270d4SMatthew G. Knepley @*/
621946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
622046e270d4SMatthew G. Knepley {
6221e5e52638SMatthew G. Knepley   PetscDS        ds;
6222f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6223f17e8794SMatthew G. Knepley 
622446e270d4SMatthew G. Knepley   PetscFunctionBegin;
622546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
622646e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6227e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6228e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
622946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
623046e270d4SMatthew G. Knepley }
623146e270d4SMatthew G. Knepley 
6232e8abe2deSMatthew G. Knepley /*@
6233e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6234e8abe2deSMatthew G. Knepley 
6235d083f849SBarry Smith   Collective on dm
6236e8abe2deSMatthew G. Knepley 
6237e8abe2deSMatthew G. Knepley   Input Parameter:
6238e8abe2deSMatthew G. Knepley . dm - The DM object
6239e8abe2deSMatthew G. Knepley 
6240e8abe2deSMatthew G. Knepley   Output Parameter:
6241e8abe2deSMatthew G. Knepley . section - The PetscSection object
6242e8abe2deSMatthew G. Knepley 
6243e8abe2deSMatthew G. Knepley   Level: intermediate
6244e8abe2deSMatthew G. Knepley 
624592fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6246e8abe2deSMatthew G. Knepley @*/
6247e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6248e8abe2deSMatthew G. Knepley {
6249e8abe2deSMatthew G. Knepley   DM             cdm;
6250e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6251e8abe2deSMatthew G. Knepley 
6252e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6253e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6254e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6255e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
625692fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6257e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6258e8abe2deSMatthew G. Knepley }
6259e8abe2deSMatthew G. Knepley 
6260e8abe2deSMatthew G. Knepley /*@
6261e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6262e8abe2deSMatthew G. Knepley 
6263e8abe2deSMatthew G. Knepley   Not Collective
6264e8abe2deSMatthew G. Knepley 
6265e8abe2deSMatthew G. Knepley   Input Parameters:
6266e8abe2deSMatthew G. Knepley + dm      - The DM object
626746e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6268e8abe2deSMatthew G. Knepley - section - The PetscSection object
6269e8abe2deSMatthew G. Knepley 
6270e8abe2deSMatthew G. Knepley   Level: intermediate
6271e8abe2deSMatthew G. Knepley 
627292fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6273e8abe2deSMatthew G. Knepley @*/
627446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6275e8abe2deSMatthew G. Knepley {
6276e8abe2deSMatthew G. Knepley   DM             cdm;
6277e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6278e8abe2deSMatthew G. Knepley 
6279e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6280e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
628146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6282e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
628392fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
628446e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
62854c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
628646e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
628746e270d4SMatthew G. Knepley 
628846e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
628946e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
629046e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
629146e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
629246e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
629346e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
629446e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
629546e270d4SMatthew G. Knepley     }
6296ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
629746e270d4SMatthew G. Knepley   }
6298e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6299e8abe2deSMatthew G. Knepley }
6300e8abe2deSMatthew G. Knepley 
63015dc8c3f7SMatthew G. Knepley /*@C
630290b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
63035dc8c3f7SMatthew G. Knepley 
63045dc8c3f7SMatthew G. Knepley   Input Parameters:
630590b157c4SStefano Zampini . dm      - The DM object
630690b157c4SStefano Zampini 
630790b157c4SStefano Zampini   Output Parameters:
630890b157c4SStefano Zampini + per     - Whether the DM is periodic or not
63095dc8c3f7SMatthew 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
63105dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
63115dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
63125dc8c3f7SMatthew G. Knepley 
63135dc8c3f7SMatthew G. Knepley   Level: developer
63145dc8c3f7SMatthew G. Knepley 
63155dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
63165dc8c3f7SMatthew G. Knepley @*/
631790b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6318c6b900c6SMatthew G. Knepley {
6319c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6320c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
632190b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6322c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6323c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
63245dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6325c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6326c6b900c6SMatthew G. Knepley }
6327c6b900c6SMatthew G. Knepley 
63285dc8c3f7SMatthew G. Knepley /*@C
63295dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
63305dc8c3f7SMatthew G. Knepley 
63315dc8c3f7SMatthew G. Knepley   Input Parameters:
63325dc8c3f7SMatthew G. Knepley + dm      - The DM object
633390b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
63345dc8c3f7SMatthew 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
63355dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
63365dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
63375dc8c3f7SMatthew G. Knepley 
63385dc8c3f7SMatthew G. Knepley   Level: developer
63395dc8c3f7SMatthew G. Knepley 
63405dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
63415dc8c3f7SMatthew G. Knepley @*/
634290b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6343c6b900c6SMatthew G. Knepley {
6344c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6345c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6346c6b900c6SMatthew G. Knepley 
6347c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6348c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
634990b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6350412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6351412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6352412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
63535dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
635490b157c4SStefano Zampini   if (maxCell) {
6355412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6356412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6357412e9a14SMatthew G. Knepley   }
6358412e9a14SMatthew G. Knepley   if (L) {
6359412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6360412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6361412e9a14SMatthew G. Knepley   }
6362412e9a14SMatthew G. Knepley   if (bd) {
6363412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6364412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
636590b157c4SStefano Zampini   }
6366072d7d67SStefano Zampini   dm->periodic = per;
6367c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6368c6b900c6SMatthew G. Knepley }
6369c6b900c6SMatthew G. Knepley 
63702e17dfb7SMatthew G. Knepley /*@
63712e17dfb7SMatthew 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.
63722e17dfb7SMatthew G. Knepley 
63732e17dfb7SMatthew G. Knepley   Input Parameters:
63742e17dfb7SMatthew G. Knepley + dm     - The DM
637565da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
637665da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
63772e17dfb7SMatthew G. Knepley 
63782e17dfb7SMatthew G. Knepley   Output Parameter:
63792e17dfb7SMatthew G. Knepley . out - The localized coordinate point
63802e17dfb7SMatthew G. Knepley 
63812e17dfb7SMatthew G. Knepley   Level: developer
63822e17dfb7SMatthew G. Knepley 
63832e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
63842e17dfb7SMatthew G. Knepley @*/
638565da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
63862e17dfb7SMatthew G. Knepley {
63872e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
63882e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
63892e17dfb7SMatthew G. Knepley 
63902e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63912e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
63922e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
63932e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
63942e17dfb7SMatthew G. Knepley   } else {
639565da65dcSMatthew G. Knepley     if (endpoint) {
639665da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6397da3333bfSMatthew 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)) {
6398da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
639965da65dcSMatthew G. Knepley         } else {
6400da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
640165da65dcSMatthew G. Knepley         }
640265da65dcSMatthew G. Knepley       }
640365da65dcSMatthew G. Knepley     } else {
64042e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
64051118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
64062e17dfb7SMatthew G. Knepley       }
64072e17dfb7SMatthew G. Knepley     }
640865da65dcSMatthew G. Knepley   }
64092e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
64102e17dfb7SMatthew G. Knepley }
64112e17dfb7SMatthew G. Knepley 
64122e17dfb7SMatthew G. Knepley /*
64132e17dfb7SMatthew 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.
64142e17dfb7SMatthew G. Knepley 
64152e17dfb7SMatthew G. Knepley   Input Parameters:
64162e17dfb7SMatthew G. Knepley + dm     - The DM
64172e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
64182e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
64192e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
64202e17dfb7SMatthew G. Knepley 
64212e17dfb7SMatthew G. Knepley   Output Parameter:
64222e17dfb7SMatthew G. Knepley . out - The localized coordinate point
64232e17dfb7SMatthew G. Knepley 
64242e17dfb7SMatthew G. Knepley   Level: developer
64252e17dfb7SMatthew G. Knepley 
64262e17dfb7SMatthew 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
64272e17dfb7SMatthew G. Knepley 
64282e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
64292e17dfb7SMatthew G. Knepley */
64302e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
64312e17dfb7SMatthew G. Knepley {
64322e17dfb7SMatthew G. Knepley   PetscInt d;
64332e17dfb7SMatthew G. Knepley 
64342e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
64352e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
64362e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
64372e17dfb7SMatthew G. Knepley   } else {
64382e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6439908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
64402e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
64412e17dfb7SMatthew G. Knepley       } else {
64422e17dfb7SMatthew G. Knepley         out[d] = in[d];
64432e17dfb7SMatthew G. Knepley       }
64442e17dfb7SMatthew G. Knepley     }
64452e17dfb7SMatthew G. Knepley   }
64462e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
64472e17dfb7SMatthew G. Knepley }
6448a5801f52SStefano Zampini 
64492e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
64502e17dfb7SMatthew G. Knepley {
64512e17dfb7SMatthew G. Knepley   PetscInt d;
64522e17dfb7SMatthew G. Knepley 
64532e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
64542e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
64552e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
64562e17dfb7SMatthew G. Knepley   } else {
64572e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6458908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
64592e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
64602e17dfb7SMatthew G. Knepley       } else {
64612e17dfb7SMatthew G. Knepley         out[d] = in[d];
64622e17dfb7SMatthew G. Knepley       }
64632e17dfb7SMatthew G. Knepley     }
64642e17dfb7SMatthew G. Knepley   }
64652e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
64662e17dfb7SMatthew G. Knepley }
64672e17dfb7SMatthew G. Knepley 
64682e17dfb7SMatthew G. Knepley /*
64692e17dfb7SMatthew 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.
64702e17dfb7SMatthew G. Knepley 
64712e17dfb7SMatthew G. Knepley   Input Parameters:
64722e17dfb7SMatthew G. Knepley + dm     - The DM
64732e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
64742e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
64752e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
64762e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
64772e17dfb7SMatthew G. Knepley 
64782e17dfb7SMatthew G. Knepley   Output Parameter:
64792e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
64802e17dfb7SMatthew G. Knepley 
64812e17dfb7SMatthew G. Knepley   Level: developer
64822e17dfb7SMatthew G. Knepley 
64832e17dfb7SMatthew 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
64842e17dfb7SMatthew G. Knepley 
64852e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
64862e17dfb7SMatthew G. Knepley */
64872e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
64882e17dfb7SMatthew G. Knepley {
64892e17dfb7SMatthew G. Knepley   PetscInt d;
64902e17dfb7SMatthew G. Knepley 
64912e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
64922e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
64932e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
64942e17dfb7SMatthew G. Knepley   } else {
64952e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6496412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6497412e9a14SMatthew G. Knepley 
6498412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6499412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6500412e9a14SMatthew G. Knepley 
6501412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6502412e9a14SMatthew G. Knepley           SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%D-Coordinate %g more than %g away from anchor %g", d, (double) PetscRealPart(in[d]), (double) maxC, (double) PetscRealPart(anchor[d]));
6503412e9a14SMatthew G. Knepley         out[d] += newCoord;
65042e17dfb7SMatthew G. Knepley       } else {
65052e17dfb7SMatthew G. Knepley         out[d] += in[d];
65062e17dfb7SMatthew G. Knepley       }
65072e17dfb7SMatthew G. Knepley     }
65082e17dfb7SMatthew G. Knepley   }
65092e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65102e17dfb7SMatthew G. Knepley }
65112e17dfb7SMatthew G. Knepley 
651236447a5eSToby Isaac /*@
65138f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
65148f700142SStefano Zampini 
65158f700142SStefano Zampini   Not collective
651636447a5eSToby Isaac 
651736447a5eSToby Isaac   Input Parameter:
651836447a5eSToby Isaac . dm - The DM
651936447a5eSToby Isaac 
652036447a5eSToby Isaac   Output Parameter:
652136447a5eSToby Isaac   areLocalized - True if localized
652236447a5eSToby Isaac 
652336447a5eSToby Isaac   Level: developer
652436447a5eSToby Isaac 
65258f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
652636447a5eSToby Isaac @*/
65278f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
652836447a5eSToby Isaac {
652936447a5eSToby Isaac   DM             cdm;
653036447a5eSToby Isaac   PetscSection   coordSection;
653146a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
653246a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
653336447a5eSToby Isaac   PetscErrorCode ierr;
653436447a5eSToby Isaac 
653536447a5eSToby Isaac   PetscFunctionBegin;
653636447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6537534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
65388b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
653946a3a80fSLisandro Dalcin 
654036447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
654136447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
654246a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
65439f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
654446a3a80fSLisandro Dalcin 
65459f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
654646a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
654736447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
654836447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
654946a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
655046a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
655136447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
655246a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
655336447a5eSToby Isaac   }
65548f700142SStefano Zampini   *areLocalized = alreadyLocalized;
655536447a5eSToby Isaac   PetscFunctionReturn(0);
655636447a5eSToby Isaac }
655736447a5eSToby Isaac 
65588f700142SStefano Zampini /*@
65598f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
65608f700142SStefano Zampini 
65618f700142SStefano Zampini   Collective on dm
65628f700142SStefano Zampini 
65638f700142SStefano Zampini   Input Parameter:
65648f700142SStefano Zampini . dm - The DM
65658f700142SStefano Zampini 
65668f700142SStefano Zampini   Output Parameter:
65678f700142SStefano Zampini   areLocalized - True if localized
65688f700142SStefano Zampini 
65698f700142SStefano Zampini   Level: developer
65708f700142SStefano Zampini 
65718f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
65728f700142SStefano Zampini @*/
65738f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
65748f700142SStefano Zampini {
65758f700142SStefano Zampini   PetscBool      localized;
65768f700142SStefano Zampini   PetscErrorCode ierr;
65778f700142SStefano Zampini 
65788f700142SStefano Zampini   PetscFunctionBegin;
65798f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6580534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
65818f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
65828f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
65838f700142SStefano Zampini   PetscFunctionReturn(0);
65848f700142SStefano Zampini }
658536447a5eSToby Isaac 
65862e17dfb7SMatthew G. Knepley /*@
6587492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
65882e17dfb7SMatthew G. Knepley 
65898f700142SStefano Zampini   Collective on dm
65908f700142SStefano Zampini 
65912e17dfb7SMatthew G. Knepley   Input Parameter:
65922e17dfb7SMatthew G. Knepley . dm - The DM
65932e17dfb7SMatthew G. Knepley 
65942e17dfb7SMatthew G. Knepley   Level: developer
65952e17dfb7SMatthew G. Knepley 
65968f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
65972e17dfb7SMatthew G. Knepley @*/
65982e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
65992e17dfb7SMatthew G. Knepley {
66002e17dfb7SMatthew G. Knepley   DM             cdm;
66012e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
66022e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
66033e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
66043e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6605e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
66063e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
66073e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
66082e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
66092e17dfb7SMatthew G. Knepley 
66102e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66112e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
661292c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6613f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6614f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6615f7cbd40bSStefano Zampini 
66162e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
66172e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
66182e17dfb7SMatthew G. Knepley   {
66192e17dfb7SMatthew G. Knepley     PetscBool isplex;
66202e17dfb7SMatthew G. Knepley 
66212e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
66222e17dfb7SMatthew G. Knepley     if (isplex) {
66232e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
66243e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
662569291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
66263e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
66273e922f36SToby Isaac       newStart = vStart;
66283e922f36SToby Isaac       newEnd   = vEnd;
66293e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
66303e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
66313e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
66323e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
66333e922f36SToby Isaac       }
66342e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
66352e17dfb7SMatthew G. Knepley   }
66362e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
663743eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
66382e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
66393e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6640e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
66413e922f36SToby Isaac 
66422e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
66432e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
66442e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
66452e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
66463e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
66473e922f36SToby Isaac 
664869291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
66493e922f36SToby Isaac   localized = &anchor[bs];
66503e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
66513e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
66523e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
66533e922f36SToby Isaac 
66543e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
66553e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
66563e922f36SToby Isaac       PetscInt     b;
66573e922f36SToby Isaac 
66583e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
66593e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
66603e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
66613e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
66623e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
66633e922f36SToby Isaac         for (b = 0; b < bs; b++) {
66643e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
66653e922f36SToby Isaac         }
66663e922f36SToby Isaac         if (b < bs) break;
66673e922f36SToby Isaac       }
66683e922f36SToby Isaac       if (d < dof/bs) {
66693e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
66703e922f36SToby Isaac           PetscInt cdof;
66713e922f36SToby Isaac 
66723e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
66733e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
66743e922f36SToby Isaac         }
66753e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
66763e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
66773e922f36SToby Isaac       }
66783e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
66793e922f36SToby Isaac     }
66803e922f36SToby Isaac   }
66813e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
66823e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
668369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
66843e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
668569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
66863e922f36SToby Isaac     PetscFunctionReturn(0);
66873e922f36SToby Isaac   }
66882e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
66892e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
66902e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
66912e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
66922e17dfb7SMatthew G. Knepley   }
66932e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
66942e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6695c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
66962e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
66972e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
66982e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
66992e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6700c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
67012e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
67022e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
67032e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
67042e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
67052e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
67062e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
67072e17dfb7SMatthew G. Knepley   }
67083e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
67093e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
67103e922f36SToby Isaac 
67112e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
67122e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
67133e922f36SToby Isaac       PetscInt     b, cdof;
67142e17dfb7SMatthew G. Knepley 
67153e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
67163e922f36SToby Isaac       if (!cdof) continue;
67172e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
67182e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
67192e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
67202e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
67212e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
67222e17dfb7SMatthew G. Knepley     }
67233e922f36SToby Isaac   }
672469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
672569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6726c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
67272e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
67282e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
67292e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
67302e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
67312e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
67322e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
67332e17dfb7SMatthew G. Knepley }
67342e17dfb7SMatthew G. Knepley 
6735e87bb0d3SMatthew G Knepley /*@
67363a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6737e87bb0d3SMatthew G Knepley 
6738d083f849SBarry Smith   Collective on v (see explanation below)
6739e87bb0d3SMatthew G Knepley 
6740e87bb0d3SMatthew G Knepley   Input Parameters:
6741e87bb0d3SMatthew G Knepley + dm - The DM
67423a93e3b7SToby Isaac . v - The Vec of points
674362a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
67443a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6745e87bb0d3SMatthew G Knepley 
674661e3bb9bSMatthew G Knepley   Output Parameter:
674762a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
674862a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
67493a93e3b7SToby Isaac 
6750e87bb0d3SMatthew G Knepley 
6751e87bb0d3SMatthew G Knepley   Level: developer
675261e3bb9bSMatthew G Knepley 
675362a38674SMatthew G. Knepley   Notes:
67543a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
675562a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
67563a93e3b7SToby Isaac 
67573a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
675862a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
67593a93e3b7SToby Isaac 
67603a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
67613a93e3b7SToby Isaac 
676262a38674SMatthew G. Knepley $    const PetscSFNode *cells;
676362a38674SMatthew G. Knepley $    PetscInt           nFound;
6764a6216909SToby Isaac $    const PetscInt    *found;
676562a38674SMatthew G. Knepley $
6766a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
67673a93e3b7SToby Isaac 
67683a93e3b7SToby 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
67693a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
67703a93e3b7SToby Isaac 
677162a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
677261e3bb9bSMatthew G Knepley @*/
677362a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6774e87bb0d3SMatthew G Knepley {
6775735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6776735aa83eSMatthew G Knepley 
6777e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6778e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6779e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6780e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
67813a93e3b7SToby Isaac   if (*cellSF) {
67823a93e3b7SToby Isaac     PetscMPIInt result;
67833a93e3b7SToby Isaac 
6784e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6785a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
67863a93e3b7SToby 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");
6787e0fc9d1bSMatthew G. Knepley   } else {
67883a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
67893a93e3b7SToby Isaac   }
6790b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
679147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
679262a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
679347a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6794e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6795e87bb0d3SMatthew G Knepley }
679614f150ffSMatthew G. Knepley 
6797f4d763aaSMatthew G. Knepley /*@
6798f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6799f4d763aaSMatthew G. Knepley 
68008f700142SStefano Zampini   Collective on dm
68018f700142SStefano Zampini 
6802f4d763aaSMatthew G. Knepley   Input Parameter:
6803f4d763aaSMatthew G. Knepley . dm - The original DM
6804f4d763aaSMatthew G. Knepley 
6805f4d763aaSMatthew G. Knepley   Output Parameter:
6806f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6807f4d763aaSMatthew G. Knepley 
6808f4d763aaSMatthew G. Knepley   Level: intermediate
6809f4d763aaSMatthew G. Knepley 
6810e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6811f4d763aaSMatthew G. Knepley @*/
681214f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
681314f150ffSMatthew G. Knepley {
6814c26acbdeSMatthew G. Knepley   PetscSection   section;
68152d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
681614f150ffSMatthew G. Knepley   PetscErrorCode ierr;
681714f150ffSMatthew G. Knepley 
681814f150ffSMatthew G. Knepley   PetscFunctionBegin;
681914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
682014f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
682192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6822c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6823127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
68242d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6825c26acbdeSMatthew G. Knepley     *odm = dm;
6826c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6827c26acbdeSMatthew G. Knepley   }
682814f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6829c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
683014f150ffSMatthew G. Knepley     PetscSF      sf;
683114f150ffSMatthew G. Knepley 
683214f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6833e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
683414f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
683592fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
683614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
683714f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
683815b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6839e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
684014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
684114f150ffSMatthew G. Knepley   }
684214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
684314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
684414f150ffSMatthew G. Knepley }
6845f4d763aaSMatthew G. Knepley 
6846f4d763aaSMatthew G. Knepley /*@
6847cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6848f4d763aaSMatthew G. Knepley 
6849f4d763aaSMatthew G. Knepley   Input Parameter:
6850f4d763aaSMatthew G. Knepley . dm - The original DM
6851f4d763aaSMatthew G. Knepley 
6852cdb7a50dSMatthew G. Knepley   Output Parameters:
6853cdb7a50dSMatthew G. Knepley + num - The output sequence number
6854cdb7a50dSMatthew G. Knepley - val - The output sequence value
6855f4d763aaSMatthew G. Knepley 
6856f4d763aaSMatthew G. Knepley   Level: intermediate
6857f4d763aaSMatthew G. Knepley 
6858f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6859f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6860f4d763aaSMatthew G. Knepley 
6861f4d763aaSMatthew G. Knepley .seealso: VecView()
6862f4d763aaSMatthew G. Knepley @*/
6863cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6864f4d763aaSMatthew G. Knepley {
6865f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6866f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6867534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6868534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6869f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6870f4d763aaSMatthew G. Knepley }
6871f4d763aaSMatthew G. Knepley 
6872f4d763aaSMatthew G. Knepley /*@
6873cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6874f4d763aaSMatthew G. Knepley 
6875f4d763aaSMatthew G. Knepley   Input Parameters:
6876f4d763aaSMatthew G. Knepley + dm - The original DM
6877cdb7a50dSMatthew G. Knepley . num - The output sequence number
6878cdb7a50dSMatthew G. Knepley - val - The output sequence value
6879f4d763aaSMatthew G. Knepley 
6880f4d763aaSMatthew G. Knepley   Level: intermediate
6881f4d763aaSMatthew G. Knepley 
6882f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6883f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6884f4d763aaSMatthew G. Knepley 
6885f4d763aaSMatthew G. Knepley .seealso: VecView()
6886f4d763aaSMatthew G. Knepley @*/
6887cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6888f4d763aaSMatthew G. Knepley {
6889f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6890f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6891f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6892cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6893cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6894cdb7a50dSMatthew G. Knepley }
6895cdb7a50dSMatthew G. Knepley 
6896cdb7a50dSMatthew G. Knepley /*@C
6897cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6898cdb7a50dSMatthew G. Knepley 
6899cdb7a50dSMatthew G. Knepley   Input Parameters:
6900cdb7a50dSMatthew G. Knepley + dm   - The original DM
6901cdb7a50dSMatthew G. Knepley . name - The sequence name
6902cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6903cdb7a50dSMatthew G. Knepley 
6904cdb7a50dSMatthew G. Knepley   Output Parameter:
6905cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6906cdb7a50dSMatthew G. Knepley 
6907cdb7a50dSMatthew G. Knepley   Level: intermediate
6908cdb7a50dSMatthew G. Knepley 
6909cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6910cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6911cdb7a50dSMatthew G. Knepley 
6912cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6913cdb7a50dSMatthew G. Knepley @*/
6914cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6915cdb7a50dSMatthew G. Knepley {
6916cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6917cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6918cdb7a50dSMatthew G. Knepley 
6919cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6920cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6921cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6922534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6923cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6924cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6925cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6926cdb7a50dSMatthew G. Knepley     PetscScalar value;
6927cdb7a50dSMatthew G. Knepley 
692839d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
69294aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6930cdb7a50dSMatthew G. Knepley #endif
6931cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6932f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6933f4d763aaSMatthew G. Knepley }
69348e4ac7eaSMatthew G. Knepley 
69358e4ac7eaSMatthew G. Knepley /*@
69368e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
69378e4ac7eaSMatthew G. Knepley 
69388e4ac7eaSMatthew G. Knepley   Not collective
69398e4ac7eaSMatthew G. Knepley 
69408e4ac7eaSMatthew G. Knepley   Input Parameter:
69418e4ac7eaSMatthew G. Knepley . dm - The DM
69428e4ac7eaSMatthew G. Knepley 
69438e4ac7eaSMatthew G. Knepley   Output Parameter:
69448e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
69458e4ac7eaSMatthew G. Knepley 
69468e4ac7eaSMatthew G. Knepley   Level: beginner
69478e4ac7eaSMatthew G. Knepley 
69488e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
69498e4ac7eaSMatthew G. Knepley @*/
69508e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
69518e4ac7eaSMatthew G. Knepley {
69528e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
69538e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6954534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
69558e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
69568e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
69578e4ac7eaSMatthew G. Knepley }
69588e4ac7eaSMatthew G. Knepley 
69598e4ac7eaSMatthew G. Knepley /*@
69605d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
69618e4ac7eaSMatthew G. Knepley 
69628e4ac7eaSMatthew G. Knepley   Collective on dm
69638e4ac7eaSMatthew G. Knepley 
69648e4ac7eaSMatthew G. Knepley   Input Parameters:
69658e4ac7eaSMatthew G. Knepley + dm - The DM
69668e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
69678e4ac7eaSMatthew G. Knepley 
69685d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
69695d3b26e6SMatthew G. Knepley 
69708e4ac7eaSMatthew G. Knepley   Level: beginner
69718e4ac7eaSMatthew G. Knepley 
69725d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
69738e4ac7eaSMatthew G. Knepley @*/
69748e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
69758e4ac7eaSMatthew G. Knepley {
69768e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
69778e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69788833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
69798e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
69808e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
69818e4ac7eaSMatthew G. Knepley }
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac 
6984c58f1c22SToby Isaac /*@C
6985c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6986c58f1c22SToby Isaac 
6987c58f1c22SToby Isaac   Not Collective
6988c58f1c22SToby Isaac 
6989c58f1c22SToby Isaac   Input Parameters:
6990c58f1c22SToby Isaac + dm   - The DM object
6991c58f1c22SToby Isaac - name - The label name
6992c58f1c22SToby Isaac 
6993c58f1c22SToby Isaac   Level: intermediate
6994c58f1c22SToby Isaac 
6995c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6996c58f1c22SToby Isaac @*/
6997c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6998c58f1c22SToby Isaac {
69995d80c0bfSVaclav Hapla   PetscBool      flg;
70005d80c0bfSVaclav Hapla   DMLabel        label;
7001c58f1c22SToby Isaac   PetscErrorCode ierr;
7002c58f1c22SToby Isaac 
7003c58f1c22SToby Isaac   PetscFunctionBegin;
7004c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7005c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
70065d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7007c58f1c22SToby Isaac   if (!flg) {
70085d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
70095d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
70105d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7011c58f1c22SToby Isaac   }
7012c58f1c22SToby Isaac   PetscFunctionReturn(0);
7013c58f1c22SToby Isaac }
7014c58f1c22SToby Isaac 
7015c58f1c22SToby Isaac /*@C
7016c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7017c58f1c22SToby Isaac 
7018c58f1c22SToby Isaac   Not Collective
7019c58f1c22SToby Isaac 
7020c58f1c22SToby Isaac   Input Parameters:
7021c58f1c22SToby Isaac + dm   - The DM object
7022c58f1c22SToby Isaac . name - The label name
7023c58f1c22SToby Isaac - point - The mesh point
7024c58f1c22SToby Isaac 
7025c58f1c22SToby Isaac   Output Parameter:
7026c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7027c58f1c22SToby Isaac 
7028c58f1c22SToby Isaac   Level: beginner
7029c58f1c22SToby Isaac 
7030c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7031c58f1c22SToby Isaac @*/
7032c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7033c58f1c22SToby Isaac {
7034c58f1c22SToby Isaac   DMLabel        label;
7035c58f1c22SToby Isaac   PetscErrorCode ierr;
7036c58f1c22SToby Isaac 
7037c58f1c22SToby Isaac   PetscFunctionBegin;
7038c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7039c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7040c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
704113903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7042c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7043c58f1c22SToby Isaac   PetscFunctionReturn(0);
7044c58f1c22SToby Isaac }
7045c58f1c22SToby Isaac 
7046c58f1c22SToby Isaac /*@C
7047c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7048c58f1c22SToby Isaac 
7049c58f1c22SToby Isaac   Not Collective
7050c58f1c22SToby Isaac 
7051c58f1c22SToby Isaac   Input Parameters:
7052c58f1c22SToby Isaac + dm   - The DM object
7053c58f1c22SToby Isaac . name - The label name
7054c58f1c22SToby Isaac . point - The mesh point
7055c58f1c22SToby Isaac - value - The label value for this point
7056c58f1c22SToby Isaac 
7057c58f1c22SToby Isaac   Output Parameter:
7058c58f1c22SToby Isaac 
7059c58f1c22SToby Isaac   Level: beginner
7060c58f1c22SToby Isaac 
7061c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7062c58f1c22SToby Isaac @*/
7063c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7064c58f1c22SToby Isaac {
7065c58f1c22SToby Isaac   DMLabel        label;
7066c58f1c22SToby Isaac   PetscErrorCode ierr;
7067c58f1c22SToby Isaac 
7068c58f1c22SToby Isaac   PetscFunctionBegin;
7069c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7070c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7071c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7072c58f1c22SToby Isaac   if (!label) {
7073c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7074c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7075c58f1c22SToby Isaac   }
7076c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7077c58f1c22SToby Isaac   PetscFunctionReturn(0);
7078c58f1c22SToby Isaac }
7079c58f1c22SToby Isaac 
7080c58f1c22SToby Isaac /*@C
7081c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7082c58f1c22SToby Isaac 
7083c58f1c22SToby Isaac   Not Collective
7084c58f1c22SToby Isaac 
7085c58f1c22SToby Isaac   Input Parameters:
7086c58f1c22SToby Isaac + dm   - The DM object
7087c58f1c22SToby Isaac . name - The label name
7088c58f1c22SToby Isaac . point - The mesh point
7089c58f1c22SToby Isaac - value - The label value for this point
7090c58f1c22SToby Isaac 
7091c58f1c22SToby Isaac   Output Parameter:
7092c58f1c22SToby Isaac 
7093c58f1c22SToby Isaac   Level: beginner
7094c58f1c22SToby Isaac 
7095c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7096c58f1c22SToby Isaac @*/
7097c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7098c58f1c22SToby Isaac {
7099c58f1c22SToby Isaac   DMLabel        label;
7100c58f1c22SToby Isaac   PetscErrorCode ierr;
7101c58f1c22SToby Isaac 
7102c58f1c22SToby Isaac   PetscFunctionBegin;
7103c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7104c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7105c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7106c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7107c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7108c58f1c22SToby Isaac   PetscFunctionReturn(0);
7109c58f1c22SToby Isaac }
7110c58f1c22SToby Isaac 
7111c58f1c22SToby Isaac /*@C
7112c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7113c58f1c22SToby Isaac 
7114c58f1c22SToby Isaac   Not Collective
7115c58f1c22SToby Isaac 
7116c58f1c22SToby Isaac   Input Parameters:
7117c58f1c22SToby Isaac + dm   - The DM object
7118c58f1c22SToby Isaac - name - The label name
7119c58f1c22SToby Isaac 
7120c58f1c22SToby Isaac   Output Parameter:
7121c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7122c58f1c22SToby Isaac 
7123c58f1c22SToby Isaac   Level: beginner
7124c58f1c22SToby Isaac 
7125df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7126c58f1c22SToby Isaac @*/
7127c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7128c58f1c22SToby Isaac {
7129c58f1c22SToby Isaac   DMLabel        label;
7130c58f1c22SToby Isaac   PetscErrorCode ierr;
7131c58f1c22SToby Isaac 
7132c58f1c22SToby Isaac   PetscFunctionBegin;
7133c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7134c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7135534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7136c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7137c58f1c22SToby Isaac   *size = 0;
7138c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7139c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7140c58f1c22SToby Isaac   PetscFunctionReturn(0);
7141c58f1c22SToby Isaac }
7142c58f1c22SToby Isaac 
7143c58f1c22SToby Isaac /*@C
7144c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7145c58f1c22SToby Isaac 
7146c58f1c22SToby Isaac   Not Collective
7147c58f1c22SToby Isaac 
7148c58f1c22SToby Isaac   Input Parameters:
7149c58f1c22SToby Isaac + mesh - The DM object
7150c58f1c22SToby Isaac - name - The label name
7151c58f1c22SToby Isaac 
7152c58f1c22SToby Isaac   Output Parameter:
7153c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7154c58f1c22SToby Isaac 
7155c58f1c22SToby Isaac   Level: beginner
7156c58f1c22SToby Isaac 
7157c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7158c58f1c22SToby Isaac @*/
7159c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7160c58f1c22SToby Isaac {
7161c58f1c22SToby Isaac   DMLabel        label;
7162c58f1c22SToby Isaac   PetscErrorCode ierr;
7163c58f1c22SToby Isaac 
7164c58f1c22SToby Isaac   PetscFunctionBegin;
7165c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7166c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7167c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7168c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7169c58f1c22SToby Isaac   *ids = NULL;
7170dab2e251SBlaise Bourdin  if (label) {
7171c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7172dab2e251SBlaise Bourdin   } else {
7173dab2e251SBlaise Bourdin     /* returning an empty IS */
7174dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7175dab2e251SBlaise Bourdin   }
7176c58f1c22SToby Isaac   PetscFunctionReturn(0);
7177c58f1c22SToby Isaac }
7178c58f1c22SToby Isaac 
7179c58f1c22SToby Isaac /*@C
7180c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7181c58f1c22SToby Isaac 
7182c58f1c22SToby Isaac   Not Collective
7183c58f1c22SToby Isaac 
7184c58f1c22SToby Isaac   Input Parameters:
7185c58f1c22SToby Isaac + dm - The DM object
7186c58f1c22SToby Isaac . name - The label name
7187c58f1c22SToby Isaac - value - The stratum value
7188c58f1c22SToby Isaac 
7189c58f1c22SToby Isaac   Output Parameter:
7190c58f1c22SToby Isaac . size - The stratum size
7191c58f1c22SToby Isaac 
7192c58f1c22SToby Isaac   Level: beginner
7193c58f1c22SToby Isaac 
7194c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7195c58f1c22SToby Isaac @*/
7196c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7197c58f1c22SToby Isaac {
7198c58f1c22SToby Isaac   DMLabel        label;
7199c58f1c22SToby Isaac   PetscErrorCode ierr;
7200c58f1c22SToby Isaac 
7201c58f1c22SToby Isaac   PetscFunctionBegin;
7202c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7203c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7204534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7205c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7206c58f1c22SToby Isaac   *size = 0;
7207c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7208c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7209c58f1c22SToby Isaac   PetscFunctionReturn(0);
7210c58f1c22SToby Isaac }
7211c58f1c22SToby Isaac 
7212c58f1c22SToby Isaac /*@C
7213c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7214c58f1c22SToby Isaac 
7215c58f1c22SToby Isaac   Not Collective
7216c58f1c22SToby Isaac 
7217c58f1c22SToby Isaac   Input Parameters:
7218c58f1c22SToby Isaac + dm - The DM object
7219c58f1c22SToby Isaac . name - The label name
7220c58f1c22SToby Isaac - value - The stratum value
7221c58f1c22SToby Isaac 
7222c58f1c22SToby Isaac   Output Parameter:
7223c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7224c58f1c22SToby Isaac 
7225c58f1c22SToby Isaac   Level: beginner
7226c58f1c22SToby Isaac 
7227c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7228c58f1c22SToby Isaac @*/
7229c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7230c58f1c22SToby Isaac {
7231c58f1c22SToby Isaac   DMLabel        label;
7232c58f1c22SToby Isaac   PetscErrorCode ierr;
7233c58f1c22SToby Isaac 
7234c58f1c22SToby Isaac   PetscFunctionBegin;
7235c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7236c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7237c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7238c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7239c58f1c22SToby Isaac   *points = NULL;
7240c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7241c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7242c58f1c22SToby Isaac   PetscFunctionReturn(0);
7243c58f1c22SToby Isaac }
7244c58f1c22SToby Isaac 
72454de306b1SToby Isaac /*@C
72469044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
72474de306b1SToby Isaac 
72484de306b1SToby Isaac   Not Collective
72494de306b1SToby Isaac 
72504de306b1SToby Isaac   Input Parameters:
72514de306b1SToby Isaac + dm - The DM object
72524de306b1SToby Isaac . name - The label name
72534de306b1SToby Isaac . value - The stratum value
72544de306b1SToby Isaac - points - The stratum points
72554de306b1SToby Isaac 
72564de306b1SToby Isaac   Level: beginner
72574de306b1SToby Isaac 
72584de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
72594de306b1SToby Isaac @*/
72604de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
72614de306b1SToby Isaac {
72624de306b1SToby Isaac   DMLabel        label;
72634de306b1SToby Isaac   PetscErrorCode ierr;
72644de306b1SToby Isaac 
72654de306b1SToby Isaac   PetscFunctionBegin;
72664de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72674de306b1SToby Isaac   PetscValidCharPointer(name, 2);
72684de306b1SToby Isaac   PetscValidPointer(points, 4);
72694de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
72704de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
72714de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
72724de306b1SToby Isaac   PetscFunctionReturn(0);
72734de306b1SToby Isaac }
72744de306b1SToby Isaac 
7275c58f1c22SToby Isaac /*@C
7276c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7277c58f1c22SToby Isaac 
7278c58f1c22SToby Isaac   Not Collective
7279c58f1c22SToby Isaac 
7280c58f1c22SToby Isaac   Input Parameters:
7281c58f1c22SToby Isaac + dm   - The DM object
7282c58f1c22SToby Isaac . name - The label name
7283c58f1c22SToby Isaac - value - The label value for this point
7284c58f1c22SToby Isaac 
7285c58f1c22SToby Isaac   Output Parameter:
7286c58f1c22SToby Isaac 
7287c58f1c22SToby Isaac   Level: beginner
7288c58f1c22SToby Isaac 
7289c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7290c58f1c22SToby Isaac @*/
7291c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7292c58f1c22SToby Isaac {
7293c58f1c22SToby Isaac   DMLabel        label;
7294c58f1c22SToby Isaac   PetscErrorCode ierr;
7295c58f1c22SToby Isaac 
7296c58f1c22SToby Isaac   PetscFunctionBegin;
7297c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7298c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7299c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7300c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7301c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7302c58f1c22SToby Isaac   PetscFunctionReturn(0);
7303c58f1c22SToby Isaac }
7304c58f1c22SToby Isaac 
7305c58f1c22SToby Isaac /*@
7306c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7307c58f1c22SToby Isaac 
7308c58f1c22SToby Isaac   Not Collective
7309c58f1c22SToby Isaac 
7310c58f1c22SToby Isaac   Input Parameter:
7311c58f1c22SToby Isaac . dm   - The DM object
7312c58f1c22SToby Isaac 
7313c58f1c22SToby Isaac   Output Parameter:
7314c58f1c22SToby Isaac . numLabels - the number of Labels
7315c58f1c22SToby Isaac 
7316c58f1c22SToby Isaac   Level: intermediate
7317c58f1c22SToby Isaac 
7318c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7319c58f1c22SToby Isaac @*/
7320c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7321c58f1c22SToby Isaac {
73225d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7323c58f1c22SToby Isaac   PetscInt  n    = 0;
7324c58f1c22SToby Isaac 
7325c58f1c22SToby Isaac   PetscFunctionBegin;
7326c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7327534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7328c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7329c58f1c22SToby Isaac   *numLabels = n;
7330c58f1c22SToby Isaac   PetscFunctionReturn(0);
7331c58f1c22SToby Isaac }
7332c58f1c22SToby Isaac 
7333c58f1c22SToby Isaac /*@C
7334c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7335c58f1c22SToby Isaac 
7336c58f1c22SToby Isaac   Not Collective
7337c58f1c22SToby Isaac 
7338c58f1c22SToby Isaac   Input Parameters:
7339c58f1c22SToby Isaac + dm - The DM object
7340c58f1c22SToby Isaac - n  - the label number
7341c58f1c22SToby Isaac 
7342c58f1c22SToby Isaac   Output Parameter:
7343c58f1c22SToby Isaac . name - the label name
7344c58f1c22SToby Isaac 
7345c58f1c22SToby Isaac   Level: intermediate
7346c58f1c22SToby Isaac 
7347c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7348c58f1c22SToby Isaac @*/
7349c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7350c58f1c22SToby Isaac {
73515d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7352c58f1c22SToby Isaac   PetscInt       l    = 0;
7353d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7354c58f1c22SToby Isaac 
7355c58f1c22SToby Isaac   PetscFunctionBegin;
7356c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7357c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7358c58f1c22SToby Isaac   while (next) {
7359c58f1c22SToby Isaac     if (l == n) {
7360d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7361c58f1c22SToby Isaac       PetscFunctionReturn(0);
7362c58f1c22SToby Isaac     }
7363c58f1c22SToby Isaac     ++l;
7364c58f1c22SToby Isaac     next = next->next;
7365c58f1c22SToby Isaac   }
7366c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7367c58f1c22SToby Isaac }
7368c58f1c22SToby Isaac 
7369c58f1c22SToby Isaac /*@C
7370c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7371c58f1c22SToby Isaac 
7372c58f1c22SToby Isaac   Not Collective
7373c58f1c22SToby Isaac 
7374c58f1c22SToby Isaac   Input Parameters:
7375c58f1c22SToby Isaac + dm   - The DM object
7376c58f1c22SToby Isaac - name - The label name
7377c58f1c22SToby Isaac 
7378c58f1c22SToby Isaac   Output Parameter:
7379c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7380c58f1c22SToby Isaac 
7381c58f1c22SToby Isaac   Level: intermediate
7382c58f1c22SToby Isaac 
7383c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7384c58f1c22SToby Isaac @*/
7385c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7386c58f1c22SToby Isaac {
73875d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7388d67d17b1SMatthew G. Knepley   const char    *lname;
7389c58f1c22SToby Isaac   PetscErrorCode ierr;
7390c58f1c22SToby Isaac 
7391c58f1c22SToby Isaac   PetscFunctionBegin;
7392c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7393c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7394534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7395c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7396c58f1c22SToby Isaac   while (next) {
7397d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7398d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7399c58f1c22SToby Isaac     if (*hasLabel) break;
7400c58f1c22SToby Isaac     next = next->next;
7401c58f1c22SToby Isaac   }
7402c58f1c22SToby Isaac   PetscFunctionReturn(0);
7403c58f1c22SToby Isaac }
7404c58f1c22SToby Isaac 
7405c58f1c22SToby Isaac /*@C
7406c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7407c58f1c22SToby Isaac 
7408c58f1c22SToby Isaac   Not Collective
7409c58f1c22SToby Isaac 
7410c58f1c22SToby Isaac   Input Parameters:
7411c58f1c22SToby Isaac + dm   - The DM object
7412c58f1c22SToby Isaac - name - The label name
7413c58f1c22SToby Isaac 
7414c58f1c22SToby Isaac   Output Parameter:
7415c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7416c58f1c22SToby Isaac 
7417c58f1c22SToby Isaac   Level: intermediate
7418c58f1c22SToby Isaac 
7419c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7420c58f1c22SToby Isaac @*/
7421c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7422c58f1c22SToby Isaac {
74235d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7424c58f1c22SToby Isaac   PetscBool      hasLabel;
7425d67d17b1SMatthew G. Knepley   const char    *lname;
7426c58f1c22SToby Isaac   PetscErrorCode ierr;
7427c58f1c22SToby Isaac 
7428c58f1c22SToby Isaac   PetscFunctionBegin;
7429c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7430c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7431c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7432c58f1c22SToby Isaac   *label = NULL;
7433c58f1c22SToby Isaac   while (next) {
7434d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7435d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7436c58f1c22SToby Isaac     if (hasLabel) {
7437c58f1c22SToby Isaac       *label = next->label;
7438c58f1c22SToby Isaac       break;
7439c58f1c22SToby Isaac     }
7440c58f1c22SToby Isaac     next = next->next;
7441c58f1c22SToby Isaac   }
7442c58f1c22SToby Isaac   PetscFunctionReturn(0);
7443c58f1c22SToby Isaac }
7444c58f1c22SToby Isaac 
7445c58f1c22SToby Isaac /*@C
7446c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7447c58f1c22SToby Isaac 
7448c58f1c22SToby Isaac   Not Collective
7449c58f1c22SToby Isaac 
7450c58f1c22SToby Isaac   Input Parameters:
7451c58f1c22SToby Isaac + dm - The DM object
7452c58f1c22SToby Isaac - n  - the label number
7453c58f1c22SToby Isaac 
7454c58f1c22SToby Isaac   Output Parameter:
7455c58f1c22SToby Isaac . label - the label
7456c58f1c22SToby Isaac 
7457c58f1c22SToby Isaac   Level: intermediate
7458c58f1c22SToby Isaac 
7459c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7460c58f1c22SToby Isaac @*/
7461c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7462c58f1c22SToby Isaac {
74635d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7464c58f1c22SToby Isaac   PetscInt    l    = 0;
7465c58f1c22SToby Isaac 
7466c58f1c22SToby Isaac   PetscFunctionBegin;
7467c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7468c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7469c58f1c22SToby Isaac   while (next) {
7470c58f1c22SToby Isaac     if (l == n) {
7471c58f1c22SToby Isaac       *label = next->label;
7472c58f1c22SToby Isaac       PetscFunctionReturn(0);
7473c58f1c22SToby Isaac     }
7474c58f1c22SToby Isaac     ++l;
7475c58f1c22SToby Isaac     next = next->next;
7476c58f1c22SToby Isaac   }
7477c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7478c58f1c22SToby Isaac }
7479c58f1c22SToby Isaac 
7480c58f1c22SToby Isaac /*@C
7481c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7482c58f1c22SToby Isaac 
7483c58f1c22SToby Isaac   Not Collective
7484c58f1c22SToby Isaac 
7485c58f1c22SToby Isaac   Input Parameters:
7486c58f1c22SToby Isaac + dm   - The DM object
7487c58f1c22SToby Isaac - label - The DMLabel
7488c58f1c22SToby Isaac 
7489c58f1c22SToby Isaac   Level: developer
7490c58f1c22SToby Isaac 
7491c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7492c58f1c22SToby Isaac @*/
7493c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7494c58f1c22SToby Isaac {
74955d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7496c58f1c22SToby Isaac   PetscBool      hasLabel;
7497d67d17b1SMatthew G. Knepley   const char    *lname;
74985d80c0bfSVaclav Hapla   PetscBool      flg;
7499c58f1c22SToby Isaac   PetscErrorCode ierr;
7500c58f1c22SToby Isaac 
7501c58f1c22SToby Isaac   PetscFunctionBegin;
7502c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7503d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7504d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7505d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7506c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7507c58f1c22SToby Isaac   tmpLabel->label  = label;
7508c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
75095d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
75105d80c0bfSVaclav Hapla   *p = tmpLabel;
751108f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
75125d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
75135d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7514ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7515ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7516c58f1c22SToby Isaac   PetscFunctionReturn(0);
7517c58f1c22SToby Isaac }
7518c58f1c22SToby Isaac 
7519c58f1c22SToby Isaac /*@C
7520e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7521c58f1c22SToby Isaac 
7522c58f1c22SToby Isaac   Not Collective
7523c58f1c22SToby Isaac 
7524c58f1c22SToby Isaac   Input Parameters:
7525c58f1c22SToby Isaac + dm   - The DM object
7526c58f1c22SToby Isaac - name - The label name
7527c58f1c22SToby Isaac 
7528c58f1c22SToby Isaac   Output Parameter:
7529c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7530c58f1c22SToby Isaac 
7531c58f1c22SToby Isaac   Level: developer
7532c58f1c22SToby Isaac 
7533e5472504SVaclav Hapla   Notes:
7534e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7535e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7536e5472504SVaclav Hapla 
7537e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7538e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7539e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7540e5472504SVaclav Hapla 
7541e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7542c58f1c22SToby Isaac @*/
7543c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7544c58f1c22SToby Isaac {
754595d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7546c58f1c22SToby Isaac   PetscBool      hasLabel;
7547d67d17b1SMatthew G. Knepley   const char    *lname;
7548c58f1c22SToby Isaac   PetscErrorCode ierr;
7549c58f1c22SToby Isaac 
7550c58f1c22SToby Isaac   PetscFunctionBegin;
7551c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7552e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7553e5472504SVaclav Hapla   if (label) {
7554e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7555c58f1c22SToby Isaac     *label = NULL;
7556e5472504SVaclav Hapla   }
75575d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
755895d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7559d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7560c58f1c22SToby Isaac     if (hasLabel) {
756195d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7562c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
756395d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7564ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7565ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
756695d578d6SVaclav Hapla       if (label) *label = link->label;
756795d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
756895d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7569c58f1c22SToby Isaac       break;
7570c58f1c22SToby Isaac     }
7571c58f1c22SToby Isaac   }
7572c58f1c22SToby Isaac   PetscFunctionReturn(0);
7573c58f1c22SToby Isaac }
7574c58f1c22SToby Isaac 
7575306894acSVaclav Hapla /*@
7576306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7577306894acSVaclav Hapla 
7578306894acSVaclav Hapla   Not Collective
7579306894acSVaclav Hapla 
7580306894acSVaclav Hapla   Input Parameters:
7581306894acSVaclav Hapla + dm   - The DM object
7582306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7583306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7584306894acSVaclav Hapla 
7585306894acSVaclav Hapla   Level: developer
7586306894acSVaclav Hapla 
7587306894acSVaclav Hapla   Notes:
7588306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7589306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7590306894acSVaclav Hapla   *label nullified.
7591306894acSVaclav Hapla 
7592306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7593306894acSVaclav Hapla @*/
7594306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7595306894acSVaclav Hapla {
759643e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7597306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7598306894acSVaclav Hapla   PetscErrorCode ierr;
7599306894acSVaclav Hapla 
7600306894acSVaclav Hapla   PetscFunctionBegin;
7601306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7602306894acSVaclav Hapla   PetscValidPointer(label, 2);
7603f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7604306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7605306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
76065d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
760743e45a93SVaclav Hapla     if (*label == link->label) {
7608306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
760943e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7610306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7611ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
761243e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
761343e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
761443e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7615306894acSVaclav Hapla       break;
7616306894acSVaclav Hapla     }
7617306894acSVaclav Hapla   }
7618306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7619306894acSVaclav Hapla   PetscFunctionReturn(0);
7620306894acSVaclav Hapla }
7621306894acSVaclav Hapla 
7622c58f1c22SToby Isaac /*@C
7623c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7624c58f1c22SToby Isaac 
7625c58f1c22SToby Isaac   Not Collective
7626c58f1c22SToby Isaac 
7627c58f1c22SToby Isaac   Input Parameters:
7628c58f1c22SToby Isaac + dm   - The DM object
7629c58f1c22SToby Isaac - name - The label name
7630c58f1c22SToby Isaac 
7631c58f1c22SToby Isaac   Output Parameter:
7632c58f1c22SToby Isaac . output - The flag for output
7633c58f1c22SToby Isaac 
7634c58f1c22SToby Isaac   Level: developer
7635c58f1c22SToby Isaac 
7636c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7637c58f1c22SToby Isaac @*/
7638c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7639c58f1c22SToby Isaac {
76405d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7641d67d17b1SMatthew G. Knepley   const char    *lname;
7642c58f1c22SToby Isaac   PetscErrorCode ierr;
7643c58f1c22SToby Isaac 
7644c58f1c22SToby Isaac   PetscFunctionBegin;
7645c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7646c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7647c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7648c58f1c22SToby Isaac   while (next) {
7649c58f1c22SToby Isaac     PetscBool flg;
7650c58f1c22SToby Isaac 
7651d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7652d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7653c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7654c58f1c22SToby Isaac     next = next->next;
7655c58f1c22SToby Isaac   }
7656c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7657c58f1c22SToby Isaac }
7658c58f1c22SToby Isaac 
7659c58f1c22SToby Isaac /*@C
7660c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7661c58f1c22SToby Isaac 
7662c58f1c22SToby Isaac   Not Collective
7663c58f1c22SToby Isaac 
7664c58f1c22SToby Isaac   Input Parameters:
7665c58f1c22SToby Isaac + dm     - The DM object
7666c58f1c22SToby Isaac . name   - The label name
7667c58f1c22SToby Isaac - output - The flag for output
7668c58f1c22SToby Isaac 
7669c58f1c22SToby Isaac   Level: developer
7670c58f1c22SToby Isaac 
7671c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7672c58f1c22SToby Isaac @*/
7673c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7674c58f1c22SToby Isaac {
76755d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7676d67d17b1SMatthew G. Knepley   const char    *lname;
7677c58f1c22SToby Isaac   PetscErrorCode ierr;
7678c58f1c22SToby Isaac 
7679c58f1c22SToby Isaac   PetscFunctionBegin;
7680c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7681534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7682c58f1c22SToby Isaac   while (next) {
7683c58f1c22SToby Isaac     PetscBool flg;
7684c58f1c22SToby Isaac 
7685d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7686d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7687c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7688c58f1c22SToby Isaac     next = next->next;
7689c58f1c22SToby Isaac   }
7690c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7691c58f1c22SToby Isaac }
7692c58f1c22SToby Isaac 
7693c58f1c22SToby Isaac /*@
7694c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7695c58f1c22SToby Isaac 
7696d083f849SBarry Smith   Collective on dmA
7697c58f1c22SToby Isaac 
7698c58f1c22SToby Isaac   Input Parameter:
76995d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
77002e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
77015d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7702ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7703c58f1c22SToby Isaac 
7704c58f1c22SToby Isaac   Level: intermediate
7705c58f1c22SToby Isaac 
7706c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7707c58f1c22SToby Isaac 
77085d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7709c58f1c22SToby Isaac @*/
77105d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7711c58f1c22SToby Isaac {
7712c58f1c22SToby Isaac   DMLabel        label, labelNew;
7713c58f1c22SToby Isaac   const char    *name;
7714c58f1c22SToby Isaac   PetscBool      flg;
77155d80c0bfSVaclav Hapla   DMLabelLink    link;
77165d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7717c58f1c22SToby Isaac 
77185d80c0bfSVaclav Hapla   PetscFunctionBegin;
77195d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
77205d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
77215d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
77225d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
77235d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
77245d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
77255d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
77265d80c0bfSVaclav Hapla     label=link->label;
77275d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
77285d80c0bfSVaclav Hapla     if (!all) {
7729c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7730c58f1c22SToby Isaac       if (flg) continue;
77317d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
77327d5acc75SStefano Zampini       if (flg) continue;
7733ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7734ba2698f1SMatthew G. Knepley       if (flg) continue;
77355d80c0bfSVaclav Hapla     }
77365d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7737c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
77385d80c0bfSVaclav Hapla     } else {
77395d80c0bfSVaclav Hapla       labelNew = label;
77405d80c0bfSVaclav Hapla     }
7741c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
77425d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7743c58f1c22SToby Isaac   }
7744c58f1c22SToby Isaac   PetscFunctionReturn(0);
7745c58f1c22SToby Isaac }
7746a8fb8f29SToby Isaac 
7747a8fb8f29SToby Isaac /*@
7748a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7749a8fb8f29SToby Isaac 
7750a8fb8f29SToby Isaac   Input Parameter:
7751a8fb8f29SToby Isaac . dm - The DM object
7752a8fb8f29SToby Isaac 
7753a8fb8f29SToby Isaac   Output Parameter:
7754a8fb8f29SToby Isaac . cdm - The coarse DM
7755a8fb8f29SToby Isaac 
7756a8fb8f29SToby Isaac   Level: intermediate
7757a8fb8f29SToby Isaac 
7758a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7759a8fb8f29SToby Isaac @*/
7760a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7761a8fb8f29SToby Isaac {
7762a8fb8f29SToby Isaac   PetscFunctionBegin;
7763a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7764a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7765a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7766a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7767a8fb8f29SToby Isaac }
7768a8fb8f29SToby Isaac 
7769a8fb8f29SToby Isaac /*@
7770a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7771a8fb8f29SToby Isaac 
7772a8fb8f29SToby Isaac   Input Parameters:
7773a8fb8f29SToby Isaac + dm - The DM object
7774a8fb8f29SToby Isaac - cdm - The coarse DM
7775a8fb8f29SToby Isaac 
7776a8fb8f29SToby Isaac   Level: intermediate
7777a8fb8f29SToby Isaac 
7778a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7779a8fb8f29SToby Isaac @*/
7780a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7781a8fb8f29SToby Isaac {
7782a8fb8f29SToby Isaac   PetscErrorCode ierr;
7783a8fb8f29SToby Isaac 
7784a8fb8f29SToby Isaac   PetscFunctionBegin;
7785a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7786a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7787a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7788a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7789a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7790a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7791a8fb8f29SToby Isaac }
7792a8fb8f29SToby Isaac 
779388bdff64SToby Isaac /*@
779488bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
779588bdff64SToby Isaac 
779688bdff64SToby Isaac   Input Parameter:
779788bdff64SToby Isaac . dm - The DM object
779888bdff64SToby Isaac 
779988bdff64SToby Isaac   Output Parameter:
780088bdff64SToby Isaac . fdm - The fine DM
780188bdff64SToby Isaac 
780288bdff64SToby Isaac   Level: intermediate
780388bdff64SToby Isaac 
780488bdff64SToby Isaac .seealso: DMSetFineDM()
780588bdff64SToby Isaac @*/
780688bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
780788bdff64SToby Isaac {
780888bdff64SToby Isaac   PetscFunctionBegin;
780988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
781088bdff64SToby Isaac   PetscValidPointer(fdm, 2);
781188bdff64SToby Isaac   *fdm = dm->fineMesh;
781288bdff64SToby Isaac   PetscFunctionReturn(0);
781388bdff64SToby Isaac }
781488bdff64SToby Isaac 
781588bdff64SToby Isaac /*@
781688bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
781788bdff64SToby Isaac 
781888bdff64SToby Isaac   Input Parameters:
781988bdff64SToby Isaac + dm - The DM object
782088bdff64SToby Isaac - fdm - The fine DM
782188bdff64SToby Isaac 
782288bdff64SToby Isaac   Level: intermediate
782388bdff64SToby Isaac 
782488bdff64SToby Isaac .seealso: DMGetFineDM()
782588bdff64SToby Isaac @*/
782688bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
782788bdff64SToby Isaac {
782888bdff64SToby Isaac   PetscErrorCode ierr;
782988bdff64SToby Isaac 
783088bdff64SToby Isaac   PetscFunctionBegin;
783188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
783288bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
783388bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
783488bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
783588bdff64SToby Isaac   dm->fineMesh = fdm;
783688bdff64SToby Isaac   PetscFunctionReturn(0);
783788bdff64SToby Isaac }
783888bdff64SToby Isaac 
7839a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7840a6ba4734SToby Isaac 
7841a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7842a6ba4734SToby Isaac {
7843e5e52638SMatthew G. Knepley   PetscInt       d;
7844a6ba4734SToby Isaac   PetscErrorCode ierr;
7845a6ba4734SToby Isaac 
7846a6ba4734SToby Isaac   PetscFunctionBegin;
7847e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7848e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7849e5e52638SMatthew G. Knepley   }
7850a6ba4734SToby Isaac   PetscFunctionReturn(0);
7851a6ba4734SToby Isaac }
7852a6ba4734SToby Isaac 
7853a6ba4734SToby Isaac /*@C
7854a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7855a6ba4734SToby Isaac 
7856783e2ec8SMatthew G. Knepley   Collective on dm
7857783e2ec8SMatthew G. Knepley 
7858a6ba4734SToby Isaac   Input Parameters:
78594c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7860f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7861a6ba4734SToby Isaac . name        - The BC name
7862a6ba4734SToby Isaac . labelname   - The label defining constrained points
7863a6ba4734SToby Isaac . field       - The field to constrain
7864e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7865a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7866a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7867a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7868a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7869a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7870a6ba4734SToby Isaac 
7871a6ba4734SToby Isaac   Options Database Keys:
7872a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7873a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7874a6ba4734SToby Isaac 
7875a6ba4734SToby Isaac   Level: developer
7876a6ba4734SToby Isaac 
7877a6ba4734SToby Isaac .seealso: DMGetBoundary()
7878a6ba4734SToby Isaac @*/
7879a30ec4eaSSatish 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)
7880a6ba4734SToby Isaac {
7881e5e52638SMatthew G. Knepley   PetscDS        ds;
7882a6ba4734SToby Isaac   PetscErrorCode ierr;
7883a6ba4734SToby Isaac 
7884a6ba4734SToby Isaac   PetscFunctionBegin;
7885a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7886783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
7887783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
7888783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
7889783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
7890e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7891783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
7892e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7893a6ba4734SToby Isaac   PetscFunctionReturn(0);
7894a6ba4734SToby Isaac }
7895a6ba4734SToby Isaac 
7896a6ba4734SToby Isaac /*@
7897a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7898a6ba4734SToby Isaac 
7899a6ba4734SToby Isaac   Input Parameters:
7900a6ba4734SToby Isaac . dm - The mesh object
7901a6ba4734SToby Isaac 
7902a6ba4734SToby Isaac   Output Parameters:
7903a6ba4734SToby Isaac . numBd - The number of BC
7904a6ba4734SToby Isaac 
7905a6ba4734SToby Isaac   Level: intermediate
7906a6ba4734SToby Isaac 
7907a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7908a6ba4734SToby Isaac @*/
7909a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7910a6ba4734SToby Isaac {
7911e5e52638SMatthew G. Knepley   PetscDS        ds;
791258ebd649SToby Isaac   PetscErrorCode ierr;
7913a6ba4734SToby Isaac 
7914a6ba4734SToby Isaac   PetscFunctionBegin;
7915a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7916e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7917e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7918a6ba4734SToby Isaac   PetscFunctionReturn(0);
7919a6ba4734SToby Isaac }
7920a6ba4734SToby Isaac 
7921a6ba4734SToby Isaac /*@C
79221c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7923a6ba4734SToby Isaac 
7924a6ba4734SToby Isaac   Input Parameters:
7925a6ba4734SToby Isaac + dm          - The mesh object
7926a6ba4734SToby Isaac - bd          - The BC number
7927a6ba4734SToby Isaac 
7928a6ba4734SToby Isaac   Output Parameters:
7929f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7930a6ba4734SToby Isaac . name        - The BC name
7931a6ba4734SToby Isaac . labelname   - The label defining constrained points
7932a6ba4734SToby Isaac . field       - The field to constrain
7933a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7934a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7935a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7936a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7937a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7938a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7939a6ba4734SToby Isaac 
7940a6ba4734SToby Isaac   Options Database Keys:
7941a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7942a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7943a6ba4734SToby Isaac 
7944a6ba4734SToby Isaac   Level: developer
7945a6ba4734SToby Isaac 
7946a6ba4734SToby Isaac .seealso: DMAddBoundary()
7947a6ba4734SToby Isaac @*/
7948a30ec4eaSSatish 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)
7949a6ba4734SToby Isaac {
7950e5e52638SMatthew G. Knepley   PetscDS        ds;
795158ebd649SToby Isaac   PetscErrorCode ierr;
7952a6ba4734SToby Isaac 
7953a6ba4734SToby Isaac   PetscFunctionBegin;
7954a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7955e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7956e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7957a6ba4734SToby Isaac   PetscFunctionReturn(0);
7958a6ba4734SToby Isaac }
7959a6ba4734SToby Isaac 
7960e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7961e6f8dbb6SToby Isaac {
7962e5e52638SMatthew G. Knepley   PetscDS        ds;
7963dff059c6SToby Isaac   DMBoundary    *lastnext;
7964e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7965e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7966e6f8dbb6SToby Isaac 
7967e6f8dbb6SToby Isaac   PetscFunctionBegin;
7968e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7969e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
797047a1f5adSToby Isaac   if (dm->boundary) {
797147a1f5adSToby Isaac     DMBoundary next = dm->boundary;
797247a1f5adSToby Isaac 
797347a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
797447a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
797547a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
797647a1f5adSToby Isaac     while (next) {
797747a1f5adSToby Isaac       DMBoundary b = next;
797847a1f5adSToby Isaac 
797947a1f5adSToby Isaac       next = b->next;
798047a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7981a6ba4734SToby Isaac     }
798247a1f5adSToby Isaac     dm->boundary = NULL;
7983a6ba4734SToby Isaac   }
798447a1f5adSToby Isaac 
7985dff059c6SToby Isaac   lastnext = &(dm->boundary);
7986e6f8dbb6SToby Isaac   while (dsbound) {
7987e6f8dbb6SToby Isaac     DMBoundary dmbound;
7988e6f8dbb6SToby Isaac 
7989e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7990e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7991e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7992994fe344SLisandro 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);}
799347a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7994dff059c6SToby Isaac     *lastnext = dmbound;
7995dff059c6SToby Isaac     lastnext = &(dmbound->next);
7996dff059c6SToby Isaac     dsbound = dsbound->next;
7997a6ba4734SToby Isaac   }
7998a6ba4734SToby Isaac   PetscFunctionReturn(0);
7999a6ba4734SToby Isaac }
8000a6ba4734SToby Isaac 
8001a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8002a6ba4734SToby Isaac {
8003b95f2879SToby Isaac   DMBoundary     b;
8004a6ba4734SToby Isaac   PetscErrorCode ierr;
8005a6ba4734SToby Isaac 
8006a6ba4734SToby Isaac   PetscFunctionBegin;
8007a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8008534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8009a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8010e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8011b95f2879SToby Isaac   b = dm->boundary;
8012a6ba4734SToby Isaac   while (b && !(*isBd)) {
8013e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8014e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
80153424c85cSToby Isaac 
80163424c85cSToby Isaac     if (label) {
8017a6ba4734SToby Isaac       PetscInt i;
8018a6ba4734SToby Isaac 
8019e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
8020e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
8021a6ba4734SToby Isaac       }
8022a6ba4734SToby Isaac     }
8023a6ba4734SToby Isaac     b = b->next;
8024a6ba4734SToby Isaac   }
8025a6ba4734SToby Isaac   PetscFunctionReturn(0);
8026a6ba4734SToby Isaac }
80274d6f44ffSToby Isaac 
80284d6f44ffSToby Isaac /*@C
8029a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8030a6e0b375SMatthew G. Knepley 
8031a6e0b375SMatthew G. Knepley   Collective on DM
80324d6f44ffSToby Isaac 
80334d6f44ffSToby Isaac   Input Parameters:
80344d6f44ffSToby Isaac + dm      - The DM
80350709b2feSToby Isaac . time    - The time
80364d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
80374d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
80384d6f44ffSToby Isaac - mode    - The insertion mode for values
80394d6f44ffSToby Isaac 
80404d6f44ffSToby Isaac   Output Parameter:
80414d6f44ffSToby Isaac . X - vector
80424d6f44ffSToby Isaac 
80434d6f44ffSToby Isaac    Calling sequence of func:
80440709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
80454d6f44ffSToby Isaac 
80464d6f44ffSToby Isaac +  dim - The spatial dimension
80474d6f44ffSToby Isaac .  x   - The coordinates
80484d6f44ffSToby Isaac .  Nf  - The number of fields
80494d6f44ffSToby Isaac .  u   - The output field values
80504d6f44ffSToby Isaac -  ctx - optional user-defined function context
80514d6f44ffSToby Isaac 
80524d6f44ffSToby Isaac   Level: developer
80534d6f44ffSToby Isaac 
8054a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
80554d6f44ffSToby Isaac @*/
80560709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
80574d6f44ffSToby Isaac {
80584d6f44ffSToby Isaac   Vec            localX;
80594d6f44ffSToby Isaac   PetscErrorCode ierr;
80604d6f44ffSToby Isaac 
80614d6f44ffSToby Isaac   PetscFunctionBegin;
80624d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80634d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
80640709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
80654d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
80664d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
80674d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
80684d6f44ffSToby Isaac   PetscFunctionReturn(0);
80694d6f44ffSToby Isaac }
80704d6f44ffSToby Isaac 
8071a6e0b375SMatthew G. Knepley /*@C
8072a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8073a6e0b375SMatthew G. Knepley 
8074a6e0b375SMatthew G. Knepley   Not collective
8075a6e0b375SMatthew G. Knepley 
8076a6e0b375SMatthew G. Knepley   Input Parameters:
8077a6e0b375SMatthew G. Knepley + dm      - The DM
8078a6e0b375SMatthew G. Knepley . time    - The time
8079a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8080a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8081a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8082a6e0b375SMatthew G. Knepley 
8083a6e0b375SMatthew G. Knepley   Output Parameter:
8084a6e0b375SMatthew G. Knepley . localX - vector
8085a6e0b375SMatthew G. Knepley 
8086a6e0b375SMatthew G. Knepley    Calling sequence of func:
8087a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8088a6e0b375SMatthew G. Knepley 
8089a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8090a6e0b375SMatthew G. Knepley .  x   - The coordinates
8091a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8092a6e0b375SMatthew G. Knepley .  u   - The output field values
8093a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8094a6e0b375SMatthew G. Knepley 
8095a6e0b375SMatthew G. Knepley   Level: developer
8096a6e0b375SMatthew G. Knepley 
8097a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8098a6e0b375SMatthew G. Knepley @*/
80990709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
81004d6f44ffSToby Isaac {
81014d6f44ffSToby Isaac   PetscErrorCode ierr;
81024d6f44ffSToby Isaac 
81034d6f44ffSToby Isaac   PetscFunctionBegin;
81044d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81054d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
81060918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
81070709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
81084d6f44ffSToby Isaac   PetscFunctionReturn(0);
81094d6f44ffSToby Isaac }
81104d6f44ffSToby Isaac 
8111a6e0b375SMatthew G. Knepley /*@C
8112a6e0b375SMatthew 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.
8113a6e0b375SMatthew G. Knepley 
8114a6e0b375SMatthew G. Knepley   Collective on DM
8115a6e0b375SMatthew G. Knepley 
8116a6e0b375SMatthew G. Knepley   Input Parameters:
8117a6e0b375SMatthew G. Knepley + dm      - The DM
8118a6e0b375SMatthew G. Knepley . time    - The time
8119a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8120a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8121a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8122a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8123a6e0b375SMatthew G. Knepley 
8124a6e0b375SMatthew G. Knepley   Output Parameter:
8125a6e0b375SMatthew G. Knepley . X - vector
8126a6e0b375SMatthew G. Knepley 
8127a6e0b375SMatthew G. Knepley    Calling sequence of func:
8128a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8129a6e0b375SMatthew G. Knepley 
8130a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8131a6e0b375SMatthew G. Knepley .  x   - The coordinates
8132a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8133a6e0b375SMatthew G. Knepley .  u   - The output field values
8134a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8135a6e0b375SMatthew G. Knepley 
8136a6e0b375SMatthew G. Knepley   Level: developer
8137a6e0b375SMatthew G. Knepley 
8138a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8139a6e0b375SMatthew G. Knepley @*/
81402c53366bSMatthew 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)
81412c53366bSMatthew G. Knepley {
81422c53366bSMatthew G. Knepley   Vec            localX;
81432c53366bSMatthew G. Knepley   PetscErrorCode ierr;
81442c53366bSMatthew G. Knepley 
81452c53366bSMatthew G. Knepley   PetscFunctionBegin;
81462c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81472c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
81482c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
81492c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
81502c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
81512c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
81522c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
81532c53366bSMatthew G. Knepley }
81542c53366bSMatthew G. Knepley 
8155a6e0b375SMatthew G. Knepley /*@C
8156a6e0b375SMatthew 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.
8157a6e0b375SMatthew G. Knepley 
8158a6e0b375SMatthew G. Knepley   Not collective
8159a6e0b375SMatthew G. Knepley 
8160a6e0b375SMatthew G. Knepley   Input Parameters:
8161a6e0b375SMatthew G. Knepley + dm      - The DM
8162a6e0b375SMatthew G. Knepley . time    - The time
8163a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8164a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8165a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8166a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8167a6e0b375SMatthew G. Knepley 
8168a6e0b375SMatthew G. Knepley   Output Parameter:
8169a6e0b375SMatthew G. Knepley . localX - vector
8170a6e0b375SMatthew G. Knepley 
8171a6e0b375SMatthew G. Knepley    Calling sequence of func:
8172a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8173a6e0b375SMatthew G. Knepley 
8174a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8175a6e0b375SMatthew G. Knepley .  x   - The coordinates
8176a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8177a6e0b375SMatthew G. Knepley .  u   - The output field values
8178a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8179a6e0b375SMatthew G. Knepley 
8180a6e0b375SMatthew G. Knepley   Level: developer
8181a6e0b375SMatthew G. Knepley 
8182a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8183a6e0b375SMatthew G. Knepley @*/
81841c531cf8SMatthew 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)
81854d6f44ffSToby Isaac {
81864d6f44ffSToby Isaac   PetscErrorCode ierr;
81874d6f44ffSToby Isaac 
81884d6f44ffSToby Isaac   PetscFunctionBegin;
81894d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81904d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
81910918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
81921c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
81934d6f44ffSToby Isaac   PetscFunctionReturn(0);
81944d6f44ffSToby Isaac }
81952716604bSToby Isaac 
8196a6e0b375SMatthew G. Knepley /*@C
8197a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8198a6e0b375SMatthew G. Knepley 
8199a6e0b375SMatthew G. Knepley   Not collective
8200a6e0b375SMatthew G. Knepley 
8201a6e0b375SMatthew G. Knepley   Input Parameters:
8202a6e0b375SMatthew G. Knepley + dm      - The DM
8203a6e0b375SMatthew G. Knepley . time    - The time
8204a6e0b375SMatthew G. Knepley . localU  - The input field vector
8205a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8206a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8207a6e0b375SMatthew G. Knepley 
8208a6e0b375SMatthew G. Knepley   Output Parameter:
8209a6e0b375SMatthew G. Knepley . localX  - The output vector
8210a6e0b375SMatthew G. Knepley 
8211a6e0b375SMatthew G. Knepley    Calling sequence of func:
8212a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8213a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8214a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8215a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8216a6e0b375SMatthew G. Knepley 
8217a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8218a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8219a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8220a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8221a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8222a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8223a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8224a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8225a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8226a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8227a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8228a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8229a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8230a6e0b375SMatthew G. Knepley .  t            - The current time
8231a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8232a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8233a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8234a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8235a6e0b375SMatthew G. Knepley 
8236a6e0b375SMatthew 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.
8237a6e0b375SMatthew 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
8238a6e0b375SMatthew 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
8239a6e0b375SMatthew 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.
8240a6e0b375SMatthew G. Knepley 
8241a6e0b375SMatthew G. Knepley   Level: intermediate
8242a6e0b375SMatthew G. Knepley 
8243a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8244a6e0b375SMatthew G. Knepley @*/
82458c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
82468c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
82478c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
82488c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8249191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
82508c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
82518c6c5593SMatthew G. Knepley {
82528c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
82538c6c5593SMatthew G. Knepley 
82548c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82558c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82568c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
82578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
82580918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
82598c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
82608c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
82618c6c5593SMatthew G. Knepley }
82628c6c5593SMatthew G. Knepley 
8263a6e0b375SMatthew G. Knepley /*@C
8264a6e0b375SMatthew 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.
8265a6e0b375SMatthew G. Knepley 
8266a6e0b375SMatthew G. Knepley   Not collective
8267a6e0b375SMatthew G. Knepley 
8268a6e0b375SMatthew G. Knepley   Input Parameters:
8269a6e0b375SMatthew G. Knepley + dm      - The DM
8270a6e0b375SMatthew G. Knepley . time    - The time
8271a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8272a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8273a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8274a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8275a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8276a6e0b375SMatthew G. Knepley . localU  - The input field vector
8277a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8278a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8279a6e0b375SMatthew G. Knepley 
8280a6e0b375SMatthew G. Knepley   Output Parameter:
8281a6e0b375SMatthew G. Knepley . localX  - The output vector
8282a6e0b375SMatthew G. Knepley 
8283a6e0b375SMatthew G. Knepley    Calling sequence of func:
8284a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8285a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8286a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8287a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8288a6e0b375SMatthew G. Knepley 
8289a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8290a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8291a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8292a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8293a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8294a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8295a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8296a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8297a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8298a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8299a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8300a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8301a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8302a6e0b375SMatthew G. Knepley .  t            - The current time
8303a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8304a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8305a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8306a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8307a6e0b375SMatthew G. Knepley 
8308a6e0b375SMatthew 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.
8309a6e0b375SMatthew 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
8310a6e0b375SMatthew 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
8311a6e0b375SMatthew 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.
8312a6e0b375SMatthew G. Knepley 
8313a6e0b375SMatthew G. Knepley   Level: intermediate
8314a6e0b375SMatthew G. Knepley 
8315a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8316a6e0b375SMatthew G. Knepley @*/
83171c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
83188c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
83198c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
83208c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8321191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
83228c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
83238c6c5593SMatthew G. Knepley {
83248c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
83258c6c5593SMatthew G. Knepley 
83268c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83278c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83288c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
83298c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8330ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
83311c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
83328c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
83338c6c5593SMatthew G. Knepley }
83348c6c5593SMatthew G. Knepley 
83352716604bSToby Isaac /*@C
8336ece3a9fcSMatthew G. Knepley   DMProjectBdFieldLabelLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector, calculating only over the portion of the domain boundary specified by the label.
8337ece3a9fcSMatthew G. Knepley 
8338ece3a9fcSMatthew G. Knepley   Not collective
8339ece3a9fcSMatthew G. Knepley 
8340ece3a9fcSMatthew G. Knepley   Input Parameters:
8341ece3a9fcSMatthew G. Knepley + dm      - The DM
8342ece3a9fcSMatthew G. Knepley . time    - The time
8343ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8344ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8345ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8346ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8347ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8348ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8349ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8350ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8351ece3a9fcSMatthew G. Knepley 
8352ece3a9fcSMatthew G. Knepley   Output Parameter:
8353ece3a9fcSMatthew G. Knepley . localX  - The output vector
8354ece3a9fcSMatthew G. Knepley 
8355ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8356ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8357ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8358ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8359ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8360ece3a9fcSMatthew G. Knepley 
8361ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8362ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8363ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8364ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8365ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8366ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8367ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8368ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8369ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8370ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8371ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8372ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8373ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8374ece3a9fcSMatthew G. Knepley .  t            - The current time
8375ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8376ece3a9fcSMatthew G. Knepley .  n            - The face normal
8377ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8378ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8379ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8380ece3a9fcSMatthew G. Knepley 
8381ece3a9fcSMatthew G. Knepley   Note:
8382ece3a9fcSMatthew G. Knepley   There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
8383ece3a9fcSMatthew 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
8384ece3a9fcSMatthew 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
8385ece3a9fcSMatthew 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.
8386ece3a9fcSMatthew G. Knepley 
8387ece3a9fcSMatthew G. Knepley   Level: intermediate
8388ece3a9fcSMatthew G. Knepley 
8389ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8390ece3a9fcSMatthew G. Knepley @*/
8391ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8392ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8393ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8394ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8395ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8396ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8397ece3a9fcSMatthew G. Knepley {
8398ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8399ece3a9fcSMatthew G. Knepley 
8400ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8401ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8402ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8403ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8404ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8405ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8406ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8407ece3a9fcSMatthew G. Knepley }
8408ece3a9fcSMatthew G. Knepley 
8409ece3a9fcSMatthew G. Knepley /*@C
84102716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
84112716604bSToby Isaac 
84122716604bSToby Isaac   Input Parameters:
84132716604bSToby Isaac + dm    - The DM
84140709b2feSToby Isaac . time  - The time
84152716604bSToby Isaac . funcs - The functions to evaluate for each field component
84162716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8417574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84182716604bSToby Isaac 
84192716604bSToby Isaac   Output Parameter:
84202716604bSToby Isaac . diff - The diff ||u - u_h||_2
84212716604bSToby Isaac 
84222716604bSToby Isaac   Level: developer
84232716604bSToby Isaac 
84241189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
84252716604bSToby Isaac @*/
84260709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
84272716604bSToby Isaac {
84282716604bSToby Isaac   PetscErrorCode ierr;
84292716604bSToby Isaac 
84302716604bSToby Isaac   PetscFunctionBegin;
84312716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8432b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
84330918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
84340709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
84352716604bSToby Isaac   PetscFunctionReturn(0);
84362716604bSToby Isaac }
8437b698f381SToby Isaac 
8438b698f381SToby Isaac /*@C
8439b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8440b698f381SToby Isaac 
8441d083f849SBarry Smith   Collective on dm
8442d083f849SBarry Smith 
8443b698f381SToby Isaac   Input Parameters:
8444b698f381SToby Isaac + dm    - The DM
8445b698f381SToby Isaac , time  - The time
8446b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8447b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8448574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8449b698f381SToby Isaac - n     - The vector to project along
8450b698f381SToby Isaac 
8451b698f381SToby Isaac   Output Parameter:
8452b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8453b698f381SToby Isaac 
8454b698f381SToby Isaac   Level: developer
8455b698f381SToby Isaac 
8456b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8457b698f381SToby Isaac @*/
8458b698f381SToby 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)
8459b698f381SToby Isaac {
8460b698f381SToby Isaac   PetscErrorCode ierr;
8461b698f381SToby Isaac 
8462b698f381SToby Isaac   PetscFunctionBegin;
8463b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8464b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8465b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8466b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8467b698f381SToby Isaac   PetscFunctionReturn(0);
8468b698f381SToby Isaac }
8469b698f381SToby Isaac 
84702a16baeaSToby Isaac /*@C
84712a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84722a16baeaSToby Isaac 
8473d083f849SBarry Smith   Collective on dm
8474d083f849SBarry Smith 
84752a16baeaSToby Isaac   Input Parameters:
84762a16baeaSToby Isaac + dm    - The DM
84772a16baeaSToby Isaac . time  - The time
84782a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84792a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8480574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84812a16baeaSToby Isaac 
84822a16baeaSToby Isaac   Output Parameter:
84832a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84842a16baeaSToby Isaac 
84852a16baeaSToby Isaac   Level: developer
84862a16baeaSToby Isaac 
84871189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
84882a16baeaSToby Isaac @*/
84891189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
84902a16baeaSToby Isaac {
84912a16baeaSToby Isaac   PetscErrorCode ierr;
84922a16baeaSToby Isaac 
84932a16baeaSToby Isaac   PetscFunctionBegin;
84942a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84952a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
84960918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
84972a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
84982a16baeaSToby Isaac   PetscFunctionReturn(0);
84992a16baeaSToby Isaac }
85002a16baeaSToby Isaac 
8501df0b854cSToby Isaac /*@C
8502df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8503cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8504df0b854cSToby Isaac 
8505df0b854cSToby Isaac   Collective on dm
8506df0b854cSToby Isaac 
8507df0b854cSToby Isaac   Input parameters:
8508df0b854cSToby Isaac + dm - the pre-adaptation DM object
8509a1b0c543SToby Isaac - label - label with the flags
8510df0b854cSToby Isaac 
8511df0b854cSToby Isaac   Output parameters:
85120d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8513df0b854cSToby Isaac 
8514df0b854cSToby Isaac   Level: intermediate
85150d1cd5e0SMatthew G. Knepley 
85160d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8517df0b854cSToby Isaac @*/
85180d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8519df0b854cSToby Isaac {
8520df0b854cSToby Isaac   PetscErrorCode ierr;
8521df0b854cSToby Isaac 
8522df0b854cSToby Isaac   PetscFunctionBegin;
8523df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8524a1b0c543SToby Isaac   PetscValidPointer(label,2);
85250d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
85260d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
85276f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
85280d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
85290d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
85300d1cd5e0SMatthew G. Knepley }
85310d1cd5e0SMatthew G. Knepley 
85320d1cd5e0SMatthew G. Knepley /*@C
85330d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
85340d1cd5e0SMatthew G. Knepley 
85350d1cd5e0SMatthew G. Knepley   Input Parameters:
85360d1cd5e0SMatthew G. Knepley + dm - The DM object
85370d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
85386f25b0d8SLisandro 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_".
85390d1cd5e0SMatthew G. Knepley 
85400d1cd5e0SMatthew G. Knepley   Output Parameter:
85410d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
85420d1cd5e0SMatthew G. Knepley 
85430d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
85440d1cd5e0SMatthew G. Knepley 
85450d1cd5e0SMatthew G. Knepley   Level: advanced
85460d1cd5e0SMatthew G. Knepley 
85470d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
85480d1cd5e0SMatthew G. Knepley @*/
85490d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
85500d1cd5e0SMatthew G. Knepley {
85510d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
85520d1cd5e0SMatthew G. Knepley 
85530d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
85540d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85550d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
85560d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
85570d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
85586f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
85596f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
85600d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8561df0b854cSToby Isaac   PetscFunctionReturn(0);
8562df0b854cSToby Isaac }
8563c4088d22SMatthew G. Knepley 
8564502a2867SDave May /*@C
8565502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8566502a2867SDave May 
8567502a2867SDave May  Not Collective
8568502a2867SDave May 
8569502a2867SDave May  Input Parameter:
8570502a2867SDave May .  dm    - The DM
8571502a2867SDave May 
85720a19bb7dSprj-  Output Parameters:
85730a19bb7dSprj- +  nranks - the number of neighbours
85740a19bb7dSprj- -  ranks - the neighbors ranks
8575502a2867SDave May 
8576502a2867SDave May  Notes:
8577502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8578502a2867SDave May 
8579502a2867SDave May  Level: beginner
8580502a2867SDave May 
8581dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8582502a2867SDave May @*/
8583502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8584502a2867SDave May {
8585502a2867SDave May   PetscErrorCode ierr;
8586502a2867SDave May 
8587502a2867SDave May   PetscFunctionBegin;
8588502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85890918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8590502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8591502a2867SDave May   PetscFunctionReturn(0);
8592502a2867SDave May }
8593502a2867SDave May 
8594531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8595531c7667SBarry Smith 
8596531c7667SBarry Smith /*
8597531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8598531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8599531c7667SBarry Smith */
8600531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8601531c7667SBarry Smith {
8602531c7667SBarry Smith   PetscErrorCode ierr;
8603531c7667SBarry Smith 
8604531c7667SBarry Smith   PetscFunctionBegin;
8605531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8606531c7667SBarry Smith     Vec x1local;
8607531c7667SBarry Smith     DM  dm;
8608531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8609531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8610531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8611531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8612531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8613531c7667SBarry Smith     x1   = x1local;
8614531c7667SBarry Smith   }
8615531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8616531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8617531c7667SBarry Smith     DM  dm;
8618531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8619531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8620531c7667SBarry Smith   }
8621531c7667SBarry Smith   PetscFunctionReturn(0);
8622531c7667SBarry Smith }
8623531c7667SBarry Smith 
8624531c7667SBarry Smith /*@
8625531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8626531c7667SBarry Smith 
8627531c7667SBarry Smith     Input Parameter:
8628531c7667SBarry Smith .    coloring - the MatFDColoring object
8629531c7667SBarry Smith 
863095452b02SPatrick Sanan     Developer Notes:
863195452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8632531c7667SBarry Smith 
86331b266c99SBarry Smith     Level: advanced
86341b266c99SBarry Smith 
8635531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8636531c7667SBarry Smith @*/
8637531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8638531c7667SBarry Smith {
8639531c7667SBarry Smith   PetscFunctionBegin;
8640531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8641531c7667SBarry Smith   PetscFunctionReturn(0);
8642531c7667SBarry Smith }
86438320bc6fSPatrick Sanan 
86448320bc6fSPatrick Sanan /*@
86458320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
86468320bc6fSPatrick Sanan 
86478320bc6fSPatrick Sanan     Collective
86488320bc6fSPatrick Sanan 
86498320bc6fSPatrick Sanan     Input Parameters:
8650a5bc1bf3SBarry Smith +    dm1 - the first DM
86518320bc6fSPatrick Sanan -    dm2 - the second DM
86528320bc6fSPatrick Sanan 
86538320bc6fSPatrick Sanan     Output Parameters:
86548320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
86558320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
86568320bc6fSPatrick Sanan 
86578320bc6fSPatrick Sanan     Notes:
86588320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
86593d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
86608320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
86613d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
86623d862458SPatrick Sanan     decomposition, but hold different data.
86638320bc6fSPatrick Sanan 
86648320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
86658320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
86668320bc6fSPatrick Sanan 
86678320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
86688320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
86698320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
86708320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
86718320bc6fSPatrick Sanan 
86728320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
86738320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
86748320bc6fSPatrick Sanan .vb
86758320bc6fSPatrick Sanan   ...
86768320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
86778320bc6fSPatrick Sanan   if (set && compatible)  {
86788320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
86798320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
86803d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
86818320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
86828320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
86838320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
86848320bc6fSPatrick Sanan       }
86858320bc6fSPatrick Sanan     }
86868320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
86878320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
86888320bc6fSPatrick Sanan   } else {
86898320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
86908320bc6fSPatrick Sanan   }
86918320bc6fSPatrick Sanan   ...
86928320bc6fSPatrick Sanan .ve
86938320bc6fSPatrick Sanan 
86948320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
86958320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
86968320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
86978320bc6fSPatrick Sanan     always check the "set" output parameter.
86988320bc6fSPatrick Sanan 
86998320bc6fSPatrick Sanan     A DM is always compatible with itself.
87008320bc6fSPatrick Sanan 
87018320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
87028320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
87038320bc6fSPatrick Sanan     incompatible.
87048320bc6fSPatrick Sanan 
87058320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
87068320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
87078320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
87088320bc6fSPatrick Sanan 
87098320bc6fSPatrick Sanan     Developer Notes:
87103d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
87113d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8712a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
87138320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
87148320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
87153d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
87163d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
87178320bc6fSPatrick Sanan 
87188320bc6fSPatrick Sanan     Level: advanced
87198320bc6fSPatrick Sanan 
87203d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
87218320bc6fSPatrick Sanan @*/
87228320bc6fSPatrick Sanan 
8723a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
87248320bc6fSPatrick Sanan {
87258320bc6fSPatrick Sanan   PetscErrorCode ierr;
87268320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
87278320bc6fSPatrick Sanan   DMType         type,type2;
87288320bc6fSPatrick Sanan   PetscBool      sameType;
87298320bc6fSPatrick Sanan 
87308320bc6fSPatrick Sanan   PetscFunctionBegin;
8731a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
87328320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
87338320bc6fSPatrick Sanan 
87348320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8735a5bc1bf3SBarry Smith   if (dm1 == dm2) {
87368320bc6fSPatrick Sanan     *set = PETSC_TRUE;
87378320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
87388320bc6fSPatrick Sanan     PetscFunctionReturn(0);
87398320bc6fSPatrick Sanan   }
87408320bc6fSPatrick Sanan 
87418320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
87428320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
87438320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
87448320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8745a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
87468320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
87478320bc6fSPatrick Sanan     *set = PETSC_TRUE;
87488320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
87498320bc6fSPatrick Sanan     PetscFunctionReturn(0);
87508320bc6fSPatrick Sanan   }
87518320bc6fSPatrick Sanan 
87528320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8753a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8754a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8755b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
87568320bc6fSPatrick Sanan   }
87578320bc6fSPatrick Sanan 
8758a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
87598320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8760a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
87618320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
87628320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
87638320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8764a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
87658320bc6fSPatrick Sanan   } else {
87668320bc6fSPatrick Sanan     *set = PETSC_FALSE;
87678320bc6fSPatrick Sanan   }
87688320bc6fSPatrick Sanan   PetscFunctionReturn(0);
87698320bc6fSPatrick Sanan }
8770c0f0dcc3SMatthew G. Knepley 
8771c0f0dcc3SMatthew G. Knepley /*@C
8772c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8773c0f0dcc3SMatthew G. Knepley 
8774c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8775c0f0dcc3SMatthew G. Knepley 
8776c0f0dcc3SMatthew G. Knepley   Input Parameters:
8777c0f0dcc3SMatthew G. Knepley + DM - the DM
8778c0f0dcc3SMatthew G. Knepley . f - the monitor function
8779c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8780c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8781c0f0dcc3SMatthew G. Knepley 
8782c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8783c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8784c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8785c0f0dcc3SMatthew G. Knepley 
8786c0f0dcc3SMatthew G. Knepley   Notes:
8787c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8788c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8789c0f0dcc3SMatthew G. Knepley   order in which they were set.
8790c0f0dcc3SMatthew G. Knepley 
8791c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8792c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8793c0f0dcc3SMatthew G. Knepley 
8794c0f0dcc3SMatthew G. Knepley   Level: intermediate
8795c0f0dcc3SMatthew G. Knepley 
8796c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8797c0f0dcc3SMatthew G. Knepley @*/
8798c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8799c0f0dcc3SMatthew G. Knepley {
8800c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8801c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8802c0f0dcc3SMatthew G. Knepley 
8803c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8804c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8805c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8806c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8807c0f0dcc3SMatthew G. Knepley 
8808c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8809c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8810c0f0dcc3SMatthew G. Knepley   }
8811c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8812c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8813c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8814c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8815c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8816c0f0dcc3SMatthew G. Knepley }
8817c0f0dcc3SMatthew G. Knepley 
8818c0f0dcc3SMatthew G. Knepley /*@
8819c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8820c0f0dcc3SMatthew G. Knepley 
8821c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8822c0f0dcc3SMatthew G. Knepley 
8823c0f0dcc3SMatthew G. Knepley   Input Parameter:
8824c0f0dcc3SMatthew G. Knepley . dm - the DM
8825c0f0dcc3SMatthew G. Knepley 
8826c0f0dcc3SMatthew G. Knepley   Options Database Key:
8827c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8828c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8829c0f0dcc3SMatthew G. Knepley   set via the options database
8830c0f0dcc3SMatthew G. Knepley 
8831c0f0dcc3SMatthew G. Knepley   Notes:
8832c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8833c0f0dcc3SMatthew G. Knepley 
8834c0f0dcc3SMatthew G. Knepley   Level: intermediate
8835c0f0dcc3SMatthew G. Knepley 
8836c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8837c0f0dcc3SMatthew G. Knepley @*/
8838c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8839c0f0dcc3SMatthew G. Knepley {
8840c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8841c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8842c0f0dcc3SMatthew G. Knepley 
8843c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8844c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8845c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8846c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
8847c0f0dcc3SMatthew G. Knepley   }
8848c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8849c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8850c0f0dcc3SMatthew G. Knepley }
8851c0f0dcc3SMatthew G. Knepley 
8852c0f0dcc3SMatthew G. Knepley /*@C
8853c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8854c0f0dcc3SMatthew G. Knepley 
8855c0f0dcc3SMatthew G. Knepley   Collective on DM
8856c0f0dcc3SMatthew G. Knepley 
8857c0f0dcc3SMatthew G. Knepley   Input Parameters:
8858c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8859c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8860c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8861c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8862c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8863c0f0dcc3SMatthew 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
8864c0f0dcc3SMatthew G. Knepley 
8865c0f0dcc3SMatthew G. Knepley   Output Parameter:
8866c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8867c0f0dcc3SMatthew G. Knepley 
8868c0f0dcc3SMatthew G. Knepley   Level: developer
8869c0f0dcc3SMatthew G. Knepley 
8870c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
8871c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
8872c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
8873c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
8874c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
8875c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
8876c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
8877c0f0dcc3SMatthew G. Knepley @*/
8878c0f0dcc3SMatthew 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)
8879c0f0dcc3SMatthew G. Knepley {
8880c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8881c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8882c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
8883c0f0dcc3SMatthew G. Knepley 
8884c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8885c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8886c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
8887c0f0dcc3SMatthew G. Knepley   if (*flg) {
8888c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8889c0f0dcc3SMatthew G. Knepley 
8890c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
8891c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
8892c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
8893c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
8894c0f0dcc3SMatthew G. Knepley   }
8895c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8896c0f0dcc3SMatthew G. Knepley }
8897c0f0dcc3SMatthew G. Knepley 
8898c0f0dcc3SMatthew G. Knepley /*@
8899c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8900c0f0dcc3SMatthew G. Knepley 
8901c0f0dcc3SMatthew G. Knepley    Collective on DM
8902c0f0dcc3SMatthew G. Knepley 
8903c0f0dcc3SMatthew G. Knepley    Input Parameters:
8904c0f0dcc3SMatthew G. Knepley .  dm - The DM
8905c0f0dcc3SMatthew G. Knepley 
8906c0f0dcc3SMatthew G. Knepley    Level: developer
8907c0f0dcc3SMatthew G. Knepley 
8908c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8909c0f0dcc3SMatthew G. Knepley @*/
8910c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8911c0f0dcc3SMatthew G. Knepley {
8912c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8913c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8914c0f0dcc3SMatthew G. Knepley 
8915c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8916c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8917c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8918c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8919c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
8920c0f0dcc3SMatthew G. Knepley   }
8921c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8922c0f0dcc3SMatthew G. Knepley }
8923