xref: /petsc/src/dm/interface/dm.c (revision 00d952a436fee6bdf6e70eb3e77eb53947e929d9)
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 
9*00d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
10*00d952a4SJed Brown #  include <valgrind/memcheck.h>
11*00d952a4SJed Brown #endif
12*00d952a4SJed 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;
1467*00d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
1468*00d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
1469*00d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
1470*00d952a4SJed 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 /*@
5292e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5293e5e52638SMatthew G. Knepley 
5294d083f849SBarry Smith   Collective on dm
5295e5e52638SMatthew G. Knepley 
5296e5e52638SMatthew G. Knepley   Input Parameter:
5297e5e52638SMatthew G. Knepley . dm - The DM
5298e5e52638SMatthew G. Knepley 
5299e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5300e5e52638SMatthew G. Knepley 
5301e5e52638SMatthew G. Knepley   Level: intermediate
5302e5e52638SMatthew G. Knepley 
5303e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5304e5e52638SMatthew G. Knepley @*/
5305e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5306e5e52638SMatthew G. Knepley {
5307e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5308083401c6SMatthew G. Knepley   PetscDS        dsDef;
5309083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5310083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5311e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5312e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5313e5e52638SMatthew G. Knepley 
5314e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5315e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5316e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5317e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5318083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5319083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5320083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5321083401c6SMatthew G. Knepley   Nl   = 0;
5322083401c6SMatthew G. Knepley   Ndef = 0;
5323083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5324083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5325083401c6SMatthew G. Knepley     PetscInt l;
5326083401c6SMatthew G. Knepley 
5327083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5328083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5329083401c6SMatthew G. Knepley     if (l < Nl) continue;
5330083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5331083401c6SMatthew G. Knepley   }
5332083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5333083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5334083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5335b3cf3223SMatthew G. Knepley     IS        fields;
5336b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5337b3cf3223SMatthew G. Knepley 
5338b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5339083401c6SMatthew G. Knepley     if (nf) {
5340b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5341b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
534288f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
534388f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
534488f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
534588f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
534688f0c812SMatthew G. Knepley 
5347083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5348083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5349083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5350b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
53512df9ee95SMatthew G. Knepley     }
5352083401c6SMatthew G. Knepley   }
5353083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5354083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5355083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5356083401c6SMatthew G. Knepley   if (Ndef && Nl) {
53570122748bSMatthew G. Knepley     DM              plex;
5358083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5359083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5360083401c6SMatthew G. Knepley     PetscInt       *fields;
5361083401c6SMatthew G. Knepley     const PetscInt *cells;
5362083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
53630122748bSMatthew G. Knepley 
53640122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
53650122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5366083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5367083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5368083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5369083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5370083401c6SMatthew G. Knepley       IS      pointIS;
5371083401c6SMatthew G. Knepley 
5372083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5373083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5374083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5375083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5376083401c6SMatthew G. Knepley     }
5377083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5378083401c6SMatthew G. Knepley 
5379083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5380083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5381083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5382083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5383083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5384083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5385083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5386083401c6SMatthew G. Knepley 
5387083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5388083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5389083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5390083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5391083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5392083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5393083401c6SMatthew G. Knepley 
5394083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5395083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5396083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5397083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5398083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5399083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
54000122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5401083401c6SMatthew G. Knepley   }
5402083401c6SMatthew G. Knepley   /* Create label DSes
5403083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5404083401c6SMatthew G. Knepley   */
5405083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5406083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5407083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5408083401c6SMatthew G. Knepley     PetscDS   ds;
5409083401c6SMatthew G. Knepley     IS        fields;
5410083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5411083401c6SMatthew G. Knepley 
5412083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5413083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5414083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5415083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5416083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5417083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5418083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5419083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5420083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5421083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5422083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5423083401c6SMatthew G. Knepley     {
5424083401c6SMatthew G. Knepley       DMPolytopeType ct;
5425083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5426083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
54270122748bSMatthew G. Knepley 
5428e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5429665f567fSMatthew G. Knepley       if (lStart >= 0) {
5430412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5431412e9a14SMatthew G. Knepley         switch (ct) {
5432412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5433412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5434412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5435412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5436083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5437083401c6SMatthew G. Knepley           default: break;
5438412e9a14SMatthew G. Knepley         }
5439665f567fSMatthew G. Knepley       }
5440083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5441083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5442e5e52638SMatthew G. Knepley     }
5443083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5444e5e52638SMatthew G. Knepley   }
5445083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5446e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5447083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5448083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5449083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5450083401c6SMatthew G. Knepley     const PetscInt *fld;
5451083401c6SMatthew G. Knepley     PetscInt        nf;
5452e5e52638SMatthew G. Knepley 
5453083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5454083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5455083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5456083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5457083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5458e5e52638SMatthew G. Knepley       PetscClassId id;
5459e5e52638SMatthew G. Knepley 
5460083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5461083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5462083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5463083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5464083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5465e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5466e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5467e5e52638SMatthew G. Knepley     }
5468083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5469e5e52638SMatthew G. Knepley   }
5470e5e52638SMatthew G. Knepley   /* Setup DSes */
5471e5e52638SMatthew G. Knepley   if (doSetup) {
5472e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5473e5e52638SMatthew G. Knepley   }
5474e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5475e5e52638SMatthew G. Knepley }
5476e5e52638SMatthew G. Knepley 
5477e5e52638SMatthew G. Knepley /*@
5478e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5479e5e52638SMatthew G. Knepley 
5480d083f849SBarry Smith   Collective on dm
5481e5e52638SMatthew G. Knepley 
5482e5e52638SMatthew G. Knepley   Input Parameter:
5483e5e52638SMatthew G. Knepley . dm - The DM
5484e5e52638SMatthew G. Knepley 
5485e5e52638SMatthew G. Knepley   Output Parameter:
5486e5e52638SMatthew G. Knepley . newdm - The DM
5487e5e52638SMatthew G. Knepley 
5488e5e52638SMatthew G. Knepley   Level: advanced
5489e5e52638SMatthew G. Knepley 
5490e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5491e5e52638SMatthew G. Knepley @*/
5492e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5493e5e52638SMatthew G. Knepley {
5494e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5495e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5496e5e52638SMatthew G. Knepley 
5497e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5498e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5499e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5500e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5501e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5502e5e52638SMatthew G. Knepley     DMLabel  label;
5503b3cf3223SMatthew G. Knepley     IS       fields;
5504e5e52638SMatthew G. Knepley     PetscDS  ds;
5505783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5506e5e52638SMatthew G. Knepley 
5507b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5508b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5509783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5510783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5511783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5512783e2ec8SMatthew G. Knepley       PetscInt    field;
5513783e2ec8SMatthew G. Knepley 
5514783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
5515783e2ec8SMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5516783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5517783e2ec8SMatthew G. Knepley     }
5518e5e52638SMatthew G. Knepley   }
5519e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5520e5e52638SMatthew G. Knepley }
5521e5e52638SMatthew G. Knepley 
5522e5e52638SMatthew G. Knepley /*@
5523e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5524e5e52638SMatthew G. Knepley 
5525d083f849SBarry Smith   Collective on dm
5526e5e52638SMatthew G. Knepley 
5527e5e52638SMatthew G. Knepley   Input Parameter:
5528e5e52638SMatthew G. Knepley . dm - The DM
5529e5e52638SMatthew G. Knepley 
5530e5e52638SMatthew G. Knepley   Output Parameter:
5531e5e52638SMatthew G. Knepley . newdm - The DM
5532e5e52638SMatthew G. Knepley 
5533e5e52638SMatthew G. Knepley   Level: advanced
5534e5e52638SMatthew G. Knepley 
5535e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5536e5e52638SMatthew G. Knepley @*/
5537e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5538e5e52638SMatthew G. Knepley {
5539e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5540e5e52638SMatthew G. Knepley 
5541e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5542e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5543e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5544e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5545e5e52638SMatthew G. Knepley }
5546e5e52638SMatthew G. Knepley 
5547b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5548b64e0483SPeter Brune {
5549b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5550b64e0483SPeter Brune   PetscErrorCode ierr;
5551b64e0483SPeter Brune   Vec coords,ccoords;
55526dbf9973SLawrence Mitchell   Mat inject;
5553b64e0483SPeter Brune   PetscFunctionBegin;
5554b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5555b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5556b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5557b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5558b64e0483SPeter Brune   if (coords && !ccoords) {
5559b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
55606668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
55616dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
55622adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
55636dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5564b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5565b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5566b64e0483SPeter Brune   }
5567b64e0483SPeter Brune   PetscFunctionReturn(0);
5568b64e0483SPeter Brune }
5569b64e0483SPeter Brune 
557003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
557103dadc2fSPeter Brune {
557203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
557303dadc2fSPeter Brune   PetscErrorCode ierr;
557403dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
557503dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
557603dadc2fSPeter Brune   PetscFunctionBegin;
557703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
557803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
557903dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
558003dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
558103dadc2fSPeter Brune   if (coords && !ccoords) {
558203dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
55836668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
558403dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
558524640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
558603dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
558703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
558803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
55891ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
559003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
559103dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
559203dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
559303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
559403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
559503dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
559603dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
559703dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
559803dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
559903dadc2fSPeter Brune   }
560003dadc2fSPeter Brune   PetscFunctionReturn(0);
560103dadc2fSPeter Brune }
560203dadc2fSPeter Brune 
5603c73cfb54SMatthew G. Knepley /*@
5604c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5605c73cfb54SMatthew G. Knepley 
5606c73cfb54SMatthew G. Knepley   Not collective
5607c73cfb54SMatthew G. Knepley 
5608c73cfb54SMatthew G. Knepley   Input Parameter:
5609c73cfb54SMatthew G. Knepley . dm - The DM
5610c73cfb54SMatthew G. Knepley 
5611c73cfb54SMatthew G. Knepley   Output Parameter:
5612c73cfb54SMatthew G. Knepley . dim - The topological dimension
5613c73cfb54SMatthew G. Knepley 
5614c73cfb54SMatthew G. Knepley   Level: beginner
5615c73cfb54SMatthew G. Knepley 
5616c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5617c73cfb54SMatthew G. Knepley @*/
5618c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5619c73cfb54SMatthew G. Knepley {
5620c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5621c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5622534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5623c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5624c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5625c73cfb54SMatthew G. Knepley }
5626c73cfb54SMatthew G. Knepley 
5627c73cfb54SMatthew G. Knepley /*@
5628c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5629c73cfb54SMatthew G. Knepley 
5630c73cfb54SMatthew G. Knepley   Collective on dm
5631c73cfb54SMatthew G. Knepley 
5632c73cfb54SMatthew G. Knepley   Input Parameters:
5633c73cfb54SMatthew G. Knepley + dm - The DM
5634c73cfb54SMatthew G. Knepley - dim - The topological dimension
5635c73cfb54SMatthew G. Knepley 
5636c73cfb54SMatthew G. Knepley   Level: beginner
5637c73cfb54SMatthew G. Knepley 
5638c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5639c73cfb54SMatthew G. Knepley @*/
5640c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5641c73cfb54SMatthew G. Knepley {
5642e5e52638SMatthew G. Knepley   PetscDS        ds;
5643f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5644f17e8794SMatthew G. Knepley 
5645c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5646c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5647c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5648c73cfb54SMatthew G. Knepley   dm->dim = dim;
5649e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5650e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5651c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5652c73cfb54SMatthew G. Knepley }
5653c73cfb54SMatthew G. Knepley 
5654793f3fe5SMatthew G. Knepley /*@
5655793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5656793f3fe5SMatthew G. Knepley 
5657d083f849SBarry Smith   Collective on dm
5658793f3fe5SMatthew G. Knepley 
5659793f3fe5SMatthew G. Knepley   Input Parameters:
5660793f3fe5SMatthew G. Knepley + dm - the DM
5661793f3fe5SMatthew G. Knepley - dim - the dimension
5662793f3fe5SMatthew G. Knepley 
5663793f3fe5SMatthew G. Knepley   Output Parameters:
5664793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5665aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5666793f3fe5SMatthew G. Knepley 
5667793f3fe5SMatthew G. Knepley   Note:
5668793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5669a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5670793f3fe5SMatthew G. Knepley   then the interval is empty.
5671793f3fe5SMatthew G. Knepley 
5672793f3fe5SMatthew G. Knepley   Level: intermediate
5673793f3fe5SMatthew G. Knepley 
5674793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5675793f3fe5SMatthew G. Knepley @*/
5676793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5677793f3fe5SMatthew G. Knepley {
5678793f3fe5SMatthew G. Knepley   PetscInt       d;
5679793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5680793f3fe5SMatthew G. Knepley 
5681793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5682793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5683793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5684793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5685b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5686793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5687793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5688793f3fe5SMatthew G. Knepley }
5689793f3fe5SMatthew G. Knepley 
56906636e97aSMatthew G Knepley /*@
56916636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
56926636e97aSMatthew G Knepley 
5693d083f849SBarry Smith   Collective on dm
56946636e97aSMatthew G Knepley 
56956636e97aSMatthew G Knepley   Input Parameters:
56966636e97aSMatthew G Knepley + dm - the DM
56976636e97aSMatthew G Knepley - c - coordinate vector
56986636e97aSMatthew G Knepley 
56992dd40e9bSPatrick Sanan   Notes:
57002dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
57012dd40e9bSPatrick Sanan 
57022dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
57036636e97aSMatthew G Knepley 
57046636e97aSMatthew G Knepley   Level: intermediate
57056636e97aSMatthew G Knepley 
57062dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
57076636e97aSMatthew G Knepley @*/
57086636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
57096636e97aSMatthew G Knepley {
57106636e97aSMatthew G Knepley   PetscErrorCode ierr;
57116636e97aSMatthew G Knepley 
57126636e97aSMatthew G Knepley   PetscFunctionBegin;
57136636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57146636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
57156636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
57166636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
57176636e97aSMatthew G Knepley   dm->coordinates = c;
57186636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5719b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
572003dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
57216636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57226636e97aSMatthew G Knepley }
57236636e97aSMatthew G Knepley 
57246636e97aSMatthew G Knepley /*@
57256636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
57266636e97aSMatthew G Knepley 
57277058e716SVaclav Hapla   Not collective
57286636e97aSMatthew G Knepley 
57296636e97aSMatthew G Knepley    Input Parameters:
57306636e97aSMatthew G Knepley +  dm - the DM
57316636e97aSMatthew G Knepley -  c - coordinate vector
57326636e97aSMatthew G Knepley 
57332dd40e9bSPatrick Sanan   Notes:
57346636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
57356636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
57366636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
57376636e97aSMatthew G Knepley 
57382dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
57392dd40e9bSPatrick Sanan 
57406636e97aSMatthew G Knepley   Level: intermediate
57416636e97aSMatthew G Knepley 
57426636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
57436636e97aSMatthew G Knepley @*/
57446636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
57456636e97aSMatthew G Knepley {
57466636e97aSMatthew G Knepley   PetscErrorCode ierr;
57476636e97aSMatthew G Knepley 
57486636e97aSMatthew G Knepley   PetscFunctionBegin;
57496636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57506636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
57516636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
57526636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
57538865f1eaSKarl Rupp 
57546636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
57558865f1eaSKarl Rupp 
57566636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
57576636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57586636e97aSMatthew G Knepley }
57596636e97aSMatthew G Knepley 
57606636e97aSMatthew G Knepley /*@
57616636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
57626636e97aSMatthew G Knepley 
5763d083f849SBarry Smith   Collective on dm
57646636e97aSMatthew G Knepley 
57656636e97aSMatthew G Knepley   Input Parameter:
57666636e97aSMatthew G Knepley . dm - the DM
57676636e97aSMatthew G Knepley 
57686636e97aSMatthew G Knepley   Output Parameter:
57696636e97aSMatthew G Knepley . c - global coordinate vector
57706636e97aSMatthew G Knepley 
57716636e97aSMatthew G Knepley   Note:
57726636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
57736636e97aSMatthew G Knepley 
57746636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
57756636e97aSMatthew G Knepley 
57766636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
57776636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
57786636e97aSMatthew G Knepley 
57796636e97aSMatthew G Knepley   Level: intermediate
57806636e97aSMatthew G Knepley 
57816636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
57826636e97aSMatthew G Knepley @*/
57836636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
57846636e97aSMatthew G Knepley {
57856636e97aSMatthew G Knepley   PetscErrorCode ierr;
57866636e97aSMatthew G Knepley 
57876636e97aSMatthew G Knepley   PetscFunctionBegin;
57886636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57896636e97aSMatthew G Knepley   PetscValidPointer(c,2);
57901f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
57910298fd71SBarry Smith     DM        cdm = NULL;
57921970a576SMatthew G. Knepley     PetscBool localized;
57936636e97aSMatthew G Knepley 
57946636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
57956636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
57961970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
57971970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
57981970a576SMatthew G. Knepley     if (localized) {
57991970a576SMatthew G. Knepley       PetscInt cdim;
58001970a576SMatthew G. Knepley 
58011970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
58021970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
58031970a576SMatthew G. Knepley     }
58046636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
58056636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
58066636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
58076636e97aSMatthew G Knepley   }
58086636e97aSMatthew G Knepley   *c = dm->coordinates;
58096636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58106636e97aSMatthew G Knepley }
58116636e97aSMatthew G Knepley 
58126636e97aSMatthew G Knepley /*@
581381e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
581481e9a530SVaclav Hapla 
5815d083f849SBarry Smith   Collective on dm
581681e9a530SVaclav Hapla 
581781e9a530SVaclav Hapla   Input Parameter:
581881e9a530SVaclav Hapla . dm - the DM
581981e9a530SVaclav Hapla 
582081e9a530SVaclav Hapla   Level: advanced
582181e9a530SVaclav Hapla 
582281e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
582381e9a530SVaclav Hapla @*/
582481e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
582581e9a530SVaclav Hapla {
582681e9a530SVaclav Hapla   PetscErrorCode ierr;
582781e9a530SVaclav Hapla 
582881e9a530SVaclav Hapla   PetscFunctionBegin;
582981e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
583081e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
58311970a576SMatthew G. Knepley     DM        cdm = NULL;
58321970a576SMatthew G. Knepley     PetscBool localized;
58331970a576SMatthew G. Knepley 
583481e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
583581e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
58361970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
58371970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
58381970a576SMatthew G. Knepley     if (localized) {
58391970a576SMatthew G. Knepley       PetscInt cdim;
58401970a576SMatthew G. Knepley 
58411970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
58421970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
58431970a576SMatthew G. Knepley     }
584481e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
584581e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
584681e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
584781e9a530SVaclav Hapla   }
584881e9a530SVaclav Hapla   PetscFunctionReturn(0);
584981e9a530SVaclav Hapla }
585081e9a530SVaclav Hapla 
585181e9a530SVaclav Hapla /*@
58526636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
58536636e97aSMatthew G Knepley 
5854d083f849SBarry Smith   Collective on dm
58556636e97aSMatthew G Knepley 
58566636e97aSMatthew G Knepley   Input Parameter:
58576636e97aSMatthew G Knepley . dm - the DM
58586636e97aSMatthew G Knepley 
58596636e97aSMatthew G Knepley   Output Parameter:
58606636e97aSMatthew G Knepley . c - coordinate vector
58616636e97aSMatthew G Knepley 
58626636e97aSMatthew G Knepley   Note:
58636636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
58646636e97aSMatthew G Knepley 
58656636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
58666636e97aSMatthew G Knepley 
58676636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
58686636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
58696636e97aSMatthew G Knepley 
58706636e97aSMatthew G Knepley   Level: intermediate
58716636e97aSMatthew G Knepley 
587281e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
58736636e97aSMatthew G Knepley @*/
58746636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
58756636e97aSMatthew G Knepley {
58766636e97aSMatthew G Knepley   PetscErrorCode ierr;
58776636e97aSMatthew G Knepley 
58786636e97aSMatthew G Knepley   PetscFunctionBegin;
58796636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58806636e97aSMatthew G Knepley   PetscValidPointer(c,2);
588181e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
58826636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
58836636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58846636e97aSMatthew G Knepley }
58856636e97aSMatthew G Knepley 
588681e9a530SVaclav Hapla /*@
588781e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
588881e9a530SVaclav Hapla 
588981e9a530SVaclav Hapla   Not collective
589081e9a530SVaclav Hapla 
589181e9a530SVaclav Hapla   Input Parameter:
589281e9a530SVaclav Hapla . dm - the DM
589381e9a530SVaclav Hapla 
589481e9a530SVaclav Hapla   Output Parameter:
589581e9a530SVaclav Hapla . c - coordinate vector
589681e9a530SVaclav Hapla 
589781e9a530SVaclav Hapla   Level: advanced
589881e9a530SVaclav Hapla 
589981e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
590081e9a530SVaclav Hapla @*/
590181e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
590281e9a530SVaclav Hapla {
590381e9a530SVaclav Hapla   PetscFunctionBegin;
590481e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
590581e9a530SVaclav Hapla   PetscValidPointer(c,2);
590681e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
59076636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
59086636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59096636e97aSMatthew G Knepley }
59106636e97aSMatthew G Knepley 
59112db98f8dSVaclav Hapla /*@
59122db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
59132db98f8dSVaclav Hapla 
59142db98f8dSVaclav Hapla   Not collective
59152db98f8dSVaclav Hapla 
59162db98f8dSVaclav Hapla   Input Parameter:
59172db98f8dSVaclav Hapla + dm - the DM
59182db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
59192db98f8dSVaclav Hapla 
59202db98f8dSVaclav Hapla   Output Parameter:
59212db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
59222db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
59232db98f8dSVaclav Hapla 
59242db98f8dSVaclav Hapla   Note:
59252db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
59262db98f8dSVaclav Hapla 
59272db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
59282db98f8dSVaclav Hapla 
59292db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
59302db98f8dSVaclav Hapla 
59312db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59322db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59332db98f8dSVaclav Hapla 
59342db98f8dSVaclav Hapla   Level: advanced
59352db98f8dSVaclav Hapla 
59362db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
59372db98f8dSVaclav Hapla @*/
59382db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
59392db98f8dSVaclav Hapla {
59402db98f8dSVaclav Hapla   PetscSection        cs, newcs;
59412db98f8dSVaclav Hapla   Vec                 coords;
59422db98f8dSVaclav Hapla   const PetscScalar   *arr;
59432db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
59442db98f8dSVaclav Hapla   PetscInt            n;
59452db98f8dSVaclav Hapla   PetscErrorCode      ierr;
59462db98f8dSVaclav Hapla 
59472db98f8dSVaclav Hapla   PetscFunctionBegin;
59482db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59492db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
59502db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
59512db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
59522db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
59531bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
59541bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
59552db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
59562db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
59572db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
59582db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
59592db98f8dSVaclav Hapla   if (pCoord) {
59602db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
59612db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
59622db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
59632db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5964ad9ac99dSVaclav Hapla   } else {
5965ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
59662db98f8dSVaclav Hapla   }
5967ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5968ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
59692db98f8dSVaclav Hapla   PetscFunctionReturn(0);
59702db98f8dSVaclav Hapla }
59712db98f8dSVaclav Hapla 
5972f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5973f19dbd58SToby Isaac {
5974f19dbd58SToby Isaac   PetscErrorCode ierr;
5975f19dbd58SToby Isaac 
5976f19dbd58SToby Isaac   PetscFunctionBegin;
5977f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5978f19dbd58SToby Isaac   PetscValidPointer(field,2);
5979f19dbd58SToby Isaac   if (!dm->coordinateField) {
5980f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5981f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5982f19dbd58SToby Isaac     }
5983f19dbd58SToby Isaac   }
5984f19dbd58SToby Isaac   *field = dm->coordinateField;
5985f19dbd58SToby Isaac   PetscFunctionReturn(0);
5986f19dbd58SToby Isaac }
5987f19dbd58SToby Isaac 
5988f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5989f19dbd58SToby Isaac {
5990f19dbd58SToby Isaac   PetscErrorCode ierr;
5991f19dbd58SToby Isaac 
5992f19dbd58SToby Isaac   PetscFunctionBegin;
5993f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5994f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5995c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5996f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5997f19dbd58SToby Isaac   dm->coordinateField = field;
5998f19dbd58SToby Isaac   PetscFunctionReturn(0);
5999f19dbd58SToby Isaac }
6000f19dbd58SToby Isaac 
60016636e97aSMatthew G Knepley /*@
60021cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
60036636e97aSMatthew G Knepley 
6004d083f849SBarry Smith   Collective on dm
60056636e97aSMatthew G Knepley 
60066636e97aSMatthew G Knepley   Input Parameter:
60076636e97aSMatthew G Knepley . dm - the DM
60086636e97aSMatthew G Knepley 
60096636e97aSMatthew G Knepley   Output Parameter:
60106636e97aSMatthew G Knepley . cdm - coordinate DM
60116636e97aSMatthew G Knepley 
60126636e97aSMatthew G Knepley   Level: intermediate
60136636e97aSMatthew G Knepley 
60141cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
60156636e97aSMatthew G Knepley @*/
60166636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
60176636e97aSMatthew G Knepley {
60186636e97aSMatthew G Knepley   PetscErrorCode ierr;
60196636e97aSMatthew G Knepley 
60206636e97aSMatthew G Knepley   PetscFunctionBegin;
60216636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60226636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
60236636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6024308f8a94SToby Isaac     DM cdm;
6025308f8a94SToby Isaac 
602682f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6027308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6028308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6029308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6030308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6031308f8a94SToby Isaac     dm->coordinateDM = cdm;
60326636e97aSMatthew G Knepley   }
60336636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
60346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60356636e97aSMatthew G Knepley }
6036e87bb0d3SMatthew G Knepley 
60371cfe2091SMatthew G. Knepley /*@
60381cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
60391cfe2091SMatthew G. Knepley 
6040d083f849SBarry Smith   Logically Collective on dm
60411cfe2091SMatthew G. Knepley 
60421cfe2091SMatthew G. Knepley   Input Parameters:
60431cfe2091SMatthew G. Knepley + dm - the DM
60441cfe2091SMatthew G. Knepley - cdm - coordinate DM
60451cfe2091SMatthew G. Knepley 
60461cfe2091SMatthew G. Knepley   Level: intermediate
60471cfe2091SMatthew G. Knepley 
60481cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
60491cfe2091SMatthew G. Knepley @*/
60501cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
60511cfe2091SMatthew G. Knepley {
60521cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
60531cfe2091SMatthew G. Knepley 
60541cfe2091SMatthew G. Knepley   PetscFunctionBegin;
60551cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60561cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6057f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
60581cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
60591cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
60601cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
60611cfe2091SMatthew G. Knepley }
60621cfe2091SMatthew G. Knepley 
606346e270d4SMatthew G. Knepley /*@
606446e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
606546e270d4SMatthew G. Knepley 
606646e270d4SMatthew G. Knepley   Not Collective
606746e270d4SMatthew G. Knepley 
606846e270d4SMatthew G. Knepley   Input Parameter:
606946e270d4SMatthew G. Knepley . dm - The DM object
607046e270d4SMatthew G. Knepley 
607146e270d4SMatthew G. Knepley   Output Parameter:
607246e270d4SMatthew G. Knepley . dim - The embedding dimension
607346e270d4SMatthew G. Knepley 
607446e270d4SMatthew G. Knepley   Level: intermediate
607546e270d4SMatthew G. Knepley 
607692fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
607746e270d4SMatthew G. Knepley @*/
607846e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
607946e270d4SMatthew G. Knepley {
608046e270d4SMatthew G. Knepley   PetscFunctionBegin;
608146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6082534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
60839a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
60849a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
60859a9a41abSToby Isaac   }
608646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
608746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
608846e270d4SMatthew G. Knepley }
608946e270d4SMatthew G. Knepley 
609046e270d4SMatthew G. Knepley /*@
609146e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
609246e270d4SMatthew G. Knepley 
609346e270d4SMatthew G. Knepley   Not Collective
609446e270d4SMatthew G. Knepley 
609546e270d4SMatthew G. Knepley   Input Parameters:
609646e270d4SMatthew G. Knepley + dm  - The DM object
609746e270d4SMatthew G. Knepley - dim - The embedding dimension
609846e270d4SMatthew G. Knepley 
609946e270d4SMatthew G. Knepley   Level: intermediate
610046e270d4SMatthew G. Knepley 
610192fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
610246e270d4SMatthew G. Knepley @*/
610346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
610446e270d4SMatthew G. Knepley {
6105e5e52638SMatthew G. Knepley   PetscDS        ds;
6106f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6107f17e8794SMatthew G. Knepley 
610846e270d4SMatthew G. Knepley   PetscFunctionBegin;
610946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
611046e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6111e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6112e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
611346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
611446e270d4SMatthew G. Knepley }
611546e270d4SMatthew G. Knepley 
6116e8abe2deSMatthew G. Knepley /*@
6117e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6118e8abe2deSMatthew G. Knepley 
6119d083f849SBarry Smith   Collective on dm
6120e8abe2deSMatthew G. Knepley 
6121e8abe2deSMatthew G. Knepley   Input Parameter:
6122e8abe2deSMatthew G. Knepley . dm - The DM object
6123e8abe2deSMatthew G. Knepley 
6124e8abe2deSMatthew G. Knepley   Output Parameter:
6125e8abe2deSMatthew G. Knepley . section - The PetscSection object
6126e8abe2deSMatthew G. Knepley 
6127e8abe2deSMatthew G. Knepley   Level: intermediate
6128e8abe2deSMatthew G. Knepley 
612992fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6130e8abe2deSMatthew G. Knepley @*/
6131e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6132e8abe2deSMatthew G. Knepley {
6133e8abe2deSMatthew G. Knepley   DM             cdm;
6134e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6135e8abe2deSMatthew G. Knepley 
6136e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6137e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6138e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6139e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
614092fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6141e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6142e8abe2deSMatthew G. Knepley }
6143e8abe2deSMatthew G. Knepley 
6144e8abe2deSMatthew G. Knepley /*@
6145e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6146e8abe2deSMatthew G. Knepley 
6147e8abe2deSMatthew G. Knepley   Not Collective
6148e8abe2deSMatthew G. Knepley 
6149e8abe2deSMatthew G. Knepley   Input Parameters:
6150e8abe2deSMatthew G. Knepley + dm      - The DM object
615146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6152e8abe2deSMatthew G. Knepley - section - The PetscSection object
6153e8abe2deSMatthew G. Knepley 
6154e8abe2deSMatthew G. Knepley   Level: intermediate
6155e8abe2deSMatthew G. Knepley 
615692fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6157e8abe2deSMatthew G. Knepley @*/
615846e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6159e8abe2deSMatthew G. Knepley {
6160e8abe2deSMatthew G. Knepley   DM             cdm;
6161e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6162e8abe2deSMatthew G. Knepley 
6163e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6164e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
616546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6166e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
616792fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
616846e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
61694c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
617046e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
617146e270d4SMatthew G. Knepley 
617246e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
617346e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
617446e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
617546e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
617646e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
617746e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
617846e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
617946e270d4SMatthew G. Knepley     }
6180ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
618146e270d4SMatthew G. Knepley   }
6182e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6183e8abe2deSMatthew G. Knepley }
6184e8abe2deSMatthew G. Knepley 
61855dc8c3f7SMatthew G. Knepley /*@C
618690b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
61875dc8c3f7SMatthew G. Knepley 
61885dc8c3f7SMatthew G. Knepley   Input Parameters:
618990b157c4SStefano Zampini . dm      - The DM object
619090b157c4SStefano Zampini 
619190b157c4SStefano Zampini   Output Parameters:
619290b157c4SStefano Zampini + per     - Whether the DM is periodic or not
61935dc8c3f7SMatthew 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
61945dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
61955dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
61965dc8c3f7SMatthew G. Knepley 
61975dc8c3f7SMatthew G. Knepley   Level: developer
61985dc8c3f7SMatthew G. Knepley 
61995dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
62005dc8c3f7SMatthew G. Knepley @*/
620190b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6202c6b900c6SMatthew G. Knepley {
6203c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6204c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
620590b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6206c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6207c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
62085dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6209c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6210c6b900c6SMatthew G. Knepley }
6211c6b900c6SMatthew G. Knepley 
62125dc8c3f7SMatthew G. Knepley /*@C
62135dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
62145dc8c3f7SMatthew G. Knepley 
62155dc8c3f7SMatthew G. Knepley   Input Parameters:
62165dc8c3f7SMatthew G. Knepley + dm      - The DM object
621790b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
62185dc8c3f7SMatthew 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
62195dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
62205dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
62215dc8c3f7SMatthew G. Knepley 
62225dc8c3f7SMatthew G. Knepley   Level: developer
62235dc8c3f7SMatthew G. Knepley 
62245dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
62255dc8c3f7SMatthew G. Knepley @*/
622690b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6227c6b900c6SMatthew G. Knepley {
6228c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6229c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6230c6b900c6SMatthew G. Knepley 
6231c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6232c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
623390b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6234412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6235412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6236412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
62375dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
623890b157c4SStefano Zampini   if (maxCell) {
6239412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6240412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6241412e9a14SMatthew G. Knepley   }
6242412e9a14SMatthew G. Knepley   if (L) {
6243412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6244412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6245412e9a14SMatthew G. Knepley   }
6246412e9a14SMatthew G. Knepley   if (bd) {
6247412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6248412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
624990b157c4SStefano Zampini   }
6250072d7d67SStefano Zampini   dm->periodic = per;
6251c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6252c6b900c6SMatthew G. Knepley }
6253c6b900c6SMatthew G. Knepley 
62542e17dfb7SMatthew G. Knepley /*@
62552e17dfb7SMatthew 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.
62562e17dfb7SMatthew G. Knepley 
62572e17dfb7SMatthew G. Knepley   Input Parameters:
62582e17dfb7SMatthew G. Knepley + dm     - The DM
625965da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
626065da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
62612e17dfb7SMatthew G. Knepley 
62622e17dfb7SMatthew G. Knepley   Output Parameter:
62632e17dfb7SMatthew G. Knepley . out - The localized coordinate point
62642e17dfb7SMatthew G. Knepley 
62652e17dfb7SMatthew G. Knepley   Level: developer
62662e17dfb7SMatthew G. Knepley 
62672e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
62682e17dfb7SMatthew G. Knepley @*/
626965da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
62702e17dfb7SMatthew G. Knepley {
62712e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
62722e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
62732e17dfb7SMatthew G. Knepley 
62742e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
62752e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
62762e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
62772e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
62782e17dfb7SMatthew G. Knepley   } else {
627965da65dcSMatthew G. Knepley     if (endpoint) {
628065da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6281da3333bfSMatthew 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)) {
6282da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
628365da65dcSMatthew G. Knepley         } else {
6284da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
628565da65dcSMatthew G. Knepley         }
628665da65dcSMatthew G. Knepley       }
628765da65dcSMatthew G. Knepley     } else {
62882e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
62891118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
62902e17dfb7SMatthew G. Knepley       }
62912e17dfb7SMatthew G. Knepley     }
629265da65dcSMatthew G. Knepley   }
62932e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
62942e17dfb7SMatthew G. Knepley }
62952e17dfb7SMatthew G. Knepley 
62962e17dfb7SMatthew G. Knepley /*
62972e17dfb7SMatthew 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.
62982e17dfb7SMatthew G. Knepley 
62992e17dfb7SMatthew G. Knepley   Input Parameters:
63002e17dfb7SMatthew G. Knepley + dm     - The DM
63012e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
63022e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
63032e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
63042e17dfb7SMatthew G. Knepley 
63052e17dfb7SMatthew G. Knepley   Output Parameter:
63062e17dfb7SMatthew G. Knepley . out - The localized coordinate point
63072e17dfb7SMatthew G. Knepley 
63082e17dfb7SMatthew G. Knepley   Level: developer
63092e17dfb7SMatthew G. Knepley 
63102e17dfb7SMatthew 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
63112e17dfb7SMatthew G. Knepley 
63122e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
63132e17dfb7SMatthew G. Knepley */
63142e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
63152e17dfb7SMatthew G. Knepley {
63162e17dfb7SMatthew G. Knepley   PetscInt d;
63172e17dfb7SMatthew G. Knepley 
63182e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63192e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
63202e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
63212e17dfb7SMatthew G. Knepley   } else {
63222e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6323908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
63242e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
63252e17dfb7SMatthew G. Knepley       } else {
63262e17dfb7SMatthew G. Knepley         out[d] = in[d];
63272e17dfb7SMatthew G. Knepley       }
63282e17dfb7SMatthew G. Knepley     }
63292e17dfb7SMatthew G. Knepley   }
63302e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63312e17dfb7SMatthew G. Knepley }
63322e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
63332e17dfb7SMatthew G. Knepley {
63342e17dfb7SMatthew G. Knepley   PetscInt d;
63352e17dfb7SMatthew G. Knepley 
63362e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63372e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
63382e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
63392e17dfb7SMatthew G. Knepley   } else {
63402e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6341908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
63422e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
63432e17dfb7SMatthew G. Knepley       } else {
63442e17dfb7SMatthew G. Knepley         out[d] = in[d];
63452e17dfb7SMatthew G. Knepley       }
63462e17dfb7SMatthew G. Knepley     }
63472e17dfb7SMatthew G. Knepley   }
63482e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63492e17dfb7SMatthew G. Knepley }
63502e17dfb7SMatthew G. Knepley 
63512e17dfb7SMatthew G. Knepley /*
63522e17dfb7SMatthew 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.
63532e17dfb7SMatthew G. Knepley 
63542e17dfb7SMatthew G. Knepley   Input Parameters:
63552e17dfb7SMatthew G. Knepley + dm     - The DM
63562e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
63572e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
63582e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
63592e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
63602e17dfb7SMatthew G. Knepley 
63612e17dfb7SMatthew G. Knepley   Output Parameter:
63622e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
63632e17dfb7SMatthew G. Knepley 
63642e17dfb7SMatthew G. Knepley   Level: developer
63652e17dfb7SMatthew G. Knepley 
63662e17dfb7SMatthew 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
63672e17dfb7SMatthew G. Knepley 
63682e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
63692e17dfb7SMatthew G. Knepley */
63702e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
63712e17dfb7SMatthew G. Knepley {
63722e17dfb7SMatthew G. Knepley   PetscInt d;
63732e17dfb7SMatthew G. Knepley 
63742e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63752e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
63762e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
63772e17dfb7SMatthew G. Knepley   } else {
63782e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6379412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6380412e9a14SMatthew G. Knepley 
6381412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6382412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6383412e9a14SMatthew G. Knepley 
6384412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6385412e9a14SMatthew 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]));
6386412e9a14SMatthew G. Knepley         out[d] += newCoord;
63872e17dfb7SMatthew G. Knepley       } else {
63882e17dfb7SMatthew G. Knepley         out[d] += in[d];
63892e17dfb7SMatthew G. Knepley       }
63902e17dfb7SMatthew G. Knepley     }
63912e17dfb7SMatthew G. Knepley   }
63922e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63932e17dfb7SMatthew G. Knepley }
63942e17dfb7SMatthew G. Knepley 
639536447a5eSToby Isaac /*@
63968f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
63978f700142SStefano Zampini 
63988f700142SStefano Zampini   Not collective
639936447a5eSToby Isaac 
640036447a5eSToby Isaac   Input Parameter:
640136447a5eSToby Isaac . dm - The DM
640236447a5eSToby Isaac 
640336447a5eSToby Isaac   Output Parameter:
640436447a5eSToby Isaac   areLocalized - True if localized
640536447a5eSToby Isaac 
640636447a5eSToby Isaac   Level: developer
640736447a5eSToby Isaac 
64088f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
640936447a5eSToby Isaac @*/
64108f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
641136447a5eSToby Isaac {
641236447a5eSToby Isaac   DM             cdm;
641336447a5eSToby Isaac   PetscSection   coordSection;
641446a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
641546a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
641636447a5eSToby Isaac   PetscErrorCode ierr;
641736447a5eSToby Isaac 
641836447a5eSToby Isaac   PetscFunctionBegin;
641936447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6420534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
64218b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
642246a3a80fSLisandro Dalcin 
642336447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
642436447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
642546a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
64269f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
642746a3a80fSLisandro Dalcin 
64289f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
642946a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
643036447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
643136447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
643246a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
643346a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
643436447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
643546a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
643636447a5eSToby Isaac   }
64378f700142SStefano Zampini   *areLocalized = alreadyLocalized;
643836447a5eSToby Isaac   PetscFunctionReturn(0);
643936447a5eSToby Isaac }
644036447a5eSToby Isaac 
64418f700142SStefano Zampini /*@
64428f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
64438f700142SStefano Zampini 
64448f700142SStefano Zampini   Collective on dm
64458f700142SStefano Zampini 
64468f700142SStefano Zampini   Input Parameter:
64478f700142SStefano Zampini . dm - The DM
64488f700142SStefano Zampini 
64498f700142SStefano Zampini   Output Parameter:
64508f700142SStefano Zampini   areLocalized - True if localized
64518f700142SStefano Zampini 
64528f700142SStefano Zampini   Level: developer
64538f700142SStefano Zampini 
64548f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
64558f700142SStefano Zampini @*/
64568f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
64578f700142SStefano Zampini {
64588f700142SStefano Zampini   PetscBool      localized;
64598f700142SStefano Zampini   PetscErrorCode ierr;
64608f700142SStefano Zampini 
64618f700142SStefano Zampini   PetscFunctionBegin;
64628f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6463534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
64648f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
64658f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
64668f700142SStefano Zampini   PetscFunctionReturn(0);
64678f700142SStefano Zampini }
646836447a5eSToby Isaac 
64692e17dfb7SMatthew G. Knepley /*@
6470492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
64712e17dfb7SMatthew G. Knepley 
64728f700142SStefano Zampini   Collective on dm
64738f700142SStefano Zampini 
64742e17dfb7SMatthew G. Knepley   Input Parameter:
64752e17dfb7SMatthew G. Knepley . dm - The DM
64762e17dfb7SMatthew G. Knepley 
64772e17dfb7SMatthew G. Knepley   Level: developer
64782e17dfb7SMatthew G. Knepley 
64798f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
64802e17dfb7SMatthew G. Knepley @*/
64812e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
64822e17dfb7SMatthew G. Knepley {
64832e17dfb7SMatthew G. Knepley   DM             cdm;
64842e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
64852e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
64863e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
64873e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6488e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
64893e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
64903e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
64912e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
64922e17dfb7SMatthew G. Knepley 
64932e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
64942e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
649592c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6496f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6497f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6498f7cbd40bSStefano Zampini 
64992e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
65002e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
65012e17dfb7SMatthew G. Knepley   {
65022e17dfb7SMatthew G. Knepley     PetscBool isplex;
65032e17dfb7SMatthew G. Knepley 
65042e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
65052e17dfb7SMatthew G. Knepley     if (isplex) {
65062e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
65073e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
650869291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
65093e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
65103e922f36SToby Isaac       newStart = vStart;
65113e922f36SToby Isaac       newEnd   = vEnd;
65123e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
65133e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
65143e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
65153e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
65163e922f36SToby Isaac       }
65172e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
65182e17dfb7SMatthew G. Knepley   }
65192e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
652043eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
65212e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
65223e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6523e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
65243e922f36SToby Isaac 
65252e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
65262e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
65272e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
65282e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
65293e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
65303e922f36SToby Isaac 
653169291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
65323e922f36SToby Isaac   localized = &anchor[bs];
65333e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
65343e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
65353e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
65363e922f36SToby Isaac 
65373e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
65383e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
65393e922f36SToby Isaac       PetscInt     b;
65403e922f36SToby Isaac 
65413e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
65423e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
65433e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
65443e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
65453e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
65463e922f36SToby Isaac         for (b = 0; b < bs; b++) {
65473e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
65483e922f36SToby Isaac         }
65493e922f36SToby Isaac         if (b < bs) break;
65503e922f36SToby Isaac       }
65513e922f36SToby Isaac       if (d < dof/bs) {
65523e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
65533e922f36SToby Isaac           PetscInt cdof;
65543e922f36SToby Isaac 
65553e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
65563e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
65573e922f36SToby Isaac         }
65583e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
65593e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
65603e922f36SToby Isaac       }
65613e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
65623e922f36SToby Isaac     }
65633e922f36SToby Isaac   }
65643e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
65653e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
656669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
65673e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
656869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
65693e922f36SToby Isaac     PetscFunctionReturn(0);
65703e922f36SToby Isaac   }
65712e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
65722e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
65732e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
65742e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
65752e17dfb7SMatthew G. Knepley   }
65762e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
65772e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6578c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
65792e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
65802e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
65812e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
65822e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6583c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
65842e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
65852e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
65862e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
65872e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
65882e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
65892e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
65902e17dfb7SMatthew G. Knepley   }
65913e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
65923e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
65933e922f36SToby Isaac 
65942e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
65952e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
65963e922f36SToby Isaac       PetscInt     b, cdof;
65972e17dfb7SMatthew G. Knepley 
65983e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
65993e922f36SToby Isaac       if (!cdof) continue;
66002e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
66012e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
66022e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
66032e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
66042e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
66052e17dfb7SMatthew G. Knepley     }
66063e922f36SToby Isaac   }
660769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
660869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6609c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
66102e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
66112e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
66122e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
66132e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
66142e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
66152e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66162e17dfb7SMatthew G. Knepley }
66172e17dfb7SMatthew G. Knepley 
6618e87bb0d3SMatthew G Knepley /*@
66193a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6620e87bb0d3SMatthew G Knepley 
6621d083f849SBarry Smith   Collective on v (see explanation below)
6622e87bb0d3SMatthew G Knepley 
6623e87bb0d3SMatthew G Knepley   Input Parameters:
6624e87bb0d3SMatthew G Knepley + dm - The DM
66253a93e3b7SToby Isaac . v - The Vec of points
662662a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
66273a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6628e87bb0d3SMatthew G Knepley 
662961e3bb9bSMatthew G Knepley   Output Parameter:
663062a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
663162a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
66323a93e3b7SToby Isaac 
6633e87bb0d3SMatthew G Knepley 
6634e87bb0d3SMatthew G Knepley   Level: developer
663561e3bb9bSMatthew G Knepley 
663662a38674SMatthew G. Knepley   Notes:
66373a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
663862a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
66393a93e3b7SToby Isaac 
66403a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
664162a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
66423a93e3b7SToby Isaac 
66433a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
66443a93e3b7SToby Isaac 
664562a38674SMatthew G. Knepley $    const PetscSFNode *cells;
664662a38674SMatthew G. Knepley $    PetscInt           nFound;
6647a6216909SToby Isaac $    const PetscInt    *found;
664862a38674SMatthew G. Knepley $
6649a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
66503a93e3b7SToby Isaac 
66513a93e3b7SToby 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
66523a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
66533a93e3b7SToby Isaac 
665462a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
665561e3bb9bSMatthew G Knepley @*/
665662a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6657e87bb0d3SMatthew G Knepley {
6658735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6659735aa83eSMatthew G Knepley 
6660e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6661e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6662e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6663e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
66643a93e3b7SToby Isaac   if (*cellSF) {
66653a93e3b7SToby Isaac     PetscMPIInt result;
66663a93e3b7SToby Isaac 
6667e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6668a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
66693a93e3b7SToby 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");
6670e0fc9d1bSMatthew G. Knepley   } else {
66713a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
66723a93e3b7SToby Isaac   }
6673b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
667447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
667562a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
667647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6677e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6678e87bb0d3SMatthew G Knepley }
667914f150ffSMatthew G. Knepley 
6680f4d763aaSMatthew G. Knepley /*@
6681f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6682f4d763aaSMatthew G. Knepley 
66838f700142SStefano Zampini   Collective on dm
66848f700142SStefano Zampini 
6685f4d763aaSMatthew G. Knepley   Input Parameter:
6686f4d763aaSMatthew G. Knepley . dm - The original DM
6687f4d763aaSMatthew G. Knepley 
6688f4d763aaSMatthew G. Knepley   Output Parameter:
6689f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6690f4d763aaSMatthew G. Knepley 
6691f4d763aaSMatthew G. Knepley   Level: intermediate
6692f4d763aaSMatthew G. Knepley 
6693e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6694f4d763aaSMatthew G. Knepley @*/
669514f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
669614f150ffSMatthew G. Knepley {
6697c26acbdeSMatthew G. Knepley   PetscSection   section;
66982d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
669914f150ffSMatthew G. Knepley   PetscErrorCode ierr;
670014f150ffSMatthew G. Knepley 
670114f150ffSMatthew G. Knepley   PetscFunctionBegin;
670214f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
670314f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
670492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6705c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6706127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
67072d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6708c26acbdeSMatthew G. Knepley     *odm = dm;
6709c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6710c26acbdeSMatthew G. Knepley   }
671114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6712c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
671314f150ffSMatthew G. Knepley     PetscSF      sf;
671414f150ffSMatthew G. Knepley 
671514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6716e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
671714f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
671892fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
671914f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
672014f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
672115b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6722e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
672314f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
672414f150ffSMatthew G. Knepley   }
672514f150ffSMatthew G. Knepley   *odm = dm->dmBC;
672614f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
672714f150ffSMatthew G. Knepley }
6728f4d763aaSMatthew G. Knepley 
6729f4d763aaSMatthew G. Knepley /*@
6730cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6731f4d763aaSMatthew G. Knepley 
6732f4d763aaSMatthew G. Knepley   Input Parameter:
6733f4d763aaSMatthew G. Knepley . dm - The original DM
6734f4d763aaSMatthew G. Knepley 
6735cdb7a50dSMatthew G. Knepley   Output Parameters:
6736cdb7a50dSMatthew G. Knepley + num - The output sequence number
6737cdb7a50dSMatthew G. Knepley - val - The output sequence value
6738f4d763aaSMatthew G. Knepley 
6739f4d763aaSMatthew G. Knepley   Level: intermediate
6740f4d763aaSMatthew G. Knepley 
6741f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6742f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6743f4d763aaSMatthew G. Knepley 
6744f4d763aaSMatthew G. Knepley .seealso: VecView()
6745f4d763aaSMatthew G. Knepley @*/
6746cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6747f4d763aaSMatthew G. Knepley {
6748f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6749f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6750534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6751534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6752f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6753f4d763aaSMatthew G. Knepley }
6754f4d763aaSMatthew G. Knepley 
6755f4d763aaSMatthew G. Knepley /*@
6756cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6757f4d763aaSMatthew G. Knepley 
6758f4d763aaSMatthew G. Knepley   Input Parameters:
6759f4d763aaSMatthew G. Knepley + dm - The original DM
6760cdb7a50dSMatthew G. Knepley . num - The output sequence number
6761cdb7a50dSMatthew G. Knepley - val - The output sequence value
6762f4d763aaSMatthew G. Knepley 
6763f4d763aaSMatthew G. Knepley   Level: intermediate
6764f4d763aaSMatthew G. Knepley 
6765f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6766f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6767f4d763aaSMatthew G. Knepley 
6768f4d763aaSMatthew G. Knepley .seealso: VecView()
6769f4d763aaSMatthew G. Knepley @*/
6770cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6771f4d763aaSMatthew G. Knepley {
6772f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6773f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6774f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6775cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6776cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6777cdb7a50dSMatthew G. Knepley }
6778cdb7a50dSMatthew G. Knepley 
6779cdb7a50dSMatthew G. Knepley /*@C
6780cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6781cdb7a50dSMatthew G. Knepley 
6782cdb7a50dSMatthew G. Knepley   Input Parameters:
6783cdb7a50dSMatthew G. Knepley + dm   - The original DM
6784cdb7a50dSMatthew G. Knepley . name - The sequence name
6785cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6786cdb7a50dSMatthew G. Knepley 
6787cdb7a50dSMatthew G. Knepley   Output Parameter:
6788cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6789cdb7a50dSMatthew G. Knepley 
6790cdb7a50dSMatthew G. Knepley   Level: intermediate
6791cdb7a50dSMatthew G. Knepley 
6792cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6793cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6794cdb7a50dSMatthew G. Knepley 
6795cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6796cdb7a50dSMatthew G. Knepley @*/
6797cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6798cdb7a50dSMatthew G. Knepley {
6799cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6800cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6801cdb7a50dSMatthew G. Knepley 
6802cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6803cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6804cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6805534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6806cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6807cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6808cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6809cdb7a50dSMatthew G. Knepley     PetscScalar value;
6810cdb7a50dSMatthew G. Knepley 
681139d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
68124aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6813cdb7a50dSMatthew G. Knepley #endif
6814cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6815f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6816f4d763aaSMatthew G. Knepley }
68178e4ac7eaSMatthew G. Knepley 
68188e4ac7eaSMatthew G. Knepley /*@
68198e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
68208e4ac7eaSMatthew G. Knepley 
68218e4ac7eaSMatthew G. Knepley   Not collective
68228e4ac7eaSMatthew G. Knepley 
68238e4ac7eaSMatthew G. Knepley   Input Parameter:
68248e4ac7eaSMatthew G. Knepley . dm - The DM
68258e4ac7eaSMatthew G. Knepley 
68268e4ac7eaSMatthew G. Knepley   Output Parameter:
68278e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
68288e4ac7eaSMatthew G. Knepley 
68298e4ac7eaSMatthew G. Knepley   Level: beginner
68308e4ac7eaSMatthew G. Knepley 
68318e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
68328e4ac7eaSMatthew G. Knepley @*/
68338e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
68348e4ac7eaSMatthew G. Knepley {
68358e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
68368e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6837534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
68388e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
68398e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
68408e4ac7eaSMatthew G. Knepley }
68418e4ac7eaSMatthew G. Knepley 
68428e4ac7eaSMatthew G. Knepley /*@
68435d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
68448e4ac7eaSMatthew G. Knepley 
68458e4ac7eaSMatthew G. Knepley   Collective on dm
68468e4ac7eaSMatthew G. Knepley 
68478e4ac7eaSMatthew G. Knepley   Input Parameters:
68488e4ac7eaSMatthew G. Knepley + dm - The DM
68498e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
68508e4ac7eaSMatthew G. Knepley 
68515d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
68525d3b26e6SMatthew G. Knepley 
68538e4ac7eaSMatthew G. Knepley   Level: beginner
68548e4ac7eaSMatthew G. Knepley 
68555d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
68568e4ac7eaSMatthew G. Knepley @*/
68578e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
68588e4ac7eaSMatthew G. Knepley {
68598e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
68608e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68618833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
68628e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
68638e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
68648e4ac7eaSMatthew G. Knepley }
6865c58f1c22SToby Isaac 
6866c58f1c22SToby Isaac 
6867c58f1c22SToby Isaac /*@C
6868c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6869c58f1c22SToby Isaac 
6870c58f1c22SToby Isaac   Not Collective
6871c58f1c22SToby Isaac 
6872c58f1c22SToby Isaac   Input Parameters:
6873c58f1c22SToby Isaac + dm   - The DM object
6874c58f1c22SToby Isaac - name - The label name
6875c58f1c22SToby Isaac 
6876c58f1c22SToby Isaac   Level: intermediate
6877c58f1c22SToby Isaac 
6878c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6879c58f1c22SToby Isaac @*/
6880c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6881c58f1c22SToby Isaac {
68825d80c0bfSVaclav Hapla   PetscBool      flg;
68835d80c0bfSVaclav Hapla   DMLabel        label;
6884c58f1c22SToby Isaac   PetscErrorCode ierr;
6885c58f1c22SToby Isaac 
6886c58f1c22SToby Isaac   PetscFunctionBegin;
6887c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6888c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
68895d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
6890c58f1c22SToby Isaac   if (!flg) {
68915d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
68925d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
68935d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
6894c58f1c22SToby Isaac   }
6895c58f1c22SToby Isaac   PetscFunctionReturn(0);
6896c58f1c22SToby Isaac }
6897c58f1c22SToby Isaac 
6898c58f1c22SToby Isaac /*@C
6899c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6900c58f1c22SToby Isaac 
6901c58f1c22SToby Isaac   Not Collective
6902c58f1c22SToby Isaac 
6903c58f1c22SToby Isaac   Input Parameters:
6904c58f1c22SToby Isaac + dm   - The DM object
6905c58f1c22SToby Isaac . name - The label name
6906c58f1c22SToby Isaac - point - The mesh point
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac   Output Parameter:
6909c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6910c58f1c22SToby Isaac 
6911c58f1c22SToby Isaac   Level: beginner
6912c58f1c22SToby Isaac 
6913c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6914c58f1c22SToby Isaac @*/
6915c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6916c58f1c22SToby Isaac {
6917c58f1c22SToby Isaac   DMLabel        label;
6918c58f1c22SToby Isaac   PetscErrorCode ierr;
6919c58f1c22SToby Isaac 
6920c58f1c22SToby Isaac   PetscFunctionBegin;
6921c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6922c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6923c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
692413903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6925c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6926c58f1c22SToby Isaac   PetscFunctionReturn(0);
6927c58f1c22SToby Isaac }
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac /*@C
6930c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6931c58f1c22SToby Isaac 
6932c58f1c22SToby Isaac   Not Collective
6933c58f1c22SToby Isaac 
6934c58f1c22SToby Isaac   Input Parameters:
6935c58f1c22SToby Isaac + dm   - The DM object
6936c58f1c22SToby Isaac . name - The label name
6937c58f1c22SToby Isaac . point - The mesh point
6938c58f1c22SToby Isaac - value - The label value for this point
6939c58f1c22SToby Isaac 
6940c58f1c22SToby Isaac   Output Parameter:
6941c58f1c22SToby Isaac 
6942c58f1c22SToby Isaac   Level: beginner
6943c58f1c22SToby Isaac 
6944c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6945c58f1c22SToby Isaac @*/
6946c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6947c58f1c22SToby Isaac {
6948c58f1c22SToby Isaac   DMLabel        label;
6949c58f1c22SToby Isaac   PetscErrorCode ierr;
6950c58f1c22SToby Isaac 
6951c58f1c22SToby Isaac   PetscFunctionBegin;
6952c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6953c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6954c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6955c58f1c22SToby Isaac   if (!label) {
6956c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6957c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6958c58f1c22SToby Isaac   }
6959c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6960c58f1c22SToby Isaac   PetscFunctionReturn(0);
6961c58f1c22SToby Isaac }
6962c58f1c22SToby Isaac 
6963c58f1c22SToby Isaac /*@C
6964c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6965c58f1c22SToby Isaac 
6966c58f1c22SToby Isaac   Not Collective
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   Input Parameters:
6969c58f1c22SToby Isaac + dm   - The DM object
6970c58f1c22SToby Isaac . name - The label name
6971c58f1c22SToby Isaac . point - The mesh point
6972c58f1c22SToby Isaac - value - The label value for this point
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   Output Parameter:
6975c58f1c22SToby Isaac 
6976c58f1c22SToby Isaac   Level: beginner
6977c58f1c22SToby Isaac 
6978c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6979c58f1c22SToby Isaac @*/
6980c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6981c58f1c22SToby Isaac {
6982c58f1c22SToby Isaac   DMLabel        label;
6983c58f1c22SToby Isaac   PetscErrorCode ierr;
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac   PetscFunctionBegin;
6986c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6987c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6988c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6989c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6990c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6991c58f1c22SToby Isaac   PetscFunctionReturn(0);
6992c58f1c22SToby Isaac }
6993c58f1c22SToby Isaac 
6994c58f1c22SToby Isaac /*@C
6995c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac   Not Collective
6998c58f1c22SToby Isaac 
6999c58f1c22SToby Isaac   Input Parameters:
7000c58f1c22SToby Isaac + dm   - The DM object
7001c58f1c22SToby Isaac - name - The label name
7002c58f1c22SToby Isaac 
7003c58f1c22SToby Isaac   Output Parameter:
7004c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac   Level: beginner
7007c58f1c22SToby Isaac 
7008df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7009c58f1c22SToby Isaac @*/
7010c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7011c58f1c22SToby Isaac {
7012c58f1c22SToby Isaac   DMLabel        label;
7013c58f1c22SToby Isaac   PetscErrorCode ierr;
7014c58f1c22SToby Isaac 
7015c58f1c22SToby Isaac   PetscFunctionBegin;
7016c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7017c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7018534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7019c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7020c58f1c22SToby Isaac   *size = 0;
7021c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7022c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7023c58f1c22SToby Isaac   PetscFunctionReturn(0);
7024c58f1c22SToby Isaac }
7025c58f1c22SToby Isaac 
7026c58f1c22SToby Isaac /*@C
7027c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac   Not Collective
7030c58f1c22SToby Isaac 
7031c58f1c22SToby Isaac   Input Parameters:
7032c58f1c22SToby Isaac + mesh - The DM object
7033c58f1c22SToby Isaac - name - The label name
7034c58f1c22SToby Isaac 
7035c58f1c22SToby Isaac   Output Parameter:
7036c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7037c58f1c22SToby Isaac 
7038c58f1c22SToby Isaac   Level: beginner
7039c58f1c22SToby Isaac 
7040c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7041c58f1c22SToby Isaac @*/
7042c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7043c58f1c22SToby Isaac {
7044c58f1c22SToby Isaac   DMLabel        label;
7045c58f1c22SToby Isaac   PetscErrorCode ierr;
7046c58f1c22SToby Isaac 
7047c58f1c22SToby Isaac   PetscFunctionBegin;
7048c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7049c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7050c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7051c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7052c58f1c22SToby Isaac   *ids = NULL;
7053dab2e251SBlaise Bourdin  if (label) {
7054c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7055dab2e251SBlaise Bourdin   } else {
7056dab2e251SBlaise Bourdin     /* returning an empty IS */
7057dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7058dab2e251SBlaise Bourdin   }
7059c58f1c22SToby Isaac   PetscFunctionReturn(0);
7060c58f1c22SToby Isaac }
7061c58f1c22SToby Isaac 
7062c58f1c22SToby Isaac /*@C
7063c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Not Collective
7066c58f1c22SToby Isaac 
7067c58f1c22SToby Isaac   Input Parameters:
7068c58f1c22SToby Isaac + dm - The DM object
7069c58f1c22SToby Isaac . name - The label name
7070c58f1c22SToby Isaac - value - The stratum value
7071c58f1c22SToby Isaac 
7072c58f1c22SToby Isaac   Output Parameter:
7073c58f1c22SToby Isaac . size - The stratum size
7074c58f1c22SToby Isaac 
7075c58f1c22SToby Isaac   Level: beginner
7076c58f1c22SToby Isaac 
7077c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7078c58f1c22SToby Isaac @*/
7079c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7080c58f1c22SToby Isaac {
7081c58f1c22SToby Isaac   DMLabel        label;
7082c58f1c22SToby Isaac   PetscErrorCode ierr;
7083c58f1c22SToby Isaac 
7084c58f1c22SToby Isaac   PetscFunctionBegin;
7085c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7086c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7087534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7088c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7089c58f1c22SToby Isaac   *size = 0;
7090c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7091c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7092c58f1c22SToby Isaac   PetscFunctionReturn(0);
7093c58f1c22SToby Isaac }
7094c58f1c22SToby Isaac 
7095c58f1c22SToby Isaac /*@C
7096c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7097c58f1c22SToby Isaac 
7098c58f1c22SToby Isaac   Not Collective
7099c58f1c22SToby Isaac 
7100c58f1c22SToby Isaac   Input Parameters:
7101c58f1c22SToby Isaac + dm - The DM object
7102c58f1c22SToby Isaac . name - The label name
7103c58f1c22SToby Isaac - value - The stratum value
7104c58f1c22SToby Isaac 
7105c58f1c22SToby Isaac   Output Parameter:
7106c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7107c58f1c22SToby Isaac 
7108c58f1c22SToby Isaac   Level: beginner
7109c58f1c22SToby Isaac 
7110c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7111c58f1c22SToby Isaac @*/
7112c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7113c58f1c22SToby Isaac {
7114c58f1c22SToby Isaac   DMLabel        label;
7115c58f1c22SToby Isaac   PetscErrorCode ierr;
7116c58f1c22SToby Isaac 
7117c58f1c22SToby Isaac   PetscFunctionBegin;
7118c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7119c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7120c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7121c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7122c58f1c22SToby Isaac   *points = NULL;
7123c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7124c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7125c58f1c22SToby Isaac   PetscFunctionReturn(0);
7126c58f1c22SToby Isaac }
7127c58f1c22SToby Isaac 
71284de306b1SToby Isaac /*@C
71299044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
71304de306b1SToby Isaac 
71314de306b1SToby Isaac   Not Collective
71324de306b1SToby Isaac 
71334de306b1SToby Isaac   Input Parameters:
71344de306b1SToby Isaac + dm - The DM object
71354de306b1SToby Isaac . name - The label name
71364de306b1SToby Isaac . value - The stratum value
71374de306b1SToby Isaac - points - The stratum points
71384de306b1SToby Isaac 
71394de306b1SToby Isaac   Level: beginner
71404de306b1SToby Isaac 
71414de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
71424de306b1SToby Isaac @*/
71434de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
71444de306b1SToby Isaac {
71454de306b1SToby Isaac   DMLabel        label;
71464de306b1SToby Isaac   PetscErrorCode ierr;
71474de306b1SToby Isaac 
71484de306b1SToby Isaac   PetscFunctionBegin;
71494de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71504de306b1SToby Isaac   PetscValidCharPointer(name, 2);
71514de306b1SToby Isaac   PetscValidPointer(points, 4);
71524de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
71534de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
71544de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
71554de306b1SToby Isaac   PetscFunctionReturn(0);
71564de306b1SToby Isaac }
71574de306b1SToby Isaac 
7158c58f1c22SToby Isaac /*@C
7159c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7160c58f1c22SToby Isaac 
7161c58f1c22SToby Isaac   Not Collective
7162c58f1c22SToby Isaac 
7163c58f1c22SToby Isaac   Input Parameters:
7164c58f1c22SToby Isaac + dm   - The DM object
7165c58f1c22SToby Isaac . name - The label name
7166c58f1c22SToby Isaac - value - The label value for this point
7167c58f1c22SToby Isaac 
7168c58f1c22SToby Isaac   Output Parameter:
7169c58f1c22SToby Isaac 
7170c58f1c22SToby Isaac   Level: beginner
7171c58f1c22SToby Isaac 
7172c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7173c58f1c22SToby Isaac @*/
7174c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7175c58f1c22SToby Isaac {
7176c58f1c22SToby Isaac   DMLabel        label;
7177c58f1c22SToby Isaac   PetscErrorCode ierr;
7178c58f1c22SToby Isaac 
7179c58f1c22SToby Isaac   PetscFunctionBegin;
7180c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7181c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7182c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7183c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7184c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7185c58f1c22SToby Isaac   PetscFunctionReturn(0);
7186c58f1c22SToby Isaac }
7187c58f1c22SToby Isaac 
7188c58f1c22SToby Isaac /*@
7189c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7190c58f1c22SToby Isaac 
7191c58f1c22SToby Isaac   Not Collective
7192c58f1c22SToby Isaac 
7193c58f1c22SToby Isaac   Input Parameter:
7194c58f1c22SToby Isaac . dm   - The DM object
7195c58f1c22SToby Isaac 
7196c58f1c22SToby Isaac   Output Parameter:
7197c58f1c22SToby Isaac . numLabels - the number of Labels
7198c58f1c22SToby Isaac 
7199c58f1c22SToby Isaac   Level: intermediate
7200c58f1c22SToby Isaac 
7201c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7202c58f1c22SToby Isaac @*/
7203c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7204c58f1c22SToby Isaac {
72055d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7206c58f1c22SToby Isaac   PetscInt  n    = 0;
7207c58f1c22SToby Isaac 
7208c58f1c22SToby Isaac   PetscFunctionBegin;
7209c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7210534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7211c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7212c58f1c22SToby Isaac   *numLabels = n;
7213c58f1c22SToby Isaac   PetscFunctionReturn(0);
7214c58f1c22SToby Isaac }
7215c58f1c22SToby Isaac 
7216c58f1c22SToby Isaac /*@C
7217c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7218c58f1c22SToby Isaac 
7219c58f1c22SToby Isaac   Not Collective
7220c58f1c22SToby Isaac 
7221c58f1c22SToby Isaac   Input Parameters:
7222c58f1c22SToby Isaac + dm - The DM object
7223c58f1c22SToby Isaac - n  - the label number
7224c58f1c22SToby Isaac 
7225c58f1c22SToby Isaac   Output Parameter:
7226c58f1c22SToby Isaac . name - the label name
7227c58f1c22SToby Isaac 
7228c58f1c22SToby Isaac   Level: intermediate
7229c58f1c22SToby Isaac 
7230c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7231c58f1c22SToby Isaac @*/
7232c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7233c58f1c22SToby Isaac {
72345d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7235c58f1c22SToby Isaac   PetscInt       l    = 0;
7236d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7237c58f1c22SToby Isaac 
7238c58f1c22SToby Isaac   PetscFunctionBegin;
7239c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7240c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7241c58f1c22SToby Isaac   while (next) {
7242c58f1c22SToby Isaac     if (l == n) {
7243d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7244c58f1c22SToby Isaac       PetscFunctionReturn(0);
7245c58f1c22SToby Isaac     }
7246c58f1c22SToby Isaac     ++l;
7247c58f1c22SToby Isaac     next = next->next;
7248c58f1c22SToby Isaac   }
7249c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7250c58f1c22SToby Isaac }
7251c58f1c22SToby Isaac 
7252c58f1c22SToby Isaac /*@C
7253c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7254c58f1c22SToby Isaac 
7255c58f1c22SToby Isaac   Not Collective
7256c58f1c22SToby Isaac 
7257c58f1c22SToby Isaac   Input Parameters:
7258c58f1c22SToby Isaac + dm   - The DM object
7259c58f1c22SToby Isaac - name - The label name
7260c58f1c22SToby Isaac 
7261c58f1c22SToby Isaac   Output Parameter:
7262c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7263c58f1c22SToby Isaac 
7264c58f1c22SToby Isaac   Level: intermediate
7265c58f1c22SToby Isaac 
7266c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7267c58f1c22SToby Isaac @*/
7268c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7269c58f1c22SToby Isaac {
72705d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7271d67d17b1SMatthew G. Knepley   const char    *lname;
7272c58f1c22SToby Isaac   PetscErrorCode ierr;
7273c58f1c22SToby Isaac 
7274c58f1c22SToby Isaac   PetscFunctionBegin;
7275c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7276c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7277534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7278c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7279c58f1c22SToby Isaac   while (next) {
7280d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7281d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7282c58f1c22SToby Isaac     if (*hasLabel) break;
7283c58f1c22SToby Isaac     next = next->next;
7284c58f1c22SToby Isaac   }
7285c58f1c22SToby Isaac   PetscFunctionReturn(0);
7286c58f1c22SToby Isaac }
7287c58f1c22SToby Isaac 
7288c58f1c22SToby Isaac /*@C
7289c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7290c58f1c22SToby Isaac 
7291c58f1c22SToby Isaac   Not Collective
7292c58f1c22SToby Isaac 
7293c58f1c22SToby Isaac   Input Parameters:
7294c58f1c22SToby Isaac + dm   - The DM object
7295c58f1c22SToby Isaac - name - The label name
7296c58f1c22SToby Isaac 
7297c58f1c22SToby Isaac   Output Parameter:
7298c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7299c58f1c22SToby Isaac 
7300c58f1c22SToby Isaac   Level: intermediate
7301c58f1c22SToby Isaac 
7302c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7303c58f1c22SToby Isaac @*/
7304c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7305c58f1c22SToby Isaac {
73065d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7307c58f1c22SToby Isaac   PetscBool      hasLabel;
7308d67d17b1SMatthew G. Knepley   const char    *lname;
7309c58f1c22SToby Isaac   PetscErrorCode ierr;
7310c58f1c22SToby Isaac 
7311c58f1c22SToby Isaac   PetscFunctionBegin;
7312c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7313c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7314c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7315c58f1c22SToby Isaac   *label = NULL;
7316c58f1c22SToby Isaac   while (next) {
7317d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7318d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7319c58f1c22SToby Isaac     if (hasLabel) {
7320c58f1c22SToby Isaac       *label = next->label;
7321c58f1c22SToby Isaac       break;
7322c58f1c22SToby Isaac     }
7323c58f1c22SToby Isaac     next = next->next;
7324c58f1c22SToby Isaac   }
7325c58f1c22SToby Isaac   PetscFunctionReturn(0);
7326c58f1c22SToby Isaac }
7327c58f1c22SToby Isaac 
7328c58f1c22SToby Isaac /*@C
7329c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7330c58f1c22SToby Isaac 
7331c58f1c22SToby Isaac   Not Collective
7332c58f1c22SToby Isaac 
7333c58f1c22SToby Isaac   Input Parameters:
7334c58f1c22SToby Isaac + dm - The DM object
7335c58f1c22SToby Isaac - n  - the label number
7336c58f1c22SToby Isaac 
7337c58f1c22SToby Isaac   Output Parameter:
7338c58f1c22SToby Isaac . label - the label
7339c58f1c22SToby Isaac 
7340c58f1c22SToby Isaac   Level: intermediate
7341c58f1c22SToby Isaac 
7342c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7343c58f1c22SToby Isaac @*/
7344c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7345c58f1c22SToby Isaac {
73465d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7347c58f1c22SToby Isaac   PetscInt    l    = 0;
7348c58f1c22SToby Isaac 
7349c58f1c22SToby Isaac   PetscFunctionBegin;
7350c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7351c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7352c58f1c22SToby Isaac   while (next) {
7353c58f1c22SToby Isaac     if (l == n) {
7354c58f1c22SToby Isaac       *label = next->label;
7355c58f1c22SToby Isaac       PetscFunctionReturn(0);
7356c58f1c22SToby Isaac     }
7357c58f1c22SToby Isaac     ++l;
7358c58f1c22SToby Isaac     next = next->next;
7359c58f1c22SToby Isaac   }
7360c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7361c58f1c22SToby Isaac }
7362c58f1c22SToby Isaac 
7363c58f1c22SToby Isaac /*@C
7364c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7365c58f1c22SToby Isaac 
7366c58f1c22SToby Isaac   Not Collective
7367c58f1c22SToby Isaac 
7368c58f1c22SToby Isaac   Input Parameters:
7369c58f1c22SToby Isaac + dm   - The DM object
7370c58f1c22SToby Isaac - label - The DMLabel
7371c58f1c22SToby Isaac 
7372c58f1c22SToby Isaac   Level: developer
7373c58f1c22SToby Isaac 
7374c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7375c58f1c22SToby Isaac @*/
7376c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7377c58f1c22SToby Isaac {
73785d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7379c58f1c22SToby Isaac   PetscBool      hasLabel;
7380d67d17b1SMatthew G. Knepley   const char    *lname;
73815d80c0bfSVaclav Hapla   PetscBool      flg;
7382c58f1c22SToby Isaac   PetscErrorCode ierr;
7383c58f1c22SToby Isaac 
7384c58f1c22SToby Isaac   PetscFunctionBegin;
7385c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7386d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7387d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7388d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7389c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7390c58f1c22SToby Isaac   tmpLabel->label  = label;
7391c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
73925d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
73935d80c0bfSVaclav Hapla   *p = tmpLabel;
739408f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
73955d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
73965d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7397ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7398ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7399c58f1c22SToby Isaac   PetscFunctionReturn(0);
7400c58f1c22SToby Isaac }
7401c58f1c22SToby Isaac 
7402c58f1c22SToby Isaac /*@C
7403e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7404c58f1c22SToby Isaac 
7405c58f1c22SToby Isaac   Not Collective
7406c58f1c22SToby Isaac 
7407c58f1c22SToby Isaac   Input Parameters:
7408c58f1c22SToby Isaac + dm   - The DM object
7409c58f1c22SToby Isaac - name - The label name
7410c58f1c22SToby Isaac 
7411c58f1c22SToby Isaac   Output Parameter:
7412c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7413c58f1c22SToby Isaac 
7414c58f1c22SToby Isaac   Level: developer
7415c58f1c22SToby Isaac 
7416e5472504SVaclav Hapla   Notes:
7417e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7418e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7419e5472504SVaclav Hapla 
7420e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7421e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7422e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7423e5472504SVaclav Hapla 
7424e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7425c58f1c22SToby Isaac @*/
7426c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7427c58f1c22SToby Isaac {
742895d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7429c58f1c22SToby Isaac   PetscBool      hasLabel;
7430d67d17b1SMatthew G. Knepley   const char    *lname;
7431c58f1c22SToby Isaac   PetscErrorCode ierr;
7432c58f1c22SToby Isaac 
7433c58f1c22SToby Isaac   PetscFunctionBegin;
7434c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7435e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7436e5472504SVaclav Hapla   if (label) {
7437e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7438c58f1c22SToby Isaac     *label = NULL;
7439e5472504SVaclav Hapla   }
74405d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
744195d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7442d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7443c58f1c22SToby Isaac     if (hasLabel) {
744495d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7445c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
744695d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7447ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7448ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
744995d578d6SVaclav Hapla       if (label) *label = link->label;
745095d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
745195d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7452c58f1c22SToby Isaac       break;
7453c58f1c22SToby Isaac     }
7454c58f1c22SToby Isaac   }
7455c58f1c22SToby Isaac   PetscFunctionReturn(0);
7456c58f1c22SToby Isaac }
7457c58f1c22SToby Isaac 
7458306894acSVaclav Hapla /*@
7459306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7460306894acSVaclav Hapla 
7461306894acSVaclav Hapla   Not Collective
7462306894acSVaclav Hapla 
7463306894acSVaclav Hapla   Input Parameters:
7464306894acSVaclav Hapla + dm   - The DM object
7465306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7466306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7467306894acSVaclav Hapla 
7468306894acSVaclav Hapla   Level: developer
7469306894acSVaclav Hapla 
7470306894acSVaclav Hapla   Notes:
7471306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7472306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7473306894acSVaclav Hapla   *label nullified.
7474306894acSVaclav Hapla 
7475306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7476306894acSVaclav Hapla @*/
7477306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7478306894acSVaclav Hapla {
747943e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7480306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7481306894acSVaclav Hapla   PetscErrorCode ierr;
7482306894acSVaclav Hapla 
7483306894acSVaclav Hapla   PetscFunctionBegin;
7484306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7485306894acSVaclav Hapla   PetscValidPointer(label, 2);
7486f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7487306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7488306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
74895d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
749043e45a93SVaclav Hapla     if (*label == link->label) {
7491306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
749243e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7493306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7494ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
749543e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
749643e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
749743e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7498306894acSVaclav Hapla       break;
7499306894acSVaclav Hapla     }
7500306894acSVaclav Hapla   }
7501306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7502306894acSVaclav Hapla   PetscFunctionReturn(0);
7503306894acSVaclav Hapla }
7504306894acSVaclav Hapla 
7505c58f1c22SToby Isaac /*@C
7506c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7507c58f1c22SToby Isaac 
7508c58f1c22SToby Isaac   Not Collective
7509c58f1c22SToby Isaac 
7510c58f1c22SToby Isaac   Input Parameters:
7511c58f1c22SToby Isaac + dm   - The DM object
7512c58f1c22SToby Isaac - name - The label name
7513c58f1c22SToby Isaac 
7514c58f1c22SToby Isaac   Output Parameter:
7515c58f1c22SToby Isaac . output - The flag for output
7516c58f1c22SToby Isaac 
7517c58f1c22SToby Isaac   Level: developer
7518c58f1c22SToby Isaac 
7519c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7520c58f1c22SToby Isaac @*/
7521c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7522c58f1c22SToby Isaac {
75235d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7524d67d17b1SMatthew G. Knepley   const char    *lname;
7525c58f1c22SToby Isaac   PetscErrorCode ierr;
7526c58f1c22SToby Isaac 
7527c58f1c22SToby Isaac   PetscFunctionBegin;
7528c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7529c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7530c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7531c58f1c22SToby Isaac   while (next) {
7532c58f1c22SToby Isaac     PetscBool flg;
7533c58f1c22SToby Isaac 
7534d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7535d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7536c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7537c58f1c22SToby Isaac     next = next->next;
7538c58f1c22SToby Isaac   }
7539c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7540c58f1c22SToby Isaac }
7541c58f1c22SToby Isaac 
7542c58f1c22SToby Isaac /*@C
7543c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7544c58f1c22SToby Isaac 
7545c58f1c22SToby Isaac   Not Collective
7546c58f1c22SToby Isaac 
7547c58f1c22SToby Isaac   Input Parameters:
7548c58f1c22SToby Isaac + dm     - The DM object
7549c58f1c22SToby Isaac . name   - The label name
7550c58f1c22SToby Isaac - output - The flag for output
7551c58f1c22SToby Isaac 
7552c58f1c22SToby Isaac   Level: developer
7553c58f1c22SToby Isaac 
7554c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7555c58f1c22SToby Isaac @*/
7556c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7557c58f1c22SToby Isaac {
75585d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7559d67d17b1SMatthew G. Knepley   const char    *lname;
7560c58f1c22SToby Isaac   PetscErrorCode ierr;
7561c58f1c22SToby Isaac 
7562c58f1c22SToby Isaac   PetscFunctionBegin;
7563c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7564534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7565c58f1c22SToby Isaac   while (next) {
7566c58f1c22SToby Isaac     PetscBool flg;
7567c58f1c22SToby Isaac 
7568d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7569d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7570c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7571c58f1c22SToby Isaac     next = next->next;
7572c58f1c22SToby Isaac   }
7573c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7574c58f1c22SToby Isaac }
7575c58f1c22SToby Isaac 
7576c58f1c22SToby Isaac /*@
7577c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7578c58f1c22SToby Isaac 
7579d083f849SBarry Smith   Collective on dmA
7580c58f1c22SToby Isaac 
7581c58f1c22SToby Isaac   Input Parameter:
75825d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
75832e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
75845d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7585ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7586c58f1c22SToby Isaac 
7587c58f1c22SToby Isaac   Level: intermediate
7588c58f1c22SToby Isaac 
7589c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7590c58f1c22SToby Isaac 
75915d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7592c58f1c22SToby Isaac @*/
75935d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7594c58f1c22SToby Isaac {
7595c58f1c22SToby Isaac   DMLabel        label, labelNew;
7596c58f1c22SToby Isaac   const char    *name;
7597c58f1c22SToby Isaac   PetscBool      flg;
75985d80c0bfSVaclav Hapla   DMLabelLink    link;
75995d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7600c58f1c22SToby Isaac 
76015d80c0bfSVaclav Hapla   PetscFunctionBegin;
76025d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
76035d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
76045d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
76055d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
76065d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
76075d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
76085d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
76095d80c0bfSVaclav Hapla     label=link->label;
76105d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
76115d80c0bfSVaclav Hapla     if (!all) {
7612c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7613c58f1c22SToby Isaac       if (flg) continue;
76147d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
76157d5acc75SStefano Zampini       if (flg) continue;
7616ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7617ba2698f1SMatthew G. Knepley       if (flg) continue;
76185d80c0bfSVaclav Hapla     }
76195d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7620c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
76215d80c0bfSVaclav Hapla     } else {
76225d80c0bfSVaclav Hapla       labelNew = label;
76235d80c0bfSVaclav Hapla     }
7624c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
76255d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7626c58f1c22SToby Isaac   }
7627c58f1c22SToby Isaac   PetscFunctionReturn(0);
7628c58f1c22SToby Isaac }
7629a8fb8f29SToby Isaac 
7630a8fb8f29SToby Isaac /*@
7631a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7632a8fb8f29SToby Isaac 
7633a8fb8f29SToby Isaac   Input Parameter:
7634a8fb8f29SToby Isaac . dm - The DM object
7635a8fb8f29SToby Isaac 
7636a8fb8f29SToby Isaac   Output Parameter:
7637a8fb8f29SToby Isaac . cdm - The coarse DM
7638a8fb8f29SToby Isaac 
7639a8fb8f29SToby Isaac   Level: intermediate
7640a8fb8f29SToby Isaac 
7641a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7642a8fb8f29SToby Isaac @*/
7643a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7644a8fb8f29SToby Isaac {
7645a8fb8f29SToby Isaac   PetscFunctionBegin;
7646a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7647a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7648a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7649a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7650a8fb8f29SToby Isaac }
7651a8fb8f29SToby Isaac 
7652a8fb8f29SToby Isaac /*@
7653a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7654a8fb8f29SToby Isaac 
7655a8fb8f29SToby Isaac   Input Parameters:
7656a8fb8f29SToby Isaac + dm - The DM object
7657a8fb8f29SToby Isaac - cdm - The coarse DM
7658a8fb8f29SToby Isaac 
7659a8fb8f29SToby Isaac   Level: intermediate
7660a8fb8f29SToby Isaac 
7661a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7662a8fb8f29SToby Isaac @*/
7663a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7664a8fb8f29SToby Isaac {
7665a8fb8f29SToby Isaac   PetscErrorCode ierr;
7666a8fb8f29SToby Isaac 
7667a8fb8f29SToby Isaac   PetscFunctionBegin;
7668a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7669a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7670a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7671a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7672a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7673a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7674a8fb8f29SToby Isaac }
7675a8fb8f29SToby Isaac 
767688bdff64SToby Isaac /*@
767788bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
767888bdff64SToby Isaac 
767988bdff64SToby Isaac   Input Parameter:
768088bdff64SToby Isaac . dm - The DM object
768188bdff64SToby Isaac 
768288bdff64SToby Isaac   Output Parameter:
768388bdff64SToby Isaac . fdm - The fine DM
768488bdff64SToby Isaac 
768588bdff64SToby Isaac   Level: intermediate
768688bdff64SToby Isaac 
768788bdff64SToby Isaac .seealso: DMSetFineDM()
768888bdff64SToby Isaac @*/
768988bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
769088bdff64SToby Isaac {
769188bdff64SToby Isaac   PetscFunctionBegin;
769288bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
769388bdff64SToby Isaac   PetscValidPointer(fdm, 2);
769488bdff64SToby Isaac   *fdm = dm->fineMesh;
769588bdff64SToby Isaac   PetscFunctionReturn(0);
769688bdff64SToby Isaac }
769788bdff64SToby Isaac 
769888bdff64SToby Isaac /*@
769988bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
770088bdff64SToby Isaac 
770188bdff64SToby Isaac   Input Parameters:
770288bdff64SToby Isaac + dm - The DM object
770388bdff64SToby Isaac - fdm - The fine DM
770488bdff64SToby Isaac 
770588bdff64SToby Isaac   Level: intermediate
770688bdff64SToby Isaac 
770788bdff64SToby Isaac .seealso: DMGetFineDM()
770888bdff64SToby Isaac @*/
770988bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
771088bdff64SToby Isaac {
771188bdff64SToby Isaac   PetscErrorCode ierr;
771288bdff64SToby Isaac 
771388bdff64SToby Isaac   PetscFunctionBegin;
771488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
771588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
771688bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
771788bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
771888bdff64SToby Isaac   dm->fineMesh = fdm;
771988bdff64SToby Isaac   PetscFunctionReturn(0);
772088bdff64SToby Isaac }
772188bdff64SToby Isaac 
7722a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7723a6ba4734SToby Isaac 
7724a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7725a6ba4734SToby Isaac {
7726e5e52638SMatthew G. Knepley   PetscInt       d;
7727a6ba4734SToby Isaac   PetscErrorCode ierr;
7728a6ba4734SToby Isaac 
7729a6ba4734SToby Isaac   PetscFunctionBegin;
7730e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7731e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7732e5e52638SMatthew G. Knepley   }
7733a6ba4734SToby Isaac   PetscFunctionReturn(0);
7734a6ba4734SToby Isaac }
7735a6ba4734SToby Isaac 
7736a6ba4734SToby Isaac /*@C
7737a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7738a6ba4734SToby Isaac 
7739783e2ec8SMatthew G. Knepley   Collective on dm
7740783e2ec8SMatthew G. Knepley 
7741a6ba4734SToby Isaac   Input Parameters:
77424c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7743f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7744a6ba4734SToby Isaac . name        - The BC name
7745a6ba4734SToby Isaac . labelname   - The label defining constrained points
7746a6ba4734SToby Isaac . field       - The field to constrain
7747e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7748a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7749a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7750a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7751a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7752a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7753a6ba4734SToby Isaac 
7754a6ba4734SToby Isaac   Options Database Keys:
7755a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7756a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7757a6ba4734SToby Isaac 
7758a6ba4734SToby Isaac   Level: developer
7759a6ba4734SToby Isaac 
7760a6ba4734SToby Isaac .seealso: DMGetBoundary()
7761a6ba4734SToby Isaac @*/
7762a30ec4eaSSatish 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)
7763a6ba4734SToby Isaac {
7764e5e52638SMatthew G. Knepley   PetscDS        ds;
7765a6ba4734SToby Isaac   PetscErrorCode ierr;
7766a6ba4734SToby Isaac 
7767a6ba4734SToby Isaac   PetscFunctionBegin;
7768a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7769783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
7770783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
7771783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
7772783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
7773e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7774783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
7775e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7776a6ba4734SToby Isaac   PetscFunctionReturn(0);
7777a6ba4734SToby Isaac }
7778a6ba4734SToby Isaac 
7779a6ba4734SToby Isaac /*@
7780a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7781a6ba4734SToby Isaac 
7782a6ba4734SToby Isaac   Input Parameters:
7783a6ba4734SToby Isaac . dm - The mesh object
7784a6ba4734SToby Isaac 
7785a6ba4734SToby Isaac   Output Parameters:
7786a6ba4734SToby Isaac . numBd - The number of BC
7787a6ba4734SToby Isaac 
7788a6ba4734SToby Isaac   Level: intermediate
7789a6ba4734SToby Isaac 
7790a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7791a6ba4734SToby Isaac @*/
7792a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7793a6ba4734SToby Isaac {
7794e5e52638SMatthew G. Knepley   PetscDS        ds;
779558ebd649SToby Isaac   PetscErrorCode ierr;
7796a6ba4734SToby Isaac 
7797a6ba4734SToby Isaac   PetscFunctionBegin;
7798a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7799e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7800e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7801a6ba4734SToby Isaac   PetscFunctionReturn(0);
7802a6ba4734SToby Isaac }
7803a6ba4734SToby Isaac 
7804a6ba4734SToby Isaac /*@C
78051c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7806a6ba4734SToby Isaac 
7807a6ba4734SToby Isaac   Input Parameters:
7808a6ba4734SToby Isaac + dm          - The mesh object
7809a6ba4734SToby Isaac - bd          - The BC number
7810a6ba4734SToby Isaac 
7811a6ba4734SToby Isaac   Output Parameters:
7812f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7813a6ba4734SToby Isaac . name        - The BC name
7814a6ba4734SToby Isaac . labelname   - The label defining constrained points
7815a6ba4734SToby Isaac . field       - The field to constrain
7816a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7817a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7818a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7819a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7820a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7821a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7822a6ba4734SToby Isaac 
7823a6ba4734SToby Isaac   Options Database Keys:
7824a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7825a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7826a6ba4734SToby Isaac 
7827a6ba4734SToby Isaac   Level: developer
7828a6ba4734SToby Isaac 
7829a6ba4734SToby Isaac .seealso: DMAddBoundary()
7830a6ba4734SToby Isaac @*/
7831a30ec4eaSSatish 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)
7832a6ba4734SToby Isaac {
7833e5e52638SMatthew G. Knepley   PetscDS        ds;
783458ebd649SToby Isaac   PetscErrorCode ierr;
7835a6ba4734SToby Isaac 
7836a6ba4734SToby Isaac   PetscFunctionBegin;
7837a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7838e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7839e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7840a6ba4734SToby Isaac   PetscFunctionReturn(0);
7841a6ba4734SToby Isaac }
7842a6ba4734SToby Isaac 
7843e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7844e6f8dbb6SToby Isaac {
7845e5e52638SMatthew G. Knepley   PetscDS        ds;
7846dff059c6SToby Isaac   DMBoundary    *lastnext;
7847e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7848e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7849e6f8dbb6SToby Isaac 
7850e6f8dbb6SToby Isaac   PetscFunctionBegin;
7851e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7852e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
785347a1f5adSToby Isaac   if (dm->boundary) {
785447a1f5adSToby Isaac     DMBoundary next = dm->boundary;
785547a1f5adSToby Isaac 
785647a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
785747a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
785847a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
785947a1f5adSToby Isaac     while (next) {
786047a1f5adSToby Isaac       DMBoundary b = next;
786147a1f5adSToby Isaac 
786247a1f5adSToby Isaac       next = b->next;
786347a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7864a6ba4734SToby Isaac     }
786547a1f5adSToby Isaac     dm->boundary = NULL;
7866a6ba4734SToby Isaac   }
786747a1f5adSToby Isaac 
7868dff059c6SToby Isaac   lastnext = &(dm->boundary);
7869e6f8dbb6SToby Isaac   while (dsbound) {
7870e6f8dbb6SToby Isaac     DMBoundary dmbound;
7871e6f8dbb6SToby Isaac 
7872e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7873e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7874e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7875994fe344SLisandro 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);}
787647a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7877dff059c6SToby Isaac     *lastnext = dmbound;
7878dff059c6SToby Isaac     lastnext = &(dmbound->next);
7879dff059c6SToby Isaac     dsbound = dsbound->next;
7880a6ba4734SToby Isaac   }
7881a6ba4734SToby Isaac   PetscFunctionReturn(0);
7882a6ba4734SToby Isaac }
7883a6ba4734SToby Isaac 
7884a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7885a6ba4734SToby Isaac {
7886b95f2879SToby Isaac   DMBoundary     b;
7887a6ba4734SToby Isaac   PetscErrorCode ierr;
7888a6ba4734SToby Isaac 
7889a6ba4734SToby Isaac   PetscFunctionBegin;
7890a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7891534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7892a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7893e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7894b95f2879SToby Isaac   b = dm->boundary;
7895a6ba4734SToby Isaac   while (b && !(*isBd)) {
7896e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7897e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
78983424c85cSToby Isaac 
78993424c85cSToby Isaac     if (label) {
7900a6ba4734SToby Isaac       PetscInt i;
7901a6ba4734SToby Isaac 
7902e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7903e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7904a6ba4734SToby Isaac       }
7905a6ba4734SToby Isaac     }
7906a6ba4734SToby Isaac     b = b->next;
7907a6ba4734SToby Isaac   }
7908a6ba4734SToby Isaac   PetscFunctionReturn(0);
7909a6ba4734SToby Isaac }
79104d6f44ffSToby Isaac 
79114d6f44ffSToby Isaac /*@C
7912a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
7913a6e0b375SMatthew G. Knepley 
7914a6e0b375SMatthew G. Knepley   Collective on DM
79154d6f44ffSToby Isaac 
79164d6f44ffSToby Isaac   Input Parameters:
79174d6f44ffSToby Isaac + dm      - The DM
79180709b2feSToby Isaac . time    - The time
79194d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
79204d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
79214d6f44ffSToby Isaac - mode    - The insertion mode for values
79224d6f44ffSToby Isaac 
79234d6f44ffSToby Isaac   Output Parameter:
79244d6f44ffSToby Isaac . X - vector
79254d6f44ffSToby Isaac 
79264d6f44ffSToby Isaac    Calling sequence of func:
79270709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
79284d6f44ffSToby Isaac 
79294d6f44ffSToby Isaac +  dim - The spatial dimension
79304d6f44ffSToby Isaac .  x   - The coordinates
79314d6f44ffSToby Isaac .  Nf  - The number of fields
79324d6f44ffSToby Isaac .  u   - The output field values
79334d6f44ffSToby Isaac -  ctx - optional user-defined function context
79344d6f44ffSToby Isaac 
79354d6f44ffSToby Isaac   Level: developer
79364d6f44ffSToby Isaac 
7937a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
79384d6f44ffSToby Isaac @*/
79390709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
79404d6f44ffSToby Isaac {
79414d6f44ffSToby Isaac   Vec            localX;
79424d6f44ffSToby Isaac   PetscErrorCode ierr;
79434d6f44ffSToby Isaac 
79444d6f44ffSToby Isaac   PetscFunctionBegin;
79454d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79464d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
79470709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
79484d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
79494d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
79504d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
79514d6f44ffSToby Isaac   PetscFunctionReturn(0);
79524d6f44ffSToby Isaac }
79534d6f44ffSToby Isaac 
7954a6e0b375SMatthew G. Knepley /*@C
7955a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
7956a6e0b375SMatthew G. Knepley 
7957a6e0b375SMatthew G. Knepley   Not collective
7958a6e0b375SMatthew G. Knepley 
7959a6e0b375SMatthew G. Knepley   Input Parameters:
7960a6e0b375SMatthew G. Knepley + dm      - The DM
7961a6e0b375SMatthew G. Knepley . time    - The time
7962a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7963a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7964a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7965a6e0b375SMatthew G. Knepley 
7966a6e0b375SMatthew G. Knepley   Output Parameter:
7967a6e0b375SMatthew G. Knepley . localX - vector
7968a6e0b375SMatthew G. Knepley 
7969a6e0b375SMatthew G. Knepley    Calling sequence of func:
7970a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7971a6e0b375SMatthew G. Knepley 
7972a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7973a6e0b375SMatthew G. Knepley .  x   - The coordinates
7974a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7975a6e0b375SMatthew G. Knepley .  u   - The output field values
7976a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7977a6e0b375SMatthew G. Knepley 
7978a6e0b375SMatthew G. Knepley   Level: developer
7979a6e0b375SMatthew G. Knepley 
7980a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
7981a6e0b375SMatthew G. Knepley @*/
79820709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
79834d6f44ffSToby Isaac {
79844d6f44ffSToby Isaac   PetscErrorCode ierr;
79854d6f44ffSToby Isaac 
79864d6f44ffSToby Isaac   PetscFunctionBegin;
79874d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79884d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
79890918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
79900709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
79914d6f44ffSToby Isaac   PetscFunctionReturn(0);
79924d6f44ffSToby Isaac }
79934d6f44ffSToby Isaac 
7994a6e0b375SMatthew G. Knepley /*@C
7995a6e0b375SMatthew 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.
7996a6e0b375SMatthew G. Knepley 
7997a6e0b375SMatthew G. Knepley   Collective on DM
7998a6e0b375SMatthew G. Knepley 
7999a6e0b375SMatthew G. Knepley   Input Parameters:
8000a6e0b375SMatthew G. Knepley + dm      - The DM
8001a6e0b375SMatthew G. Knepley . time    - The time
8002a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8003a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8004a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8005a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8006a6e0b375SMatthew G. Knepley 
8007a6e0b375SMatthew G. Knepley   Output Parameter:
8008a6e0b375SMatthew G. Knepley . X - vector
8009a6e0b375SMatthew G. Knepley 
8010a6e0b375SMatthew G. Knepley    Calling sequence of func:
8011a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8012a6e0b375SMatthew G. Knepley 
8013a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8014a6e0b375SMatthew G. Knepley .  x   - The coordinates
8015a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8016a6e0b375SMatthew G. Knepley .  u   - The output field values
8017a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8018a6e0b375SMatthew G. Knepley 
8019a6e0b375SMatthew G. Knepley   Level: developer
8020a6e0b375SMatthew G. Knepley 
8021a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8022a6e0b375SMatthew G. Knepley @*/
80232c53366bSMatthew 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)
80242c53366bSMatthew G. Knepley {
80252c53366bSMatthew G. Knepley   Vec            localX;
80262c53366bSMatthew G. Knepley   PetscErrorCode ierr;
80272c53366bSMatthew G. Knepley 
80282c53366bSMatthew G. Knepley   PetscFunctionBegin;
80292c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80302c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
80312c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
80322c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
80332c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
80342c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
80352c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
80362c53366bSMatthew G. Knepley }
80372c53366bSMatthew G. Knepley 
8038a6e0b375SMatthew G. Knepley /*@C
8039a6e0b375SMatthew 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.
8040a6e0b375SMatthew G. Knepley 
8041a6e0b375SMatthew G. Knepley   Not collective
8042a6e0b375SMatthew G. Knepley 
8043a6e0b375SMatthew G. Knepley   Input Parameters:
8044a6e0b375SMatthew G. Knepley + dm      - The DM
8045a6e0b375SMatthew G. Knepley . time    - The time
8046a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8047a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8048a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8049a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8050a6e0b375SMatthew G. Knepley 
8051a6e0b375SMatthew G. Knepley   Output Parameter:
8052a6e0b375SMatthew G. Knepley . localX - vector
8053a6e0b375SMatthew G. Knepley 
8054a6e0b375SMatthew G. Knepley    Calling sequence of func:
8055a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8056a6e0b375SMatthew G. Knepley 
8057a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8058a6e0b375SMatthew G. Knepley .  x   - The coordinates
8059a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8060a6e0b375SMatthew G. Knepley .  u   - The output field values
8061a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8062a6e0b375SMatthew G. Knepley 
8063a6e0b375SMatthew G. Knepley   Level: developer
8064a6e0b375SMatthew G. Knepley 
8065a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8066a6e0b375SMatthew G. Knepley @*/
80671c531cf8SMatthew 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)
80684d6f44ffSToby Isaac {
80694d6f44ffSToby Isaac   PetscErrorCode ierr;
80704d6f44ffSToby Isaac 
80714d6f44ffSToby Isaac   PetscFunctionBegin;
80724d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80734d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
80740918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
80751c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
80764d6f44ffSToby Isaac   PetscFunctionReturn(0);
80774d6f44ffSToby Isaac }
80782716604bSToby Isaac 
8079a6e0b375SMatthew G. Knepley /*@C
8080a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8081a6e0b375SMatthew G. Knepley 
8082a6e0b375SMatthew G. Knepley   Not collective
8083a6e0b375SMatthew G. Knepley 
8084a6e0b375SMatthew G. Knepley   Input Parameters:
8085a6e0b375SMatthew G. Knepley + dm      - The DM
8086a6e0b375SMatthew G. Knepley . time    - The time
8087a6e0b375SMatthew G. Knepley . localU  - The input field vector
8088a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8089a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8090a6e0b375SMatthew G. Knepley 
8091a6e0b375SMatthew G. Knepley   Output Parameter:
8092a6e0b375SMatthew G. Knepley . localX  - The output vector
8093a6e0b375SMatthew G. Knepley 
8094a6e0b375SMatthew G. Knepley    Calling sequence of func:
8095a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8096a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8097a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8098a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8099a6e0b375SMatthew G. Knepley 
8100a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8101a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8102a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8103a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8104a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8105a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8106a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8107a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8108a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8109a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8110a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8111a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8112a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8113a6e0b375SMatthew G. Knepley .  t            - The current time
8114a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8115a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8116a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8117a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8118a6e0b375SMatthew G. Knepley 
8119a6e0b375SMatthew 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.
8120a6e0b375SMatthew 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
8121a6e0b375SMatthew 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
8122a6e0b375SMatthew 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.
8123a6e0b375SMatthew G. Knepley 
8124a6e0b375SMatthew G. Knepley   Level: intermediate
8125a6e0b375SMatthew G. Knepley 
8126a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8127a6e0b375SMatthew G. Knepley @*/
81288c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
81298c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
81308c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
81318c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8132191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
81338c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
81348c6c5593SMatthew G. Knepley {
81358c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
81368c6c5593SMatthew G. Knepley 
81378c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81388c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81398c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
81408c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
81410918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
81428c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
81438c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
81448c6c5593SMatthew G. Knepley }
81458c6c5593SMatthew G. Knepley 
8146a6e0b375SMatthew G. Knepley /*@C
8147a6e0b375SMatthew 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.
8148a6e0b375SMatthew G. Knepley 
8149a6e0b375SMatthew G. Knepley   Not collective
8150a6e0b375SMatthew G. Knepley 
8151a6e0b375SMatthew G. Knepley   Input Parameters:
8152a6e0b375SMatthew G. Knepley + dm      - The DM
8153a6e0b375SMatthew G. Knepley . time    - The time
8154a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8155a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8156a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8157a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8158a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8159a6e0b375SMatthew G. Knepley . localU  - The input field vector
8160a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8161a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8162a6e0b375SMatthew G. Knepley 
8163a6e0b375SMatthew G. Knepley   Output Parameter:
8164a6e0b375SMatthew G. Knepley . localX  - The output vector
8165a6e0b375SMatthew G. Knepley 
8166a6e0b375SMatthew G. Knepley    Calling sequence of func:
8167a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8168a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8169a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8170a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8171a6e0b375SMatthew G. Knepley 
8172a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8173a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8174a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8175a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8176a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8177a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8178a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8179a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8180a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8181a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8182a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8183a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8184a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8185a6e0b375SMatthew G. Knepley .  t            - The current time
8186a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8187a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8188a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8189a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8190a6e0b375SMatthew G. Knepley 
8191a6e0b375SMatthew 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.
8192a6e0b375SMatthew 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
8193a6e0b375SMatthew 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
8194a6e0b375SMatthew 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.
8195a6e0b375SMatthew G. Knepley 
8196a6e0b375SMatthew G. Knepley   Level: intermediate
8197a6e0b375SMatthew G. Knepley 
8198a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8199a6e0b375SMatthew G. Knepley @*/
82001c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
82018c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
82028c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
82038c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8204191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
82058c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
82068c6c5593SMatthew G. Knepley {
82078c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
82088c6c5593SMatthew G. Knepley 
82098c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82108c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82118c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
82128c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8213ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
82141c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
82158c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
82168c6c5593SMatthew G. Knepley }
82178c6c5593SMatthew G. Knepley 
82182716604bSToby Isaac /*@C
8219ece3a9fcSMatthew 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.
8220ece3a9fcSMatthew G. Knepley 
8221ece3a9fcSMatthew G. Knepley   Not collective
8222ece3a9fcSMatthew G. Knepley 
8223ece3a9fcSMatthew G. Knepley   Input Parameters:
8224ece3a9fcSMatthew G. Knepley + dm      - The DM
8225ece3a9fcSMatthew G. Knepley . time    - The time
8226ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8227ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8228ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8229ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8230ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8231ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8232ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8233ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8234ece3a9fcSMatthew G. Knepley 
8235ece3a9fcSMatthew G. Knepley   Output Parameter:
8236ece3a9fcSMatthew G. Knepley . localX  - The output vector
8237ece3a9fcSMatthew G. Knepley 
8238ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8239ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8240ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8241ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8242ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8243ece3a9fcSMatthew G. Knepley 
8244ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8245ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8246ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8247ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8248ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8249ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8250ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8251ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8252ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8253ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8254ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8255ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8256ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8257ece3a9fcSMatthew G. Knepley .  t            - The current time
8258ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8259ece3a9fcSMatthew G. Knepley .  n            - The face normal
8260ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8261ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8262ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8263ece3a9fcSMatthew G. Knepley 
8264ece3a9fcSMatthew G. Knepley   Note:
8265ece3a9fcSMatthew 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.
8266ece3a9fcSMatthew 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
8267ece3a9fcSMatthew 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
8268ece3a9fcSMatthew 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.
8269ece3a9fcSMatthew G. Knepley 
8270ece3a9fcSMatthew G. Knepley   Level: intermediate
8271ece3a9fcSMatthew G. Knepley 
8272ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8273ece3a9fcSMatthew G. Knepley @*/
8274ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8275ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8276ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8277ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8278ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8279ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8280ece3a9fcSMatthew G. Knepley {
8281ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8282ece3a9fcSMatthew G. Knepley 
8283ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8284ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8285ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8286ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8287ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8288ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8289ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8290ece3a9fcSMatthew G. Knepley }
8291ece3a9fcSMatthew G. Knepley 
8292ece3a9fcSMatthew G. Knepley /*@C
82932716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
82942716604bSToby Isaac 
82952716604bSToby Isaac   Input Parameters:
82962716604bSToby Isaac + dm    - The DM
82970709b2feSToby Isaac . time  - The time
82982716604bSToby Isaac . funcs - The functions to evaluate for each field component
82992716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8300574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83012716604bSToby Isaac 
83022716604bSToby Isaac   Output Parameter:
83032716604bSToby Isaac . diff - The diff ||u - u_h||_2
83042716604bSToby Isaac 
83052716604bSToby Isaac   Level: developer
83062716604bSToby Isaac 
83071189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
83082716604bSToby Isaac @*/
83090709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
83102716604bSToby Isaac {
83112716604bSToby Isaac   PetscErrorCode ierr;
83122716604bSToby Isaac 
83132716604bSToby Isaac   PetscFunctionBegin;
83142716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8315b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
83160918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
83170709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
83182716604bSToby Isaac   PetscFunctionReturn(0);
83192716604bSToby Isaac }
8320b698f381SToby Isaac 
8321b698f381SToby Isaac /*@C
8322b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8323b698f381SToby Isaac 
8324d083f849SBarry Smith   Collective on dm
8325d083f849SBarry Smith 
8326b698f381SToby Isaac   Input Parameters:
8327b698f381SToby Isaac + dm    - The DM
8328b698f381SToby Isaac , time  - The time
8329b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8330b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8331574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8332b698f381SToby Isaac - n     - The vector to project along
8333b698f381SToby Isaac 
8334b698f381SToby Isaac   Output Parameter:
8335b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8336b698f381SToby Isaac 
8337b698f381SToby Isaac   Level: developer
8338b698f381SToby Isaac 
8339b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8340b698f381SToby Isaac @*/
8341b698f381SToby 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)
8342b698f381SToby Isaac {
8343b698f381SToby Isaac   PetscErrorCode ierr;
8344b698f381SToby Isaac 
8345b698f381SToby Isaac   PetscFunctionBegin;
8346b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8347b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8348b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8349b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8350b698f381SToby Isaac   PetscFunctionReturn(0);
8351b698f381SToby Isaac }
8352b698f381SToby Isaac 
83532a16baeaSToby Isaac /*@C
83542a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
83552a16baeaSToby Isaac 
8356d083f849SBarry Smith   Collective on dm
8357d083f849SBarry Smith 
83582a16baeaSToby Isaac   Input Parameters:
83592a16baeaSToby Isaac + dm    - The DM
83602a16baeaSToby Isaac . time  - The time
83612a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
83622a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8363574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83642a16baeaSToby Isaac 
83652a16baeaSToby Isaac   Output Parameter:
83662a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
83672a16baeaSToby Isaac 
83682a16baeaSToby Isaac   Level: developer
83692a16baeaSToby Isaac 
83701189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
83712a16baeaSToby Isaac @*/
83721189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
83732a16baeaSToby Isaac {
83742a16baeaSToby Isaac   PetscErrorCode ierr;
83752a16baeaSToby Isaac 
83762a16baeaSToby Isaac   PetscFunctionBegin;
83772a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83782a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
83790918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
83802a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
83812a16baeaSToby Isaac   PetscFunctionReturn(0);
83822a16baeaSToby Isaac }
83832a16baeaSToby Isaac 
8384df0b854cSToby Isaac /*@C
8385df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8386cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8387df0b854cSToby Isaac 
8388df0b854cSToby Isaac   Collective on dm
8389df0b854cSToby Isaac 
8390df0b854cSToby Isaac   Input parameters:
8391df0b854cSToby Isaac + dm - the pre-adaptation DM object
8392a1b0c543SToby Isaac - label - label with the flags
8393df0b854cSToby Isaac 
8394df0b854cSToby Isaac   Output parameters:
83950d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8396df0b854cSToby Isaac 
8397df0b854cSToby Isaac   Level: intermediate
83980d1cd5e0SMatthew G. Knepley 
83990d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8400df0b854cSToby Isaac @*/
84010d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8402df0b854cSToby Isaac {
8403df0b854cSToby Isaac   PetscErrorCode ierr;
8404df0b854cSToby Isaac 
8405df0b854cSToby Isaac   PetscFunctionBegin;
8406df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8407a1b0c543SToby Isaac   PetscValidPointer(label,2);
84080d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
84090d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
84106f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
84110d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
84120d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
84130d1cd5e0SMatthew G. Knepley }
84140d1cd5e0SMatthew G. Knepley 
84150d1cd5e0SMatthew G. Knepley /*@C
84160d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
84170d1cd5e0SMatthew G. Knepley 
84180d1cd5e0SMatthew G. Knepley   Input Parameters:
84190d1cd5e0SMatthew G. Knepley + dm - The DM object
84200d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
84216f25b0d8SLisandro 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_".
84220d1cd5e0SMatthew G. Knepley 
84230d1cd5e0SMatthew G. Knepley   Output Parameter:
84240d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
84250d1cd5e0SMatthew G. Knepley 
84260d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
84270d1cd5e0SMatthew G. Knepley 
84280d1cd5e0SMatthew G. Knepley   Level: advanced
84290d1cd5e0SMatthew G. Knepley 
84300d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
84310d1cd5e0SMatthew G. Knepley @*/
84320d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
84330d1cd5e0SMatthew G. Knepley {
84340d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
84350d1cd5e0SMatthew G. Knepley 
84360d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
84370d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84380d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
84390d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
84400d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
84416f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
84426f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
84430d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8444df0b854cSToby Isaac   PetscFunctionReturn(0);
8445df0b854cSToby Isaac }
8446c4088d22SMatthew G. Knepley 
8447502a2867SDave May /*@C
8448502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8449502a2867SDave May 
8450502a2867SDave May  Not Collective
8451502a2867SDave May 
8452502a2867SDave May  Input Parameter:
8453502a2867SDave May .  dm    - The DM
8454502a2867SDave May 
84550a19bb7dSprj-  Output Parameters:
84560a19bb7dSprj- +  nranks - the number of neighbours
84570a19bb7dSprj- -  ranks - the neighbors ranks
8458502a2867SDave May 
8459502a2867SDave May  Notes:
8460502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8461502a2867SDave May 
8462502a2867SDave May  Level: beginner
8463502a2867SDave May 
8464dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8465502a2867SDave May @*/
8466502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8467502a2867SDave May {
8468502a2867SDave May   PetscErrorCode ierr;
8469502a2867SDave May 
8470502a2867SDave May   PetscFunctionBegin;
8471502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84720918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8473502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8474502a2867SDave May   PetscFunctionReturn(0);
8475502a2867SDave May }
8476502a2867SDave May 
8477531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8478531c7667SBarry Smith 
8479531c7667SBarry Smith /*
8480531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8481531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8482531c7667SBarry Smith */
8483531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8484531c7667SBarry Smith {
8485531c7667SBarry Smith   PetscErrorCode ierr;
8486531c7667SBarry Smith 
8487531c7667SBarry Smith   PetscFunctionBegin;
8488531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8489531c7667SBarry Smith     Vec x1local;
8490531c7667SBarry Smith     DM  dm;
8491531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8492531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8493531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8494531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8495531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8496531c7667SBarry Smith     x1   = x1local;
8497531c7667SBarry Smith   }
8498531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8499531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8500531c7667SBarry Smith     DM  dm;
8501531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8502531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8503531c7667SBarry Smith   }
8504531c7667SBarry Smith   PetscFunctionReturn(0);
8505531c7667SBarry Smith }
8506531c7667SBarry Smith 
8507531c7667SBarry Smith /*@
8508531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8509531c7667SBarry Smith 
8510531c7667SBarry Smith     Input Parameter:
8511531c7667SBarry Smith .    coloring - the MatFDColoring object
8512531c7667SBarry Smith 
851395452b02SPatrick Sanan     Developer Notes:
851495452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8515531c7667SBarry Smith 
85161b266c99SBarry Smith     Level: advanced
85171b266c99SBarry Smith 
8518531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8519531c7667SBarry Smith @*/
8520531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8521531c7667SBarry Smith {
8522531c7667SBarry Smith   PetscFunctionBegin;
8523531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8524531c7667SBarry Smith   PetscFunctionReturn(0);
8525531c7667SBarry Smith }
85268320bc6fSPatrick Sanan 
85278320bc6fSPatrick Sanan /*@
85288320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
85298320bc6fSPatrick Sanan 
85308320bc6fSPatrick Sanan     Collective
85318320bc6fSPatrick Sanan 
85328320bc6fSPatrick Sanan     Input Parameters:
8533a5bc1bf3SBarry Smith +    dm1 - the first DM
85348320bc6fSPatrick Sanan -    dm2 - the second DM
85358320bc6fSPatrick Sanan 
85368320bc6fSPatrick Sanan     Output Parameters:
85378320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
85388320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
85398320bc6fSPatrick Sanan 
85408320bc6fSPatrick Sanan     Notes:
85418320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
85423d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85438320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
85443d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
85453d862458SPatrick Sanan     decomposition, but hold different data.
85468320bc6fSPatrick Sanan 
85478320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
85488320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
85498320bc6fSPatrick Sanan 
85508320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
85518320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85528320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
85538320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
85548320bc6fSPatrick Sanan 
85558320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
85568320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
85578320bc6fSPatrick Sanan .vb
85588320bc6fSPatrick Sanan   ...
85598320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
85608320bc6fSPatrick Sanan   if (set && compatible)  {
85618320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
85628320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
85633d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
85648320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
85658320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
85668320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
85678320bc6fSPatrick Sanan       }
85688320bc6fSPatrick Sanan     }
85698320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
85708320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
85718320bc6fSPatrick Sanan   } else {
85728320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
85738320bc6fSPatrick Sanan   }
85748320bc6fSPatrick Sanan   ...
85758320bc6fSPatrick Sanan .ve
85768320bc6fSPatrick Sanan 
85778320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
85788320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
85798320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
85808320bc6fSPatrick Sanan     always check the "set" output parameter.
85818320bc6fSPatrick Sanan 
85828320bc6fSPatrick Sanan     A DM is always compatible with itself.
85838320bc6fSPatrick Sanan 
85848320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
85858320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
85868320bc6fSPatrick Sanan     incompatible.
85878320bc6fSPatrick Sanan 
85888320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
85898320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
85908320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
85918320bc6fSPatrick Sanan 
85928320bc6fSPatrick Sanan     Developer Notes:
85933d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
85943d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8595a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
85968320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
85978320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
85983d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
85993d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
86008320bc6fSPatrick Sanan 
86018320bc6fSPatrick Sanan     Level: advanced
86028320bc6fSPatrick Sanan 
86033d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
86048320bc6fSPatrick Sanan @*/
86058320bc6fSPatrick Sanan 
8606a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
86078320bc6fSPatrick Sanan {
86088320bc6fSPatrick Sanan   PetscErrorCode ierr;
86098320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
86108320bc6fSPatrick Sanan   DMType         type,type2;
86118320bc6fSPatrick Sanan   PetscBool      sameType;
86128320bc6fSPatrick Sanan 
86138320bc6fSPatrick Sanan   PetscFunctionBegin;
8614a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
86158320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
86168320bc6fSPatrick Sanan 
86178320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8618a5bc1bf3SBarry Smith   if (dm1 == dm2) {
86198320bc6fSPatrick Sanan     *set = PETSC_TRUE;
86208320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86218320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86228320bc6fSPatrick Sanan   }
86238320bc6fSPatrick Sanan 
86248320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86258320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86268320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86278320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8628a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
86298320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86308320bc6fSPatrick Sanan     *set = PETSC_TRUE;
86318320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86328320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86338320bc6fSPatrick Sanan   }
86348320bc6fSPatrick Sanan 
86358320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8636a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8637a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8638b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
86398320bc6fSPatrick Sanan   }
86408320bc6fSPatrick Sanan 
8641a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86428320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8643a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
86448320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
86458320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
86468320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8647a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
86488320bc6fSPatrick Sanan   } else {
86498320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86508320bc6fSPatrick Sanan   }
86518320bc6fSPatrick Sanan   PetscFunctionReturn(0);
86528320bc6fSPatrick Sanan }
8653c0f0dcc3SMatthew G. Knepley 
8654c0f0dcc3SMatthew G. Knepley /*@C
8655c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8656c0f0dcc3SMatthew G. Knepley 
8657c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8658c0f0dcc3SMatthew G. Knepley 
8659c0f0dcc3SMatthew G. Knepley   Input Parameters:
8660c0f0dcc3SMatthew G. Knepley + DM - the DM
8661c0f0dcc3SMatthew G. Knepley . f - the monitor function
8662c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8663c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8664c0f0dcc3SMatthew G. Knepley 
8665c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8666c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8667c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8668c0f0dcc3SMatthew G. Knepley 
8669c0f0dcc3SMatthew G. Knepley   Notes:
8670c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8671c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8672c0f0dcc3SMatthew G. Knepley   order in which they were set.
8673c0f0dcc3SMatthew G. Knepley 
8674c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8675c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8676c0f0dcc3SMatthew G. Knepley 
8677c0f0dcc3SMatthew G. Knepley   Level: intermediate
8678c0f0dcc3SMatthew G. Knepley 
8679c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8680c0f0dcc3SMatthew G. Knepley @*/
8681c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8682c0f0dcc3SMatthew G. Knepley {
8683c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8684c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8685c0f0dcc3SMatthew G. Knepley 
8686c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8687c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8688c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8689c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8690c0f0dcc3SMatthew G. Knepley 
8691c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8692c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8693c0f0dcc3SMatthew G. Knepley   }
8694c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8695c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8696c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8697c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8698c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8699c0f0dcc3SMatthew G. Knepley }
8700c0f0dcc3SMatthew G. Knepley 
8701c0f0dcc3SMatthew G. Knepley /*@
8702c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8703c0f0dcc3SMatthew G. Knepley 
8704c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8705c0f0dcc3SMatthew G. Knepley 
8706c0f0dcc3SMatthew G. Knepley   Input Parameter:
8707c0f0dcc3SMatthew G. Knepley . dm - the DM
8708c0f0dcc3SMatthew G. Knepley 
8709c0f0dcc3SMatthew G. Knepley   Options Database Key:
8710c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8711c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8712c0f0dcc3SMatthew G. Knepley   set via the options database
8713c0f0dcc3SMatthew G. Knepley 
8714c0f0dcc3SMatthew G. Knepley   Notes:
8715c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8716c0f0dcc3SMatthew G. Knepley 
8717c0f0dcc3SMatthew G. Knepley   Level: intermediate
8718c0f0dcc3SMatthew G. Knepley 
8719c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8720c0f0dcc3SMatthew G. Knepley @*/
8721c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8722c0f0dcc3SMatthew G. Knepley {
8723c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8724c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8725c0f0dcc3SMatthew G. Knepley 
8726c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8727c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8728c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8729c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
8730c0f0dcc3SMatthew G. Knepley   }
8731c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8732c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8733c0f0dcc3SMatthew G. Knepley }
8734c0f0dcc3SMatthew G. Knepley 
8735c0f0dcc3SMatthew G. Knepley /*@C
8736c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8737c0f0dcc3SMatthew G. Knepley 
8738c0f0dcc3SMatthew G. Knepley   Collective on DM
8739c0f0dcc3SMatthew G. Knepley 
8740c0f0dcc3SMatthew G. Knepley   Input Parameters:
8741c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8742c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8743c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8744c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8745c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8746c0f0dcc3SMatthew 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
8747c0f0dcc3SMatthew G. Knepley 
8748c0f0dcc3SMatthew G. Knepley   Output Parameter:
8749c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8750c0f0dcc3SMatthew G. Knepley 
8751c0f0dcc3SMatthew G. Knepley   Level: developer
8752c0f0dcc3SMatthew G. Knepley 
8753c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
8754c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
8755c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
8756c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
8757c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
8758c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
8759c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
8760c0f0dcc3SMatthew G. Knepley @*/
8761c0f0dcc3SMatthew 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)
8762c0f0dcc3SMatthew G. Knepley {
8763c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8764c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8765c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
8766c0f0dcc3SMatthew G. Knepley 
8767c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8768c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8769c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
8770c0f0dcc3SMatthew G. Knepley   if (*flg) {
8771c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8772c0f0dcc3SMatthew G. Knepley 
8773c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
8774c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
8775c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
8776c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
8777c0f0dcc3SMatthew G. Knepley   }
8778c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8779c0f0dcc3SMatthew G. Knepley }
8780c0f0dcc3SMatthew G. Knepley 
8781c0f0dcc3SMatthew G. Knepley /*@
8782c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8783c0f0dcc3SMatthew G. Knepley 
8784c0f0dcc3SMatthew G. Knepley    Collective on DM
8785c0f0dcc3SMatthew G. Knepley 
8786c0f0dcc3SMatthew G. Knepley    Input Parameters:
8787c0f0dcc3SMatthew G. Knepley .  dm - The DM
8788c0f0dcc3SMatthew G. Knepley 
8789c0f0dcc3SMatthew G. Knepley    Level: developer
8790c0f0dcc3SMatthew G. Knepley 
8791c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8792c0f0dcc3SMatthew G. Knepley @*/
8793c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8794c0f0dcc3SMatthew G. Knepley {
8795c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8796c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8797c0f0dcc3SMatthew G. Knepley 
8798c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8799c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8800c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8801c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8802c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
8803c0f0dcc3SMatthew G. Knepley   }
8804c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8805c0f0dcc3SMatthew G. Knepley }
8806