xref: /petsc/src/dm/interface/dm.c (revision a587d1398bd356a7db20a09283619c724e82d622)
1d0295fc0SJunchao Zhang #include <petscvec.h>
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
53e922f36SToby Isaac #include <petscdmplex.h>
6f19dbd58SToby Isaac #include <petscdmfield.h>
70c312b8eSJed Brown #include <petscsf.h>
82764a2aaSMatthew G. Knepley #include <petscds.h>
947c6ae99SBarry Smith 
1000d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
1100d952a4SJed Brown #  include <valgrind/memcheck.h>
1200d952a4SJed Brown #endif
1300d952a4SJed Brown 
14732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
15d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
16557cf195SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_Load, DM_AdaptInterpolator;
1767a56275SMatthew G Knepley 
18ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_", NULL};
19ea78f98cSLisandro Dalcin const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_", NULL};
20da9060c4SMatthew G. Knepley const char *const DMPolytopeTypes[] = {"vertex", "segment", "tensor_segment", "triangle", "quadrilateral", "tensor_quad", "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "pyramid", "FV_ghost_cell", "interior_ghost_cell", "unknown", "invalid", "DMPolytopeType", "DM_POLYTOPE_", NULL};
21a4121054SBarry Smith /*@
22de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
23a4121054SBarry Smith 
24a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
25a4121054SBarry Smith    error when you try to use the vector.
26a4121054SBarry Smith 
27d083f849SBarry Smith   Collective
28a4121054SBarry Smith 
29a4121054SBarry Smith   Input Parameter:
30a4121054SBarry Smith . comm - The communicator for the DM object
31a4121054SBarry Smith 
32a4121054SBarry Smith   Output Parameter:
33a4121054SBarry Smith . dm - The DM object
34a4121054SBarry Smith 
35a4121054SBarry Smith   Level: beginner
36a4121054SBarry Smith 
378472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
38a4121054SBarry Smith @*/
397087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
40a4121054SBarry Smith {
41a4121054SBarry Smith   DM             v;
42e5e52638SMatthew G. Knepley   PetscDS        ds;
43a4121054SBarry Smith   PetscErrorCode ierr;
44a4121054SBarry Smith 
45a4121054SBarry Smith   PetscFunctionBegin;
461411c6eeSJed Brown   PetscValidPointer(dm,2);
470298fd71SBarry Smith   *dm = NULL;
48607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
49a4121054SBarry Smith 
5073107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
51e7c4fc90SDmitry Karpeev 
5249be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5349be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
540298fd71SBarry Smith   v->ltogmap                  = NULL;
551411c6eeSJed Brown   v->bs                       = 1;
56171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5788ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
581bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
59c58f1c22SToby Isaac   v->labels                   = NULL;
6034aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6134aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
62c58f1c22SToby Isaac   v->depthLabel               = NULL;
63ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
641bb6d2a8SBarry Smith   v->localSection             = NULL;
651bb6d2a8SBarry Smith   v->globalSection            = NULL;
66fba222abSToby Isaac   v->defaultConstraintSection = NULL;
67fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
68c6b900c6SMatthew G. Knepley   v->L                        = NULL;
69c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
705dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
719a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7296173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
73435a35e8SMatthew G Knepley   {
74435a35e8SMatthew G Knepley     PetscInt i;
75435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
760298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
77f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
78435a35e8SMatthew G Knepley     }
79435a35e8SMatthew G Knepley   }
80e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
81b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
82e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
8314f150ffSMatthew G. Knepley   v->dmBC = NULL;
84a8fb8f29SToby Isaac   v->coarseMesh = NULL;
85f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
86cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
87c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
88b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
894a7a4c06SLawrence Mitchell 
901411c6eeSJed Brown   *dm = v;
91a4121054SBarry Smith   PetscFunctionReturn(0);
92a4121054SBarry Smith }
93a4121054SBarry Smith 
9438221697SMatthew G. Knepley /*@
9538221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9638221697SMatthew G. Knepley 
97d083f849SBarry Smith   Collective
9838221697SMatthew G. Knepley 
9938221697SMatthew G. Knepley   Input Parameter:
10038221697SMatthew G. Knepley . dm - The original DM object
10138221697SMatthew G. Knepley 
10238221697SMatthew G. Knepley   Output Parameter:
10338221697SMatthew G. Knepley . newdm  - The new DM object
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley   Level: beginner
10638221697SMatthew G. Knepley 
1071cb8cacdSPatrick Sanan   Notes:
1081cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1091cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1101cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1111bb6d2a8SBarry Smith 
11289706ed2SPatrick Sanan   The clone is considered set up iff the original is.
11389706ed2SPatrick Sanan 
11492cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1151bb6d2a8SBarry Smith 
11638221697SMatthew G. Knepley @*/
11738221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11838221697SMatthew G. Knepley {
11938221697SMatthew G. Knepley   PetscSF        sf;
12038221697SMatthew G. Knepley   Vec            coords;
12138221697SMatthew G. Knepley   void          *ctx;
122a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12338221697SMatthew G. Knepley   PetscErrorCode ierr;
12438221697SMatthew G. Knepley 
12538221697SMatthew G. Knepley   PetscFunctionBegin;
12638221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12738221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12838221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1295d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
130ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
131ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
132c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
133*a587d139SMark   ierr = PetscFree((*newdm)->vectype);CHKERRQ(ierr);
134*a587d139SMark   ierr = PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype);CHKERRQ(ierr);
135*a587d139SMark   ierr = PetscFree((*newdm)->mattype);CHKERRQ(ierr);
136*a587d139SMark   ierr = PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype);CHKERRQ(ierr);
1371de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1381de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   if (dm->ops->clone) {
14038221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   }
1423f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
14338221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
14438221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
14538221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14638221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
147be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
148be4c1c3eSMatthew G. Knepley     DM           ncdm;
149be4c1c3eSMatthew G. Knepley     PetscSection cs;
1505a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
151be4c1c3eSMatthew G. Knepley 
15292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
153be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1545a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1555a0206caSToby Isaac     if (pEndMax >= 0) {
156be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
15783bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
15892fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
159a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
160be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
161be4c1c3eSMatthew G. Knepley     }
162be4c1c3eSMatthew G. Knepley   }
163a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
164a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
16538221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16638221697SMatthew G. Knepley   if (coords) {
16738221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
16838221697SMatthew G. Knepley   } else {
16938221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
17038221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
17138221697SMatthew G. Knepley   }
17290b157c4SStefano Zampini   {
17390b157c4SStefano Zampini     PetscBool             isper;
174c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1755dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17690b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17790b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
178c6b900c6SMatthew G. Knepley   }
17934aa8a36SMatthew G. Knepley   {
18034aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
18134aa8a36SMatthew G. Knepley 
18234aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
18334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
18434aa8a36SMatthew G. Knepley   }
18538221697SMatthew G. Knepley   PetscFunctionReturn(0);
18638221697SMatthew G. Knepley }
18738221697SMatthew G. Knepley 
1889a42bb27SBarry Smith /*@C
189564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1909a42bb27SBarry Smith 
191d083f849SBarry Smith    Logically Collective on da
1929a42bb27SBarry Smith 
1939a42bb27SBarry Smith    Input Parameter:
1949a42bb27SBarry Smith +  da - initial distributed array
195e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1969a42bb27SBarry Smith 
1979a42bb27SBarry Smith    Options Database:
198dd85299cSBarry Smith .   -dm_vec_type ctype
1999a42bb27SBarry Smith 
2009a42bb27SBarry Smith    Level: intermediate
2019a42bb27SBarry Smith 
202a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
2039a42bb27SBarry Smith @*/
20419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2059a42bb27SBarry Smith {
2069a42bb27SBarry Smith   PetscErrorCode ierr;
2079a42bb27SBarry Smith 
2089a42bb27SBarry Smith   PetscFunctionBegin;
2099a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2109a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
21119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2129a42bb27SBarry Smith   PetscFunctionReturn(0);
2139a42bb27SBarry Smith }
2149a42bb27SBarry Smith 
215c0dedaeaSBarry Smith /*@C
216c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
217c0dedaeaSBarry Smith 
218d083f849SBarry Smith    Logically Collective on da
219c0dedaeaSBarry Smith 
220c0dedaeaSBarry Smith    Input Parameter:
221c0dedaeaSBarry Smith .  da - initial distributed array
222c0dedaeaSBarry Smith 
223c0dedaeaSBarry Smith    Output Parameter:
224c0dedaeaSBarry Smith .  ctype - the vector type
225c0dedaeaSBarry Smith 
226c0dedaeaSBarry Smith    Level: intermediate
227c0dedaeaSBarry Smith 
228a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
229c0dedaeaSBarry Smith @*/
230c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
231c0dedaeaSBarry Smith {
232c0dedaeaSBarry Smith   PetscFunctionBegin;
233c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
234c0dedaeaSBarry Smith   *ctype = da->vectype;
235c0dedaeaSBarry Smith   PetscFunctionReturn(0);
236c0dedaeaSBarry Smith }
237c0dedaeaSBarry Smith 
2385f1ad066SMatthew G Knepley /*@
23934f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2405f1ad066SMatthew G Knepley 
2415f1ad066SMatthew G Knepley   Not collective
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley   Input Parameter:
2445f1ad066SMatthew G Knepley . v - The Vec
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   Output Parameter:
2475f1ad066SMatthew G Knepley . dm - The DM
2485f1ad066SMatthew G Knepley 
2495f1ad066SMatthew G Knepley   Level: intermediate
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2525f1ad066SMatthew G Knepley @*/
2535f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2545f1ad066SMatthew G Knepley {
2555f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2565f1ad066SMatthew G Knepley 
2575f1ad066SMatthew G Knepley   PetscFunctionBegin;
2585f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2595f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2605f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2615f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2625f1ad066SMatthew G Knepley }
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley /*@
265d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley   Not collective
2685f1ad066SMatthew G Knepley 
2695f1ad066SMatthew G Knepley   Input Parameters:
2705f1ad066SMatthew G Knepley + v - The Vec
2715f1ad066SMatthew G Knepley - dm - The DM
2725f1ad066SMatthew G Knepley 
273d9805387SMatthew 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.
274d9805387SMatthew G. Knepley 
2755f1ad066SMatthew G Knepley   Level: intermediate
2765f1ad066SMatthew G Knepley 
2775f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2785f1ad066SMatthew G Knepley @*/
2795f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2805f1ad066SMatthew G Knepley {
2815f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2825f1ad066SMatthew G Knepley 
2835f1ad066SMatthew G Knepley   PetscFunctionBegin;
2845f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
285d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2865f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2875f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2885f1ad066SMatthew G Knepley }
2895f1ad066SMatthew G Knepley 
290521d9a4cSLisandro Dalcin /*@C
2918f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2928f1509bcSBarry Smith 
293d083f849SBarry Smith    Logically Collective on dm
2948f1509bcSBarry Smith 
2958f1509bcSBarry Smith    Input Parameters:
2968f1509bcSBarry Smith +  dm - the DM context
2978f1509bcSBarry Smith -  ctype - the matrix type
2988f1509bcSBarry Smith 
2998f1509bcSBarry Smith    Options Database:
3008f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3018f1509bcSBarry Smith 
3028f1509bcSBarry Smith    Level: intermediate
3038f1509bcSBarry Smith 
3048f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3058f1509bcSBarry Smith           DMGetISColoringType()
3068f1509bcSBarry Smith @*/
3078f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3088f1509bcSBarry Smith {
3098f1509bcSBarry Smith   PetscFunctionBegin;
3108f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3118f1509bcSBarry Smith   dm->coloringtype = ctype;
3128f1509bcSBarry Smith   PetscFunctionReturn(0);
3138f1509bcSBarry Smith }
3148f1509bcSBarry Smith 
3158f1509bcSBarry Smith /*@C
3168f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
317521d9a4cSLisandro Dalcin 
318d083f849SBarry Smith    Logically Collective on dm
319521d9a4cSLisandro Dalcin 
320521d9a4cSLisandro Dalcin    Input Parameter:
3218f1509bcSBarry Smith .  dm - the DM context
3228f1509bcSBarry Smith 
3238f1509bcSBarry Smith    Output Parameter:
3248f1509bcSBarry Smith .  ctype - the matrix type
3258f1509bcSBarry Smith 
3268f1509bcSBarry Smith    Options Database:
3278f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3288f1509bcSBarry Smith 
3298f1509bcSBarry Smith    Level: intermediate
3308f1509bcSBarry Smith 
3318f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3328f1509bcSBarry Smith           DMGetISColoringType()
3338f1509bcSBarry Smith @*/
3348f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3358f1509bcSBarry Smith {
3368f1509bcSBarry Smith   PetscFunctionBegin;
3378f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3388f1509bcSBarry Smith   *ctype = dm->coloringtype;
3398f1509bcSBarry Smith   PetscFunctionReturn(0);
3408f1509bcSBarry Smith }
3418f1509bcSBarry Smith 
3428f1509bcSBarry Smith /*@C
3438f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3448f1509bcSBarry Smith 
345d083f849SBarry Smith    Logically Collective on dm
3468f1509bcSBarry Smith 
3478f1509bcSBarry Smith    Input Parameters:
348521d9a4cSLisandro Dalcin +  dm - the DM context
349a2b5a043SBarry Smith -  ctype - the matrix type
350521d9a4cSLisandro Dalcin 
351521d9a4cSLisandro Dalcin    Options Database:
352521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
353521d9a4cSLisandro Dalcin 
354521d9a4cSLisandro Dalcin    Level: intermediate
355521d9a4cSLisandro Dalcin 
356a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
357521d9a4cSLisandro Dalcin @*/
35819fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
359521d9a4cSLisandro Dalcin {
360521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
36188f0584fSBarry Smith 
362521d9a4cSLisandro Dalcin   PetscFunctionBegin;
363521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
364521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
36519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
366521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
367521d9a4cSLisandro Dalcin }
368521d9a4cSLisandro Dalcin 
369c0dedaeaSBarry Smith /*@C
370c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
371c0dedaeaSBarry Smith 
372d083f849SBarry Smith    Logically Collective on dm
373c0dedaeaSBarry Smith 
374c0dedaeaSBarry Smith    Input Parameter:
375c0dedaeaSBarry Smith .  dm - the DM context
376c0dedaeaSBarry Smith 
377c0dedaeaSBarry Smith    Output Parameter:
378c0dedaeaSBarry Smith .  ctype - the matrix type
379c0dedaeaSBarry Smith 
380c0dedaeaSBarry Smith    Options Database:
381c0dedaeaSBarry Smith .   -dm_mat_type ctype
382c0dedaeaSBarry Smith 
383c0dedaeaSBarry Smith    Level: intermediate
384c0dedaeaSBarry Smith 
385a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
386c0dedaeaSBarry Smith @*/
387c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
388c0dedaeaSBarry Smith {
389c0dedaeaSBarry Smith   PetscFunctionBegin;
390c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
391c0dedaeaSBarry Smith   *ctype = dm->mattype;
392c0dedaeaSBarry Smith   PetscFunctionReturn(0);
393c0dedaeaSBarry Smith }
394c0dedaeaSBarry Smith 
395c688c046SMatthew G Knepley /*@
39634f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
397c688c046SMatthew G Knepley 
398c688c046SMatthew G Knepley   Not collective
399c688c046SMatthew G Knepley 
400c688c046SMatthew G Knepley   Input Parameter:
401c688c046SMatthew G Knepley . A - The Mat
402c688c046SMatthew G Knepley 
403c688c046SMatthew G Knepley   Output Parameter:
404c688c046SMatthew G Knepley . dm - The DM
405c688c046SMatthew G Knepley 
406c688c046SMatthew G Knepley   Level: intermediate
407c688c046SMatthew G Knepley 
4088f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4098f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4108f1509bcSBarry Smith 
411c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
412c688c046SMatthew G Knepley @*/
413c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
414c688c046SMatthew G Knepley {
415c688c046SMatthew G Knepley   PetscErrorCode ierr;
416c688c046SMatthew G Knepley 
417c688c046SMatthew G Knepley   PetscFunctionBegin;
418c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
419c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
420c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
421c688c046SMatthew G Knepley   PetscFunctionReturn(0);
422c688c046SMatthew G Knepley }
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley /*@
425c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
426c688c046SMatthew G Knepley 
427c688c046SMatthew G Knepley   Not collective
428c688c046SMatthew G Knepley 
429c688c046SMatthew G Knepley   Input Parameters:
430c688c046SMatthew G Knepley + A - The Mat
431c688c046SMatthew G Knepley - dm - The DM
432c688c046SMatthew G Knepley 
433c688c046SMatthew G Knepley   Level: intermediate
434c688c046SMatthew G Knepley 
4358f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4368f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4378f1509bcSBarry Smith 
4388f1509bcSBarry Smith 
439c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
440c688c046SMatthew G Knepley @*/
441c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
442c688c046SMatthew G Knepley {
443c688c046SMatthew G Knepley   PetscErrorCode ierr;
444c688c046SMatthew G Knepley 
445c688c046SMatthew G Knepley   PetscFunctionBegin;
446c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4478865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
448c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
449c688c046SMatthew G Knepley   PetscFunctionReturn(0);
450c688c046SMatthew G Knepley }
451c688c046SMatthew G Knepley 
4529a42bb27SBarry Smith /*@C
4539a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4546757b960SDave May    DM options in the database.
4559a42bb27SBarry Smith 
456d083f849SBarry Smith    Logically Collective on dm
4579a42bb27SBarry Smith 
4589a42bb27SBarry Smith    Input Parameter:
4598353ddbbSDave May +  da - the DM context
4609a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4619a42bb27SBarry Smith 
4629a42bb27SBarry Smith    Notes:
4639a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4649a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4659a42bb27SBarry Smith 
4669a42bb27SBarry Smith    Level: advanced
4679a42bb27SBarry Smith 
4689a42bb27SBarry Smith .seealso: DMSetFromOptions()
4699a42bb27SBarry Smith @*/
4707087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4719a42bb27SBarry Smith {
4729a42bb27SBarry Smith   PetscErrorCode ierr;
4739a42bb27SBarry Smith 
4749a42bb27SBarry Smith   PetscFunctionBegin;
4759a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4769a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
477691be533SLawrence Mitchell   if (dm->sf) {
478691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
479691be533SLawrence Mitchell   }
4801bb6d2a8SBarry Smith   if (dm->sectionSF) {
4811bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
482691be533SLawrence Mitchell   }
4839a42bb27SBarry Smith   PetscFunctionReturn(0);
4849a42bb27SBarry Smith }
4859a42bb27SBarry Smith 
48631697293SDave May /*@C
48731697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48831697293SDave May    DM options in the database.
48931697293SDave May 
490d083f849SBarry Smith    Logically Collective on dm
49131697293SDave May 
49231697293SDave May    Input Parameters:
49331697293SDave May +  dm - the DM context
49431697293SDave May -  prefix - the prefix string to prepend to all DM option requests
49531697293SDave May 
49631697293SDave May    Notes:
49731697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49831697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49931697293SDave May 
50031697293SDave May    Level: advanced
50131697293SDave May 
50231697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
50331697293SDave May @*/
50431697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
50531697293SDave May {
50631697293SDave May   PetscErrorCode ierr;
50731697293SDave May 
50831697293SDave May   PetscFunctionBegin;
50931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51031697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
51131697293SDave May   PetscFunctionReturn(0);
51231697293SDave May }
51331697293SDave May 
51431697293SDave May /*@C
51531697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
51631697293SDave May    DM options in the database.
51731697293SDave May 
51831697293SDave May    Not Collective
51931697293SDave May 
52031697293SDave May    Input Parameters:
52131697293SDave May .  dm - the DM context
52231697293SDave May 
52331697293SDave May    Output Parameters:
52431697293SDave May .  prefix - pointer to the prefix string used is returned
52531697293SDave May 
52695452b02SPatrick Sanan    Notes:
52795452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52831697293SDave May    sufficient length to hold the prefix.
52931697293SDave May 
53031697293SDave May    Level: advanced
53131697293SDave May 
53231697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
53331697293SDave May @*/
53431697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
53531697293SDave May {
53631697293SDave May   PetscErrorCode ierr;
53731697293SDave May 
53831697293SDave May   PetscFunctionBegin;
53931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54031697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
54131697293SDave May   PetscFunctionReturn(0);
54231697293SDave May }
54331697293SDave May 
54488bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
54588bdff64SToby Isaac {
5466eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
54788bdff64SToby Isaac   PetscErrorCode ierr;
54888bdff64SToby Isaac 
54988bdff64SToby Isaac   PetscFunctionBegin;
550aab5bcd8SJed Brown   *ncrefct = 0;
55188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55288bdff64SToby Isaac     refct--;
55388bdff64SToby Isaac     if (recurseCoarse) {
55488bdff64SToby Isaac       PetscInt coarseCount;
55588bdff64SToby Isaac 
55688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55788bdff64SToby Isaac       refct += coarseCount;
55888bdff64SToby Isaac     }
55988bdff64SToby Isaac   }
56088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56188bdff64SToby Isaac     refct--;
56288bdff64SToby Isaac     if (recurseFine) {
56388bdff64SToby Isaac       PetscInt fineCount;
56488bdff64SToby Isaac 
56588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56688bdff64SToby Isaac       refct += fineCount;
56788bdff64SToby Isaac     }
56888bdff64SToby Isaac   }
56988bdff64SToby Isaac   *ncrefct = refct;
57088bdff64SToby Isaac   PetscFunctionReturn(0);
57188bdff64SToby Isaac }
57288bdff64SToby Isaac 
573f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
574354557abSToby Isaac {
5755d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
576354557abSToby Isaac   PetscErrorCode ierr;
577354557abSToby Isaac 
578354557abSToby Isaac   PetscFunctionBegin;
579354557abSToby Isaac   /* destroy the labels */
580354557abSToby Isaac   while (next) {
581354557abSToby Isaac     DMLabelLink tmp = next->next;
582354557abSToby Isaac 
5835d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
584ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
585354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
586354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
587354557abSToby Isaac     next = tmp;
588354557abSToby Isaac   }
5895d80c0bfSVaclav Hapla   dm->labels = NULL;
590354557abSToby Isaac   PetscFunctionReturn(0);
591354557abSToby Isaac }
592354557abSToby Isaac 
59347c6ae99SBarry Smith /*@
5948472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59547c6ae99SBarry Smith 
596d083f849SBarry Smith     Collective on dm
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith     Input Parameter:
59947c6ae99SBarry Smith .   dm - the DM object to destroy
60047c6ae99SBarry Smith 
60147c6ae99SBarry Smith     Level: developer
60247c6ae99SBarry Smith 
603e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith @*/
606fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60747c6ae99SBarry Smith {
6086eb26441SStefano Zampini   PetscInt       cnt;
609dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61047c6ae99SBarry Smith   PetscErrorCode ierr;
61147c6ae99SBarry Smith 
61247c6ae99SBarry Smith   PetscFunctionBegin;
6136bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6146bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61587e657c6SBarry Smith 
61688bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61788bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61888bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
619ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6206bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6216bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6226eb26441SStefano Zampini 
6236eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6246eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6256eb26441SStefano Zampini 
626f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6270298fd71SBarry Smith   (*dm)->namedglobal = NULL;
628f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6292348bcf4SPeter Brune     nnext = nlink->next;
6302348bcf4SPeter 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);
6312348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6322348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6332348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6342348bcf4SPeter Brune   }
635f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6360298fd71SBarry Smith   (*dm)->namedlocal = NULL;
637f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
638f490541aSPeter Brune     nnext = nlink->next;
639f490541aSPeter 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);
640f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
641f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
642f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
643f490541aSPeter Brune   }
6442348bcf4SPeter Brune 
645b17ce1afSJed Brown   /* Destroy the list of hooks */
646c833c3b5SJed Brown   {
647c833c3b5SJed Brown     DMCoarsenHookLink link,next;
648b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
649b17ce1afSJed Brown       next = link->next;
650b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
651b17ce1afSJed Brown     }
6520298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
653c833c3b5SJed Brown   }
654c833c3b5SJed Brown   {
655c833c3b5SJed Brown     DMRefineHookLink link,next;
656c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
657c833c3b5SJed Brown       next = link->next;
658c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
659c833c3b5SJed Brown     }
6600298fd71SBarry Smith     (*dm)->refinehook = NULL;
661c833c3b5SJed Brown   }
662be081cd6SPeter Brune   {
663be081cd6SPeter Brune     DMSubDomainHookLink link,next;
664be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
665be081cd6SPeter Brune       next = link->next;
666be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
667be081cd6SPeter Brune     }
6680298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
669be081cd6SPeter Brune   }
670baf369e7SPeter Brune   {
671baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
672baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
673baf369e7SPeter Brune       next = link->next;
674baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
675baf369e7SPeter Brune     }
6760298fd71SBarry Smith     (*dm)->gtolhook = NULL;
677baf369e7SPeter Brune   }
678d4d07f1eSToby Isaac   {
679d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
680d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
681d4d07f1eSToby Isaac       next = link->next;
682d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
683d4d07f1eSToby Isaac     }
684d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
685d4d07f1eSToby Isaac   }
686aa1993deSMatthew G Knepley   /* Destroy the work arrays */
687aa1993deSMatthew G Knepley   {
688aa1993deSMatthew G Knepley     DMWorkLink link,next;
689aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
690aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
691aa1993deSMatthew G Knepley       next = link->next;
692aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
693aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
694aa1993deSMatthew G Knepley     }
6950298fd71SBarry Smith     (*dm)->workin = NULL;
696aa1993deSMatthew G Knepley   }
697c58f1c22SToby Isaac   /* destroy the labels */
698f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
699f4cdcedcSVaclav Hapla   /* destroy the fields */
700e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
701f4cdcedcSVaclav Hapla   /* destroy the boundaries */
702e6f8dbb6SToby Isaac   {
703e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
704e6f8dbb6SToby Isaac     while (next) {
705e6f8dbb6SToby Isaac       DMBoundary b = next;
706e6f8dbb6SToby Isaac 
707e6f8dbb6SToby Isaac       next = b->next;
708e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
709e6f8dbb6SToby Isaac     }
710e6f8dbb6SToby Isaac   }
711b17ce1afSJed Brown 
71252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
71352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
71452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
71552536dc3SBarry Smith 
7161a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7171a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7181a266240SBarry Smith   }
71971cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7206bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7216bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
722073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
72388ed4aceSMatthew G Knepley 
7241bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7251bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7268b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
727fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
728fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
72988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7301bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
731736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
732736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7338e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
734736995cdSBlaise Bourdin     }
735736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
736736995cdSBlaise Bourdin   }
73788bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
73888bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
73988bdff64SToby Isaac   }
7406eb26441SStefano Zampini 
741a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
74288bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
74388bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
74488bdff64SToby Isaac   }
74588bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
746f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7476636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7486636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7496636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
750412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
751412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
752412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
753ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
754ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
755ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7566636e97aSMatthew G Knepley 
757e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
75814f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
759e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
760e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
761732e2eb9SMatthew G Knepley 
762ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7636bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
764ed3c66a1SDave May   }
765c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
766435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
767732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
76847c6ae99SBarry Smith   PetscFunctionReturn(0);
76947c6ae99SBarry Smith }
77047c6ae99SBarry Smith 
771d7bf68aeSBarry Smith /*@
772d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
773d7bf68aeSBarry Smith 
774d083f849SBarry Smith     Collective on dm
775d7bf68aeSBarry Smith 
776d7bf68aeSBarry Smith     Input Parameter:
777d7bf68aeSBarry Smith .   dm - the DM object to setup
778d7bf68aeSBarry Smith 
779d7bf68aeSBarry Smith     Level: developer
780d7bf68aeSBarry Smith 
781e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
782d7bf68aeSBarry Smith 
783d7bf68aeSBarry Smith @*/
7847087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
785d7bf68aeSBarry Smith {
786d7bf68aeSBarry Smith   PetscErrorCode ierr;
787d7bf68aeSBarry Smith 
788d7bf68aeSBarry Smith   PetscFunctionBegin;
789171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7908387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
791d7bf68aeSBarry Smith   if (dm->ops->setup) {
792d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
793d7bf68aeSBarry Smith   }
7948387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
795d7bf68aeSBarry Smith   PetscFunctionReturn(0);
796d7bf68aeSBarry Smith }
797d7bf68aeSBarry Smith 
798d7bf68aeSBarry Smith /*@
799d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
800d7bf68aeSBarry Smith 
801d083f849SBarry Smith     Collective on dm
802d7bf68aeSBarry Smith 
803d7bf68aeSBarry Smith     Input Parameter:
804d7bf68aeSBarry Smith .   dm - the DM object to set options for
805d7bf68aeSBarry Smith 
806732e2eb9SMatthew G Knepley     Options Database:
8074164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8084164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8094164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8108f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
811732e2eb9SMatthew G Knepley 
812384a6580SVaclav Hapla     DMPLEX Specific Checks
813384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
814384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
815384a6580SVaclav 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()
816384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
817384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
818384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
819384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
820d7bf68aeSBarry Smith 
82195eb5ee5SVaclav Hapla     Level: intermediate
82295eb5ee5SVaclav Hapla 
82395eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
82495eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
825d7bf68aeSBarry Smith 
826d7bf68aeSBarry Smith @*/
8277087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
828d7bf68aeSBarry Smith {
8297781c08eSBarry Smith   char           typeName[256];
830ca266f36SBarry Smith   PetscBool      flg;
831d7bf68aeSBarry Smith   PetscErrorCode ierr;
832d7bf68aeSBarry Smith 
833d7bf68aeSBarry Smith   PetscFunctionBegin;
834171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83549be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
83649be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8371bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8383194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8390298fd71SBarry 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);
840a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
841f9ba7244SBarry Smith   if (flg) {
842f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
843f9ba7244SBarry Smith   }
844a264d7a6SBarry 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);
845073dac72SJed Brown   if (flg) {
846521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
847073dac72SJed Brown   }
8488f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
849f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
850e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
851f9ba7244SBarry Smith   }
852f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8530633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
85482fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
855d7bf68aeSBarry Smith   PetscFunctionReturn(0);
856d7bf68aeSBarry Smith }
857d7bf68aeSBarry Smith 
858fc9bc008SSatish Balay /*@C
859fe2efc57SMark    DMViewFromOptions - View from Options
860fe2efc57SMark 
861fe2efc57SMark    Collective on DM
862fe2efc57SMark 
863fe2efc57SMark    Input Parameters:
864fe2efc57SMark +  dm - the DM object
865736c3998SJose E. Roman .  obj - Optional object
866736c3998SJose E. Roman -  name - command line option
867fe2efc57SMark 
868fe2efc57SMark    Level: intermediate
869fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
870fe2efc57SMark @*/
871fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
872fe2efc57SMark {
873fe2efc57SMark   PetscErrorCode ierr;
874fe2efc57SMark 
875fe2efc57SMark   PetscFunctionBegin;
876fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
877fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
878fe2efc57SMark   PetscFunctionReturn(0);
879fe2efc57SMark }
880fe2efc57SMark 
881fe2efc57SMark /*@C
882224748a4SBarry Smith     DMView - Views a DM
88347c6ae99SBarry Smith 
884d083f849SBarry Smith     Collective on dm
88547c6ae99SBarry Smith 
88647c6ae99SBarry Smith     Input Parameter:
88747c6ae99SBarry Smith +   dm - the DM object to view
88847c6ae99SBarry Smith -   v - the viewer
88947c6ae99SBarry Smith 
890224748a4SBarry Smith     Level: beginner
89147c6ae99SBarry Smith 
892e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
89347c6ae99SBarry Smith 
89447c6ae99SBarry Smith @*/
8957087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
89647c6ae99SBarry Smith {
89747c6ae99SBarry Smith   PetscErrorCode    ierr;
89832c0f0efSBarry Smith   PetscBool         isbinary;
89976a8abe0SBarry Smith   PetscMPIInt       size;
90076a8abe0SBarry Smith   PetscViewerFormat format;
90147c6ae99SBarry Smith 
90247c6ae99SBarry Smith   PetscFunctionBegin;
903171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9043014e516SBarry Smith   if (!v) {
905ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9063014e516SBarry Smith   }
907b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
90874903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
90974903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
91074903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
91174903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
91274903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
91374903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
91474903a4fSStefano Zampini      in an error here */
91574903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9168bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
917b1b135c8SBarry Smith 
91876a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
91976a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
92076a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
92198c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
92232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
92332c0f0efSBarry Smith   if (isbinary) {
92455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
92532c0f0efSBarry Smith     char     type[256];
92632c0f0efSBarry Smith 
927f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
92832c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
929f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
93032c0f0efSBarry Smith   }
9310c010503SBarry Smith   if (dm->ops->view) {
9320c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
93347c6ae99SBarry Smith   }
93447c6ae99SBarry Smith   PetscFunctionReturn(0);
93547c6ae99SBarry Smith }
93647c6ae99SBarry Smith 
93747c6ae99SBarry Smith /*@
9388472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
93947c6ae99SBarry Smith 
940d083f849SBarry Smith     Collective on dm
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith     Input Parameter:
94347c6ae99SBarry Smith .   dm - the DM object
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith     Output Parameter:
94647c6ae99SBarry Smith .   vec - the global vector
94747c6ae99SBarry Smith 
948073dac72SJed Brown     Level: beginner
94947c6ae99SBarry Smith 
950ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith @*/
9537087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
95447c6ae99SBarry Smith {
95547c6ae99SBarry Smith   PetscErrorCode ierr;
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith   PetscFunctionBegin;
958171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
959b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
960b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
96147c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
96276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
963c6b011d8SStefano Zampini     DM vdm;
964c6b011d8SStefano Zampini 
965c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
966c6b011d8SStefano 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);
967c6b011d8SStefano Zampini   }
96847c6ae99SBarry Smith   PetscFunctionReturn(0);
96947c6ae99SBarry Smith }
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith /*@
9728472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith     Not Collective
97547c6ae99SBarry Smith 
97647c6ae99SBarry Smith     Input Parameter:
97747c6ae99SBarry Smith .   dm - the DM object
97847c6ae99SBarry Smith 
97947c6ae99SBarry Smith     Output Parameter:
98047c6ae99SBarry Smith .   vec - the local vector
98147c6ae99SBarry Smith 
982073dac72SJed Brown     Level: beginner
98347c6ae99SBarry Smith 
984ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
98547c6ae99SBarry Smith 
98647c6ae99SBarry Smith @*/
9877087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
98847c6ae99SBarry Smith {
98947c6ae99SBarry Smith   PetscErrorCode ierr;
99047c6ae99SBarry Smith 
99147c6ae99SBarry Smith   PetscFunctionBegin;
992171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
993b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
994b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
99547c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
99676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
997c6b011d8SStefano Zampini     DM vdm;
998c6b011d8SStefano Zampini 
999c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1000c6b011d8SStefano 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);
1001c6b011d8SStefano Zampini   }
100247c6ae99SBarry Smith   PetscFunctionReturn(0);
100347c6ae99SBarry Smith }
100447c6ae99SBarry Smith 
10051411c6eeSJed Brown /*@
10061411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10071411c6eeSJed Brown 
1008d083f849SBarry Smith    Collective on dm
10091411c6eeSJed Brown 
10101411c6eeSJed Brown    Input Parameter:
10111411c6eeSJed Brown .  dm - the DM that provides the mapping
10121411c6eeSJed Brown 
10131411c6eeSJed Brown    Output Parameter:
10141411c6eeSJed Brown .  ltog - the mapping
10151411c6eeSJed Brown 
10161411c6eeSJed Brown    Level: intermediate
10171411c6eeSJed Brown 
10181411c6eeSJed Brown    Notes:
10191411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10201411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10211411c6eeSJed Brown 
1022fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10231411c6eeSJed Brown @*/
10247087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10251411c6eeSJed Brown {
10260be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10271411c6eeSJed Brown   PetscErrorCode ierr;
10281411c6eeSJed Brown 
10291411c6eeSJed Brown   PetscFunctionBegin;
10301411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10311411c6eeSJed Brown   PetscValidPointer(ltog,2);
10321411c6eeSJed Brown   if (!dm->ltogmap) {
103337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
103437d0c07bSMatthew G Knepley 
103592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
103637d0c07bSMatthew G Knepley     if (section) {
1037a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
103837d0c07bSMatthew G Knepley       PetscInt       *ltog;
1039ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
104037d0c07bSMatthew G Knepley 
1041e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
104237d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1043ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1044ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
104537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1046a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
104737d0c07bSMatthew G Knepley 
104837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
104937d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1050a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1051a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
105237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10531a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10541a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10551a7dc684SMatthew G. Knepley         if (dof) {
1056b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1057b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10581a7dc684SMatthew G. Knepley         }
105937d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1060a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1061a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
106237d0c07bSMatthew G Knepley         }
106337d0c07bSMatthew G Knepley       }
1064bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10650be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10660be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10670be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10680be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10697591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10707591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1071ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1072ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1073ccf3bd66SMatthew G. Knepley         n /= bs;
1074ccf3bd66SMatthew G. Knepley       }
1075ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10763bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
107737d0c07bSMatthew G Knepley     } else {
1078b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1079184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10801411c6eeSJed Brown     }
108137d0c07bSMatthew G Knepley   }
10821411c6eeSJed Brown   *ltog = dm->ltogmap;
10831411c6eeSJed Brown   PetscFunctionReturn(0);
10841411c6eeSJed Brown }
10851411c6eeSJed Brown 
10861411c6eeSJed Brown /*@
10871411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10881411c6eeSJed Brown 
10891411c6eeSJed Brown    Not Collective
10901411c6eeSJed Brown 
10911411c6eeSJed Brown    Input Parameter:
10921411c6eeSJed Brown .  dm - the DM with block structure
10931411c6eeSJed Brown 
10941411c6eeSJed Brown    Output Parameter:
10951411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10961411c6eeSJed Brown 
10971411c6eeSJed Brown    Level: intermediate
10981411c6eeSJed Brown 
1099fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
11001411c6eeSJed Brown @*/
11017087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11021411c6eeSJed Brown {
11031411c6eeSJed Brown   PetscFunctionBegin;
11041411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1105534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11061411c6eeSJed 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");
11071411c6eeSJed Brown   *bs = dm->bs;
11081411c6eeSJed Brown   PetscFunctionReturn(0);
11091411c6eeSJed Brown }
11101411c6eeSJed Brown 
111147c6ae99SBarry Smith /*@
11128472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
111347c6ae99SBarry Smith 
1114a5bc1bf3SBarry Smith     Collective on dmc
111547c6ae99SBarry Smith 
111647c6ae99SBarry Smith     Input Parameter:
1117a5bc1bf3SBarry Smith +   dmc - the DM object
1118a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
111947c6ae99SBarry Smith 
112047c6ae99SBarry Smith     Output Parameter:
112147c6ae99SBarry Smith +  mat - the interpolation
112247c6ae99SBarry Smith -  vec - the scaling (optional)
112347c6ae99SBarry Smith 
112447c6ae99SBarry Smith     Level: developer
112547c6ae99SBarry Smith 
112695452b02SPatrick Sanan     Notes:
112795452b02SPatrick 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
112885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1129d52bd9f3SBarry Smith 
11301f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1131d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
113285afcc9aSBarry Smith 
113385afcc9aSBarry Smith 
11342ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
113547c6ae99SBarry Smith 
113647c6ae99SBarry Smith @*/
1137a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
113847c6ae99SBarry Smith {
113947c6ae99SBarry Smith   PetscErrorCode ierr;
114047c6ae99SBarry Smith 
114147c6ae99SBarry Smith   PetscFunctionBegin;
1142a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1143a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1144c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1145a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1146a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1147a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1148a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
114947c6ae99SBarry Smith   PetscFunctionReturn(0);
115047c6ae99SBarry Smith }
115147c6ae99SBarry Smith 
11523ad4599aSBarry Smith /*@
1153d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11542ed6491fSPatrick Sanan 
11552ed6491fSPatrick Sanan   Input Parameters:
11562ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11572ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11582ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11592ed6491fSPatrick Sanan 
11602ed6491fSPatrick Sanan   Output Parameter:
11612ed6491fSPatrick Sanan .    scale - the scaled vector
11622ed6491fSPatrick Sanan 
11632ed6491fSPatrick Sanan   Level: developer
11642ed6491fSPatrick Sanan 
11652ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
11662ed6491fSPatrick Sanan 
11672ed6491fSPatrick Sanan @*/
11682ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11692ed6491fSPatrick Sanan {
11702ed6491fSPatrick Sanan   PetscErrorCode ierr;
11712ed6491fSPatrick Sanan   Vec            fine;
11722ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11732ed6491fSPatrick Sanan 
11742ed6491fSPatrick Sanan   PetscFunctionBegin;
11752ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
11762ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
11772ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
11782ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
11792ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
11802ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
11812ed6491fSPatrick Sanan   PetscFunctionReturn(0);
11822ed6491fSPatrick Sanan }
11832ed6491fSPatrick Sanan 
11842ed6491fSPatrick Sanan /*@
11853ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11863ad4599aSBarry Smith 
1187a5bc1bf3SBarry Smith     Collective on dmc
11883ad4599aSBarry Smith 
11893ad4599aSBarry Smith     Input Parameter:
1190a5bc1bf3SBarry Smith +   dmc - the DM object
1191a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
11923ad4599aSBarry Smith 
11933ad4599aSBarry Smith     Output Parameter:
11943ad4599aSBarry Smith .  mat - the restriction
11953ad4599aSBarry Smith 
11963ad4599aSBarry Smith 
11973ad4599aSBarry Smith     Level: developer
11983ad4599aSBarry Smith 
119995452b02SPatrick Sanan     Notes:
120095452b02SPatrick 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
12013ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12023ad4599aSBarry Smith 
12033ad4599aSBarry Smith 
12043ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12053ad4599aSBarry Smith 
12063ad4599aSBarry Smith @*/
1207a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12083ad4599aSBarry Smith {
12093ad4599aSBarry Smith   PetscErrorCode ierr;
12103ad4599aSBarry Smith 
12113ad4599aSBarry Smith   PetscFunctionBegin;
1212a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1213a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12145a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1215a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1216a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1217a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1218a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12193ad4599aSBarry Smith   PetscFunctionReturn(0);
12203ad4599aSBarry Smith }
12213ad4599aSBarry Smith 
122247c6ae99SBarry Smith /*@
12238472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
122447c6ae99SBarry Smith 
1225a5bc1bf3SBarry Smith     Collective on dac
122647c6ae99SBarry Smith 
122747c6ae99SBarry Smith     Input Parameter:
1228a5bc1bf3SBarry Smith +   dac - the DM object
1229a5bc1bf3SBarry Smith -   daf - the second, finer DM object
123047c6ae99SBarry Smith 
123147c6ae99SBarry Smith     Output Parameter:
12326dbf9973SLawrence Mitchell .   mat - the injection
123347c6ae99SBarry Smith 
123447c6ae99SBarry Smith     Level: developer
123547c6ae99SBarry Smith 
123695452b02SPatrick Sanan    Notes:
123795452b02SPatrick 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
123885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
123985afcc9aSBarry Smith 
1240e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
124147c6ae99SBarry Smith 
124247c6ae99SBarry Smith @*/
1243a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
124447c6ae99SBarry Smith {
124547c6ae99SBarry Smith   PetscErrorCode ierr;
124647c6ae99SBarry Smith 
124747c6ae99SBarry Smith   PetscFunctionBegin;
1248a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1249a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12505a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1251a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1252a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1253a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1254a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
125547c6ae99SBarry Smith   PetscFunctionReturn(0);
125647c6ae99SBarry Smith }
125747c6ae99SBarry Smith 
1258b412c318SBarry Smith /*@
1259bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1260bd041c0cSMatthew G. Knepley 
1261a5bc1bf3SBarry Smith   Collective on dac
1262bd041c0cSMatthew G. Knepley 
1263bd041c0cSMatthew G. Knepley   Input Parameter:
1264a5bc1bf3SBarry Smith + dac - the DM object
1265a5bc1bf3SBarry Smith - daf - the second, finer DM object
1266bd041c0cSMatthew G. Knepley 
1267bd041c0cSMatthew G. Knepley   Output Parameter:
1268bd041c0cSMatthew G. Knepley . mat - the interpolation
1269bd041c0cSMatthew G. Knepley 
1270bd041c0cSMatthew G. Knepley   Level: developer
1271bd041c0cSMatthew G. Knepley 
1272bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1273bd041c0cSMatthew G. Knepley @*/
1274a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1275bd041c0cSMatthew G. Knepley {
1276bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1277bd041c0cSMatthew G. Knepley 
1278bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1279a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1280a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
12815a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1282a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1283a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1284bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1285bd041c0cSMatthew G. Knepley }
1286bd041c0cSMatthew G. Knepley 
1287bd041c0cSMatthew G. Knepley /*@
1288b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
128947c6ae99SBarry Smith 
1290d083f849SBarry Smith     Collective on dm
129147c6ae99SBarry Smith 
129247c6ae99SBarry Smith     Input Parameter:
129347c6ae99SBarry Smith +   dm - the DM object
12945bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
129547c6ae99SBarry Smith 
129647c6ae99SBarry Smith     Output Parameter:
129747c6ae99SBarry Smith .   coloring - the coloring
129847c6ae99SBarry Smith 
1299ec5066bdSBarry Smith     Notes:
1300ec5066bdSBarry 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
1301ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1302ec5066bdSBarry Smith 
1303ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1304ec5066bdSBarry Smith 
130547c6ae99SBarry Smith     Level: developer
130647c6ae99SBarry Smith 
1307ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
130847c6ae99SBarry Smith 
1309aab9d709SJed Brown @*/
1310b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
131147c6ae99SBarry Smith {
131247c6ae99SBarry Smith   PetscErrorCode ierr;
131347c6ae99SBarry Smith 
131447c6ae99SBarry Smith   PetscFunctionBegin;
1315171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13165a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1317b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1318b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
131947c6ae99SBarry Smith   PetscFunctionReturn(0);
132047c6ae99SBarry Smith }
132147c6ae99SBarry Smith 
1322b412c318SBarry Smith /*@
13238472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
132447c6ae99SBarry Smith 
1325d083f849SBarry Smith     Collective on dm
132647c6ae99SBarry Smith 
132747c6ae99SBarry Smith     Input Parameter:
1328b412c318SBarry Smith .   dm - the DM object
132947c6ae99SBarry Smith 
133047c6ae99SBarry Smith     Output Parameter:
133147c6ae99SBarry Smith .   mat - the empty Jacobian
133247c6ae99SBarry Smith 
1333073dac72SJed Brown     Level: beginner
133447c6ae99SBarry Smith 
133595452b02SPatrick Sanan     Notes:
133695452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
133794013140SBarry Smith        do not need to do it yourself.
133894013140SBarry Smith 
133994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13407889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
134194013140SBarry Smith 
134294013140SBarry 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
134394013140SBarry Smith        internally by PETSc.
134494013140SBarry Smith 
134594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1346aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
134794013140SBarry Smith 
1348b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
134947c6ae99SBarry Smith 
1350aab9d709SJed Brown @*/
1351b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
135247c6ae99SBarry Smith {
135347c6ae99SBarry Smith   PetscErrorCode ierr;
135447c6ae99SBarry Smith 
135547c6ae99SBarry Smith   PetscFunctionBegin;
1356171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1357c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1358b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13595a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1360fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1361b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
136276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1363c6b011d8SStefano Zampini     DM mdm;
1364c6b011d8SStefano Zampini 
1365c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1366c6b011d8SStefano 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);
1367c6b011d8SStefano Zampini   }
1368e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1369e5e52638SMatthew G. Knepley   if (dm->Nf) {
1370e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1371649ef022SMatthew Knepley     PetscInt     Nf, f;
1372e571a35bSMatthew G. Knepley 
1373e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1374649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1375649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1376649ef022SMatthew Knepley         ierr = (*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1377e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1378e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1379649ef022SMatthew Knepley         break;
1380e571a35bSMatthew G. Knepley       }
1381649ef022SMatthew Knepley     }
1382649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1383649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1384649ef022SMatthew Knepley         ierr = (*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1385e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1386e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1387e571a35bSMatthew G. Knepley       }
1388e571a35bSMatthew G. Knepley     }
1389e571a35bSMatthew G. Knepley   }
1390fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
139147c6ae99SBarry Smith   PetscFunctionReturn(0);
139247c6ae99SBarry Smith }
139347c6ae99SBarry Smith 
1394732e2eb9SMatthew G Knepley /*@
1395950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1396732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1397732e2eb9SMatthew G Knepley 
1398d083f849SBarry Smith   Logically Collective on dm
1399732e2eb9SMatthew G Knepley 
1400732e2eb9SMatthew G Knepley   Input Parameter:
1401732e2eb9SMatthew G Knepley + dm - the DM
1402732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1403732e2eb9SMatthew G Knepley 
1404732e2eb9SMatthew G Knepley   Level: developer
1405b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1406732e2eb9SMatthew G Knepley @*/
1407732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1408732e2eb9SMatthew G Knepley {
1409732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1410732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1411732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1412732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1413732e2eb9SMatthew G Knepley }
1414732e2eb9SMatthew G Knepley 
1415b06ff27eSHong Zhang /*@
1416b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1417b06ff27eSHong Zhang     but the array for values will not be allocated.
1418b06ff27eSHong Zhang 
1419d083f849SBarry Smith   Logically Collective on dm
1420b06ff27eSHong Zhang 
1421b06ff27eSHong Zhang   Input Parameter:
1422b06ff27eSHong Zhang + dm - the DM
1423b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1424b06ff27eSHong Zhang 
1425b06ff27eSHong Zhang   Level: developer
1426b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1427b06ff27eSHong Zhang @*/
1428b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1429b06ff27eSHong Zhang {
1430b06ff27eSHong Zhang   PetscFunctionBegin;
1431b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1432b06ff27eSHong Zhang   dm->structure_only = only;
1433b06ff27eSHong Zhang   PetscFunctionReturn(0);
1434b06ff27eSHong Zhang }
1435b06ff27eSHong Zhang 
1436a89ea682SMatthew G Knepley /*@C
1437aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1438a89ea682SMatthew G Knepley 
1439a89ea682SMatthew G Knepley   Not Collective
1440a89ea682SMatthew G Knepley 
1441a89ea682SMatthew G Knepley   Input Parameters:
1442a89ea682SMatthew G Knepley + dm - the DM object
1443aa1993deSMatthew G Knepley . count - The minium size
144469291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1445a89ea682SMatthew G Knepley 
1446a89ea682SMatthew G Knepley   Output Parameter:
1447a89ea682SMatthew G Knepley . array - the work array
1448a89ea682SMatthew G Knepley 
1449a89ea682SMatthew G Knepley   Level: developer
1450a89ea682SMatthew G Knepley 
1451a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1452a89ea682SMatthew G Knepley @*/
145369291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1454a89ea682SMatthew G Knepley {
1455a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1456aa1993deSMatthew G Knepley   DMWorkLink     link;
145769291d52SBarry Smith   PetscMPIInt    dsize;
1458a89ea682SMatthew G Knepley 
1459a89ea682SMatthew G Knepley   PetscFunctionBegin;
1460a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1461aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1462aa1993deSMatthew G Knepley   if (dm->workin) {
1463aa1993deSMatthew G Knepley     link       = dm->workin;
1464aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1465aa1993deSMatthew G Knepley   } else {
1466b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1467a89ea682SMatthew G Knepley   }
146869291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14695056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1470aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1471854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1472854ce69bSBarry Smith     link->bytes = dsize*count;
1473aa1993deSMatthew G Knepley   }
1474aa1993deSMatthew G Knepley   link->next   = dm->workout;
1475aa1993deSMatthew G Knepley   dm->workout  = link;
147600d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
147700d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
147800d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
147900d952a4SJed Brown #endif
1480aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1481a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1482a89ea682SMatthew G Knepley }
1483a89ea682SMatthew G Knepley 
1484aa1993deSMatthew G Knepley /*@C
1485aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1486aa1993deSMatthew G Knepley 
1487aa1993deSMatthew G Knepley   Not Collective
1488aa1993deSMatthew G Knepley 
1489aa1993deSMatthew G Knepley   Input Parameters:
1490aa1993deSMatthew G Knepley + dm - the DM object
1491aa1993deSMatthew G Knepley . count - The minium size
149269291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1493aa1993deSMatthew G Knepley 
1494aa1993deSMatthew G Knepley   Output Parameter:
1495aa1993deSMatthew G Knepley . array - the work array
1496aa1993deSMatthew G Knepley 
1497aa1993deSMatthew G Knepley   Level: developer
1498aa1993deSMatthew G Knepley 
149995452b02SPatrick Sanan   Developer Notes:
150095452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1501aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1502aa1993deSMatthew G Knepley @*/
150369291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1504aa1993deSMatthew G Knepley {
1505aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1506aa1993deSMatthew G Knepley 
1507aa1993deSMatthew G Knepley   PetscFunctionBegin;
1508aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1509aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1510aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1511aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1512aa1993deSMatthew G Knepley       *p           = link->next;
1513aa1993deSMatthew G Knepley       link->next   = dm->workin;
1514aa1993deSMatthew G Knepley       dm->workin   = link;
15150298fd71SBarry Smith       *(void**)mem = NULL;
1516aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1517aa1993deSMatthew G Knepley     }
1518aa1993deSMatthew G Knepley   }
1519aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1520aa1993deSMatthew G Knepley }
1521e7c4fc90SDmitry Karpeev 
15228cda7954SMatthew G. Knepley /*@C
15238cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
15248cda7954SMatthew G. Knepley 
15258cda7954SMatthew G. Knepley   Logically collective on DM
15268cda7954SMatthew G. Knepley 
15278cda7954SMatthew G. Knepley   Input Parameters:
15288cda7954SMatthew G. Knepley + dm     - The DM
15298cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15308cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
15318cda7954SMatthew G. Knepley 
15328cda7954SMatthew G. Knepley   Notes:
15338cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15348cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15358cda7954SMatthew G. Knepley $ dm        - The present DM
15368cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15378cda7954SMatthew G. Knepley $ field     - The field number in dm
15388cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15398cda7954SMatthew G. Knepley 
15408cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15418cda7954SMatthew G. Knepley 
15428cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15438cda7954SMatthew G. Knepley */
15448cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1545435a35e8SMatthew G Knepley {
1546435a35e8SMatthew G Knepley   PetscFunctionBegin;
1547435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
154882f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1549435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1550435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1551435a35e8SMatthew G Knepley }
1552435a35e8SMatthew G Knepley 
15538cda7954SMatthew G. Knepley /*@C
15548cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
15558cda7954SMatthew G. Knepley 
15568cda7954SMatthew G. Knepley   Not collective
15578cda7954SMatthew G. Knepley 
15588cda7954SMatthew G. Knepley   Input Parameters:
15598cda7954SMatthew G. Knepley + dm     - The DM
15608cda7954SMatthew G. Knepley - field  - The field number for the nullspace
15618cda7954SMatthew G. Knepley 
15628cda7954SMatthew G. Knepley   Output Parameter:
15638cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
15648cda7954SMatthew G. Knepley 
15658cda7954SMatthew G. Knepley   Notes:
15668cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15678cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15688cda7954SMatthew G. Knepley $ dm        - The present DM
15698cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15708cda7954SMatthew G. Knepley $ field     - The field number in dm
15718cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15728cda7954SMatthew G. Knepley 
15738cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15748cda7954SMatthew G. Knepley 
15758cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15768cda7954SMatthew G. Knepley */
15778cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
15780a50eb56SMatthew G. Knepley {
15790a50eb56SMatthew G. Knepley   PetscFunctionBegin;
15800a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1581f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
15820a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15830a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15840a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15850a50eb56SMatthew G. Knepley }
15860a50eb56SMatthew G. Knepley 
15878cda7954SMatthew G. Knepley /*@C
15888cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
15898cda7954SMatthew G. Knepley 
15908cda7954SMatthew G. Knepley   Logically collective on DM
15918cda7954SMatthew G. Knepley 
15928cda7954SMatthew G. Knepley   Input Parameters:
15938cda7954SMatthew G. Knepley + dm     - The DM
15948cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15958cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
15968cda7954SMatthew G. Knepley 
15978cda7954SMatthew G. Knepley   Notes:
15988cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15998cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16008cda7954SMatthew G. Knepley $ dm        - The present DM
16018cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16028cda7954SMatthew G. Knepley $ field     - The field number in dm
16038cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16048cda7954SMatthew G. Knepley 
16058cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16068cda7954SMatthew G. Knepley 
16078cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16088cda7954SMatthew G. Knepley */
16098cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1610f9d4088aSMatthew G. Knepley {
1611f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1612f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1613f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1614f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1615f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1616f9d4088aSMatthew G. Knepley }
1617f9d4088aSMatthew G. Knepley 
16188cda7954SMatthew G. Knepley /*@C
16198cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
16208cda7954SMatthew G. Knepley 
16218cda7954SMatthew G. Knepley   Not collective
16228cda7954SMatthew G. Knepley 
16238cda7954SMatthew G. Knepley   Input Parameters:
16248cda7954SMatthew G. Knepley + dm     - The DM
16258cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16268cda7954SMatthew G. Knepley 
16278cda7954SMatthew G. Knepley   Output Parameter:
16288cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
16298cda7954SMatthew G. Knepley 
16308cda7954SMatthew G. Knepley   Notes:
16318cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16328cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16338cda7954SMatthew G. Knepley $ dm        - The present DM
16348cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16358cda7954SMatthew G. Knepley $ field     - The field number in dm
16368cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16378cda7954SMatthew G. Knepley 
16388cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16398cda7954SMatthew G. Knepley 
16408cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16418cda7954SMatthew G. Knepley */
16428cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1643f9d4088aSMatthew G. Knepley {
1644f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1645f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1646f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1647f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1648f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1649f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1650f9d4088aSMatthew G. Knepley }
1651f9d4088aSMatthew G. Knepley 
16524f3b5142SJed Brown /*@C
16534d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
16544d343eeaSMatthew G Knepley 
16554d343eeaSMatthew G Knepley   Not collective
16564d343eeaSMatthew G Knepley 
16574d343eeaSMatthew G Knepley   Input Parameter:
16584d343eeaSMatthew G Knepley . dm - the DM object
16594d343eeaSMatthew G Knepley 
16604d343eeaSMatthew G Knepley   Output Parameters:
16610298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
16620298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
16630298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
16644d343eeaSMatthew G Knepley 
16654d343eeaSMatthew G Knepley   Level: intermediate
16664d343eeaSMatthew G Knepley 
166721c9b008SJed Brown   Notes:
166821c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
166921c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
167021c9b008SJed Brown   PetscFree().
167121c9b008SJed Brown 
16724d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
16734d343eeaSMatthew G Knepley @*/
167437d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
16754d343eeaSMatthew G Knepley {
167637d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
16774d343eeaSMatthew G Knepley   PetscErrorCode ierr;
16784d343eeaSMatthew G Knepley 
16794d343eeaSMatthew G Knepley   PetscFunctionBegin;
16804d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
168169ca1f37SDmitry Karpeev   if (numFields) {
1682534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
168369ca1f37SDmitry Karpeev     *numFields = 0;
168469ca1f37SDmitry Karpeev   }
168537d0c07bSMatthew G Knepley   if (fieldNames) {
168637d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
16870298fd71SBarry Smith     *fieldNames = NULL;
168869ca1f37SDmitry Karpeev   }
168969ca1f37SDmitry Karpeev   if (fields) {
169069ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
16910298fd71SBarry Smith     *fields = NULL;
169269ca1f37SDmitry Karpeev   }
169392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
169437d0c07bSMatthew G Knepley   if (section) {
16953a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
169637d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
169737d0c07bSMatthew G Knepley 
1698e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
169937d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
17003a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
170137d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
170237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
170337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
17043a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
170537d0c07bSMatthew G Knepley     }
170637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
170737d0c07bSMatthew G Knepley       PetscInt gdof;
170837d0c07bSMatthew G Knepley 
170937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
171037d0c07bSMatthew G Knepley       if (gdof > 0) {
171137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
17123a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
171337d0c07bSMatthew G Knepley 
171437d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
171537d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
17163a544194SStefano Zampini           fpdof = fdof-fcdof;
17173a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
17183a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
17193a544194SStefano Zampini             fieldNc[f] = 1;
17203a544194SStefano Zampini           }
17213a544194SStefano Zampini           fieldSizes[f] += fpdof;
172237d0c07bSMatthew G Knepley         }
172337d0c07bSMatthew G Knepley       }
172437d0c07bSMatthew G Knepley     }
172537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1726785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
172737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
172837d0c07bSMatthew G Knepley     }
172937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
173037d0c07bSMatthew G Knepley       PetscInt gdof, goff;
173137d0c07bSMatthew G Knepley 
173237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
173337d0c07bSMatthew G Knepley       if (gdof > 0) {
173437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
173537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
173637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
173737d0c07bSMatthew G Knepley 
173837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
173937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
174037d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
174137d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
174237d0c07bSMatthew G Knepley           }
174337d0c07bSMatthew G Knepley         }
174437d0c07bSMatthew G Knepley       }
174537d0c07bSMatthew G Knepley     }
17468865f1eaSKarl Rupp     if (numFields) *numFields = nF;
174737d0c07bSMatthew G Knepley     if (fieldNames) {
1748785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
174937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
175037d0c07bSMatthew G Knepley         const char *fieldName;
175137d0c07bSMatthew G Knepley 
175237d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
175337d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
175437d0c07bSMatthew G Knepley       }
175537d0c07bSMatthew G Knepley     }
175637d0c07bSMatthew G Knepley     if (fields) {
1757785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
175837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
17593a544194SStefano Zampini         PetscInt bs, in[2], out[2];
17603a544194SStefano Zampini 
176182f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
17623a544194SStefano Zampini         in[0] = -fieldNc[f];
17633a544194SStefano Zampini         in[1] = fieldNc[f];
17643a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
17653a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
17663a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
176737d0c07bSMatthew G Knepley       }
176837d0c07bSMatthew G Knepley     }
17693a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
17708865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
17718865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
177269ca1f37SDmitry Karpeev   }
17734d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
17744d343eeaSMatthew G Knepley }
17754d343eeaSMatthew G Knepley 
177616621825SDmitry Karpeev 
177716621825SDmitry Karpeev /*@C
177816621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
177916621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
178016621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1781e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1782e7c4fc90SDmitry Karpeev 
1783e7c4fc90SDmitry Karpeev   Not collective
1784e7c4fc90SDmitry Karpeev 
1785e7c4fc90SDmitry Karpeev   Input Parameter:
1786e7c4fc90SDmitry Karpeev . dm - the DM object
1787e7c4fc90SDmitry Karpeev 
1788e7c4fc90SDmitry Karpeev   Output Parameters:
17890298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
17900298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
17910298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
17920298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1793e7c4fc90SDmitry Karpeev 
1794e7c4fc90SDmitry Karpeev   Level: intermediate
1795e7c4fc90SDmitry Karpeev 
1796e7c4fc90SDmitry Karpeev   Notes:
1797e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1798e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1799e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1800e7c4fc90SDmitry Karpeev 
1801e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1802e7c4fc90SDmitry Karpeev @*/
180316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1804e7c4fc90SDmitry Karpeev {
1805e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1806e7c4fc90SDmitry Karpeev 
1807e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1808e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18098865f1eaSKarl Rupp   if (len) {
1810534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
18118865f1eaSKarl Rupp     *len = 0;
18128865f1eaSKarl Rupp   }
18138865f1eaSKarl Rupp   if (namelist) {
18148865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1815ea78f98cSLisandro Dalcin     *namelist = NULL;
18168865f1eaSKarl Rupp   }
18178865f1eaSKarl Rupp   if (islist) {
18188865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1819ea78f98cSLisandro Dalcin     *islist = NULL;
18208865f1eaSKarl Rupp   }
18218865f1eaSKarl Rupp   if (dmlist) {
18228865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1823ea78f98cSLisandro Dalcin     *dmlist = NULL;
18248865f1eaSKarl Rupp   }
1825f3f0edfdSDmitry Karpeev   /*
1826f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1827f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1828f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1829f3f0edfdSDmitry Karpeev    */
1830ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
183116621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1832435a35e8SMatthew G Knepley     PetscSection section;
1833435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1834435a35e8SMatthew G Knepley 
183592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1836435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1837435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1838f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
183903dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
184003dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
184103dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1842435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1843435a35e8SMatthew G Knepley         const char *fieldName;
1844435a35e8SMatthew G Knepley 
184503dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
184603dc3394SMatthew G. Knepley         if (namelist) {
1847435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1848435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1849435a35e8SMatthew G Knepley         }
185003dc3394SMatthew G. Knepley       }
1851435a35e8SMatthew G Knepley     } else {
185269ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1853e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
18540298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1855e7c4fc90SDmitry Karpeev     }
18568865f1eaSKarl Rupp   } else {
185716621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
185816621825SDmitry Karpeev   }
185916621825SDmitry Karpeev   PetscFunctionReturn(0);
186016621825SDmitry Karpeev }
186116621825SDmitry Karpeev 
1862564cec59SMatthew G. Knepley /*@
1863435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1864435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1865435a35e8SMatthew G Knepley 
1866435a35e8SMatthew G Knepley   Not collective
1867435a35e8SMatthew G Knepley 
1868435a35e8SMatthew G Knepley   Input Parameters:
18692adcc780SMatthew G. Knepley + dm        - The DM object
18702adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
18712adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1872435a35e8SMatthew G Knepley 
1873435a35e8SMatthew G Knepley   Output Parameters:
18742adcc780SMatthew G. Knepley + is - The global indices for the subproblem
18752adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1876435a35e8SMatthew G Knepley 
18775d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
18785d3b26e6SMatthew G. Knepley 
1879435a35e8SMatthew G Knepley   Level: intermediate
1880435a35e8SMatthew G Knepley 
18815d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1882435a35e8SMatthew G Knepley @*/
188337bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1884435a35e8SMatthew G Knepley {
1885435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1886435a35e8SMatthew G Knepley 
1887435a35e8SMatthew G Knepley   PetscFunctionBegin;
1888435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1889435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
18908865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
18918865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1892b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1893435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1894435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1895435a35e8SMatthew G Knepley }
1896435a35e8SMatthew G Knepley 
18972adcc780SMatthew G. Knepley /*@C
18982adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
18992adcc780SMatthew G. Knepley 
19002adcc780SMatthew G. Knepley   Not collective
19012adcc780SMatthew G. Knepley 
19022adcc780SMatthew G. Knepley   Input Parameter:
19032adcc780SMatthew G. Knepley + dms - The DM objects
19042adcc780SMatthew G. Knepley - len - The number of DMs
19052adcc780SMatthew G. Knepley 
19062adcc780SMatthew G. Knepley   Output Parameters:
1907a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
19082adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19092adcc780SMatthew G. Knepley 
19105d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19115d3b26e6SMatthew G. Knepley 
19122adcc780SMatthew G. Knepley   Level: intermediate
19132adcc780SMatthew G. Knepley 
19145d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
19152adcc780SMatthew G. Knepley @*/
19162adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
19172adcc780SMatthew G. Knepley {
19182adcc780SMatthew G. Knepley   PetscInt       i;
19192adcc780SMatthew G. Knepley   PetscErrorCode ierr;
19202adcc780SMatthew G. Knepley 
19212adcc780SMatthew G. Knepley   PetscFunctionBegin;
19222adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
19232adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
19242adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1925a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
19262adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
19272adcc780SMatthew G. Knepley   if (len) {
1928b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1929b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1930b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
19312adcc780SMatthew G. Knepley   }
19322adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
19332adcc780SMatthew G. Knepley }
19342adcc780SMatthew G. Knepley 
193516621825SDmitry Karpeev 
193616621825SDmitry Karpeev /*@C
19378d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
19388d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
19398d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
19408d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
19418d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
194216621825SDmitry Karpeev 
194316621825SDmitry Karpeev   Not collective
194416621825SDmitry Karpeev 
194516621825SDmitry Karpeev   Input Parameter:
194616621825SDmitry Karpeev . dm - the DM object
194716621825SDmitry Karpeev 
194816621825SDmitry Karpeev   Output Parameters:
19490298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
19500298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
19510298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
19520298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
19530298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
195416621825SDmitry Karpeev 
195516621825SDmitry Karpeev   Level: intermediate
195616621825SDmitry Karpeev 
195716621825SDmitry Karpeev   Notes:
195816621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
195916621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
196016621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
196116621825SDmitry Karpeev 
1962245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
196316621825SDmitry Karpeev @*/
19648d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
196516621825SDmitry Karpeev {
196616621825SDmitry Karpeev   PetscErrorCode      ierr;
1967be081cd6SPeter Brune   DMSubDomainHookLink link;
1968be081cd6SPeter Brune   PetscInt            i,l;
196916621825SDmitry Karpeev 
197016621825SDmitry Karpeev   PetscFunctionBegin;
197116621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
197214a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
19730298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
19740298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
19750298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
19760298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1977f3f0edfdSDmitry Karpeev   /*
1978f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1979f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1980f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1981f3f0edfdSDmitry Karpeev    */
1982ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
198316621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1984be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
198514a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1986f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1987be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1988be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1989be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1990be081cd6SPeter Brune         }
1991648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1992e7c4fc90SDmitry Karpeev       }
199314a18fd3SPeter Brune     }
199414a18fd3SPeter Brune     if (len) *len = l;
199514a18fd3SPeter Brune   }
1996e30e807fSPeter Brune   PetscFunctionReturn(0);
1997e30e807fSPeter Brune }
1998e30e807fSPeter Brune 
1999e30e807fSPeter Brune 
2000e30e807fSPeter Brune /*@C
2001e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2002e30e807fSPeter Brune 
2003e30e807fSPeter Brune   Not collective
2004e30e807fSPeter Brune 
2005e30e807fSPeter Brune   Input Parameters:
2006e30e807fSPeter Brune + dm - the DM object
2007e30e807fSPeter Brune . n  - the number of subdomain scatters
2008e30e807fSPeter Brune - subdms - the local subdomains
2009e30e807fSPeter Brune 
2010e30e807fSPeter Brune   Output Parameters:
2011e30e807fSPeter Brune + n     - the number of scatters returned
2012e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2013e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2014e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2015e30e807fSPeter Brune 
201695452b02SPatrick Sanan   Notes:
201795452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2018e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2019e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2020e30e807fSPeter Brune   solution and residual data.
2021e30e807fSPeter Brune 
2022e30e807fSPeter Brune   Level: developer
2023e30e807fSPeter Brune 
2024e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2025e30e807fSPeter Brune @*/
2026e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2027e30e807fSPeter Brune {
2028e30e807fSPeter Brune   PetscErrorCode ierr;
2029e30e807fSPeter Brune 
2030e30e807fSPeter Brune   PetscFunctionBegin;
2031e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2032e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2033b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2034e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2035e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2036e7c4fc90SDmitry Karpeev }
2037e7c4fc90SDmitry Karpeev 
203847c6ae99SBarry Smith /*@
203947c6ae99SBarry Smith   DMRefine - Refines a DM object
204047c6ae99SBarry Smith 
2041d083f849SBarry Smith   Collective on dm
204247c6ae99SBarry Smith 
204347c6ae99SBarry Smith   Input Parameter:
204447c6ae99SBarry Smith + dm   - the DM object
204591d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
204647c6ae99SBarry Smith 
204747c6ae99SBarry Smith   Output Parameter:
20480298fd71SBarry Smith . dmf - the refined DM, or NULL
2049ae0a1c52SMatthew G Knepley 
2050412e9a14SMatthew G. Knepley   Options Dtabase Keys:
2051412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2052412e9a14SMatthew G. Knepley 
20530298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
205447c6ae99SBarry Smith 
205547c6ae99SBarry Smith   Level: developer
205647c6ae99SBarry Smith 
2057e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
205847c6ae99SBarry Smith @*/
20597087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
206047c6ae99SBarry Smith {
206147c6ae99SBarry Smith   PetscErrorCode   ierr;
2062c833c3b5SJed Brown   DMRefineHookLink link;
206347c6ae99SBarry Smith 
206447c6ae99SBarry Smith   PetscFunctionBegin;
2065732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2066b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
20671ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
206847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
20694057135bSMatthew G Knepley   if (*dmf) {
207043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
20718865f1eaSKarl Rupp 
20728cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
20738865f1eaSKarl Rupp 
2074644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
20750598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2076656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
20778865f1eaSKarl Rupp 
2078e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2079c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
20808865f1eaSKarl Rupp       if (link->refinehook) {
20818865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
20828865f1eaSKarl Rupp       }
2083c833c3b5SJed Brown     }
2084c833c3b5SJed Brown   }
20851ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2086c833c3b5SJed Brown   PetscFunctionReturn(0);
2087c833c3b5SJed Brown }
2088c833c3b5SJed Brown 
2089bb9467b5SJed Brown /*@C
2090c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2091c833c3b5SJed Brown 
2092c833c3b5SJed Brown    Logically Collective
2093c833c3b5SJed Brown 
2094c833c3b5SJed Brown    Input Arguments:
2095c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2096c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2097c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20980298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2099c833c3b5SJed Brown 
2100c833c3b5SJed Brown    Calling sequence of refinehook:
2101c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2102c833c3b5SJed Brown 
2103c833c3b5SJed Brown +  coarse - coarse level DM
2104c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2105c833c3b5SJed Brown -  ctx - optional user-defined function context
2106c833c3b5SJed Brown 
2107c833c3b5SJed Brown    Calling sequence for interphook:
2108c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2109c833c3b5SJed Brown 
2110c833c3b5SJed Brown +  coarse - coarse level DM
2111c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2112c833c3b5SJed Brown .  fine - fine level DM to update
2113c833c3b5SJed Brown -  ctx - optional user-defined function context
2114c833c3b5SJed Brown 
2115c833c3b5SJed Brown    Level: advanced
2116c833c3b5SJed Brown 
2117c833c3b5SJed Brown    Notes:
2118c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2119c833c3b5SJed Brown 
2120c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2121c833c3b5SJed Brown 
2122bb9467b5SJed Brown    This function is currently not available from Fortran.
2123bb9467b5SJed Brown 
2124c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2125c833c3b5SJed Brown @*/
2126c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2127c833c3b5SJed Brown {
2128c833c3b5SJed Brown   PetscErrorCode   ierr;
2129c833c3b5SJed Brown   DMRefineHookLink link,*p;
2130c833c3b5SJed Brown 
2131c833c3b5SJed Brown   PetscFunctionBegin;
2132c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21333d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
21343d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
21353d8e3701SJed Brown   }
213695dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2137c833c3b5SJed Brown   link->refinehook = refinehook;
2138c833c3b5SJed Brown   link->interphook = interphook;
2139c833c3b5SJed Brown   link->ctx        = ctx;
21400298fd71SBarry Smith   link->next       = NULL;
2141c833c3b5SJed Brown   *p               = link;
2142c833c3b5SJed Brown   PetscFunctionReturn(0);
2143c833c3b5SJed Brown }
2144c833c3b5SJed Brown 
21453d8e3701SJed Brown /*@C
21463d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
21473d8e3701SJed Brown 
21483d8e3701SJed Brown    Logically Collective
21493d8e3701SJed Brown 
21503d8e3701SJed Brown    Input Arguments:
21513d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
21523d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
21533d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21543d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
21553d8e3701SJed Brown 
21563d8e3701SJed Brown    Level: advanced
21573d8e3701SJed Brown 
21583d8e3701SJed Brown    Notes:
21593d8e3701SJed Brown    This function does nothing if the hook is not in the list.
21603d8e3701SJed Brown 
21613d8e3701SJed Brown    This function is currently not available from Fortran.
21623d8e3701SJed Brown 
21633d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
21643d8e3701SJed Brown @*/
21653d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
21663d8e3701SJed Brown {
21673d8e3701SJed Brown   PetscErrorCode   ierr;
21683d8e3701SJed Brown   DMRefineHookLink link,*p;
21693d8e3701SJed Brown 
21703d8e3701SJed Brown   PetscFunctionBegin;
21713d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21723d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
21733d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
21743d8e3701SJed Brown       link = *p;
21753d8e3701SJed Brown       *p = link->next;
21763d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
21773d8e3701SJed Brown       break;
21783d8e3701SJed Brown     }
21793d8e3701SJed Brown   }
21803d8e3701SJed Brown   PetscFunctionReturn(0);
21813d8e3701SJed Brown }
21823d8e3701SJed Brown 
2183c833c3b5SJed Brown /*@
2184c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2185c833c3b5SJed Brown 
2186c833c3b5SJed Brown    Collective if any hooks are
2187c833c3b5SJed Brown 
2188c833c3b5SJed Brown    Input Arguments:
2189c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2190e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2191c833c3b5SJed Brown -  fine - finer DM to update
2192c833c3b5SJed Brown 
2193c833c3b5SJed Brown    Level: developer
2194c833c3b5SJed Brown 
2195c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2196c833c3b5SJed Brown @*/
2197c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2198c833c3b5SJed Brown {
2199c833c3b5SJed Brown   PetscErrorCode   ierr;
2200c833c3b5SJed Brown   DMRefineHookLink link;
2201c833c3b5SJed Brown 
2202c833c3b5SJed Brown   PetscFunctionBegin;
2203c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22048865f1eaSKarl Rupp     if (link->interphook) {
22058865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
22068865f1eaSKarl Rupp     }
22074057135bSMatthew G Knepley   }
220847c6ae99SBarry Smith   PetscFunctionReturn(0);
220947c6ae99SBarry Smith }
221047c6ae99SBarry Smith 
2211eb3f98d2SBarry Smith /*@
2212aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2213eb3f98d2SBarry Smith 
2214eb3f98d2SBarry Smith     Not Collective
2215eb3f98d2SBarry Smith 
2216eb3f98d2SBarry Smith     Input Parameter:
2217eb3f98d2SBarry Smith .   dm - the DM object
2218eb3f98d2SBarry Smith 
2219eb3f98d2SBarry Smith     Output Parameter:
2220eb3f98d2SBarry Smith .   level - number of refinements
2221eb3f98d2SBarry Smith 
2222eb3f98d2SBarry Smith     Level: developer
2223eb3f98d2SBarry Smith 
22246a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2225eb3f98d2SBarry Smith 
2226eb3f98d2SBarry Smith @*/
2227eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2228eb3f98d2SBarry Smith {
2229eb3f98d2SBarry Smith   PetscFunctionBegin;
2230eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2231eb3f98d2SBarry Smith   *level = dm->levelup;
2232eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2233eb3f98d2SBarry Smith }
2234eb3f98d2SBarry Smith 
2235fef3a512SBarry Smith /*@
2236aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2237fef3a512SBarry Smith 
2238fef3a512SBarry Smith     Not Collective
2239fef3a512SBarry Smith 
2240fef3a512SBarry Smith     Input Parameter:
2241fef3a512SBarry Smith +   dm - the DM object
2242fef3a512SBarry Smith -   level - number of refinements
2243fef3a512SBarry Smith 
2244fef3a512SBarry Smith     Level: advanced
2245fef3a512SBarry Smith 
224695452b02SPatrick Sanan     Notes:
224795452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2248fef3a512SBarry Smith 
2249fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2250fef3a512SBarry Smith 
2251fef3a512SBarry Smith @*/
2252fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2253fef3a512SBarry Smith {
2254fef3a512SBarry Smith   PetscFunctionBegin;
2255fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2256fef3a512SBarry Smith   dm->levelup = level;
2257fef3a512SBarry Smith   PetscFunctionReturn(0);
2258fef3a512SBarry Smith }
2259fef3a512SBarry Smith 
2260ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2261ca3d3a14SMatthew G. Knepley {
2262ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2263ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2264ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2265ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2266ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2267ca3d3a14SMatthew G. Knepley }
2268ca3d3a14SMatthew G. Knepley 
2269ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2270ca3d3a14SMatthew G. Knepley {
2271ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2272ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2273ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2274ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2275ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2276ca3d3a14SMatthew G. Knepley }
2277ca3d3a14SMatthew G. Knepley 
2278ca3d3a14SMatthew G. Knepley /*@
2279c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2280ca3d3a14SMatthew G. Knepley 
2281ca3d3a14SMatthew G. Knepley   Input Parameter:
2282ca3d3a14SMatthew G. Knepley . dm - The DM
2283ca3d3a14SMatthew G. Knepley 
2284ca3d3a14SMatthew G. Knepley   Output Parameter:
2285ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2286ca3d3a14SMatthew G. Knepley 
2287ca3d3a14SMatthew G. Knepley   Level: developer
2288ca3d3a14SMatthew G. Knepley 
2289436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2290ca3d3a14SMatthew G. Knepley @*/
2291ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2292ca3d3a14SMatthew G. Knepley {
2293ca3d3a14SMatthew G. Knepley   Vec            tv;
2294ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2295ca3d3a14SMatthew G. Knepley 
2296ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2297ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2298534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2299ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2300ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2301ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2302ca3d3a14SMatthew G. Knepley }
2303ca3d3a14SMatthew G. Knepley 
2304ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2305ca3d3a14SMatthew G. Knepley {
2306ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2307ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2308ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2309ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2310ca3d3a14SMatthew G. Knepley 
2311ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2312ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
231392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2314ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2315ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2316ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
231792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2318ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2319ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2320ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2321ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2322ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2323ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2324ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2325ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2326ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2327ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2328ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2329ca3d3a14SMatthew G. Knepley     }
2330ca3d3a14SMatthew G. Knepley   }
2331ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2332ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2333ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2334ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2335ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2336ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2337ca3d3a14SMatthew G. Knepley       if (dof) {
2338ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2339ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2340ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2341ca3d3a14SMatthew G. Knepley 
2342ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2343ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2344ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2345580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2346ca3d3a14SMatthew G. Knepley       }
2347ca3d3a14SMatthew G. Knepley     }
2348ca3d3a14SMatthew G. Knepley   }
2349ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2350ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2351ca3d3a14SMatthew G. Knepley }
2352ca3d3a14SMatthew G. Knepley 
2353ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2354ca3d3a14SMatthew G. Knepley {
2355ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2356ca3d3a14SMatthew G. Knepley 
2357ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2358ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2359ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2360ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2361ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2362ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2363ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2364ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2365ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2366ca3d3a14SMatthew G. Knepley }
2367ca3d3a14SMatthew G. Knepley 
2368bb9467b5SJed Brown /*@C
2369baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2370baf369e7SPeter Brune 
2371baf369e7SPeter Brune    Logically Collective
2372baf369e7SPeter Brune 
2373baf369e7SPeter Brune    Input Arguments:
2374baf369e7SPeter Brune +  dm - the DM
2375baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2376baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
23770298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2378baf369e7SPeter Brune 
2379baf369e7SPeter Brune    Calling sequence for beginhook:
2380baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2381baf369e7SPeter Brune 
2382baf369e7SPeter Brune +  dm - global DM
2383baf369e7SPeter Brune .  g - global vector
2384baf369e7SPeter Brune .  mode - mode
2385baf369e7SPeter Brune .  l - local vector
2386baf369e7SPeter Brune -  ctx - optional user-defined function context
2387baf369e7SPeter Brune 
2388baf369e7SPeter Brune 
2389baf369e7SPeter Brune    Calling sequence for endhook:
2390ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2391baf369e7SPeter Brune 
2392baf369e7SPeter Brune +  global - global DM
2393baf369e7SPeter Brune -  ctx - optional user-defined function context
2394baf369e7SPeter Brune 
2395baf369e7SPeter Brune    Level: advanced
2396baf369e7SPeter Brune 
2397baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2398baf369e7SPeter Brune @*/
2399baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2400baf369e7SPeter Brune {
2401baf369e7SPeter Brune   PetscErrorCode          ierr;
2402baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2403baf369e7SPeter Brune 
2404baf369e7SPeter Brune   PetscFunctionBegin;
2405baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2406baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
240795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2408baf369e7SPeter Brune   link->beginhook = beginhook;
2409baf369e7SPeter Brune   link->endhook   = endhook;
2410baf369e7SPeter Brune   link->ctx       = ctx;
24110298fd71SBarry Smith   link->next      = NULL;
2412baf369e7SPeter Brune   *p              = link;
2413baf369e7SPeter Brune   PetscFunctionReturn(0);
2414baf369e7SPeter Brune }
2415baf369e7SPeter Brune 
24164c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
24174c274da1SToby Isaac {
24184c274da1SToby Isaac   Mat cMat;
24194c274da1SToby Isaac   Vec cVec;
24204c274da1SToby Isaac   PetscSection section, cSec;
24214c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24224c274da1SToby Isaac   PetscErrorCode ierr;
24234c274da1SToby Isaac 
24244c274da1SToby Isaac   PetscFunctionBegin;
24254c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24264c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24274c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
24285db9a05bSToby Isaac     PetscInt nRows;
24295db9a05bSToby Isaac 
24305db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24315db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
243292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
24337711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24344c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
24354c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24364c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24374c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24384c274da1SToby Isaac       if (dof) {
24394c274da1SToby Isaac         PetscScalar *vals;
24404c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
24414c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
24424c274da1SToby Isaac       }
24434c274da1SToby Isaac     }
24444c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24454c274da1SToby Isaac   }
24464c274da1SToby Isaac   PetscFunctionReturn(0);
24474c274da1SToby Isaac }
24484c274da1SToby Isaac 
244947c6ae99SBarry Smith /*@
245001729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
245101729b5cSPatrick Sanan 
2452d083f849SBarry Smith     Neighbor-wise Collective on dm
245301729b5cSPatrick Sanan 
245401729b5cSPatrick Sanan     Input Parameters:
245501729b5cSPatrick Sanan +   dm - the DM object
245601729b5cSPatrick Sanan .   g - the global vector
245701729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
245801729b5cSPatrick Sanan -   l - the local vector
245901729b5cSPatrick Sanan 
246001729b5cSPatrick Sanan     Notes:
246101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
246201729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
246301729b5cSPatrick Sanan 
246401729b5cSPatrick Sanan     Level: beginner
246501729b5cSPatrick Sanan 
246601729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
246701729b5cSPatrick Sanan 
246801729b5cSPatrick Sanan @*/
246901729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
247001729b5cSPatrick Sanan {
247101729b5cSPatrick Sanan   PetscErrorCode ierr;
247201729b5cSPatrick Sanan 
247301729b5cSPatrick Sanan   PetscFunctionBegin;
247401729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
247501729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
247601729b5cSPatrick Sanan   PetscFunctionReturn(0);
247701729b5cSPatrick Sanan }
247801729b5cSPatrick Sanan 
247901729b5cSPatrick Sanan /*@
248047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
248147c6ae99SBarry Smith 
2482d083f849SBarry Smith     Neighbor-wise Collective on dm
248347c6ae99SBarry Smith 
248447c6ae99SBarry Smith     Input Parameters:
248547c6ae99SBarry Smith +   dm - the DM object
248647c6ae99SBarry Smith .   g - the global vector
248747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
248847c6ae99SBarry Smith -   l - the local vector
248947c6ae99SBarry Smith 
249001729b5cSPatrick Sanan     Level: intermediate
249147c6ae99SBarry Smith 
249201729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
249347c6ae99SBarry Smith 
249447c6ae99SBarry Smith @*/
24957087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
249647c6ae99SBarry Smith {
24977128ae9fSMatthew G Knepley   PetscSF                 sf;
249847c6ae99SBarry Smith   PetscErrorCode          ierr;
2499baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
250047c6ae99SBarry Smith 
2501d0295fc0SJunchao Zhang 
250247c6ae99SBarry Smith   PetscFunctionBegin;
2503171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2504baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
25058865f1eaSKarl Rupp     if (link->beginhook) {
25068865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
25078865f1eaSKarl Rupp     }
2508baf369e7SPeter Brune   }
25091bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
25107128ae9fSMatthew G Knepley   if (sf) {
2511ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2512ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2513d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
25147128ae9fSMatthew G Knepley 
251582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2516a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2517a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2518d0295fc0SJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray);CHKERRQ(ierr);
2519a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2520a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
25217128ae9fSMatthew G Knepley   } else {
252233907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2523843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
25247128ae9fSMatthew G Knepley   }
252547c6ae99SBarry Smith   PetscFunctionReturn(0);
252647c6ae99SBarry Smith }
252747c6ae99SBarry Smith 
252847c6ae99SBarry Smith /*@
252947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
253047c6ae99SBarry Smith 
2531d083f849SBarry Smith     Neighbor-wise Collective on dm
253247c6ae99SBarry Smith 
253347c6ae99SBarry Smith     Input Parameters:
253447c6ae99SBarry Smith +   dm - the DM object
253547c6ae99SBarry Smith .   g - the global vector
253647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
253747c6ae99SBarry Smith -   l - the local vector
253847c6ae99SBarry Smith 
253901729b5cSPatrick Sanan     Level: intermediate
254047c6ae99SBarry Smith 
254101729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
254247c6ae99SBarry Smith 
254347c6ae99SBarry Smith @*/
25447087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
254547c6ae99SBarry Smith {
25467128ae9fSMatthew G Knepley   PetscSF                 sf;
254747c6ae99SBarry Smith   PetscErrorCode          ierr;
2548ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2549ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2550ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2551baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2552d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
255347c6ae99SBarry Smith 
255447c6ae99SBarry Smith   PetscFunctionBegin;
2555171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25561bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2557ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
25587128ae9fSMatthew G Knepley   if (sf) {
255982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25607128ae9fSMatthew G Knepley 
2561a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2562a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
25637128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2564a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2565a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
2566ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
25677128ae9fSMatthew G Knepley   } else {
256833907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2569843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
25707128ae9fSMatthew G Knepley   }
25714c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2572baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2573baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2574baf369e7SPeter Brune   }
257547c6ae99SBarry Smith   PetscFunctionReturn(0);
257647c6ae99SBarry Smith }
257747c6ae99SBarry Smith 
2578d4d07f1eSToby Isaac /*@C
2579d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2580d4d07f1eSToby Isaac 
2581d4d07f1eSToby Isaac    Logically Collective
2582d4d07f1eSToby Isaac 
2583d4d07f1eSToby Isaac    Input Arguments:
2584d4d07f1eSToby Isaac +  dm - the DM
2585d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2586d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2587d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2588d4d07f1eSToby Isaac 
2589d4d07f1eSToby Isaac    Calling sequence for beginhook:
2590d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2591d4d07f1eSToby Isaac 
2592d4d07f1eSToby Isaac +  dm - global DM
2593d4d07f1eSToby Isaac .  l - local vector
2594d4d07f1eSToby Isaac .  mode - mode
2595d4d07f1eSToby Isaac .  g - global vector
2596d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2597d4d07f1eSToby Isaac 
2598d4d07f1eSToby Isaac 
2599d4d07f1eSToby Isaac    Calling sequence for endhook:
2600d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2601d4d07f1eSToby Isaac 
2602d4d07f1eSToby Isaac +  global - global DM
2603d4d07f1eSToby Isaac .  l - local vector
2604d4d07f1eSToby Isaac .  mode - mode
2605d4d07f1eSToby Isaac .  g - global vector
2606d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2607d4d07f1eSToby Isaac 
2608d4d07f1eSToby Isaac    Level: advanced
2609d4d07f1eSToby Isaac 
2610d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2611d4d07f1eSToby Isaac @*/
2612d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2613d4d07f1eSToby Isaac {
2614d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2615d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2616d4d07f1eSToby Isaac 
2617d4d07f1eSToby Isaac   PetscFunctionBegin;
2618d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2619d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
262095dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2621d4d07f1eSToby Isaac   link->beginhook = beginhook;
2622d4d07f1eSToby Isaac   link->endhook   = endhook;
2623d4d07f1eSToby Isaac   link->ctx       = ctx;
2624d4d07f1eSToby Isaac   link->next      = NULL;
2625d4d07f1eSToby Isaac   *p              = link;
2626d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2627d4d07f1eSToby Isaac }
2628d4d07f1eSToby Isaac 
26294c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
26304c274da1SToby Isaac {
26314c274da1SToby Isaac   Mat cMat;
26324c274da1SToby Isaac   Vec cVec;
26334c274da1SToby Isaac   PetscSection section, cSec;
26344c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
26354c274da1SToby Isaac   PetscErrorCode ierr;
26364c274da1SToby Isaac 
26374c274da1SToby Isaac   PetscFunctionBegin;
26384c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26394c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
26404c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
26415db9a05bSToby Isaac     PetscInt nRows;
26425db9a05bSToby Isaac 
26435db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
26445db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
264592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
26467711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
26474c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
26484c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
26494c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
26504c274da1SToby Isaac       if (dof) {
26514c274da1SToby Isaac         PetscInt d;
26524c274da1SToby Isaac         PetscScalar *vals;
26534c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
26544c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
26554c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
26564c274da1SToby Isaac          * we just extracted */
26574c274da1SToby Isaac         for (d = 0; d < dof; d++) {
26584c274da1SToby Isaac           vals[d] = 0.;
26594c274da1SToby Isaac         }
26604c274da1SToby Isaac       }
26614c274da1SToby Isaac     }
26624c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
26634c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
26644c274da1SToby Isaac   }
26654c274da1SToby Isaac   PetscFunctionReturn(0);
26664c274da1SToby Isaac }
266701729b5cSPatrick Sanan /*@
266801729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
266901729b5cSPatrick Sanan 
2670d083f849SBarry Smith     Neighbor-wise Collective on dm
267101729b5cSPatrick Sanan 
267201729b5cSPatrick Sanan     Input Parameters:
267301729b5cSPatrick Sanan +   dm - the DM object
267401729b5cSPatrick Sanan .   l - the local vector
267501729b5cSPatrick 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.
267601729b5cSPatrick Sanan -   g - the global vector
267701729b5cSPatrick Sanan 
267801729b5cSPatrick Sanan     Notes:
267901729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
268001729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
268101729b5cSPatrick Sanan 
268201729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
268301729b5cSPatrick 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.
268401729b5cSPatrick Sanan 
268501729b5cSPatrick Sanan     Level: beginner
268601729b5cSPatrick Sanan 
268701729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
268801729b5cSPatrick Sanan 
268901729b5cSPatrick Sanan @*/
269001729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
269101729b5cSPatrick Sanan {
269201729b5cSPatrick Sanan   PetscErrorCode ierr;
269301729b5cSPatrick Sanan 
269401729b5cSPatrick Sanan   PetscFunctionBegin;
269501729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
269601729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
269701729b5cSPatrick Sanan   PetscFunctionReturn(0);
269801729b5cSPatrick Sanan }
26994c274da1SToby Isaac 
270047c6ae99SBarry Smith /*@
270101729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
27029a42bb27SBarry Smith 
2703d083f849SBarry Smith     Neighbor-wise Collective on dm
27049a42bb27SBarry Smith 
27059a42bb27SBarry Smith     Input Parameters:
27069a42bb27SBarry Smith +   dm - the DM object
2707f6813fd5SJed Brown .   l - the local vector
27081eb28f2eSBarry 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.
27091eb28f2eSBarry Smith -   g - the global vector
27109a42bb27SBarry Smith 
271195452b02SPatrick Sanan     Notes:
271295452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
271384330215SMatthew 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.
27149a42bb27SBarry Smith 
271501729b5cSPatrick Sanan     Level: intermediate
27169a42bb27SBarry Smith 
271701729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
27189a42bb27SBarry Smith 
27199a42bb27SBarry Smith @*/
27207087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
27219a42bb27SBarry Smith {
27227128ae9fSMatthew G Knepley   PetscSF                 sf;
272384330215SMatthew G. Knepley   PetscSection            s, gs;
2724d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2725ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2726ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2727ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2728fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
272984330215SMatthew G. Knepley   PetscErrorCode          ierr;
2730d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
27319a42bb27SBarry Smith 
27329a42bb27SBarry Smith   PetscFunctionBegin;
2733171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2734d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2735d4d07f1eSToby Isaac     if (link->beginhook) {
2736d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2737d4d07f1eSToby Isaac     }
2738d4d07f1eSToby Isaac   }
27394c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
27401bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
274192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27427128ae9fSMatthew G Knepley   switch (mode) {
27437128ae9fSMatthew G Knepley   case INSERT_VALUES:
27447128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2745304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
274684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27477128ae9fSMatthew G Knepley   case ADD_VALUES:
27487128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2749304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
275084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27517128ae9fSMatthew G Knepley   default:
275282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27537128ae9fSMatthew G Knepley   }
2754ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2755ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2756ca3d3a14SMatthew G. Knepley     if (transform) {
2757ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2758ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2759ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2760ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2761fa88e482SJed Brown     } else if (isInsert) {
2762ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2763fa88e482SJed Brown     } else {
2764a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2765fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2766ca3d3a14SMatthew G. Knepley     }
2767fa88e482SJed Brown     if (s && isInsert) {
27687128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2769fa88e482SJed Brown     } else {
2770a256111fSJunchao Zhang       ierr = VecGetArrayAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2771fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2772fa88e482SJed Brown     }
2773ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2774d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
277584330215SMatthew G. Knepley     } else if (s && isInsert) {
277684330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
277784330215SMatthew G. Knepley 
2778e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
277984330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
278084330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
278184330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2782b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
278384330215SMatthew G. Knepley 
278484330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
278503442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
278684330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2787b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
278884330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
278984330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2790b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
279103442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2792b3b16f48SMatthew 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);
2793b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2794b3b16f48SMatthew G. Knepley         if (!gcdof) {
279584330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2796b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2797b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
279884330215SMatthew G. Knepley           const PetscInt *cdofs;
279984330215SMatthew G. Knepley           PetscInt        cind = 0;
280084330215SMatthew G. Knepley 
280184330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2802b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
280384330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2804b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
280584330215SMatthew G. Knepley           }
2806b3b16f48SMatthew 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);
280784330215SMatthew G. Knepley       }
2808ca3d3a14SMatthew G. Knepley     }
2809fa88e482SJed Brown     if (g_inplace) {
2810a256111fSJunchao Zhang       ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
2811fa88e482SJed Brown     } else {
28127128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2813fa88e482SJed Brown     }
2814ca3d3a14SMatthew G. Knepley     if (transform) {
2815ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2816ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2817fa88e482SJed Brown     } else if (l_inplace) {
2818a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2819ca3d3a14SMatthew G. Knepley     } else {
2820ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2821ca3d3a14SMatthew G. Knepley     }
28227128ae9fSMatthew G Knepley   } else {
2823b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2824843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
28257128ae9fSMatthew G Knepley   }
28269a42bb27SBarry Smith   PetscFunctionReturn(0);
28279a42bb27SBarry Smith }
28289a42bb27SBarry Smith 
28299a42bb27SBarry Smith /*@
28309a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
283147c6ae99SBarry Smith 
2832d083f849SBarry Smith     Neighbor-wise Collective on dm
283347c6ae99SBarry Smith 
283447c6ae99SBarry Smith     Input Parameters:
283547c6ae99SBarry Smith +   dm - the DM object
2836f6813fd5SJed Brown .   l - the local vector
283747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2838f6813fd5SJed Brown -   g - the global vector
283947c6ae99SBarry Smith 
284001729b5cSPatrick Sanan     Level: intermediate
284147c6ae99SBarry Smith 
2842e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
284347c6ae99SBarry Smith 
284447c6ae99SBarry Smith @*/
28457087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
284647c6ae99SBarry Smith {
28477128ae9fSMatthew G Knepley   PetscSF                 sf;
284884330215SMatthew G. Knepley   PetscSection            s;
2849d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2850ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
285184330215SMatthew G. Knepley   PetscErrorCode          ierr;
285247c6ae99SBarry Smith 
285347c6ae99SBarry Smith   PetscFunctionBegin;
2854171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28551bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
285692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
28577128ae9fSMatthew G Knepley   switch (mode) {
28587128ae9fSMatthew G Knepley   case INSERT_VALUES:
28597128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
286084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28617128ae9fSMatthew G Knepley   case ADD_VALUES:
28627128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
286384330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28647128ae9fSMatthew G Knepley   default:
286582f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
28667128ae9fSMatthew G Knepley   }
286784330215SMatthew G. Knepley   if (sf && !isInsert) {
2868ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2869ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2870ca3d3a14SMatthew G. Knepley     Vec                tmpl;
287184330215SMatthew G. Knepley 
2872ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2873ca3d3a14SMatthew G. Knepley     if (transform) {
2874ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2875ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2876ca3d3a14SMatthew G. Knepley     } else {
2877a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
2878ca3d3a14SMatthew G. Knepley     }
2879a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(g, &gArray, NULL);CHKERRQ(ierr);
2880a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2881ca3d3a14SMatthew G. Knepley     if (transform) {
2882ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2883ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2884ca3d3a14SMatthew G. Knepley     } else {
2885a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2886ca3d3a14SMatthew G. Knepley     }
2887a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
288884330215SMatthew G. Knepley   } else if (s && isInsert) {
28897128ae9fSMatthew G Knepley   } else {
2890b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2891843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
28927128ae9fSMatthew G Knepley   }
2893d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2894d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2895d4d07f1eSToby Isaac   }
289647c6ae99SBarry Smith   PetscFunctionReturn(0);
289747c6ae99SBarry Smith }
289847c6ae99SBarry Smith 
2899f089877aSRichard Tran Mills /*@
2900bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2901bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2902d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2903f089877aSRichard Tran Mills 
2904d083f849SBarry Smith    Neighbor-wise Collective on dm
2905f089877aSRichard Tran Mills 
2906f089877aSRichard Tran Mills    Input Parameters:
2907f089877aSRichard Tran Mills +  dm - the DM object
2908bc0a1609SRichard Tran Mills .  g - the original local vector
2909bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2910f089877aSRichard Tran Mills 
2911bc0a1609SRichard Tran Mills    Output Parameter:
2912bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2913f089877aSRichard Tran Mills 
2914f089877aSRichard Tran Mills    Level: intermediate
2915f089877aSRichard Tran Mills 
2916bc0a1609SRichard Tran Mills    Notes:
2917bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2918bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2919bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2920bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2921bc0a1609SRichard Tran Mills 
2922bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2923f089877aSRichard Tran Mills 
2924f089877aSRichard Tran Mills @*/
2925f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2926f089877aSRichard Tran Mills {
2927f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2928f089877aSRichard Tran Mills 
2929f089877aSRichard Tran Mills   PetscFunctionBegin;
2930f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2931bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2932f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2933f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2934f089877aSRichard Tran Mills }
2935f089877aSRichard Tran Mills 
2936f089877aSRichard Tran Mills /*@
2937bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2938bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2939d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2940f089877aSRichard Tran Mills 
2941d083f849SBarry Smith    Neighbor-wise Collective on dm
2942f089877aSRichard Tran Mills 
2943f089877aSRichard Tran Mills    Input Parameters:
2944bc0a1609SRichard Tran Mills +  da - the DM object
2945bc0a1609SRichard Tran Mills .  g - the original local vector
2946bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2947f089877aSRichard Tran Mills 
2948bc0a1609SRichard Tran Mills    Output Parameter:
2949bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2950f089877aSRichard Tran Mills 
2951f089877aSRichard Tran Mills    Level: intermediate
2952f089877aSRichard Tran Mills 
2953bc0a1609SRichard Tran Mills    Notes:
2954bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2955bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2956bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2957bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2958bc0a1609SRichard Tran Mills 
2959bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2960f089877aSRichard Tran Mills 
2961f089877aSRichard Tran Mills @*/
2962f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2963f089877aSRichard Tran Mills {
2964f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2965f089877aSRichard Tran Mills 
2966f089877aSRichard Tran Mills   PetscFunctionBegin;
2967f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2968bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2969f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2970f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2971f089877aSRichard Tran Mills }
2972f089877aSRichard Tran Mills 
2973f089877aSRichard Tran Mills 
297447c6ae99SBarry Smith /*@
297547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
297647c6ae99SBarry Smith 
2977d083f849SBarry Smith     Collective on dm
297847c6ae99SBarry Smith 
297947c6ae99SBarry Smith     Input Parameter:
298047c6ae99SBarry Smith +   dm - the DM object
298191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
298247c6ae99SBarry Smith 
298347c6ae99SBarry Smith     Output Parameter:
298447c6ae99SBarry Smith .   dmc - the coarsened DM
298547c6ae99SBarry Smith 
298647c6ae99SBarry Smith     Level: developer
298747c6ae99SBarry Smith 
2988e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
298947c6ae99SBarry Smith 
299047c6ae99SBarry Smith @*/
29917087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
299247c6ae99SBarry Smith {
299347c6ae99SBarry Smith   PetscErrorCode    ierr;
2994b17ce1afSJed Brown   DMCoarsenHookLink link;
299547c6ae99SBarry Smith 
299647c6ae99SBarry Smith   PetscFunctionBegin;
2997171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2998b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
299947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
300047c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
3001b9d85ea2SLisandro Dalcin   if (*dmc) {
3002a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
300343842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
30048cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
3005644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
30060598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3007656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3008e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3009b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3010b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3011b17ce1afSJed Brown     }
3012b9d85ea2SLisandro Dalcin   }
301347a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3014b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3015b17ce1afSJed Brown   PetscFunctionReturn(0);
3016b17ce1afSJed Brown }
3017b17ce1afSJed Brown 
3018bb9467b5SJed Brown /*@C
3019b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3020b17ce1afSJed Brown 
3021b17ce1afSJed Brown    Logically Collective
3022b17ce1afSJed Brown 
3023b17ce1afSJed Brown    Input Arguments:
3024b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3025b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3026b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
30270298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3028b17ce1afSJed Brown 
3029b17ce1afSJed Brown    Calling sequence of coarsenhook:
3030b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3031b17ce1afSJed Brown 
3032b17ce1afSJed Brown +  fine - fine level DM
3033b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3034b17ce1afSJed Brown -  ctx - optional user-defined function context
3035b17ce1afSJed Brown 
3036b17ce1afSJed Brown    Calling sequence for restricthook:
3037c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3038b17ce1afSJed Brown 
3039b17ce1afSJed Brown +  fine - fine level DM
3040b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3041c833c3b5SJed Brown .  rscale - scaling vector for restriction
3042c833c3b5SJed Brown .  inject - matrix restricting by injection
3043b17ce1afSJed Brown .  coarse - coarse level DM to update
3044b17ce1afSJed Brown -  ctx - optional user-defined function context
3045b17ce1afSJed Brown 
3046b17ce1afSJed Brown    Level: advanced
3047b17ce1afSJed Brown 
3048b17ce1afSJed Brown    Notes:
3049b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3050b17ce1afSJed Brown 
3051b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3052b17ce1afSJed Brown 
3053b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3054b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3055b17ce1afSJed Brown 
3056bb9467b5SJed Brown    This function is currently not available from Fortran.
3057bb9467b5SJed Brown 
3058dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3059b17ce1afSJed Brown @*/
3060b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3061b17ce1afSJed Brown {
3062b17ce1afSJed Brown   PetscErrorCode    ierr;
3063b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3064b17ce1afSJed Brown 
3065b17ce1afSJed Brown   PetscFunctionBegin;
3066b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
30671e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
30681e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
30691e3d8eccSJed Brown   }
307095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3071b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3072b17ce1afSJed Brown   link->restricthook = restricthook;
3073b17ce1afSJed Brown   link->ctx          = ctx;
30740298fd71SBarry Smith   link->next         = NULL;
3075b17ce1afSJed Brown   *p                 = link;
3076b17ce1afSJed Brown   PetscFunctionReturn(0);
3077b17ce1afSJed Brown }
3078b17ce1afSJed Brown 
3079dc822a44SJed Brown /*@C
3080dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3081dc822a44SJed Brown 
3082dc822a44SJed Brown    Logically Collective
3083dc822a44SJed Brown 
3084dc822a44SJed Brown    Input Arguments:
3085dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3086dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3087dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3088dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3089dc822a44SJed Brown 
3090dc822a44SJed Brown    Level: advanced
3091dc822a44SJed Brown 
3092dc822a44SJed Brown    Notes:
3093dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3094dc822a44SJed Brown 
3095dc822a44SJed Brown    This function is currently not available from Fortran.
3096dc822a44SJed Brown 
3097dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3098dc822a44SJed Brown @*/
3099dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3100dc822a44SJed Brown {
3101dc822a44SJed Brown   PetscErrorCode    ierr;
3102dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3103dc822a44SJed Brown 
3104dc822a44SJed Brown   PetscFunctionBegin;
3105dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3106dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3107dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3108dc822a44SJed Brown       link = *p;
3109dc822a44SJed Brown       *p = link->next;
3110dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3111dc822a44SJed Brown       break;
3112dc822a44SJed Brown     }
3113dc822a44SJed Brown   }
3114dc822a44SJed Brown   PetscFunctionReturn(0);
3115dc822a44SJed Brown }
3116dc822a44SJed Brown 
3117dc822a44SJed Brown 
3118b17ce1afSJed Brown /*@
3119b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3120b17ce1afSJed Brown 
3121b17ce1afSJed Brown    Collective if any hooks are
3122b17ce1afSJed Brown 
3123b17ce1afSJed Brown    Input Arguments:
3124b17ce1afSJed Brown +  fine - finer DM to use as a base
3125b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3126e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3127b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3128e91eccc2SStefano Zampini -  coarse - coarser DM to update
3129b17ce1afSJed Brown 
3130b17ce1afSJed Brown    Level: developer
3131b17ce1afSJed Brown 
3132b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3133b17ce1afSJed Brown @*/
3134b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3135b17ce1afSJed Brown {
3136b17ce1afSJed Brown   PetscErrorCode    ierr;
3137b17ce1afSJed Brown   DMCoarsenHookLink link;
3138b17ce1afSJed Brown 
3139b17ce1afSJed Brown   PetscFunctionBegin;
3140b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
31418865f1eaSKarl Rupp     if (link->restricthook) {
31428865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
31438865f1eaSKarl Rupp     }
3144b17ce1afSJed Brown   }
314547c6ae99SBarry Smith   PetscFunctionReturn(0);
314647c6ae99SBarry Smith }
314747c6ae99SBarry Smith 
3148bb9467b5SJed Brown /*@C
3149be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
31505dbd56e3SPeter Brune 
3151d083f849SBarry Smith    Logically Collective on global
31525dbd56e3SPeter Brune 
31535dbd56e3SPeter Brune    Input Arguments:
31545dbd56e3SPeter Brune +  global - global DM
3155ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
31565dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
31570298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
31585dbd56e3SPeter Brune 
3159ec4806b8SPeter Brune 
3160ec4806b8SPeter Brune    Calling sequence for ddhook:
3161ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3162ec4806b8SPeter Brune 
3163ec4806b8SPeter Brune +  global - global DM
3164ec4806b8SPeter Brune .  block  - block DM
3165ec4806b8SPeter Brune -  ctx - optional user-defined function context
3166ec4806b8SPeter Brune 
31675dbd56e3SPeter Brune    Calling sequence for restricthook:
3168ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
31695dbd56e3SPeter Brune 
31705dbd56e3SPeter Brune +  global - global DM
31715dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
31725dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3173ec4806b8SPeter Brune .  block  - block DM
31745dbd56e3SPeter Brune -  ctx - optional user-defined function context
31755dbd56e3SPeter Brune 
31765dbd56e3SPeter Brune    Level: advanced
31775dbd56e3SPeter Brune 
31785dbd56e3SPeter Brune    Notes:
3179ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
31805dbd56e3SPeter Brune 
31815dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
31825dbd56e3SPeter Brune 
31835dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3184ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
31855dbd56e3SPeter Brune 
3186bb9467b5SJed Brown    This function is currently not available from Fortran.
3187bb9467b5SJed Brown 
31885dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
31895dbd56e3SPeter Brune @*/
3190be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
31915dbd56e3SPeter Brune {
31925dbd56e3SPeter Brune   PetscErrorCode      ierr;
3193be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
31945dbd56e3SPeter Brune 
31955dbd56e3SPeter Brune   PetscFunctionBegin;
31965dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3197b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3198b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3199b3a6b972SJed Brown   }
320095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
32015dbd56e3SPeter Brune   link->restricthook = restricthook;
3202be081cd6SPeter Brune   link->ddhook       = ddhook;
32035dbd56e3SPeter Brune   link->ctx          = ctx;
32040298fd71SBarry Smith   link->next         = NULL;
32055dbd56e3SPeter Brune   *p                 = link;
32065dbd56e3SPeter Brune   PetscFunctionReturn(0);
32075dbd56e3SPeter Brune }
32085dbd56e3SPeter Brune 
3209b3a6b972SJed Brown /*@C
3210b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3211b3a6b972SJed Brown 
3212b3a6b972SJed Brown    Logically Collective
3213b3a6b972SJed Brown 
3214b3a6b972SJed Brown    Input Arguments:
3215b3a6b972SJed Brown +  global - global DM
3216b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3217b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3218b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3219b3a6b972SJed Brown 
3220b3a6b972SJed Brown    Level: advanced
3221b3a6b972SJed Brown 
3222b3a6b972SJed Brown    Notes:
3223b3a6b972SJed Brown 
3224b3a6b972SJed Brown    This function is currently not available from Fortran.
3225b3a6b972SJed Brown 
3226b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3227b3a6b972SJed Brown @*/
3228b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3229b3a6b972SJed Brown {
3230b3a6b972SJed Brown   PetscErrorCode      ierr;
3231b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3232b3a6b972SJed Brown 
3233b3a6b972SJed Brown   PetscFunctionBegin;
3234b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3235b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3236b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3237b3a6b972SJed Brown       link = *p;
3238b3a6b972SJed Brown       *p = link->next;
3239b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3240b3a6b972SJed Brown       break;
3241b3a6b972SJed Brown     }
3242b3a6b972SJed Brown   }
3243b3a6b972SJed Brown   PetscFunctionReturn(0);
3244b3a6b972SJed Brown }
3245b3a6b972SJed Brown 
32465dbd56e3SPeter Brune /*@
3247be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
32485dbd56e3SPeter Brune 
32495dbd56e3SPeter Brune    Collective if any hooks are
32505dbd56e3SPeter Brune 
32515dbd56e3SPeter Brune    Input Arguments:
32525dbd56e3SPeter Brune +  fine - finer DM to use as a base
3253be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3254be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
32555dbd56e3SPeter Brune -  coarse - coarer DM to update
32565dbd56e3SPeter Brune 
32575dbd56e3SPeter Brune    Level: developer
32585dbd56e3SPeter Brune 
32595dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
32605dbd56e3SPeter Brune @*/
3261be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
32625dbd56e3SPeter Brune {
32635dbd56e3SPeter Brune   PetscErrorCode      ierr;
3264be081cd6SPeter Brune   DMSubDomainHookLink link;
32655dbd56e3SPeter Brune 
32665dbd56e3SPeter Brune   PetscFunctionBegin;
3267be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
32688865f1eaSKarl Rupp     if (link->restricthook) {
32698865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
32708865f1eaSKarl Rupp     }
32715dbd56e3SPeter Brune   }
32725dbd56e3SPeter Brune   PetscFunctionReturn(0);
32735dbd56e3SPeter Brune }
32745dbd56e3SPeter Brune 
32755fe1f584SPeter Brune /*@
32766a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
32775fe1f584SPeter Brune 
32785fe1f584SPeter Brune     Not Collective
32795fe1f584SPeter Brune 
32805fe1f584SPeter Brune     Input Parameter:
32815fe1f584SPeter Brune .   dm - the DM object
32825fe1f584SPeter Brune 
32835fe1f584SPeter Brune     Output Parameter:
32846a7d9d85SPeter Brune .   level - number of coarsenings
32855fe1f584SPeter Brune 
32865fe1f584SPeter Brune     Level: developer
32875fe1f584SPeter Brune 
32886a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
32895fe1f584SPeter Brune 
32905fe1f584SPeter Brune @*/
32915fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
32925fe1f584SPeter Brune {
32935fe1f584SPeter Brune   PetscFunctionBegin;
32945fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3295b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
32965fe1f584SPeter Brune   *level = dm->leveldown;
32975fe1f584SPeter Brune   PetscFunctionReturn(0);
32985fe1f584SPeter Brune }
32995fe1f584SPeter Brune 
33009a64c4a8SMatthew G. Knepley /*@
33019a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
33029a64c4a8SMatthew G. Knepley 
33039a64c4a8SMatthew G. Knepley     Not Collective
33049a64c4a8SMatthew G. Knepley 
33059a64c4a8SMatthew G. Knepley     Input Parameters:
33069a64c4a8SMatthew G. Knepley +   dm - the DM object
33079a64c4a8SMatthew G. Knepley -   level - number of coarsenings
33089a64c4a8SMatthew G. Knepley 
33099a64c4a8SMatthew G. Knepley     Level: developer
33109a64c4a8SMatthew G. Knepley 
33119a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
33129a64c4a8SMatthew G. Knepley @*/
33139a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
33149a64c4a8SMatthew G. Knepley {
33159a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
33169a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33179a64c4a8SMatthew G. Knepley   dm->leveldown = level;
33189a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
33199a64c4a8SMatthew G. Knepley }
33209a64c4a8SMatthew G. Knepley 
33215fe1f584SPeter Brune 
33225fe1f584SPeter Brune 
332347c6ae99SBarry Smith /*@C
332447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
332547c6ae99SBarry Smith 
3326d083f849SBarry Smith     Collective on dm
332747c6ae99SBarry Smith 
332847c6ae99SBarry Smith     Input Parameter:
332947c6ae99SBarry Smith +   dm - the DM object
333047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
333147c6ae99SBarry Smith 
333247c6ae99SBarry Smith     Output Parameter:
333347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
333447c6ae99SBarry Smith 
333547c6ae99SBarry Smith     Level: developer
333647c6ae99SBarry Smith 
3337e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
333847c6ae99SBarry Smith 
333947c6ae99SBarry Smith @*/
33407087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
334147c6ae99SBarry Smith {
334247c6ae99SBarry Smith   PetscErrorCode ierr;
334347c6ae99SBarry Smith 
334447c6ae99SBarry Smith   PetscFunctionBegin;
3345171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3346ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
334747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3348b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
334947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
335047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
335147c6ae99SBarry Smith   } else if (dm->ops->refine) {
335247c6ae99SBarry Smith     PetscInt i;
335347c6ae99SBarry Smith 
3354ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
335547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3356ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
335747c6ae99SBarry Smith     }
3358ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
335947c6ae99SBarry Smith   PetscFunctionReturn(0);
336047c6ae99SBarry Smith }
336147c6ae99SBarry Smith 
336247c6ae99SBarry Smith /*@C
336347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
336447c6ae99SBarry Smith 
3365d083f849SBarry Smith     Collective on dm
336647c6ae99SBarry Smith 
336747c6ae99SBarry Smith     Input Parameter:
336847c6ae99SBarry Smith +   dm - the DM object
336947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
337047c6ae99SBarry Smith 
337147c6ae99SBarry Smith     Output Parameter:
337247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
337347c6ae99SBarry Smith 
337447c6ae99SBarry Smith     Level: developer
337547c6ae99SBarry Smith 
3376e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
337747c6ae99SBarry Smith 
337847c6ae99SBarry Smith @*/
33797087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
338047c6ae99SBarry Smith {
338147c6ae99SBarry Smith   PetscErrorCode ierr;
338247c6ae99SBarry Smith 
338347c6ae99SBarry Smith   PetscFunctionBegin;
3384171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3385ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
338647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
338747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
338847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
338947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
339047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
339147c6ae99SBarry Smith     PetscInt i;
339247c6ae99SBarry Smith 
3393ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
339447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3395ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
339647c6ae99SBarry Smith     }
3397ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
339847c6ae99SBarry Smith   PetscFunctionReturn(0);
339947c6ae99SBarry Smith }
340047c6ae99SBarry Smith 
34011a266240SBarry Smith /*@C
34021a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
34031a266240SBarry Smith 
34041a266240SBarry Smith     Not Collective
34051a266240SBarry Smith 
34061a266240SBarry Smith     Input Parameters:
34071a266240SBarry Smith +   dm - the DM object
34081a266240SBarry Smith -   destroy - the destroy function
34091a266240SBarry Smith 
34101a266240SBarry Smith     Level: intermediate
34111a266240SBarry Smith 
3412e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
34131a266240SBarry Smith 
3414f07f9ceaSJed Brown @*/
34151a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
34161a266240SBarry Smith {
34171a266240SBarry Smith   PetscFunctionBegin;
3418171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34191a266240SBarry Smith   dm->ctxdestroy = destroy;
34201a266240SBarry Smith   PetscFunctionReturn(0);
34211a266240SBarry Smith }
34221a266240SBarry Smith 
3423b07ff414SBarry Smith /*@
34241b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
342547c6ae99SBarry Smith 
342647c6ae99SBarry Smith     Not Collective
342747c6ae99SBarry Smith 
342847c6ae99SBarry Smith     Input Parameters:
342947c6ae99SBarry Smith +   dm - the DM object
343047c6ae99SBarry Smith -   ctx - the user context
343147c6ae99SBarry Smith 
343247c6ae99SBarry Smith     Level: intermediate
343347c6ae99SBarry Smith 
3434e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
343547c6ae99SBarry Smith 
343647c6ae99SBarry Smith @*/
34371b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
343847c6ae99SBarry Smith {
343947c6ae99SBarry Smith   PetscFunctionBegin;
3440171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
344147c6ae99SBarry Smith   dm->ctx = ctx;
344247c6ae99SBarry Smith   PetscFunctionReturn(0);
344347c6ae99SBarry Smith }
344447c6ae99SBarry Smith 
344547c6ae99SBarry Smith /*@
34461b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
344747c6ae99SBarry Smith 
344847c6ae99SBarry Smith     Not Collective
344947c6ae99SBarry Smith 
345047c6ae99SBarry Smith     Input Parameter:
345147c6ae99SBarry Smith .   dm - the DM object
345247c6ae99SBarry Smith 
345347c6ae99SBarry Smith     Output Parameter:
345447c6ae99SBarry Smith .   ctx - the user context
345547c6ae99SBarry Smith 
345647c6ae99SBarry Smith     Level: intermediate
345747c6ae99SBarry Smith 
3458e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
345947c6ae99SBarry Smith 
346047c6ae99SBarry Smith @*/
34611b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
346247c6ae99SBarry Smith {
346347c6ae99SBarry Smith   PetscFunctionBegin;
3464171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34651b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
346647c6ae99SBarry Smith   PetscFunctionReturn(0);
346747c6ae99SBarry Smith }
346847c6ae99SBarry Smith 
346908da532bSDmitry Karpeev /*@C
3470df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
347108da532bSDmitry Karpeev 
3472d083f849SBarry Smith     Logically Collective on dm
347308da532bSDmitry Karpeev 
347408da532bSDmitry Karpeev     Input Parameter:
347508da532bSDmitry Karpeev +   dm - the DM object
34760298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
347708da532bSDmitry Karpeev 
347808da532bSDmitry Karpeev     Level: intermediate
347908da532bSDmitry Karpeev 
3480835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
348108da532bSDmitry Karpeev          DMSetJacobian()
348208da532bSDmitry Karpeev 
348308da532bSDmitry Karpeev @*/
348408da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
348508da532bSDmitry Karpeev {
348608da532bSDmitry Karpeev   PetscFunctionBegin;
34875a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
348808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
348908da532bSDmitry Karpeev   PetscFunctionReturn(0);
349008da532bSDmitry Karpeev }
349108da532bSDmitry Karpeev 
349208da532bSDmitry Karpeev /*@
349308da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
349408da532bSDmitry Karpeev 
349508da532bSDmitry Karpeev     Not Collective
349608da532bSDmitry Karpeev 
349708da532bSDmitry Karpeev     Input Parameter:
349808da532bSDmitry Karpeev .   dm - the DM object to destroy
349908da532bSDmitry Karpeev 
350008da532bSDmitry Karpeev     Output Parameter:
350108da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
350208da532bSDmitry Karpeev 
350308da532bSDmitry Karpeev     Level: developer
350408da532bSDmitry Karpeev 
350574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
350608da532bSDmitry Karpeev 
350708da532bSDmitry Karpeev @*/
350808da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
350908da532bSDmitry Karpeev {
351008da532bSDmitry Karpeev   PetscFunctionBegin;
35115a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3512534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
351308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
351408da532bSDmitry Karpeev   PetscFunctionReturn(0);
351508da532bSDmitry Karpeev }
351608da532bSDmitry Karpeev 
351708da532bSDmitry Karpeev /*@C
351808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
351908da532bSDmitry Karpeev 
3520d083f849SBarry Smith     Logically Collective on dm
352108da532bSDmitry Karpeev 
352208da532bSDmitry Karpeev     Input Parameters:
3523907376e6SBarry Smith .   dm - the DM object
352408da532bSDmitry Karpeev 
352508da532bSDmitry Karpeev     Output parameters:
352608da532bSDmitry Karpeev +   xl - lower bound
352708da532bSDmitry Karpeev -   xu - upper bound
352808da532bSDmitry Karpeev 
3529907376e6SBarry Smith     Level: advanced
3530907376e6SBarry Smith 
353195452b02SPatrick Sanan     Notes:
353295452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
353308da532bSDmitry Karpeev 
353474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
353508da532bSDmitry Karpeev 
353608da532bSDmitry Karpeev @*/
353708da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
353808da532bSDmitry Karpeev {
353908da532bSDmitry Karpeev   PetscErrorCode ierr;
35405fd66863SKarl Rupp 
354108da532bSDmitry Karpeev   PetscFunctionBegin;
35425a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
354308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
35445a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3545b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
354608da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
354708da532bSDmitry Karpeev   PetscFunctionReturn(0);
354808da532bSDmitry Karpeev }
354908da532bSDmitry Karpeev 
3550b0ae01b7SPeter Brune /*@
3551b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3552b0ae01b7SPeter Brune 
3553b0ae01b7SPeter Brune     Not Collective
3554b0ae01b7SPeter Brune 
3555b0ae01b7SPeter Brune     Input Parameter:
3556b0ae01b7SPeter Brune .   dm - the DM object
3557b0ae01b7SPeter Brune 
3558b0ae01b7SPeter Brune     Output Parameter:
3559b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3560b0ae01b7SPeter Brune 
3561b0ae01b7SPeter Brune     Level: developer
3562b0ae01b7SPeter Brune 
35631565f0a7SPatrick Sanan .seealso DMCreateColoring()
3564b0ae01b7SPeter Brune 
3565b0ae01b7SPeter Brune @*/
3566b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3567b0ae01b7SPeter Brune {
3568b0ae01b7SPeter Brune   PetscFunctionBegin;
35695a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3570534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3571b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3572b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3573b0ae01b7SPeter Brune }
3574b0ae01b7SPeter Brune 
35753ad4599aSBarry Smith /*@
35763ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
35773ad4599aSBarry Smith 
35783ad4599aSBarry Smith     Not Collective
35793ad4599aSBarry Smith 
35803ad4599aSBarry Smith     Input Parameter:
35813ad4599aSBarry Smith .   dm - the DM object
35823ad4599aSBarry Smith 
35833ad4599aSBarry Smith     Output Parameter:
35843ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
35853ad4599aSBarry Smith 
35863ad4599aSBarry Smith     Level: developer
35873ad4599aSBarry Smith 
35881565f0a7SPatrick Sanan .seealso DMCreateRestriction()
35893ad4599aSBarry Smith 
35903ad4599aSBarry Smith @*/
35913ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
35923ad4599aSBarry Smith {
35933ad4599aSBarry Smith   PetscFunctionBegin;
35945a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3595534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
35963ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
35973ad4599aSBarry Smith   PetscFunctionReturn(0);
35983ad4599aSBarry Smith }
35993ad4599aSBarry Smith 
3600a7058e45SLawrence Mitchell 
3601a7058e45SLawrence Mitchell /*@
3602a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3603a7058e45SLawrence Mitchell 
3604a7058e45SLawrence Mitchell     Not Collective
3605a7058e45SLawrence Mitchell 
3606a7058e45SLawrence Mitchell     Input Parameter:
3607a7058e45SLawrence Mitchell .   dm - the DM object
3608a7058e45SLawrence Mitchell 
3609a7058e45SLawrence Mitchell     Output Parameter:
3610a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3611a7058e45SLawrence Mitchell 
3612a7058e45SLawrence Mitchell     Level: developer
3613a7058e45SLawrence Mitchell 
36141565f0a7SPatrick Sanan .seealso DMCreateInjection()
3615a7058e45SLawrence Mitchell 
3616a7058e45SLawrence Mitchell @*/
3617a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3618a7058e45SLawrence Mitchell {
36194a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
36205a84ad33SLisandro Dalcin 
3621a7058e45SLawrence Mitchell   PetscFunctionBegin;
36225a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3623534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
36245a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
36254a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
36265a84ad33SLisandro Dalcin   } else {
36275a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
36285a84ad33SLisandro Dalcin   }
3629a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3630a7058e45SLawrence Mitchell }
3631a7058e45SLawrence Mitchell 
36320298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3633264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3634264ace61SBarry Smith 
3635264ace61SBarry Smith /*@C
3636264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3637264ace61SBarry Smith 
3638d083f849SBarry Smith   Collective on dm
3639264ace61SBarry Smith 
3640264ace61SBarry Smith   Input Parameters:
3641264ace61SBarry Smith + dm     - The DM object
3642264ace61SBarry Smith - method - The name of the DM type
3643264ace61SBarry Smith 
3644264ace61SBarry Smith   Options Database Key:
3645264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3646264ace61SBarry Smith 
3647264ace61SBarry Smith   Notes:
3648e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3649264ace61SBarry Smith 
3650264ace61SBarry Smith   Level: intermediate
3651264ace61SBarry Smith 
3652264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3653264ace61SBarry Smith @*/
365419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3655264ace61SBarry Smith {
3656264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3657264ace61SBarry Smith   PetscBool      match;
3658264ace61SBarry Smith   PetscErrorCode ierr;
3659264ace61SBarry Smith 
3660264ace61SBarry Smith   PetscFunctionBegin;
3661264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3662251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3663264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3664264ace61SBarry Smith 
36650f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
36661c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3667ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3668264ace61SBarry Smith 
3669264ace61SBarry Smith   if (dm->ops->destroy) {
3670264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3671264ace61SBarry Smith   }
3672d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3673264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3674d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3675264ace61SBarry Smith   PetscFunctionReturn(0);
3676264ace61SBarry Smith }
3677264ace61SBarry Smith 
3678264ace61SBarry Smith /*@C
3679264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3680264ace61SBarry Smith 
3681264ace61SBarry Smith   Not Collective
3682264ace61SBarry Smith 
3683264ace61SBarry Smith   Input Parameter:
3684264ace61SBarry Smith . dm  - The DM
3685264ace61SBarry Smith 
3686264ace61SBarry Smith   Output Parameter:
3687264ace61SBarry Smith . type - The DM type name
3688264ace61SBarry Smith 
3689264ace61SBarry Smith   Level: intermediate
3690264ace61SBarry Smith 
3691264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3692264ace61SBarry Smith @*/
369319fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3694264ace61SBarry Smith {
3695264ace61SBarry Smith   PetscErrorCode ierr;
3696264ace61SBarry Smith 
3697264ace61SBarry Smith   PetscFunctionBegin;
3698264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3699c959eef4SJed Brown   PetscValidPointer(type,2);
3700607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3701264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3702264ace61SBarry Smith   PetscFunctionReturn(0);
3703264ace61SBarry Smith }
3704264ace61SBarry Smith 
370567a56275SMatthew G Knepley /*@C
370667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
370767a56275SMatthew G Knepley 
3708d083f849SBarry Smith   Collective on dm
370967a56275SMatthew G Knepley 
371067a56275SMatthew G Knepley   Input Parameters:
371167a56275SMatthew G Knepley + dm - the DM
371267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
371367a56275SMatthew G Knepley 
371467a56275SMatthew G Knepley   Output Parameter:
371567a56275SMatthew G Knepley . M - pointer to new DM
371667a56275SMatthew G Knepley 
371767a56275SMatthew G Knepley   Notes:
371867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
371967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
372067a56275SMatthew G Knepley   of the input DM.
372167a56275SMatthew G Knepley 
372267a56275SMatthew G Knepley   Level: intermediate
372367a56275SMatthew G Knepley 
372467a56275SMatthew G Knepley .seealso: DMCreate()
372567a56275SMatthew G Knepley @*/
372619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
372767a56275SMatthew G Knepley {
372867a56275SMatthew G Knepley   DM             B;
372967a56275SMatthew G Knepley   char           convname[256];
3730c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
373167a56275SMatthew G Knepley   PetscErrorCode ierr;
373267a56275SMatthew G Knepley 
373367a56275SMatthew G Knepley   PetscFunctionBegin;
373467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
373567a56275SMatthew G Knepley   PetscValidType(dm,1);
373667a56275SMatthew G Knepley   PetscValidPointer(M,3);
3737251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3738c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3739c067b6caSMatthew G. Knepley   if (sametype) {
3740c067b6caSMatthew G. Knepley     *M   = dm;
3741c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3742c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3743c067b6caSMatthew G. Knepley   } else {
37440298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
374567a56275SMatthew G Knepley 
374667a56275SMatthew G Knepley     /*
374767a56275SMatthew G Knepley        Order of precedence:
374867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
374967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
375067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
375167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
375267a56275SMatthew G Knepley        5) Use a really basic converter.
375367a56275SMatthew G Knepley     */
375467a56275SMatthew G Knepley 
375567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3756a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3757a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3758a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3759a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3760a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
37610005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
376267a56275SMatthew G Knepley     if (conv) goto foundconv;
376367a56275SMatthew G Knepley 
376467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
376582f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
376667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3767a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3768a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3769a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3770a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3771a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
37720005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
377367a56275SMatthew G Knepley     if (conv) {
3774fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
377567a56275SMatthew G Knepley       goto foundconv;
377667a56275SMatthew G Knepley     }
377767a56275SMatthew G Knepley 
377867a56275SMatthew G Knepley #if 0
377967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
378067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3781fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
378267a56275SMatthew G Knepley     if (conv) goto foundconv;
378367a56275SMatthew G Knepley 
378467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
378567a56275SMatthew G Knepley     if (dm->ops->convert) {
378667a56275SMatthew G Knepley       conv = dm->ops->convert;
378767a56275SMatthew G Knepley     }
378867a56275SMatthew G Knepley     if (conv) goto foundconv;
378967a56275SMatthew G Knepley #endif
379067a56275SMatthew G Knepley 
379167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
379282f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
379367a56275SMatthew G Knepley 
379467a56275SMatthew G Knepley foundconv:
379567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
379667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
379712fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
379890b157c4SStefano Zampini     {
379990b157c4SStefano Zampini       PetscBool             isper;
380012fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
380112fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
380290b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
380390b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
3804c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
3805*a587d139SMark       ierr = PetscFree((*M)->vectype);CHKERRQ(ierr);
3806*a587d139SMark       ierr = PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype);CHKERRQ(ierr);
3807*a587d139SMark       ierr = PetscFree((*M)->mattype);CHKERRQ(ierr);
3808*a587d139SMark       ierr = PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype);CHKERRQ(ierr);
380912fa691eSMatthew G. Knepley     }
381067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
381167a56275SMatthew G Knepley   }
381267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
381367a56275SMatthew G Knepley   PetscFunctionReturn(0);
381467a56275SMatthew G Knepley }
3815264ace61SBarry Smith 
3816264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3817264ace61SBarry Smith 
3818264ace61SBarry Smith /*@C
38191c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
38201c84c290SBarry Smith 
38211c84c290SBarry Smith   Not Collective
38221c84c290SBarry Smith 
38231c84c290SBarry Smith   Input Parameters:
38241c84c290SBarry Smith + name        - The name of a new user-defined creation routine
38251c84c290SBarry Smith - create_func - The creation routine itself
38261c84c290SBarry Smith 
38271c84c290SBarry Smith   Notes:
38281c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
38291c84c290SBarry Smith 
38301c84c290SBarry Smith 
38311c84c290SBarry Smith   Sample usage:
38321c84c290SBarry Smith .vb
3833bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
38341c84c290SBarry Smith .ve
38351c84c290SBarry Smith 
38361c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
38371c84c290SBarry Smith .vb
38381c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
38391c84c290SBarry Smith     DMSetType(DM,"my_da");
38401c84c290SBarry Smith .ve
38411c84c290SBarry Smith    or at runtime via the option
38421c84c290SBarry Smith .vb
38431c84c290SBarry Smith     -da_type my_da
38441c84c290SBarry Smith .ve
3845264ace61SBarry Smith 
3846264ace61SBarry Smith   Level: advanced
38471c84c290SBarry Smith 
3848bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
38491c84c290SBarry Smith 
3850264ace61SBarry Smith @*/
3851bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3852264ace61SBarry Smith {
3853264ace61SBarry Smith   PetscErrorCode ierr;
3854264ace61SBarry Smith 
3855264ace61SBarry Smith   PetscFunctionBegin;
38561d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3857a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3858264ace61SBarry Smith   PetscFunctionReturn(0);
3859264ace61SBarry Smith }
3860264ace61SBarry Smith 
3861b859378eSBarry Smith /*@C
386255849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3863b859378eSBarry Smith 
3864d083f849SBarry Smith   Collective on viewer
3865b859378eSBarry Smith 
3866b859378eSBarry Smith   Input Parameters:
3867b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3868b859378eSBarry Smith            some related function before a call to DMLoad().
3869b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3870b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3871b859378eSBarry Smith 
3872b859378eSBarry Smith    Level: intermediate
3873b859378eSBarry Smith 
3874b859378eSBarry Smith   Notes:
387555849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3876b859378eSBarry Smith 
3877b859378eSBarry Smith   Notes for advanced users:
3878b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3879b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3880b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3881b859378eSBarry Smith   format is
3882b859378eSBarry Smith .vb
3883b859378eSBarry Smith      has not yet been determined
3884b859378eSBarry Smith .ve
3885b859378eSBarry Smith 
3886b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3887b859378eSBarry Smith @*/
3888b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3889b859378eSBarry Smith {
38909331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3891b859378eSBarry Smith   PetscErrorCode ierr;
3892b859378eSBarry Smith 
3893b859378eSBarry Smith   PetscFunctionBegin;
3894b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3895b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3896fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
389732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
38989331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
389958cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
39009331c7a4SMatthew G. Knepley   if (isbinary) {
39019331c7a4SMatthew G. Knepley     PetscInt classid;
39029331c7a4SMatthew G. Knepley     char     type[256];
3903b859378eSBarry Smith 
3904060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
39059200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3906060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
390732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
39089331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
39099331c7a4SMatthew G. Knepley   } else if (ishdf5) {
39109331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
39119331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
391258cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3913b859378eSBarry Smith   PetscFunctionReturn(0);
3914b859378eSBarry Smith }
3915b859378eSBarry Smith 
3916b2e4378dSMatthew G. Knepley /*@
3917b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3918b2e4378dSMatthew G. Knepley 
3919b2e4378dSMatthew G. Knepley   Not collective
3920b2e4378dSMatthew G. Knepley 
3921b2e4378dSMatthew G. Knepley   Input Parameter:
3922b2e4378dSMatthew G. Knepley . dm - the DM
3923b2e4378dSMatthew G. Knepley 
3924b2e4378dSMatthew G. Knepley   Output Parameters:
3925b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3926b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3927b2e4378dSMatthew G. Knepley 
3928b2e4378dSMatthew G. Knepley   Level: beginner
3929b2e4378dSMatthew G. Knepley 
3930b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3931b2e4378dSMatthew G. Knepley 
3932b2e4378dSMatthew G. Knepley 
3933b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3934b2e4378dSMatthew G. Knepley @*/
3935b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3936b2e4378dSMatthew G. Knepley {
3937b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3938b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3939b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3940b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3941b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3942b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3943b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3944b2e4378dSMatthew G. Knepley 
3945b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3946b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3947b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3948b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3949b2e4378dSMatthew G. Knepley   if (coords) {
3950b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3951b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3952b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3953b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3954b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3955b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3956b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3957b2e4378dSMatthew G. Knepley       }
3958b2e4378dSMatthew G. Knepley     }
3959b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3960b2e4378dSMatthew G. Knepley   } else {
3961b2e4378dSMatthew G. Knepley     PetscBool isda;
3962b2e4378dSMatthew G. Knepley 
3963b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3964b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3965b2e4378dSMatthew G. Knepley   }
3966b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3967b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3968b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3969b2e4378dSMatthew G. Knepley }
3970b2e4378dSMatthew G. Knepley 
3971b2e4378dSMatthew G. Knepley /*@
3972b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3973b2e4378dSMatthew G. Knepley 
3974b2e4378dSMatthew G. Knepley   Collective
3975b2e4378dSMatthew G. Knepley 
3976b2e4378dSMatthew G. Knepley   Input Parameter:
3977b2e4378dSMatthew G. Knepley . dm - the DM
3978b2e4378dSMatthew G. Knepley 
3979b2e4378dSMatthew G. Knepley   Output Parameters:
3980b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3981b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3982b2e4378dSMatthew G. Knepley 
3983b2e4378dSMatthew G. Knepley   Level: beginner
3984b2e4378dSMatthew G. Knepley 
3985b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3986b2e4378dSMatthew G. Knepley @*/
3987b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3988b2e4378dSMatthew G. Knepley {
3989b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
39906ce308c4SMatthew G. Knepley   PetscInt       cdim;
39916ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3992b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3993b2e4378dSMatthew G. Knepley 
3994b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3995b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3996b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3997b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3998b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3999b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
4000b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
4001b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4002b2e4378dSMatthew G. Knepley }
4003b2e4378dSMatthew G. Knepley 
40047da65231SMatthew G Knepley /******************************** FEM Support **********************************/
40057da65231SMatthew G Knepley 
4006a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4007a6dfd86eSKarl Rupp {
40081d47ebbbSSatish Balay   PetscInt       f;
40091b30c384SMatthew G Knepley   PetscErrorCode ierr;
40101b30c384SMatthew G Knepley 
40117da65231SMatthew G Knepley   PetscFunctionBegin;
401274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
40131d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
401457622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
40157da65231SMatthew G Knepley   }
40167da65231SMatthew G Knepley   PetscFunctionReturn(0);
40177da65231SMatthew G Knepley }
40187da65231SMatthew G Knepley 
4019a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4020a6dfd86eSKarl Rupp {
40211b30c384SMatthew G Knepley   PetscInt       f, g;
40227da65231SMatthew G Knepley   PetscErrorCode ierr;
40237da65231SMatthew G Knepley 
40247da65231SMatthew G Knepley   PetscFunctionBegin;
402574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
40261d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
402774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
40281d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4029e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
40307da65231SMatthew G Knepley     }
403174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
40327da65231SMatthew G Knepley   }
40337da65231SMatthew G Knepley   PetscFunctionReturn(0);
40347da65231SMatthew G Knepley }
4035e7c4fc90SDmitry Karpeev 
40366113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4037e759306cSMatthew G. Knepley {
40380c5b8624SToby Isaac   PetscInt          localSize, bs;
40390c5b8624SToby Isaac   PetscMPIInt       size;
40400c5b8624SToby Isaac   Vec               x, xglob;
40410c5b8624SToby Isaac   const PetscScalar *xarray;
4042e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4043e759306cSMatthew G. Knepley 
4044e759306cSMatthew G. Knepley   PetscFunctionBegin;
40459852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
4046e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4047e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
40486113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
40490c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
40500c5b8624SToby Isaac   if (size > 1) {
40510c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
40520c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
40530c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
40540c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
40550c5b8624SToby Isaac   } else {
40560c5b8624SToby Isaac     xglob = x;
40570c5b8624SToby Isaac   }
40580c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
40590c5b8624SToby Isaac   if (size > 1) {
40600c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
40610c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
40620c5b8624SToby Isaac   }
4063e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4064e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4065e759306cSMatthew G. Knepley }
4066e759306cSMatthew G. Knepley 
406788ed4aceSMatthew G Knepley /*@
40681bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4069061576a5SJed Brown 
4070061576a5SJed Brown   Input Parameter:
4071061576a5SJed Brown . dm - The DM
4072061576a5SJed Brown 
4073061576a5SJed Brown   Output Parameter:
4074061576a5SJed Brown . section - The PetscSection
4075061576a5SJed Brown 
4076061576a5SJed Brown   Options Database Keys:
4077061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4078061576a5SJed Brown 
4079061576a5SJed Brown   Level: advanced
4080061576a5SJed Brown 
4081061576a5SJed Brown   Notes:
4082061576a5SJed Brown   Use DMGetLocalSection() in new code.
4083061576a5SJed Brown 
4084061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4085061576a5SJed Brown 
4086061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4087061576a5SJed Brown @*/
4088061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4089061576a5SJed Brown {
4090061576a5SJed Brown   PetscErrorCode ierr;
4091061576a5SJed Brown 
4092061576a5SJed Brown   PetscFunctionBegin;
4093061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4094061576a5SJed Brown   PetscFunctionReturn(0);
4095061576a5SJed Brown }
4096061576a5SJed Brown 
4097061576a5SJed Brown /*@
4098061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
409988ed4aceSMatthew G Knepley 
410088ed4aceSMatthew G Knepley   Input Parameter:
410188ed4aceSMatthew G Knepley . dm - The DM
410288ed4aceSMatthew G Knepley 
410388ed4aceSMatthew G Knepley   Output Parameter:
410488ed4aceSMatthew G Knepley . section - The PetscSection
410588ed4aceSMatthew G Knepley 
4106e5893cccSMatthew G. Knepley   Options Database Keys:
4107e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4108e5893cccSMatthew G. Knepley 
410988ed4aceSMatthew G Knepley   Level: intermediate
411088ed4aceSMatthew G Knepley 
411188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
411288ed4aceSMatthew G Knepley 
4113061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
411488ed4aceSMatthew G Knepley @*/
4115061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
41160adebc6cSBarry Smith {
4117fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4118fd59a867SMatthew G. Knepley 
411988ed4aceSMatthew G Knepley   PetscFunctionBegin;
412088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
412188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41221bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4123e5e52638SMatthew G. Knepley     PetscInt d;
4124e5e52638SMatthew G. Knepley 
4125e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
41261bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
41271bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
41282f0f8703SMatthew G. Knepley   }
41291bb6d2a8SBarry Smith   *section = dm->localSection;
413088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
413188ed4aceSMatthew G Knepley }
413288ed4aceSMatthew G Knepley 
413388ed4aceSMatthew G Knepley /*@
41341bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4135061576a5SJed Brown 
4136061576a5SJed Brown   Input Parameters:
4137061576a5SJed Brown + dm - The DM
4138061576a5SJed Brown - section - The PetscSection
4139061576a5SJed Brown 
4140061576a5SJed Brown   Level: advanced
4141061576a5SJed Brown 
4142061576a5SJed Brown   Notes:
4143061576a5SJed Brown   Use DMSetLocalSection() in new code.
4144061576a5SJed Brown 
4145061576a5SJed Brown   Any existing Section will be destroyed
4146061576a5SJed Brown 
4147061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4148061576a5SJed Brown @*/
4149061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4150061576a5SJed Brown {
4151061576a5SJed Brown   PetscErrorCode ierr;
4152061576a5SJed Brown 
4153061576a5SJed Brown   PetscFunctionBegin;
4154061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4155061576a5SJed Brown   PetscFunctionReturn(0);
4156061576a5SJed Brown }
4157061576a5SJed Brown 
4158061576a5SJed Brown /*@
4159061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
416088ed4aceSMatthew G Knepley 
416188ed4aceSMatthew G Knepley   Input Parameters:
416288ed4aceSMatthew G Knepley + dm - The DM
416388ed4aceSMatthew G Knepley - section - The PetscSection
416488ed4aceSMatthew G Knepley 
416588ed4aceSMatthew G Knepley   Level: intermediate
416688ed4aceSMatthew G Knepley 
416788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
416888ed4aceSMatthew G Knepley 
4169061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
417088ed4aceSMatthew G Knepley @*/
4171061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
41720adebc6cSBarry Smith {
4173c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4174af122d2aSMatthew G Knepley   PetscInt       f;
417588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
417688ed4aceSMatthew G Knepley 
417788ed4aceSMatthew G Knepley   PetscFunctionBegin;
417888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4179b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
41801d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41811bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
41821bb6d2a8SBarry Smith   dm->localSection = section;
41831bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4184af122d2aSMatthew G Knepley   if (numFields) {
4185af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4186af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
41870f21e855SMatthew G. Knepley       PetscObject disc;
4188af122d2aSMatthew G Knepley       const char *name;
4189af122d2aSMatthew G Knepley 
41901bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
419144a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
41920f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4193af122d2aSMatthew G Knepley     }
4194af122d2aSMatthew G Knepley   }
4195e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
41961bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
419788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
419888ed4aceSMatthew G Knepley }
419988ed4aceSMatthew G Knepley 
42009435951eSToby Isaac /*@
4201b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
42029435951eSToby Isaac 
4203e228b242SToby Isaac   not collective
4204e228b242SToby Isaac 
42059435951eSToby Isaac   Input Parameter:
42069435951eSToby Isaac . dm - The DM
42079435951eSToby Isaac 
42089435951eSToby Isaac   Output Parameter:
42099435951eSToby 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.
42109435951eSToby 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.
42119435951eSToby Isaac 
42129435951eSToby Isaac   Level: advanced
42139435951eSToby Isaac 
42149435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
42159435951eSToby Isaac 
42169435951eSToby Isaac .seealso: DMSetDefaultConstraints()
42179435951eSToby Isaac @*/
42189435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
42199435951eSToby Isaac {
42209435951eSToby Isaac   PetscErrorCode ierr;
42219435951eSToby Isaac 
42229435951eSToby Isaac   PetscFunctionBegin;
42239435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42249435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
422545a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
422645a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
42279435951eSToby Isaac   PetscFunctionReturn(0);
42289435951eSToby Isaac }
42299435951eSToby Isaac 
42309435951eSToby Isaac /*@
4231b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
42329435951eSToby Isaac 
42339435951eSToby 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().
42349435951eSToby Isaac 
42359435951eSToby 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.
42369435951eSToby Isaac 
4237e228b242SToby Isaac   collective on dm
4238e228b242SToby Isaac 
42399435951eSToby Isaac   Input Parameters:
42409435951eSToby Isaac + dm - The DM
4241e228b242SToby 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).
4242e228b242SToby 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).
42439435951eSToby Isaac 
42449435951eSToby Isaac   Level: advanced
42459435951eSToby Isaac 
42469435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
42479435951eSToby Isaac 
42489435951eSToby Isaac .seealso: DMGetDefaultConstraints()
42499435951eSToby Isaac @*/
42509435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
42519435951eSToby Isaac {
4252e228b242SToby Isaac   PetscMPIInt result;
42539435951eSToby Isaac   PetscErrorCode ierr;
42549435951eSToby Isaac 
42559435951eSToby Isaac   PetscFunctionBegin;
42569435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4257e228b242SToby Isaac   if (section) {
4258e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4259e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4260f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4261e228b242SToby Isaac   }
4262e228b242SToby Isaac   if (mat) {
4263e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4264e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4265f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4266e228b242SToby Isaac   }
42679435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42689435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
42699435951eSToby Isaac   dm->defaultConstraintSection = section;
42709435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
42719435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
42729435951eSToby Isaac   dm->defaultConstraintMat = mat;
42739435951eSToby Isaac   PetscFunctionReturn(0);
42749435951eSToby Isaac }
42759435951eSToby Isaac 
4276497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4277507e4973SMatthew G. Knepley /*
4278507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4279507e4973SMatthew G. Knepley 
4280507e4973SMatthew G. Knepley   Input Parameters:
4281507e4973SMatthew G. Knepley + dm - The DM
4282507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4283507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4284507e4973SMatthew G. Knepley 
4285507e4973SMatthew G. Knepley   Level: intermediate
4286507e4973SMatthew G. Knepley 
42871bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4288507e4973SMatthew G. Knepley */
4289f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4290507e4973SMatthew G. Knepley {
4291507e4973SMatthew G. Knepley   MPI_Comm        comm;
4292507e4973SMatthew G. Knepley   PetscLayout     layout;
4293507e4973SMatthew G. Knepley   const PetscInt *ranges;
4294507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4295507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4296507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4297507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4298507e4973SMatthew G. Knepley 
4299507e4973SMatthew G. Knepley   PetscFunctionBegin;
4300507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4301507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4302507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4303507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4304507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4305507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4306507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4307507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4308507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4309507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4310507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4311507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4312f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4313507e4973SMatthew G. Knepley 
4314507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4315507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4316507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4317507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4318507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4319507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4320507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4321507e4973SMatthew 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;}
4322507e4973SMatthew 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;}
4323507e4973SMatthew G. Knepley     if (gdof < 0) {
4324507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4325507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4326507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4327507e4973SMatthew G. Knepley 
4328507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4329507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4330507e4973SMatthew 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;}
4331507e4973SMatthew G. Knepley       }
4332507e4973SMatthew G. Knepley     }
4333507e4973SMatthew G. Knepley   }
4334507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4335507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4336b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4337507e4973SMatthew G. Knepley   if (!gvalid) {
4338507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4339507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4340507e4973SMatthew G. Knepley   }
4341507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4342507e4973SMatthew G. Knepley }
4343f741bcd2SMatthew G. Knepley #endif
4344507e4973SMatthew G. Knepley 
434588ed4aceSMatthew G Knepley /*@
4346e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
434788ed4aceSMatthew G Knepley 
4348d083f849SBarry Smith   Collective on dm
43498b1ab98fSJed Brown 
435088ed4aceSMatthew G Knepley   Input Parameter:
435188ed4aceSMatthew G Knepley . dm - The DM
435288ed4aceSMatthew G Knepley 
435388ed4aceSMatthew G Knepley   Output Parameter:
435488ed4aceSMatthew G Knepley . section - The PetscSection
435588ed4aceSMatthew G Knepley 
435688ed4aceSMatthew G Knepley   Level: intermediate
435788ed4aceSMatthew G Knepley 
435888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
435988ed4aceSMatthew G Knepley 
436092fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
436188ed4aceSMatthew G Knepley @*/
4362e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
43630adebc6cSBarry Smith {
436488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
436588ed4aceSMatthew G Knepley 
436688ed4aceSMatthew G Knepley   PetscFunctionBegin;
436788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
436888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
43691bb6d2a8SBarry Smith   if (!dm->globalSection) {
4370fd59a867SMatthew G. Knepley     PetscSection s;
4371fd59a867SMatthew G. Knepley 
437292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4373fd59a867SMatthew 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");
437433907cc2SStefano 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");
43751bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4376cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
43771bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
43781bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
437988ed4aceSMatthew G Knepley   }
43801bb6d2a8SBarry Smith   *section = dm->globalSection;
438188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
438288ed4aceSMatthew G Knepley }
438388ed4aceSMatthew G Knepley 
4384b21d0597SMatthew G Knepley /*@
4385e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4386b21d0597SMatthew G Knepley 
4387b21d0597SMatthew G Knepley   Input Parameters:
4388b21d0597SMatthew G Knepley + dm - The DM
43895080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4390b21d0597SMatthew G Knepley 
4391b21d0597SMatthew G Knepley   Level: intermediate
4392b21d0597SMatthew G Knepley 
4393b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4394b21d0597SMatthew G Knepley 
439592fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4396b21d0597SMatthew G Knepley @*/
4397e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
43980adebc6cSBarry Smith {
4399b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4400b21d0597SMatthew G Knepley 
4401b21d0597SMatthew G Knepley   PetscFunctionBegin;
4402b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44035080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
44041d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
44051bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
44061bb6d2a8SBarry Smith   dm->globalSection = section;
4407497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
44081bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4409507e4973SMatthew G. Knepley #endif
4410b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4411b21d0597SMatthew G Knepley }
4412b21d0597SMatthew G Knepley 
441388ed4aceSMatthew G Knepley /*@
44141bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
441588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
441688ed4aceSMatthew G Knepley 
441788ed4aceSMatthew G Knepley   Input Parameter:
441888ed4aceSMatthew G Knepley . dm - The DM
441988ed4aceSMatthew G Knepley 
442088ed4aceSMatthew G Knepley   Output Parameter:
442188ed4aceSMatthew G Knepley . sf - The PetscSF
442288ed4aceSMatthew G Knepley 
442388ed4aceSMatthew G Knepley   Level: intermediate
442488ed4aceSMatthew G Knepley 
442588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
442688ed4aceSMatthew G Knepley 
44271bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
442888ed4aceSMatthew G Knepley @*/
44291bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
44300adebc6cSBarry Smith {
443188ed4aceSMatthew G Knepley   PetscInt       nroots;
443288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
443388ed4aceSMatthew G Knepley 
443488ed4aceSMatthew G Knepley   PetscFunctionBegin;
443588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
443688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
44371bb6d2a8SBarry Smith   if (!dm->sectionSF) {
44381bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
443933907cc2SStefano Zampini   }
44401bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
444188ed4aceSMatthew G Knepley   if (nroots < 0) {
444288ed4aceSMatthew G Knepley     PetscSection section, gSection;
444388ed4aceSMatthew G Knepley 
444492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
444531ea6d37SMatthew G Knepley     if (section) {
4446e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
44471bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
444831ea6d37SMatthew G Knepley     } else {
44490298fd71SBarry Smith       *sf = NULL;
445031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
445131ea6d37SMatthew G Knepley     }
445288ed4aceSMatthew G Knepley   }
44531bb6d2a8SBarry Smith   *sf = dm->sectionSF;
445488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
445588ed4aceSMatthew G Knepley }
445688ed4aceSMatthew G Knepley 
445788ed4aceSMatthew G Knepley /*@
44581bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
445988ed4aceSMatthew G Knepley 
446088ed4aceSMatthew G Knepley   Input Parameters:
446188ed4aceSMatthew G Knepley + dm - The DM
446288ed4aceSMatthew G Knepley - sf - The PetscSF
446388ed4aceSMatthew G Knepley 
446488ed4aceSMatthew G Knepley   Level: intermediate
446588ed4aceSMatthew G Knepley 
446688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
446788ed4aceSMatthew G Knepley 
44681bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
446988ed4aceSMatthew G Knepley @*/
44701bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
44710adebc6cSBarry Smith {
447288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
447388ed4aceSMatthew G Knepley 
447488ed4aceSMatthew G Knepley   PetscFunctionBegin;
447588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4476b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
447733907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
44781bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
44791bb6d2a8SBarry Smith   dm->sectionSF = sf;
448088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
448188ed4aceSMatthew G Knepley }
448288ed4aceSMatthew G Knepley 
448388ed4aceSMatthew G Knepley /*@C
44841bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
448588ed4aceSMatthew G Knepley   describing the data layout.
448688ed4aceSMatthew G Knepley 
448788ed4aceSMatthew G Knepley   Input Parameters:
448888ed4aceSMatthew G Knepley + dm - The DM
448988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
449088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
449188ed4aceSMatthew G Knepley 
44921bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
449388ed4aceSMatthew G Knepley 
44941bb6d2a8SBarry Smith   Level: developer
44951bb6d2a8SBarry Smith 
44961bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
44971bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
44981bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
44991bb6d2a8SBarry Smith 
45001bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
450188ed4aceSMatthew G Knepley @*/
45021bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
450388ed4aceSMatthew G Knepley {
450488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
450588ed4aceSMatthew G Knepley 
450688ed4aceSMatthew G Knepley   PetscFunctionBegin;
450788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4508b0c7db22SLisandro Dalcin   ierr = PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection);CHKERRQ(ierr);
450988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
451088ed4aceSMatthew G Knepley }
4511af122d2aSMatthew G Knepley 
4512b21d0597SMatthew G Knepley /*@
4513b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4514b21d0597SMatthew G Knepley 
4515b21d0597SMatthew G Knepley   Input Parameter:
4516b21d0597SMatthew G Knepley . dm - The DM
4517b21d0597SMatthew G Knepley 
4518b21d0597SMatthew G Knepley   Output Parameter:
4519b21d0597SMatthew G Knepley . sf - The PetscSF
4520b21d0597SMatthew G Knepley 
4521b21d0597SMatthew G Knepley   Level: intermediate
4522b21d0597SMatthew G Knepley 
4523b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4524b21d0597SMatthew G Knepley 
45251bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4526b21d0597SMatthew G Knepley @*/
45270adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
45280adebc6cSBarry Smith {
4529b21d0597SMatthew G Knepley   PetscFunctionBegin;
4530b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4531b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4532b21d0597SMatthew G Knepley   *sf = dm->sf;
4533b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4534b21d0597SMatthew G Knepley }
4535b21d0597SMatthew G Knepley 
4536057b4bcdSMatthew G Knepley /*@
4537057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4538057b4bcdSMatthew G Knepley 
4539057b4bcdSMatthew G Knepley   Input Parameters:
4540057b4bcdSMatthew G Knepley + dm - The DM
4541057b4bcdSMatthew G Knepley - sf - The PetscSF
4542057b4bcdSMatthew G Knepley 
4543057b4bcdSMatthew G Knepley   Level: intermediate
4544057b4bcdSMatthew G Knepley 
45451bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4546057b4bcdSMatthew G Knepley @*/
45470adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
45480adebc6cSBarry Smith {
4549057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4550057b4bcdSMatthew G Knepley 
4551057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4552057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4553b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4554057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
455533907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4556057b4bcdSMatthew G Knepley   dm->sf = sf;
4557057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4558057b4bcdSMatthew G Knepley }
4559057b4bcdSMatthew G Knepley 
456034aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
456134aa8a36SMatthew G. Knepley {
456234aa8a36SMatthew G. Knepley   PetscClassId   id;
456334aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
456434aa8a36SMatthew G. Knepley 
456534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
456634aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
456734aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
456834aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
456934aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
457034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
457117c1d62eSMatthew G. Knepley   } else {
457217c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
457334aa8a36SMatthew G. Knepley   }
457434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
457534aa8a36SMatthew G. Knepley }
457634aa8a36SMatthew G. Knepley 
457744a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
457844a7f3ddSMatthew G. Knepley {
457944a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
458044a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
458144a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
458244a7f3ddSMatthew G. Knepley 
458344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
458444a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
458544a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
458644a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4587e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
458844a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
458944a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
459044a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
459144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
459244a7f3ddSMatthew G. Knepley }
459344a7f3ddSMatthew G. Knepley 
459444a7f3ddSMatthew G. Knepley /*@
459544a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
459644a7f3ddSMatthew G. Knepley 
4597d083f849SBarry Smith   Logically collective on dm
459844a7f3ddSMatthew G. Knepley 
459944a7f3ddSMatthew G. Knepley   Input Parameter:
460044a7f3ddSMatthew G. Knepley . dm - The DM
460144a7f3ddSMatthew G. Knepley 
460244a7f3ddSMatthew G. Knepley   Level: intermediate
460344a7f3ddSMatthew G. Knepley 
460444a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
460544a7f3ddSMatthew G. Knepley @*/
460644a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
460744a7f3ddSMatthew G. Knepley {
460844a7f3ddSMatthew G. Knepley   PetscInt       f;
460944a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
461044a7f3ddSMatthew G. Knepley 
461144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
461244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
461344a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
461444a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
461544a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
461644a7f3ddSMatthew G. Knepley   }
461744a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
461844a7f3ddSMatthew G. Knepley   dm->fields = NULL;
461944a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
462044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
462144a7f3ddSMatthew G. Knepley }
462244a7f3ddSMatthew G. Knepley 
4623689b5837SMatthew G. Knepley /*@
4624689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4625689b5837SMatthew G. Knepley 
4626689b5837SMatthew G. Knepley   Not collective
4627689b5837SMatthew G. Knepley 
4628689b5837SMatthew G. Knepley   Input Parameter:
4629689b5837SMatthew G. Knepley . dm - The DM
4630689b5837SMatthew G. Knepley 
4631689b5837SMatthew G. Knepley   Output Parameter:
4632689b5837SMatthew G. Knepley . Nf - The number of fields
4633689b5837SMatthew G. Knepley 
4634689b5837SMatthew G. Knepley   Level: intermediate
4635689b5837SMatthew G. Knepley 
4636689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4637689b5837SMatthew G. Knepley @*/
46380f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
46390f21e855SMatthew G. Knepley {
46400f21e855SMatthew G. Knepley   PetscFunctionBegin;
46410f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4642534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
464344a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4644af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4645af122d2aSMatthew G Knepley }
4646af122d2aSMatthew G Knepley 
4647689b5837SMatthew G. Knepley /*@
4648689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4649689b5837SMatthew G. Knepley 
4650d083f849SBarry Smith   Logically collective on dm
4651689b5837SMatthew G. Knepley 
4652689b5837SMatthew G. Knepley   Input Parameters:
4653689b5837SMatthew G. Knepley + dm - The DM
4654689b5837SMatthew G. Knepley - Nf - The number of fields
4655689b5837SMatthew G. Knepley 
4656689b5837SMatthew G. Knepley   Level: intermediate
4657689b5837SMatthew G. Knepley 
4658689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4659689b5837SMatthew G. Knepley @*/
4660af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4661af122d2aSMatthew G Knepley {
46620f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4663af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4664af122d2aSMatthew G Knepley 
4665af122d2aSMatthew G Knepley   PetscFunctionBegin;
4666af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
466744a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
46680f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
46690f21e855SMatthew G. Knepley     PetscContainer obj;
46700f21e855SMatthew G. Knepley 
46710f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
467244a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
46730f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4674af122d2aSMatthew G Knepley   }
4675af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4676af122d2aSMatthew G Knepley }
4677af122d2aSMatthew G Knepley 
4678c1929be8SMatthew G. Knepley /*@
4679c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4680c1929be8SMatthew G. Knepley 
4681c1929be8SMatthew G. Knepley   Not collective
4682c1929be8SMatthew G. Knepley 
4683c1929be8SMatthew G. Knepley   Input Parameters:
4684c1929be8SMatthew G. Knepley + dm - The DM
4685c1929be8SMatthew G. Knepley - f  - The field number
4686c1929be8SMatthew G. Knepley 
468744a7f3ddSMatthew G. Knepley   Output Parameters:
468844a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
468944a7f3ddSMatthew G. Knepley - field - The discretization object
4690c1929be8SMatthew G. Knepley 
469144a7f3ddSMatthew G. Knepley   Level: intermediate
4692c1929be8SMatthew G. Knepley 
469344a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4694c1929be8SMatthew G. Knepley @*/
469544a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4696af122d2aSMatthew G Knepley {
4697af122d2aSMatthew G Knepley   PetscFunctionBegin;
4698af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
469944a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
470044a7f3ddSMatthew 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);
470144a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
470244a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4703decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4704decb47aaSMatthew G. Knepley }
4705decb47aaSMatthew G. Knepley 
4706083401c6SMatthew G. Knepley /* Does not clear the DS */
4707083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4708083401c6SMatthew G. Knepley {
4709083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4710083401c6SMatthew G. Knepley 
4711083401c6SMatthew G. Knepley   PetscFunctionBegin;
4712083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4713083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4714083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4715083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4716083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4717083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4718083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4719083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4720083401c6SMatthew G. Knepley }
4721083401c6SMatthew G. Knepley 
4722c1929be8SMatthew G. Knepley /*@
4723c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4724c1929be8SMatthew G. Knepley 
4725d083f849SBarry Smith   Logically collective on dm
4726c1929be8SMatthew G. Knepley 
4727c1929be8SMatthew G. Knepley   Input Parameters:
4728c1929be8SMatthew G. Knepley + dm    - The DM
4729c1929be8SMatthew G. Knepley . f     - The field number
473044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4731c1929be8SMatthew G. Knepley - field - The discretization object
4732c1929be8SMatthew G. Knepley 
473344a7f3ddSMatthew G. Knepley   Level: intermediate
4734c1929be8SMatthew G. Knepley 
473544a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4736c1929be8SMatthew G. Knepley @*/
473744a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4738decb47aaSMatthew G. Knepley {
4739decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4740decb47aaSMatthew G. Knepley 
4741decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4742decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4743e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
474444a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4745e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4746083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
474734aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
47482df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
474944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
475044a7f3ddSMatthew G. Knepley }
475144a7f3ddSMatthew G. Knepley 
475244a7f3ddSMatthew G. Knepley /*@
475344a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
475444a7f3ddSMatthew G. Knepley 
4755d083f849SBarry Smith   Logically collective on dm
475644a7f3ddSMatthew G. Knepley 
475744a7f3ddSMatthew G. Knepley   Input Parameters:
475844a7f3ddSMatthew G. Knepley + dm    - The DM
475944a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
476044a7f3ddSMatthew G. Knepley - field - The discretization object
476144a7f3ddSMatthew G. Knepley 
476244a7f3ddSMatthew G. Knepley   Level: intermediate
476344a7f3ddSMatthew G. Knepley 
476444a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
476544a7f3ddSMatthew G. Knepley @*/
476644a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
476744a7f3ddSMatthew G. Knepley {
476844a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
476944a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
477044a7f3ddSMatthew G. Knepley 
477144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
477244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477344a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
477444a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
477544a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
477644a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
477744a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
477844a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
477944a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
478034aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
47812df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4782af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4783af122d2aSMatthew G Knepley }
47846636e97aSMatthew G Knepley 
4785e5e52638SMatthew G. Knepley /*@
4786e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4787e0b68406SMatthew Knepley 
4788e0b68406SMatthew Knepley   Logically collective on dm
4789e0b68406SMatthew Knepley 
4790e0b68406SMatthew Knepley   Input Parameters:
4791e0b68406SMatthew Knepley + dm          - The DM
4792e0b68406SMatthew Knepley . f           - The field index
4793e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4794e0b68406SMatthew Knepley 
4795e0b68406SMatthew Knepley   Level: intermediate
4796e0b68406SMatthew Knepley 
4797e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
4798e0b68406SMatthew Knepley @*/
4799e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4800e0b68406SMatthew Knepley {
4801e0b68406SMatthew Knepley   PetscFunctionBegin;
4802e0b68406SMatthew Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
4803e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4804e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4805e0b68406SMatthew Knepley }
4806e0b68406SMatthew Knepley 
4807e0b68406SMatthew Knepley /*@
4808e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4809e0b68406SMatthew Knepley 
4810e0b68406SMatthew Knepley   Logically collective on dm
4811e0b68406SMatthew Knepley 
4812e0b68406SMatthew Knepley   Input Parameters:
4813e0b68406SMatthew Knepley + dm          - The DM
4814e0b68406SMatthew Knepley - f           - The field index
4815e0b68406SMatthew Knepley 
4816e0b68406SMatthew Knepley   Output Parameter:
4817e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4818e0b68406SMatthew Knepley 
4819e0b68406SMatthew Knepley   Level: intermediate
4820e0b68406SMatthew Knepley 
4821e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
4822e0b68406SMatthew Knepley @*/
4823e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4824e0b68406SMatthew Knepley {
4825e0b68406SMatthew Knepley   PetscFunctionBegin;
4826e0b68406SMatthew Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
4827e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4828e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4829e0b68406SMatthew Knepley }
4830e0b68406SMatthew Knepley 
4831e0b68406SMatthew Knepley /*@
4832e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4833e5e52638SMatthew G. Knepley 
4834d083f849SBarry Smith   Collective on dm
4835e5e52638SMatthew G. Knepley 
4836e5e52638SMatthew G. Knepley   Input Parameter:
4837e5e52638SMatthew G. Knepley . dm - The DM
4838e5e52638SMatthew G. Knepley 
4839e5e52638SMatthew G. Knepley   Output Parameter:
4840e5e52638SMatthew G. Knepley . newdm - The DM
4841e5e52638SMatthew G. Knepley 
4842e5e52638SMatthew G. Knepley   Level: advanced
4843e5e52638SMatthew G. Knepley 
4844e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4845e5e52638SMatthew G. Knepley @*/
4846e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4847e5e52638SMatthew G. Knepley {
4848e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4849e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4850e5e52638SMatthew G. Knepley 
4851e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4852e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4853e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4854e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4855e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4856e5e52638SMatthew G. Knepley     DMLabel     label;
4857e5e52638SMatthew G. Knepley     PetscObject field;
485834aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4859e5e52638SMatthew G. Knepley 
4860e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4861e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
486234aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
486334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
486434aa8a36SMatthew G. Knepley   }
486534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
486634aa8a36SMatthew G. Knepley }
486734aa8a36SMatthew G. Knepley 
486834aa8a36SMatthew G. Knepley /*@
486934aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
487034aa8a36SMatthew G. Knepley 
487134aa8a36SMatthew G. Knepley   Not collective
487234aa8a36SMatthew G. Knepley 
487334aa8a36SMatthew G. Knepley   Input Parameters:
487434aa8a36SMatthew G. Knepley + dm - The DM object
487534aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
487634aa8a36SMatthew G. Knepley 
487734aa8a36SMatthew G. Knepley   Output Parameter:
487834aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
487934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
488034aa8a36SMatthew G. Knepley 
488134aa8a36SMatthew G. Knepley   Notes:
488234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
488334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
488434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4885979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
488634aa8a36SMatthew G. Knepley 
488734aa8a36SMatthew G. Knepley   Level: developer
488834aa8a36SMatthew G. Knepley 
488934aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
489034aa8a36SMatthew G. Knepley @*/
489134aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
489234aa8a36SMatthew G. Knepley {
489334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
489434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4895534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4896534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
489734aa8a36SMatthew G. Knepley   if (f < 0) {
489834aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
489934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
490034aa8a36SMatthew G. Knepley   } else {
490134aa8a36SMatthew G. Knepley     PetscInt       Nf;
490234aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
490334aa8a36SMatthew G. Knepley 
490434aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
490534aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
490634aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
490734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
490834aa8a36SMatthew G. Knepley   }
490934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
491034aa8a36SMatthew G. Knepley }
491134aa8a36SMatthew G. Knepley 
491234aa8a36SMatthew G. Knepley /*@
491334aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
491434aa8a36SMatthew G. Knepley 
491534aa8a36SMatthew G. Knepley   Not collective
491634aa8a36SMatthew G. Knepley 
491734aa8a36SMatthew G. Knepley   Input Parameters:
491834aa8a36SMatthew G. Knepley + dm         - The DM object
491934aa8a36SMatthew G. Knepley . f          - The field number
492034aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
492134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
492234aa8a36SMatthew G. Knepley 
492334aa8a36SMatthew G. Knepley   Notes:
492434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
492534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
492634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4927979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
492834aa8a36SMatthew G. Knepley 
492934aa8a36SMatthew G. Knepley   Level: developer
493034aa8a36SMatthew G. Knepley 
493134aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
493234aa8a36SMatthew G. Knepley @*/
493334aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
493434aa8a36SMatthew G. Knepley {
493534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
493634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
493734aa8a36SMatthew G. Knepley   if (f < 0) {
493834aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
493934aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
494034aa8a36SMatthew G. Knepley   } else {
494134aa8a36SMatthew G. Knepley     PetscInt       Nf;
494234aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
494334aa8a36SMatthew G. Knepley 
494434aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
494534aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
494634aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
494734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4948e5e52638SMatthew G. Knepley   }
4949e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4950e5e52638SMatthew G. Knepley }
4951e5e52638SMatthew G. Knepley 
4952b0441da4SMatthew G. Knepley /*@
4953b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4954b0441da4SMatthew G. Knepley 
4955b0441da4SMatthew G. Knepley   Not collective
4956b0441da4SMatthew G. Knepley 
4957b0441da4SMatthew G. Knepley   Input Parameters:
4958b0441da4SMatthew G. Knepley . dm - The DM object
4959b0441da4SMatthew G. Knepley 
4960b0441da4SMatthew G. Knepley   Output Parameter:
4961b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4962b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4963b0441da4SMatthew G. Knepley 
4964b0441da4SMatthew G. Knepley   Notes:
4965b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4966b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4967b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4968b0441da4SMatthew G. Knepley 
4969b0441da4SMatthew G. Knepley   Level: developer
4970b0441da4SMatthew G. Knepley 
4971b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4972b0441da4SMatthew G. Knepley @*/
4973b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4974b0441da4SMatthew G. Knepley {
4975b0441da4SMatthew G. Knepley   PetscInt       Nf;
4976b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4977b0441da4SMatthew G. Knepley 
4978b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4979b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4980534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4981534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4982b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4983b0441da4SMatthew G. Knepley   if (!Nf) {
4984b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4985b0441da4SMatthew G. Knepley   } else {
4986b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4987b0441da4SMatthew G. Knepley   }
4988b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4989b0441da4SMatthew G. Knepley }
4990b0441da4SMatthew G. Knepley 
4991b0441da4SMatthew G. Knepley /*@
4992b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4993b0441da4SMatthew G. Knepley 
4994b0441da4SMatthew G. Knepley   Not collective
4995b0441da4SMatthew G. Knepley 
4996b0441da4SMatthew G. Knepley   Input Parameters:
4997b0441da4SMatthew G. Knepley + dm         - The DM object
4998b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4999b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5000b0441da4SMatthew G. Knepley 
5001b0441da4SMatthew G. Knepley   Notes:
5002b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5003b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5004b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5005b0441da4SMatthew G. Knepley 
5006b0441da4SMatthew G. Knepley   Level: developer
5007b0441da4SMatthew G. Knepley 
5008b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5009b0441da4SMatthew G. Knepley @*/
5010b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5011b0441da4SMatthew G. Knepley {
5012b0441da4SMatthew G. Knepley   PetscInt       Nf;
5013b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5014b0441da4SMatthew G. Knepley 
5015b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5016b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5017b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5018b0441da4SMatthew G. Knepley   if (!Nf) {
5019b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5020b0441da4SMatthew G. Knepley   } else {
5021b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5022e5e52638SMatthew G. Knepley   }
5023e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5024e5e52638SMatthew G. Knepley }
5025e5e52638SMatthew G. Knepley 
5026783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
5027783e2ec8SMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, const char labelname[])
5028783e2ec8SMatthew G. Knepley {
5029783e2ec8SMatthew G. Knepley   DMLabel        label;
5030783e2ec8SMatthew G. Knepley   PetscObject    obj;
5031783e2ec8SMatthew G. Knepley   PetscClassId   id;
5032783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5033783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5034783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5035783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5036783e2ec8SMatthew G. Knepley 
5037783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5038783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5039783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5040783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5041783e2ec8SMatthew G. Knepley   ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
5042783e2ec8SMatthew G. Knepley   if (isFE && label) {
5043783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5044783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5045783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
5046783e2ec8SMatthew G. Knepley       const char *lname;
5047783e2ec8SMatthew G. Knepley 
504856cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, &lname, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5049783e2ec8SMatthew G. Knepley       ierr = PetscStrcmp(lname, labelname, &duplicate);CHKERRQ(ierr);
5050783e2ec8SMatthew G. Knepley       if (duplicate) break;
5051783e2ec8SMatthew G. Knepley     }
5052783e2ec8SMatthew G. Knepley     if (!duplicate) {
5053783e2ec8SMatthew G. Knepley       DM plex;
5054783e2ec8SMatthew G. Knepley 
5055783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5056783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5057783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5058783e2ec8SMatthew G. Knepley     }
5059783e2ec8SMatthew G. Knepley   }
5060783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5061783e2ec8SMatthew G. Knepley }
5062783e2ec8SMatthew G. Knepley 
5063e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5064e5e52638SMatthew G. Knepley {
5065e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5066e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5067e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5068e5e52638SMatthew G. Knepley 
5069e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5070e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5071e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5072e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5073b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5074e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5075e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5076e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5077e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5078e5e52638SMatthew G. Knepley }
5079e5e52638SMatthew G. Knepley 
5080e5e52638SMatthew G. Knepley /*@
5081e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5082e5e52638SMatthew G. Knepley 
5083e5e52638SMatthew G. Knepley   Not collective
5084e5e52638SMatthew G. Knepley 
5085e5e52638SMatthew G. Knepley   Input Parameter:
5086e5e52638SMatthew G. Knepley . dm - The DM
5087e5e52638SMatthew G. Knepley 
5088e5e52638SMatthew G. Knepley   Output Parameter:
5089e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5090e5e52638SMatthew G. Knepley 
5091e5e52638SMatthew G. Knepley   Level: intermediate
5092e5e52638SMatthew G. Knepley 
5093e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5094e5e52638SMatthew G. Knepley @*/
5095e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5096e5e52638SMatthew G. Knepley {
5097e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5098e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5099534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5100e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5101e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5102e5e52638SMatthew G. Knepley }
5103e5e52638SMatthew G. Knepley 
5104e5e52638SMatthew G. Knepley /*@
5105e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5106e5e52638SMatthew G. Knepley 
5107d083f849SBarry Smith   Logically collective on dm
5108e5e52638SMatthew G. Knepley 
5109e5e52638SMatthew G. Knepley   Input Parameter:
5110e5e52638SMatthew G. Knepley . dm - The DM
5111e5e52638SMatthew G. Knepley 
5112e5e52638SMatthew G. Knepley   Level: intermediate
5113e5e52638SMatthew G. Knepley 
5114e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5115e5e52638SMatthew G. Knepley @*/
5116e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5117e5e52638SMatthew G. Knepley {
5118e5e52638SMatthew G. Knepley   PetscInt       s;
5119e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5120e5e52638SMatthew G. Knepley 
5121e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5122e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5123e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5124e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5125e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5126b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5127e5e52638SMatthew G. Knepley   }
5128e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5129e5e52638SMatthew G. Knepley   dm->probs = NULL;
5130e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5131e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5132e5e52638SMatthew G. Knepley }
5133e5e52638SMatthew G. Knepley 
5134e5e52638SMatthew G. Knepley /*@
5135e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5136e5e52638SMatthew G. Knepley 
5137e5e52638SMatthew G. Knepley   Not collective
5138e5e52638SMatthew G. Knepley 
5139e5e52638SMatthew G. Knepley   Input Parameter:
5140e5e52638SMatthew G. Knepley . dm    - The DM
5141e5e52638SMatthew G. Knepley 
5142e5e52638SMatthew G. Knepley   Output Parameter:
5143e5e52638SMatthew G. Knepley . prob - The default PetscDS
5144e5e52638SMatthew G. Knepley 
5145e5e52638SMatthew G. Knepley   Level: intermediate
5146e5e52638SMatthew G. Knepley 
5147e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5148e5e52638SMatthew G. Knepley @*/
5149e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5150e5e52638SMatthew G. Knepley {
5151b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5152b0143b4dSMatthew G. Knepley 
5153e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5154e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5155e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5156b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5157b0143b4dSMatthew G. Knepley     PetscDS ds;
5158b0143b4dSMatthew G. Knepley 
5159b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5160b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5161b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5162b0143b4dSMatthew G. Knepley   }
5163b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5164e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5165e5e52638SMatthew G. Knepley }
5166e5e52638SMatthew G. Knepley 
5167e5e52638SMatthew G. Knepley /*@
5168e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5169e5e52638SMatthew G. Knepley 
5170e5e52638SMatthew G. Knepley   Not collective
5171e5e52638SMatthew G. Knepley 
5172e5e52638SMatthew G. Knepley   Input Parameters:
5173e5e52638SMatthew G. Knepley + dm    - The DM
5174e5e52638SMatthew G. Knepley - point - Cell for the DS
5175e5e52638SMatthew G. Knepley 
5176e5e52638SMatthew G. Knepley   Output Parameter:
5177e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5178e5e52638SMatthew G. Knepley 
5179e5e52638SMatthew G. Knepley   Level: developer
5180e5e52638SMatthew G. Knepley 
5181b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5182e5e52638SMatthew G. Knepley @*/
5183e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5184e5e52638SMatthew G. Knepley {
5185e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5186e5e52638SMatthew G. Knepley   PetscInt       s;
5187e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5188e5e52638SMatthew G. Knepley 
5189e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5190e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5191e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5192360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5193e5e52638SMatthew G. Knepley   *prob = NULL;
5194e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5195e5e52638SMatthew G. Knepley     PetscInt val;
5196e5e52638SMatthew G. Knepley 
5197e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5198e5e52638SMatthew G. Knepley     else {
5199e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5200e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5201e5e52638SMatthew G. Knepley     }
5202e5e52638SMatthew G. Knepley   }
5203e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5204e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5205e5e52638SMatthew G. Knepley }
5206e5e52638SMatthew G. Knepley 
5207e5e52638SMatthew G. Knepley /*@
5208e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5209e5e52638SMatthew G. Knepley 
5210e5e52638SMatthew G. Knepley   Not collective
5211e5e52638SMatthew G. Knepley 
5212e5e52638SMatthew G. Knepley   Input Parameters:
5213e5e52638SMatthew G. Knepley + dm    - The DM
5214e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5215e5e52638SMatthew G. Knepley 
5216b3cf3223SMatthew G. Knepley   Output Parameters:
5217b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5218b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5219e5e52638SMatthew G. Knepley 
5220e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5221e5e52638SMatthew G. Knepley 
5222e5e52638SMatthew G. Knepley   Level: advanced
5223e5e52638SMatthew G. Knepley 
5224e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5225e5e52638SMatthew G. Knepley @*/
5226b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5227e5e52638SMatthew G. Knepley {
5228e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5229e5e52638SMatthew G. Knepley 
5230e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5231e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5232e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5233b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5234b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5235e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5236b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5237b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5238b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5239b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5240b3cf3223SMatthew G. Knepley     }
5241e5e52638SMatthew G. Knepley   }
52422df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5243e5e52638SMatthew G. Knepley }
5244e5e52638SMatthew G. Knepley 
5245e5e52638SMatthew G. Knepley /*@
5246083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5247083401c6SMatthew G. Knepley 
5248083401c6SMatthew G. Knepley   Collective on dm
5249083401c6SMatthew G. Knepley 
5250083401c6SMatthew G. Knepley   Input Parameters:
5251083401c6SMatthew G. Knepley + dm     - The DM
5252083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5253083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5254083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5255083401c6SMatthew G. Knepley 
5256083401c6SMatthew 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,
5257083401c6SMatthew G. Knepley   the fields argument is ignored.
5258083401c6SMatthew G. Knepley 
5259083401c6SMatthew G. Knepley   Level: advanced
5260083401c6SMatthew G. Knepley 
5261083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5262083401c6SMatthew G. Knepley @*/
5263083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5264083401c6SMatthew G. Knepley {
5265083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5266083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5267083401c6SMatthew G. Knepley 
5268083401c6SMatthew G. Knepley   PetscFunctionBegin;
5269083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5270083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5271083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5272083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5273083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5274083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5275083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5276083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5277083401c6SMatthew G. Knepley     }
5278083401c6SMatthew G. Knepley   }
5279083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5280083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5281083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5282083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5283083401c6SMatthew G. Knepley   if (!label) {
5284083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5285083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5286083401c6SMatthew G. Knepley     Nds = 0;
5287083401c6SMatthew G. Knepley   }
5288083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5289083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5290083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5291083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5292083401c6SMatthew G. Knepley }
5293083401c6SMatthew G. Knepley 
5294083401c6SMatthew G. Knepley /*@
5295e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5296e5e52638SMatthew G. Knepley 
5297e5e52638SMatthew G. Knepley   Not collective
5298e5e52638SMatthew G. Knepley 
5299e5e52638SMatthew G. Knepley   Input Parameters:
5300e5e52638SMatthew G. Knepley + dm  - The DM
5301e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5302e5e52638SMatthew G. Knepley 
5303e5e52638SMatthew G. Knepley   Output Parameters:
5304b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5305b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5306083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5307e5e52638SMatthew G. Knepley 
5308e5e52638SMatthew G. Knepley   Level: advanced
5309e5e52638SMatthew G. Knepley 
5310e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5311e5e52638SMatthew G. Knepley @*/
5312b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5313e5e52638SMatthew G. Knepley {
5314e5e52638SMatthew G. Knepley   PetscInt       Nds;
5315e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5316e5e52638SMatthew G. Knepley 
5317e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5318e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5319e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5320e5e52638SMatthew 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);
5321e5e52638SMatthew G. Knepley   if (label) {
5322e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5323e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5324e5e52638SMatthew G. Knepley   }
5325b3cf3223SMatthew G. Knepley   if (fields) {
5326b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5327b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5328b3cf3223SMatthew G. Knepley   }
5329e5e52638SMatthew G. Knepley   if (ds) {
5330b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5331e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5332e5e52638SMatthew G. Knepley   }
5333e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5334e5e52638SMatthew G. Knepley }
5335e5e52638SMatthew G. Knepley 
5336e5e52638SMatthew G. Knepley /*@
5337083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5338e5e52638SMatthew G. Knepley 
5339083401c6SMatthew G. Knepley   Not collective
5340e5e52638SMatthew G. Knepley 
5341e5e52638SMatthew G. Knepley   Input Parameters:
5342e5e52638SMatthew G. Knepley + dm     - The DM
5343083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5344083401c6SMatthew G. Knepley . label  - The region label, or NULL
5345083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5346083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5347e5e52638SMatthew G. Knepley 
5348e5e52638SMatthew G. Knepley   Level: advanced
5349e5e52638SMatthew G. Knepley 
5350083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5351e5e52638SMatthew G. Knepley @*/
5352083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5353e5e52638SMatthew G. Knepley {
5354083401c6SMatthew G. Knepley   PetscInt       Nds;
5355e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5356e5e52638SMatthew G. Knepley 
5357e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5358e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5359083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5360083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5361083401c6SMatthew 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);
5362e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5363083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5364083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5365083401c6SMatthew G. Knepley   if (fields) {
5366083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5367b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5368083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5369083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5370e5e52638SMatthew G. Knepley   }
5371083401c6SMatthew G. Knepley   if (ds) {
5372083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5373083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5374083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5375083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5376083401c6SMatthew G. Knepley   }
5377e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5378e5e52638SMatthew G. Knepley }
5379e5e52638SMatthew G. Knepley 
5380e5e52638SMatthew G. Knepley /*@
53811d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
53821d3af9e0SMatthew G. Knepley 
53831d3af9e0SMatthew G. Knepley   Not collective
53841d3af9e0SMatthew G. Knepley 
53851d3af9e0SMatthew G. Knepley   Input Parameters:
53861d3af9e0SMatthew G. Knepley + dm  - The DM
53871d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
53881d3af9e0SMatthew G. Knepley 
53891d3af9e0SMatthew G. Knepley   Output Parameter:
53901d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
53911d3af9e0SMatthew G. Knepley 
53921d3af9e0SMatthew G. Knepley   Level: advanced
53931d3af9e0SMatthew G. Knepley 
53941d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
53951d3af9e0SMatthew G. Knepley @*/
53961d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
53971d3af9e0SMatthew G. Knepley {
53981d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
53991d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
54001d3af9e0SMatthew G. Knepley 
54011d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
54021d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54031d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
54041d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
54051d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
54061d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
54071d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
54081d3af9e0SMatthew G. Knepley   else          *num = n;
54091d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
54101d3af9e0SMatthew G. Knepley }
54111d3af9e0SMatthew G. Knepley 
54121d3af9e0SMatthew G. Knepley /*@
5413e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5414e5e52638SMatthew G. Knepley 
5415d083f849SBarry Smith   Collective on dm
5416e5e52638SMatthew G. Knepley 
5417e5e52638SMatthew G. Knepley   Input Parameter:
5418e5e52638SMatthew G. Knepley . dm - The DM
5419e5e52638SMatthew G. Knepley 
5420e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5421e5e52638SMatthew G. Knepley 
5422e5e52638SMatthew G. Knepley   Level: intermediate
5423e5e52638SMatthew G. Knepley 
5424e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5425e5e52638SMatthew G. Knepley @*/
5426e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5427e5e52638SMatthew G. Knepley {
5428e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5429083401c6SMatthew G. Knepley   PetscDS        dsDef;
5430083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5431083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5432e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5433e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5434e5e52638SMatthew G. Knepley 
5435e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5436e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5437e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5438e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5439083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5440083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5441083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5442083401c6SMatthew G. Knepley   Nl   = 0;
5443083401c6SMatthew G. Knepley   Ndef = 0;
5444083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5445083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5446083401c6SMatthew G. Knepley     PetscInt l;
5447083401c6SMatthew G. Knepley 
5448083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5449083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5450083401c6SMatthew G. Knepley     if (l < Nl) continue;
5451083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5452083401c6SMatthew G. Knepley   }
5453083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5454083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5455083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5456b3cf3223SMatthew G. Knepley     IS        fields;
5457b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5458b3cf3223SMatthew G. Knepley 
5459b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5460083401c6SMatthew G. Knepley     if (nf) {
5461b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5462b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
546388f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
546488f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
546588f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
546688f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
546788f0c812SMatthew G. Knepley 
5468083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5469083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5470083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5471b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
54722df9ee95SMatthew G. Knepley     }
5473083401c6SMatthew G. Knepley   }
5474083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5475083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5476083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5477083401c6SMatthew G. Knepley   if (Ndef && Nl) {
54780122748bSMatthew G. Knepley     DM              plex;
5479083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5480083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5481083401c6SMatthew G. Knepley     PetscInt       *fields;
5482083401c6SMatthew G. Knepley     const PetscInt *cells;
5483083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
54840122748bSMatthew G. Knepley 
54850122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
54860122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5487083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5488083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5489083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5490083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5491083401c6SMatthew G. Knepley       IS      pointIS;
5492083401c6SMatthew G. Knepley 
5493083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5494083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5495083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5496083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5497083401c6SMatthew G. Knepley     }
5498083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5499083401c6SMatthew G. Knepley 
5500083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5501083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5502083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5503083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5504083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5505083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5506083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5507083401c6SMatthew G. Knepley 
5508083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5509083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5510083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5511083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5512083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5513083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5514083401c6SMatthew G. Knepley 
5515083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5516083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5517083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5518083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5519083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5520083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
55210122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5522083401c6SMatthew G. Knepley   }
5523083401c6SMatthew G. Knepley   /* Create label DSes
5524083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5525083401c6SMatthew G. Knepley   */
5526083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5527083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5528083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5529083401c6SMatthew G. Knepley     PetscDS   ds;
5530083401c6SMatthew G. Knepley     IS        fields;
5531083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5532083401c6SMatthew G. Knepley 
5533083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5534083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5535083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5536083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5537083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5538083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5539083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5540083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5541083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5542083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5543083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5544083401c6SMatthew G. Knepley     {
5545083401c6SMatthew G. Knepley       DMPolytopeType ct;
5546083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5547083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
55480122748bSMatthew G. Knepley 
5549e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5550665f567fSMatthew G. Knepley       if (lStart >= 0) {
5551412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5552412e9a14SMatthew G. Knepley         switch (ct) {
5553412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5554412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5555412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5556412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5557083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5558083401c6SMatthew G. Knepley           default: break;
5559412e9a14SMatthew G. Knepley         }
5560665f567fSMatthew G. Knepley       }
5561083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5562083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5563e5e52638SMatthew G. Knepley     }
5564083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5565e5e52638SMatthew G. Knepley   }
5566083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5567e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5568083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5569083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5570083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5571083401c6SMatthew G. Knepley     const PetscInt *fld;
5572083401c6SMatthew G. Knepley     PetscInt        nf;
5573e5e52638SMatthew G. Knepley 
5574083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5575083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5576083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5577083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5578083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5579e5e52638SMatthew G. Knepley       PetscClassId id;
5580e5e52638SMatthew G. Knepley 
5581083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5582083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5583083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5584083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5585083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5586e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5587e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5588e5e52638SMatthew G. Knepley     }
5589083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5590e5e52638SMatthew G. Knepley   }
5591e5e52638SMatthew G. Knepley   /* Setup DSes */
5592e5e52638SMatthew G. Knepley   if (doSetup) {
5593e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5594e5e52638SMatthew G. Knepley   }
5595e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5596e5e52638SMatthew G. Knepley }
5597e5e52638SMatthew G. Knepley 
5598e5e52638SMatthew G. Knepley /*@
55997f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
56007f96f943SMatthew G. Knepley 
5601f2cacb80SMatthew G. Knepley   Collective on DM
5602f2cacb80SMatthew G. Knepley 
56037f96f943SMatthew G. Knepley   Input Parameters:
56047f96f943SMatthew G. Knepley + dm   - The DM
56057f96f943SMatthew G. Knepley - time - The time
56067f96f943SMatthew G. Knepley 
56077f96f943SMatthew G. Knepley   Output Parameters:
5608f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5609f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
56107f96f943SMatthew G. Knepley 
56117f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
56127f96f943SMatthew G. Knepley 
56137f96f943SMatthew G. Knepley   Level: developer
56147f96f943SMatthew G. Knepley 
56157f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
56167f96f943SMatthew G. Knepley @*/
5617f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
56187f96f943SMatthew G. Knepley {
56197f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
56207f96f943SMatthew G. Knepley   void            **ectxs;
56217f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
56227f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
56237f96f943SMatthew G. Knepley 
56247f96f943SMatthew G. Knepley   PetscFunctionBegin;
5625f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5626f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5627f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
56287f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
56297f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
56307f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
56317f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
56327f96f943SMatthew G. Knepley     PetscDS         ds;
56337f96f943SMatthew G. Knepley     DMLabel         label;
56347f96f943SMatthew G. Knepley     IS              fieldIS;
56357f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
56367f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
56377f96f943SMatthew G. Knepley 
56387f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
56397f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
56407f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
56417f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
56427f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5643f2cacb80SMatthew G. Knepley     if (u) {
56447f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
56457f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
56467f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
56477f96f943SMatthew G. Knepley       }
56487f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
56497f96f943SMatthew G. Knepley       if (label) {
56507f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56517f96f943SMatthew G. Knepley       } else {
56527f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56537f96f943SMatthew G. Knepley       }
56547f96f943SMatthew G. Knepley     }
5655f2cacb80SMatthew G. Knepley     if (u_t) {
5656f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5657f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5658f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5659f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5660f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5661f2cacb80SMatthew G. Knepley       }
5662f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5663f2cacb80SMatthew G. Knepley       if (label) {
5664f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5665f2cacb80SMatthew G. Knepley       } else {
5666f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5667f2cacb80SMatthew G. Knepley       }
5668f2cacb80SMatthew G. Knepley     }
5669f2cacb80SMatthew G. Knepley   }
5670f2cacb80SMatthew G. Knepley   if (u) {
56717f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
56727f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5673f2cacb80SMatthew G. Knepley   }
5674f2cacb80SMatthew G. Knepley   if (u_t) {
5675f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5676f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5677f2cacb80SMatthew G. Knepley   }
56787f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
56797f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
56807f96f943SMatthew G. Knepley }
56817f96f943SMatthew G. Knepley 
56827f96f943SMatthew G. Knepley /*@
5683e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5684e5e52638SMatthew G. Knepley 
5685d083f849SBarry Smith   Collective on dm
5686e5e52638SMatthew G. Knepley 
5687e5e52638SMatthew G. Knepley   Input Parameter:
5688e5e52638SMatthew G. Knepley . dm - The DM
5689e5e52638SMatthew G. Knepley 
5690e5e52638SMatthew G. Knepley   Output Parameter:
5691e5e52638SMatthew G. Knepley . newdm - The DM
5692e5e52638SMatthew G. Knepley 
5693e5e52638SMatthew G. Knepley   Level: advanced
5694e5e52638SMatthew G. Knepley 
5695e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5696e5e52638SMatthew G. Knepley @*/
5697e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5698e5e52638SMatthew G. Knepley {
5699e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5700e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5701e5e52638SMatthew G. Knepley 
5702e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5703e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5704e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5705e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5706e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5707e5e52638SMatthew G. Knepley     DMLabel  label;
5708b3cf3223SMatthew G. Knepley     IS       fields;
5709e5e52638SMatthew G. Knepley     PetscDS  ds;
5710783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5711e5e52638SMatthew G. Knepley 
5712b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5713b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5714783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5715783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5716783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5717783e2ec8SMatthew G. Knepley       PetscInt    field;
5718783e2ec8SMatthew G. Knepley 
5719783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
572056cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5721783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5722783e2ec8SMatthew G. Knepley     }
5723e5e52638SMatthew G. Knepley   }
5724e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5725e5e52638SMatthew G. Knepley }
5726e5e52638SMatthew G. Knepley 
5727e5e52638SMatthew G. Knepley /*@
5728e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5729e5e52638SMatthew G. Knepley 
5730d083f849SBarry Smith   Collective on dm
5731e5e52638SMatthew G. Knepley 
5732e5e52638SMatthew G. Knepley   Input Parameter:
5733e5e52638SMatthew G. Knepley . dm - The DM
5734e5e52638SMatthew G. Knepley 
5735e5e52638SMatthew G. Knepley   Output Parameter:
5736e5e52638SMatthew G. Knepley . newdm - The DM
5737e5e52638SMatthew G. Knepley 
5738e5e52638SMatthew G. Knepley   Level: advanced
5739e5e52638SMatthew G. Knepley 
5740e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5741e5e52638SMatthew G. Knepley @*/
5742e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5743e5e52638SMatthew G. Knepley {
5744e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5745e5e52638SMatthew G. Knepley 
5746e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5747e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5748e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5749e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5750e5e52638SMatthew G. Knepley }
5751e5e52638SMatthew G. Knepley 
5752b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5753b64e0483SPeter Brune {
5754b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5755b64e0483SPeter Brune   PetscErrorCode ierr;
5756b64e0483SPeter Brune   Vec coords,ccoords;
57576dbf9973SLawrence Mitchell   Mat inject;
5758b64e0483SPeter Brune   PetscFunctionBegin;
5759b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5760b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5761b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5762b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5763b64e0483SPeter Brune   if (coords && !ccoords) {
5764b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
57656668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
57666dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
57672adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
57686dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5769b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5770b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5771b64e0483SPeter Brune   }
5772b64e0483SPeter Brune   PetscFunctionReturn(0);
5773b64e0483SPeter Brune }
5774b64e0483SPeter Brune 
577503dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
577603dadc2fSPeter Brune {
577703dadc2fSPeter Brune   DM dm_coord,subdm_coord;
577803dadc2fSPeter Brune   PetscErrorCode ierr;
577903dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
578003dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
578103dadc2fSPeter Brune   PetscFunctionBegin;
578203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
578303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
578403dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
578503dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
578603dadc2fSPeter Brune   if (coords && !ccoords) {
578703dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
57886668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
578903dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
579024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
579103dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
579203dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
579303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
57941ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
579503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
579603dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
579703dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
579803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
579903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
580003dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
580103dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
580203dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
580303dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
580403dadc2fSPeter Brune   }
580503dadc2fSPeter Brune   PetscFunctionReturn(0);
580603dadc2fSPeter Brune }
580703dadc2fSPeter Brune 
5808c73cfb54SMatthew G. Knepley /*@
5809c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5810c73cfb54SMatthew G. Knepley 
5811c73cfb54SMatthew G. Knepley   Not collective
5812c73cfb54SMatthew G. Knepley 
5813c73cfb54SMatthew G. Knepley   Input Parameter:
5814c73cfb54SMatthew G. Knepley . dm - The DM
5815c73cfb54SMatthew G. Knepley 
5816c73cfb54SMatthew G. Knepley   Output Parameter:
5817c73cfb54SMatthew G. Knepley . dim - The topological dimension
5818c73cfb54SMatthew G. Knepley 
5819c73cfb54SMatthew G. Knepley   Level: beginner
5820c73cfb54SMatthew G. Knepley 
5821c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5822c73cfb54SMatthew G. Knepley @*/
5823c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5824c73cfb54SMatthew G. Knepley {
5825c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5826c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5827534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5828c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5829c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5830c73cfb54SMatthew G. Knepley }
5831c73cfb54SMatthew G. Knepley 
5832c73cfb54SMatthew G. Knepley /*@
5833c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5834c73cfb54SMatthew G. Knepley 
5835c73cfb54SMatthew G. Knepley   Collective on dm
5836c73cfb54SMatthew G. Knepley 
5837c73cfb54SMatthew G. Knepley   Input Parameters:
5838c73cfb54SMatthew G. Knepley + dm - The DM
5839c73cfb54SMatthew G. Knepley - dim - The topological dimension
5840c73cfb54SMatthew G. Knepley 
5841c73cfb54SMatthew G. Knepley   Level: beginner
5842c73cfb54SMatthew G. Knepley 
5843c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5844c73cfb54SMatthew G. Knepley @*/
5845c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5846c73cfb54SMatthew G. Knepley {
5847e5e52638SMatthew G. Knepley   PetscDS        ds;
5848f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5849f17e8794SMatthew G. Knepley 
5850c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5851c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5852c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5853c73cfb54SMatthew G. Knepley   dm->dim = dim;
5854e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5855e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5856c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5857c73cfb54SMatthew G. Knepley }
5858c73cfb54SMatthew G. Knepley 
5859793f3fe5SMatthew G. Knepley /*@
5860793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5861793f3fe5SMatthew G. Knepley 
5862d083f849SBarry Smith   Collective on dm
5863793f3fe5SMatthew G. Knepley 
5864793f3fe5SMatthew G. Knepley   Input Parameters:
5865793f3fe5SMatthew G. Knepley + dm - the DM
5866793f3fe5SMatthew G. Knepley - dim - the dimension
5867793f3fe5SMatthew G. Knepley 
5868793f3fe5SMatthew G. Knepley   Output Parameters:
5869793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5870aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5871793f3fe5SMatthew G. Knepley 
5872793f3fe5SMatthew G. Knepley   Note:
5873793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5874a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5875793f3fe5SMatthew G. Knepley   then the interval is empty.
5876793f3fe5SMatthew G. Knepley 
5877793f3fe5SMatthew G. Knepley   Level: intermediate
5878793f3fe5SMatthew G. Knepley 
5879793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5880793f3fe5SMatthew G. Knepley @*/
5881793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5882793f3fe5SMatthew G. Knepley {
5883793f3fe5SMatthew G. Knepley   PetscInt       d;
5884793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5885793f3fe5SMatthew G. Knepley 
5886793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5887793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5888793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5889793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5890b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5891793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5892793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5893793f3fe5SMatthew G. Knepley }
5894793f3fe5SMatthew G. Knepley 
58956636e97aSMatthew G Knepley /*@
58966636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
58976636e97aSMatthew G Knepley 
5898d083f849SBarry Smith   Collective on dm
58996636e97aSMatthew G Knepley 
59006636e97aSMatthew G Knepley   Input Parameters:
59016636e97aSMatthew G Knepley + dm - the DM
59026636e97aSMatthew G Knepley - c - coordinate vector
59036636e97aSMatthew G Knepley 
59042dd40e9bSPatrick Sanan   Notes:
59052dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
59062dd40e9bSPatrick Sanan 
59072dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59086636e97aSMatthew G Knepley 
59096636e97aSMatthew G Knepley   Level: intermediate
59106636e97aSMatthew G Knepley 
59112dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59126636e97aSMatthew G Knepley @*/
59136636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
59146636e97aSMatthew G Knepley {
59156636e97aSMatthew G Knepley   PetscErrorCode ierr;
59166636e97aSMatthew G Knepley 
59176636e97aSMatthew G Knepley   PetscFunctionBegin;
59186636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59196636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59206636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59216636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59226636e97aSMatthew G Knepley   dm->coordinates = c;
59236636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5924b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
592503dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
59266636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59276636e97aSMatthew G Knepley }
59286636e97aSMatthew G Knepley 
59296636e97aSMatthew G Knepley /*@
59306636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
59316636e97aSMatthew G Knepley 
59327058e716SVaclav Hapla   Not collective
59336636e97aSMatthew G Knepley 
59346636e97aSMatthew G Knepley    Input Parameters:
59356636e97aSMatthew G Knepley +  dm - the DM
59366636e97aSMatthew G Knepley -  c - coordinate vector
59376636e97aSMatthew G Knepley 
59382dd40e9bSPatrick Sanan   Notes:
59396636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
59406636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
59416636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
59426636e97aSMatthew G Knepley 
59432dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59442dd40e9bSPatrick Sanan 
59456636e97aSMatthew G Knepley   Level: intermediate
59466636e97aSMatthew G Knepley 
59476636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
59486636e97aSMatthew G Knepley @*/
59496636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
59506636e97aSMatthew G Knepley {
59516636e97aSMatthew G Knepley   PetscErrorCode ierr;
59526636e97aSMatthew G Knepley 
59536636e97aSMatthew G Knepley   PetscFunctionBegin;
59546636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59556636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59566636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59576636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
59588865f1eaSKarl Rupp 
59596636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
59608865f1eaSKarl Rupp 
59616636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59626636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59636636e97aSMatthew G Knepley }
59646636e97aSMatthew G Knepley 
59656636e97aSMatthew G Knepley /*@
59666636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
59676636e97aSMatthew G Knepley 
5968d083f849SBarry Smith   Collective on dm
59696636e97aSMatthew G Knepley 
59706636e97aSMatthew G Knepley   Input Parameter:
59716636e97aSMatthew G Knepley . dm - the DM
59726636e97aSMatthew G Knepley 
59736636e97aSMatthew G Knepley   Output Parameter:
59746636e97aSMatthew G Knepley . c - global coordinate vector
59756636e97aSMatthew G Knepley 
59766636e97aSMatthew G Knepley   Note:
59776636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
59786636e97aSMatthew G Knepley 
59796636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
59806636e97aSMatthew G Knepley 
59816636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59826636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59836636e97aSMatthew G Knepley 
59846636e97aSMatthew G Knepley   Level: intermediate
59856636e97aSMatthew G Knepley 
59866636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59876636e97aSMatthew G Knepley @*/
59886636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
59896636e97aSMatthew G Knepley {
59906636e97aSMatthew G Knepley   PetscErrorCode ierr;
59916636e97aSMatthew G Knepley 
59926636e97aSMatthew G Knepley   PetscFunctionBegin;
59936636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59946636e97aSMatthew G Knepley   PetscValidPointer(c,2);
59951f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
59960298fd71SBarry Smith     DM        cdm = NULL;
59971970a576SMatthew G. Knepley     PetscBool localized;
59986636e97aSMatthew G Knepley 
59996636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
60006636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
60011970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
60021970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
60031970a576SMatthew G. Knepley     if (localized) {
60041970a576SMatthew G. Knepley       PetscInt cdim;
60051970a576SMatthew G. Knepley 
60061970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
60071970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60081970a576SMatthew G. Knepley     }
60096636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
60106636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60116636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60126636e97aSMatthew G Knepley   }
60136636e97aSMatthew G Knepley   *c = dm->coordinates;
60146636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60156636e97aSMatthew G Knepley }
60166636e97aSMatthew G Knepley 
60176636e97aSMatthew G Knepley /*@
601881e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
601981e9a530SVaclav Hapla 
6020d083f849SBarry Smith   Collective on dm
602181e9a530SVaclav Hapla 
602281e9a530SVaclav Hapla   Input Parameter:
602381e9a530SVaclav Hapla . dm - the DM
602481e9a530SVaclav Hapla 
602581e9a530SVaclav Hapla   Level: advanced
602681e9a530SVaclav Hapla 
602781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
602881e9a530SVaclav Hapla @*/
602981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
603081e9a530SVaclav Hapla {
603181e9a530SVaclav Hapla   PetscErrorCode ierr;
603281e9a530SVaclav Hapla 
603381e9a530SVaclav Hapla   PetscFunctionBegin;
603481e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
603581e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
60361970a576SMatthew G. Knepley     DM        cdm = NULL;
60371970a576SMatthew G. Knepley     PetscBool localized;
60381970a576SMatthew G. Knepley 
603981e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
604081e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
60411970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
60421970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
60431970a576SMatthew G. Knepley     if (localized) {
60441970a576SMatthew G. Knepley       PetscInt cdim;
60451970a576SMatthew G. Knepley 
60461970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
60471970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60481970a576SMatthew G. Knepley     }
604981e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
605081e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
605181e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
605281e9a530SVaclav Hapla   }
605381e9a530SVaclav Hapla   PetscFunctionReturn(0);
605481e9a530SVaclav Hapla }
605581e9a530SVaclav Hapla 
605681e9a530SVaclav Hapla /*@
60576636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
60586636e97aSMatthew G Knepley 
6059d083f849SBarry Smith   Collective on dm
60606636e97aSMatthew G Knepley 
60616636e97aSMatthew G Knepley   Input Parameter:
60626636e97aSMatthew G Knepley . dm - the DM
60636636e97aSMatthew G Knepley 
60646636e97aSMatthew G Knepley   Output Parameter:
60656636e97aSMatthew G Knepley . c - coordinate vector
60666636e97aSMatthew G Knepley 
60676636e97aSMatthew G Knepley   Note:
60686636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
60696636e97aSMatthew G Knepley 
60706636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
60716636e97aSMatthew G Knepley 
60726636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
60736636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
60746636e97aSMatthew G Knepley 
60756636e97aSMatthew G Knepley   Level: intermediate
60766636e97aSMatthew G Knepley 
607781e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
60786636e97aSMatthew G Knepley @*/
60796636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
60806636e97aSMatthew G Knepley {
60816636e97aSMatthew G Knepley   PetscErrorCode ierr;
60826636e97aSMatthew G Knepley 
60836636e97aSMatthew G Knepley   PetscFunctionBegin;
60846636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60856636e97aSMatthew G Knepley   PetscValidPointer(c,2);
608681e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
60876636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
60886636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60896636e97aSMatthew G Knepley }
60906636e97aSMatthew G Knepley 
609181e9a530SVaclav Hapla /*@
609281e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
609381e9a530SVaclav Hapla 
609481e9a530SVaclav Hapla   Not collective
609581e9a530SVaclav Hapla 
609681e9a530SVaclav Hapla   Input Parameter:
609781e9a530SVaclav Hapla . dm - the DM
609881e9a530SVaclav Hapla 
609981e9a530SVaclav Hapla   Output Parameter:
610081e9a530SVaclav Hapla . c - coordinate vector
610181e9a530SVaclav Hapla 
610281e9a530SVaclav Hapla   Level: advanced
610381e9a530SVaclav Hapla 
610481e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
610581e9a530SVaclav Hapla @*/
610681e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
610781e9a530SVaclav Hapla {
610881e9a530SVaclav Hapla   PetscFunctionBegin;
610981e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
611081e9a530SVaclav Hapla   PetscValidPointer(c,2);
611181e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
61126636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
61136636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61146636e97aSMatthew G Knepley }
61156636e97aSMatthew G Knepley 
61162db98f8dSVaclav Hapla /*@
61172db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
61182db98f8dSVaclav Hapla 
61192db98f8dSVaclav Hapla   Not collective
61202db98f8dSVaclav Hapla 
61212db98f8dSVaclav Hapla   Input Parameter:
61222db98f8dSVaclav Hapla + dm - the DM
61232db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
61242db98f8dSVaclav Hapla 
61252db98f8dSVaclav Hapla   Output Parameter:
61262db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
61272db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
61282db98f8dSVaclav Hapla 
61292db98f8dSVaclav Hapla   Note:
61302db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
61312db98f8dSVaclav Hapla 
61322db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
61332db98f8dSVaclav Hapla 
61342db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
61352db98f8dSVaclav Hapla 
61362db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
61372db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
61382db98f8dSVaclav Hapla 
61392db98f8dSVaclav Hapla   Level: advanced
61402db98f8dSVaclav Hapla 
61412db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
61422db98f8dSVaclav Hapla @*/
61432db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
61442db98f8dSVaclav Hapla {
61452db98f8dSVaclav Hapla   PetscSection        cs, newcs;
61462db98f8dSVaclav Hapla   Vec                 coords;
61472db98f8dSVaclav Hapla   const PetscScalar   *arr;
61482db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
61492db98f8dSVaclav Hapla   PetscInt            n;
61502db98f8dSVaclav Hapla   PetscErrorCode      ierr;
61512db98f8dSVaclav Hapla 
61522db98f8dSVaclav Hapla   PetscFunctionBegin;
61532db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61542db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
61552db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
61562db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
61572db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
61581bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
61591bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
61602db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
61612db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
61622db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
61632db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
61642db98f8dSVaclav Hapla   if (pCoord) {
61652db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
61662db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
61672db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
61682db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6169ad9ac99dSVaclav Hapla   } else {
6170ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
61712db98f8dSVaclav Hapla   }
6172ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6173ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
61742db98f8dSVaclav Hapla   PetscFunctionReturn(0);
61752db98f8dSVaclav Hapla }
61762db98f8dSVaclav Hapla 
6177f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6178f19dbd58SToby Isaac {
6179f19dbd58SToby Isaac   PetscErrorCode ierr;
6180f19dbd58SToby Isaac 
6181f19dbd58SToby Isaac   PetscFunctionBegin;
6182f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6183f19dbd58SToby Isaac   PetscValidPointer(field,2);
6184f19dbd58SToby Isaac   if (!dm->coordinateField) {
6185f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6186f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6187f19dbd58SToby Isaac     }
6188f19dbd58SToby Isaac   }
6189f19dbd58SToby Isaac   *field = dm->coordinateField;
6190f19dbd58SToby Isaac   PetscFunctionReturn(0);
6191f19dbd58SToby Isaac }
6192f19dbd58SToby Isaac 
6193f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6194f19dbd58SToby Isaac {
6195f19dbd58SToby Isaac   PetscErrorCode ierr;
6196f19dbd58SToby Isaac 
6197f19dbd58SToby Isaac   PetscFunctionBegin;
6198f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6199f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6200c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6201f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6202f19dbd58SToby Isaac   dm->coordinateField = field;
6203f19dbd58SToby Isaac   PetscFunctionReturn(0);
6204f19dbd58SToby Isaac }
6205f19dbd58SToby Isaac 
62066636e97aSMatthew G Knepley /*@
62071cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
62086636e97aSMatthew G Knepley 
6209d083f849SBarry Smith   Collective on dm
62106636e97aSMatthew G Knepley 
62116636e97aSMatthew G Knepley   Input Parameter:
62126636e97aSMatthew G Knepley . dm - the DM
62136636e97aSMatthew G Knepley 
62146636e97aSMatthew G Knepley   Output Parameter:
62156636e97aSMatthew G Knepley . cdm - coordinate DM
62166636e97aSMatthew G Knepley 
62176636e97aSMatthew G Knepley   Level: intermediate
62186636e97aSMatthew G Knepley 
62191cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62206636e97aSMatthew G Knepley @*/
62216636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
62226636e97aSMatthew G Knepley {
62236636e97aSMatthew G Knepley   PetscErrorCode ierr;
62246636e97aSMatthew G Knepley 
62256636e97aSMatthew G Knepley   PetscFunctionBegin;
62266636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62276636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
62286636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6229308f8a94SToby Isaac     DM cdm;
6230308f8a94SToby Isaac 
623182f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6232308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6233308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6234308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6235308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6236308f8a94SToby Isaac     dm->coordinateDM = cdm;
62376636e97aSMatthew G Knepley   }
62386636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
62396636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62406636e97aSMatthew G Knepley }
6241e87bb0d3SMatthew G Knepley 
62421cfe2091SMatthew G. Knepley /*@
62431cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
62441cfe2091SMatthew G. Knepley 
6245d083f849SBarry Smith   Logically Collective on dm
62461cfe2091SMatthew G. Knepley 
62471cfe2091SMatthew G. Knepley   Input Parameters:
62481cfe2091SMatthew G. Knepley + dm - the DM
62491cfe2091SMatthew G. Knepley - cdm - coordinate DM
62501cfe2091SMatthew G. Knepley 
62511cfe2091SMatthew G. Knepley   Level: intermediate
62521cfe2091SMatthew G. Knepley 
62531cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62541cfe2091SMatthew G. Knepley @*/
62551cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
62561cfe2091SMatthew G. Knepley {
62571cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
62581cfe2091SMatthew G. Knepley 
62591cfe2091SMatthew G. Knepley   PetscFunctionBegin;
62601cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62611cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6262f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
62631cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
62641cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
62651cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
62661cfe2091SMatthew G. Knepley }
62671cfe2091SMatthew G. Knepley 
626846e270d4SMatthew G. Knepley /*@
626946e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
627046e270d4SMatthew G. Knepley 
627146e270d4SMatthew G. Knepley   Not Collective
627246e270d4SMatthew G. Knepley 
627346e270d4SMatthew G. Knepley   Input Parameter:
627446e270d4SMatthew G. Knepley . dm - The DM object
627546e270d4SMatthew G. Knepley 
627646e270d4SMatthew G. Knepley   Output Parameter:
627746e270d4SMatthew G. Knepley . dim - The embedding dimension
627846e270d4SMatthew G. Knepley 
627946e270d4SMatthew G. Knepley   Level: intermediate
628046e270d4SMatthew G. Knepley 
628192fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
628246e270d4SMatthew G. Knepley @*/
628346e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
628446e270d4SMatthew G. Knepley {
628546e270d4SMatthew G. Knepley   PetscFunctionBegin;
628646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6287534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
62889a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
62899a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
62909a9a41abSToby Isaac   }
629146e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
629246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
629346e270d4SMatthew G. Knepley }
629446e270d4SMatthew G. Knepley 
629546e270d4SMatthew G. Knepley /*@
629646e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
629746e270d4SMatthew G. Knepley 
629846e270d4SMatthew G. Knepley   Not Collective
629946e270d4SMatthew G. Knepley 
630046e270d4SMatthew G. Knepley   Input Parameters:
630146e270d4SMatthew G. Knepley + dm  - The DM object
630246e270d4SMatthew G. Knepley - dim - The embedding dimension
630346e270d4SMatthew G. Knepley 
630446e270d4SMatthew G. Knepley   Level: intermediate
630546e270d4SMatthew G. Knepley 
630692fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
630746e270d4SMatthew G. Knepley @*/
630846e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
630946e270d4SMatthew G. Knepley {
6310e5e52638SMatthew G. Knepley   PetscDS        ds;
6311f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6312f17e8794SMatthew G. Knepley 
631346e270d4SMatthew G. Knepley   PetscFunctionBegin;
631446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
631546e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6316e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6317e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
631846e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
631946e270d4SMatthew G. Knepley }
632046e270d4SMatthew G. Knepley 
6321e8abe2deSMatthew G. Knepley /*@
6322e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6323e8abe2deSMatthew G. Knepley 
6324d083f849SBarry Smith   Collective on dm
6325e8abe2deSMatthew G. Knepley 
6326e8abe2deSMatthew G. Knepley   Input Parameter:
6327e8abe2deSMatthew G. Knepley . dm - The DM object
6328e8abe2deSMatthew G. Knepley 
6329e8abe2deSMatthew G. Knepley   Output Parameter:
6330e8abe2deSMatthew G. Knepley . section - The PetscSection object
6331e8abe2deSMatthew G. Knepley 
6332e8abe2deSMatthew G. Knepley   Level: intermediate
6333e8abe2deSMatthew G. Knepley 
633492fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6335e8abe2deSMatthew G. Knepley @*/
6336e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6337e8abe2deSMatthew G. Knepley {
6338e8abe2deSMatthew G. Knepley   DM             cdm;
6339e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6340e8abe2deSMatthew G. Knepley 
6341e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6342e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6343e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6344e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
634592fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6346e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6347e8abe2deSMatthew G. Knepley }
6348e8abe2deSMatthew G. Knepley 
6349e8abe2deSMatthew G. Knepley /*@
6350e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6351e8abe2deSMatthew G. Knepley 
6352e8abe2deSMatthew G. Knepley   Not Collective
6353e8abe2deSMatthew G. Knepley 
6354e8abe2deSMatthew G. Knepley   Input Parameters:
6355e8abe2deSMatthew G. Knepley + dm      - The DM object
635646e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6357e8abe2deSMatthew G. Knepley - section - The PetscSection object
6358e8abe2deSMatthew G. Knepley 
6359e8abe2deSMatthew G. Knepley   Level: intermediate
6360e8abe2deSMatthew G. Knepley 
636192fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6362e8abe2deSMatthew G. Knepley @*/
636346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6364e8abe2deSMatthew G. Knepley {
6365e8abe2deSMatthew G. Knepley   DM             cdm;
6366e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6367e8abe2deSMatthew G. Knepley 
6368e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6369e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
637046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6371e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
637292fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
637346e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
63744c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
637546e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
637646e270d4SMatthew G. Knepley 
637746e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
637846e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
637946e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
638046e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
638146e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
638246e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
638346e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
638446e270d4SMatthew G. Knepley     }
6385ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
638646e270d4SMatthew G. Knepley   }
6387e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6388e8abe2deSMatthew G. Knepley }
6389e8abe2deSMatthew G. Knepley 
6390d864a3eaSLisandro Dalcin /*@
6391d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6392d864a3eaSLisandro Dalcin 
6393d864a3eaSLisandro Dalcin   Input Parameters:
6394d864a3eaSLisandro Dalcin + dm      - The DM object
6395d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6396d864a3eaSLisandro Dalcin 
6397d864a3eaSLisandro Dalcin   Level: intermediate
6398d864a3eaSLisandro Dalcin 
6399d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6400d864a3eaSLisandro Dalcin @*/
6401d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6402d864a3eaSLisandro Dalcin {
6403d864a3eaSLisandro Dalcin   PetscObject    discOld;
6404d864a3eaSLisandro Dalcin   PetscClassId   classid;
6405d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6406d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6407d864a3eaSLisandro Dalcin   Mat            matInterp;
6408d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6409d864a3eaSLisandro Dalcin 
6410d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6411d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6412d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6413d864a3eaSLisandro Dalcin 
6414d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6415d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6416d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6417d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
641829ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
641929ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
642029ad44c5SMatthew G. Knepley       PetscFE        feLinear;
642129ad44c5SMatthew G. Knepley       DMPolytopeType ct;
642229ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
642329ad44c5SMatthew G. Knepley       PetscBool      simplex;
642429ad44c5SMatthew G. Knepley 
642529ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
642629ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
642729ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
642829ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
642929ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
643029ad44c5SMatthew G. Knepley       switch (ct) {
643129ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
643229ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
643329ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
643429ad44c5SMatthew G. Knepley         default: break;
643529ad44c5SMatthew G. Knepley       }
643629ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
643729ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
643829ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
643929ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
644029ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
644129ad44c5SMatthew G. Knepley     } else {
644229ad44c5SMatthew G. Knepley       const char *discname;
644329ad44c5SMatthew G. Knepley 
644429ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
644529ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
644629ad44c5SMatthew G. Knepley     }
644729ad44c5SMatthew G. Knepley   }
6448d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6449d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6450d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6451d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6452d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6453d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6454d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6455d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6456d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6457d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6458d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6459d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6460d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6461d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6462d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6463d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6464d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6465d864a3eaSLisandro Dalcin }
6466d864a3eaSLisandro Dalcin 
64675dc8c3f7SMatthew G. Knepley /*@C
646890b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
64695dc8c3f7SMatthew G. Knepley 
64705dc8c3f7SMatthew G. Knepley   Input Parameters:
647190b157c4SStefano Zampini . dm      - The DM object
647290b157c4SStefano Zampini 
647390b157c4SStefano Zampini   Output Parameters:
647490b157c4SStefano Zampini + per     - Whether the DM is periodic or not
64755dc8c3f7SMatthew 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
64765dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
64775dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
64785dc8c3f7SMatthew G. Knepley 
64795dc8c3f7SMatthew G. Knepley   Level: developer
64805dc8c3f7SMatthew G. Knepley 
64815dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
64825dc8c3f7SMatthew G. Knepley @*/
648390b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6484c6b900c6SMatthew G. Knepley {
6485c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6486c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
648790b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6488c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6489c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
64905dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6491c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6492c6b900c6SMatthew G. Knepley }
6493c6b900c6SMatthew G. Knepley 
64945dc8c3f7SMatthew G. Knepley /*@C
64955dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
64965dc8c3f7SMatthew G. Knepley 
64975dc8c3f7SMatthew G. Knepley   Input Parameters:
64985dc8c3f7SMatthew G. Knepley + dm      - The DM object
6499db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6500db2bf62eSStefano Zampini . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates. Pass NULL to remove such information.
65015dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
65025dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
65035dc8c3f7SMatthew G. Knepley 
6504db2bf62eSStefano Zampini   Notes: If per is PETSC_TRUE and maxCell is not provided, coordinates need to be already localized, or must be localized by hand by the user.
6505db2bf62eSStefano Zampini 
65065dc8c3f7SMatthew G. Knepley   Level: developer
65075dc8c3f7SMatthew G. Knepley 
65085dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
65095dc8c3f7SMatthew G. Knepley @*/
651090b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6511c6b900c6SMatthew G. Knepley {
6512c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6513c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6514c6b900c6SMatthew G. Knepley 
6515c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6516c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
651790b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6518412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6519412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6520412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
65215dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
652290b157c4SStefano Zampini   if (maxCell) {
6523412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6524412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6525db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6526db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6527412e9a14SMatthew G. Knepley   }
6528db2bf62eSStefano Zampini 
6529412e9a14SMatthew G. Knepley   if (L) {
6530412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6531412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6532412e9a14SMatthew G. Knepley   }
6533412e9a14SMatthew G. Knepley   if (bd) {
6534412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6535412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
653690b157c4SStefano Zampini   }
6537072d7d67SStefano Zampini   dm->periodic = per;
6538c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6539c6b900c6SMatthew G. Knepley }
6540c6b900c6SMatthew G. Knepley 
65412e17dfb7SMatthew G. Knepley /*@
65422e17dfb7SMatthew 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.
65432e17dfb7SMatthew G. Knepley 
65442e17dfb7SMatthew G. Knepley   Input Parameters:
65452e17dfb7SMatthew G. Knepley + dm     - The DM
654665da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
654765da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
65482e17dfb7SMatthew G. Knepley 
65492e17dfb7SMatthew G. Knepley   Output Parameter:
65502e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65512e17dfb7SMatthew G. Knepley 
65522e17dfb7SMatthew G. Knepley   Level: developer
65532e17dfb7SMatthew G. Knepley 
65542e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
65552e17dfb7SMatthew G. Knepley @*/
655665da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
65572e17dfb7SMatthew G. Knepley {
65582e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
65592e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
65602e17dfb7SMatthew G. Knepley 
65612e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65622e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
65632e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65642e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
65652e17dfb7SMatthew G. Knepley   } else {
656665da65dcSMatthew G. Knepley     if (endpoint) {
656765da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6568da3333bfSMatthew 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)) {
6569da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
657065da65dcSMatthew G. Knepley         } else {
6571da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
657265da65dcSMatthew G. Knepley         }
657365da65dcSMatthew G. Knepley       }
657465da65dcSMatthew G. Knepley     } else {
65752e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
65761118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
65772e17dfb7SMatthew G. Knepley       }
65782e17dfb7SMatthew G. Knepley     }
657965da65dcSMatthew G. Knepley   }
65802e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65812e17dfb7SMatthew G. Knepley }
65822e17dfb7SMatthew G. Knepley 
65832e17dfb7SMatthew G. Knepley /*
65842e17dfb7SMatthew 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.
65852e17dfb7SMatthew G. Knepley 
65862e17dfb7SMatthew G. Knepley   Input Parameters:
65872e17dfb7SMatthew G. Knepley + dm     - The DM
65882e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
65892e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
65902e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
65912e17dfb7SMatthew G. Knepley 
65922e17dfb7SMatthew G. Knepley   Output Parameter:
65932e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65942e17dfb7SMatthew G. Knepley 
65952e17dfb7SMatthew G. Knepley   Level: developer
65962e17dfb7SMatthew G. Knepley 
65972e17dfb7SMatthew 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
65982e17dfb7SMatthew G. Knepley 
65992e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
66002e17dfb7SMatthew G. Knepley */
66012e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
66022e17dfb7SMatthew G. Knepley {
66032e17dfb7SMatthew G. Knepley   PetscInt d;
66042e17dfb7SMatthew G. Knepley 
66052e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66062e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66072e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
66082e17dfb7SMatthew G. Knepley   } else {
66092e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6610908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
66112e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
66122e17dfb7SMatthew G. Knepley       } else {
66132e17dfb7SMatthew G. Knepley         out[d] = in[d];
66142e17dfb7SMatthew G. Knepley       }
66152e17dfb7SMatthew G. Knepley     }
66162e17dfb7SMatthew G. Knepley   }
66172e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66182e17dfb7SMatthew G. Knepley }
6619a5801f52SStefano Zampini 
66202e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
66212e17dfb7SMatthew G. Knepley {
66222e17dfb7SMatthew G. Knepley   PetscInt d;
66232e17dfb7SMatthew G. Knepley 
66242e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66252e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66262e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
66272e17dfb7SMatthew G. Knepley   } else {
66282e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6629908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
66302e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
66312e17dfb7SMatthew G. Knepley       } else {
66322e17dfb7SMatthew G. Knepley         out[d] = in[d];
66332e17dfb7SMatthew G. Knepley       }
66342e17dfb7SMatthew G. Knepley     }
66352e17dfb7SMatthew G. Knepley   }
66362e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66372e17dfb7SMatthew G. Knepley }
66382e17dfb7SMatthew G. Knepley 
66392e17dfb7SMatthew G. Knepley /*
66402e17dfb7SMatthew 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.
66412e17dfb7SMatthew G. Knepley 
66422e17dfb7SMatthew G. Knepley   Input Parameters:
66432e17dfb7SMatthew G. Knepley + dm     - The DM
66442e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
66452e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
66462e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
66472e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
66482e17dfb7SMatthew G. Knepley 
66492e17dfb7SMatthew G. Knepley   Output Parameter:
66502e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
66512e17dfb7SMatthew G. Knepley 
66522e17dfb7SMatthew G. Knepley   Level: developer
66532e17dfb7SMatthew G. Knepley 
66542e17dfb7SMatthew 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
66552e17dfb7SMatthew G. Knepley 
66562e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
66572e17dfb7SMatthew G. Knepley */
66582e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
66592e17dfb7SMatthew G. Knepley {
66602e17dfb7SMatthew G. Knepley   PetscInt d;
66612e17dfb7SMatthew G. Knepley 
66622e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66632e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66642e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
66652e17dfb7SMatthew G. Knepley   } else {
66662e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6667412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6668412e9a14SMatthew G. Knepley 
6669412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6670412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6671412e9a14SMatthew G. Knepley 
6672412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6673412e9a14SMatthew 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]));
6674412e9a14SMatthew G. Knepley         out[d] += newCoord;
66752e17dfb7SMatthew G. Knepley       } else {
66762e17dfb7SMatthew G. Knepley         out[d] += in[d];
66772e17dfb7SMatthew G. Knepley       }
66782e17dfb7SMatthew G. Knepley     }
66792e17dfb7SMatthew G. Knepley   }
66802e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66812e17dfb7SMatthew G. Knepley }
66822e17dfb7SMatthew G. Knepley 
668336447a5eSToby Isaac /*@
66848f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
66858f700142SStefano Zampini 
66868f700142SStefano Zampini   Not collective
668736447a5eSToby Isaac 
668836447a5eSToby Isaac   Input Parameter:
668936447a5eSToby Isaac . dm - The DM
669036447a5eSToby Isaac 
669136447a5eSToby Isaac   Output Parameter:
669236447a5eSToby Isaac   areLocalized - True if localized
669336447a5eSToby Isaac 
669436447a5eSToby Isaac   Level: developer
669536447a5eSToby Isaac 
66968f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
669736447a5eSToby Isaac @*/
66988f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
669936447a5eSToby Isaac {
670036447a5eSToby Isaac   DM             cdm;
670136447a5eSToby Isaac   PetscSection   coordSection;
670246a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
670346a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
670436447a5eSToby Isaac   PetscErrorCode ierr;
670536447a5eSToby Isaac 
670636447a5eSToby Isaac   PetscFunctionBegin;
670736447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6708534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
67098b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
671046a3a80fSLisandro Dalcin 
671136447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
671236447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
671346a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
67149f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
671546a3a80fSLisandro Dalcin 
67169f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
671746a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
671836447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
671936447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
672046a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
672146a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
672236447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
672346a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
672436447a5eSToby Isaac   }
67258f700142SStefano Zampini   *areLocalized = alreadyLocalized;
672636447a5eSToby Isaac   PetscFunctionReturn(0);
672736447a5eSToby Isaac }
672836447a5eSToby Isaac 
67298f700142SStefano Zampini /*@
67308f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
67318f700142SStefano Zampini 
67328f700142SStefano Zampini   Collective on dm
67338f700142SStefano Zampini 
67348f700142SStefano Zampini   Input Parameter:
67358f700142SStefano Zampini . dm - The DM
67368f700142SStefano Zampini 
67378f700142SStefano Zampini   Output Parameter:
67388f700142SStefano Zampini   areLocalized - True if localized
67398f700142SStefano Zampini 
67408f700142SStefano Zampini   Level: developer
67418f700142SStefano Zampini 
67428f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
67438f700142SStefano Zampini @*/
67448f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
67458f700142SStefano Zampini {
67468f700142SStefano Zampini   PetscBool      localized;
67478f700142SStefano Zampini   PetscErrorCode ierr;
67488f700142SStefano Zampini 
67498f700142SStefano Zampini   PetscFunctionBegin;
67508f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6751534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
67528f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
67538f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
67548f700142SStefano Zampini   PetscFunctionReturn(0);
67558f700142SStefano Zampini }
675636447a5eSToby Isaac 
67572e17dfb7SMatthew G. Knepley /*@
6758492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
67592e17dfb7SMatthew G. Knepley 
67608f700142SStefano Zampini   Collective on dm
67618f700142SStefano Zampini 
67622e17dfb7SMatthew G. Knepley   Input Parameter:
67632e17dfb7SMatthew G. Knepley . dm - The DM
67642e17dfb7SMatthew G. Knepley 
67652e17dfb7SMatthew G. Knepley   Level: developer
67662e17dfb7SMatthew G. Knepley 
67678f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
67682e17dfb7SMatthew G. Knepley @*/
67692e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
67702e17dfb7SMatthew G. Knepley {
67712e17dfb7SMatthew G. Knepley   DM             cdm;
67722e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
67732e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
67743e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
67753e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6776e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
67773e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
67783e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
67792e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
67802e17dfb7SMatthew G. Knepley 
67812e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67822e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
678392c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6784f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6785f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6786f7cbd40bSStefano Zampini 
67872e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
67882e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
67892e17dfb7SMatthew G. Knepley   {
67902e17dfb7SMatthew G. Knepley     PetscBool isplex;
67912e17dfb7SMatthew G. Knepley 
67922e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
67932e17dfb7SMatthew G. Knepley     if (isplex) {
67942e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
67953e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
679669291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
67973e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
67983e922f36SToby Isaac       newStart = vStart;
67993e922f36SToby Isaac       newEnd   = vEnd;
68003e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
68013e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
68023e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
68033e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
68043e922f36SToby Isaac       }
68052e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
68062e17dfb7SMatthew G. Knepley   }
68072e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
680843eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
68092e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
68103e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6811e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
68123e922f36SToby Isaac 
68132e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
68142e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
68152e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
68162e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
68173e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
68183e922f36SToby Isaac 
681969291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68203e922f36SToby Isaac   localized = &anchor[bs];
68213e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
68223e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68233e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68243e922f36SToby Isaac 
68253e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
68263e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
68273e922f36SToby Isaac       PetscInt     b;
68283e922f36SToby Isaac 
68293e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
68303e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68313e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68323e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
68333e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
68343e922f36SToby Isaac         for (b = 0; b < bs; b++) {
68353e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
68363e922f36SToby Isaac         }
68373e922f36SToby Isaac         if (b < bs) break;
68383e922f36SToby Isaac       }
68393e922f36SToby Isaac       if (d < dof/bs) {
68403e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
68413e922f36SToby Isaac           PetscInt cdof;
68423e922f36SToby Isaac 
68433e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
68443e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
68453e922f36SToby Isaac         }
68463e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
68473e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
68483e922f36SToby Isaac       }
68493e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68503e922f36SToby Isaac     }
68513e922f36SToby Isaac   }
68523e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
68533e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
685469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68553e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
685669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
68573e922f36SToby Isaac     PetscFunctionReturn(0);
68583e922f36SToby Isaac   }
68592e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68602e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68612e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
68622e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
68632e17dfb7SMatthew G. Knepley   }
68642e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
68652e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6866c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
68672e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
68682e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
68692e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
68702e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6871c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68722e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
68732e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68742e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68752e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
68762e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
68772e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
68782e17dfb7SMatthew G. Knepley   }
68793e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68803e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68813e922f36SToby Isaac 
68822e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
68832e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
68843e922f36SToby Isaac       PetscInt     b, cdof;
68852e17dfb7SMatthew G. Knepley 
68863e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
68873e922f36SToby Isaac       if (!cdof) continue;
68882e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68892e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
68902e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68912e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
68922e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68932e17dfb7SMatthew G. Knepley     }
68943e922f36SToby Isaac   }
689569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
689669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6897c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68982e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
68992e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
69002e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
69012e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
69022e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
69032e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69042e17dfb7SMatthew G. Knepley }
69052e17dfb7SMatthew G. Knepley 
6906e87bb0d3SMatthew G Knepley /*@
69073a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6908e87bb0d3SMatthew G Knepley 
6909d083f849SBarry Smith   Collective on v (see explanation below)
6910e87bb0d3SMatthew G Knepley 
6911e87bb0d3SMatthew G Knepley   Input Parameters:
6912e87bb0d3SMatthew G Knepley + dm - The DM
69133a93e3b7SToby Isaac . v - The Vec of points
691462a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
69153a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6916e87bb0d3SMatthew G Knepley 
691761e3bb9bSMatthew G Knepley   Output Parameter:
691862a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
691962a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
69203a93e3b7SToby Isaac 
6921e87bb0d3SMatthew G Knepley 
6922e87bb0d3SMatthew G Knepley   Level: developer
692361e3bb9bSMatthew G Knepley 
692462a38674SMatthew G. Knepley   Notes:
69253a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
692662a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
69273a93e3b7SToby Isaac 
69283a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
692962a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
69303a93e3b7SToby Isaac 
69313a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
69323a93e3b7SToby Isaac 
693362a38674SMatthew G. Knepley $    const PetscSFNode *cells;
693462a38674SMatthew G. Knepley $    PetscInt           nFound;
6935a6216909SToby Isaac $    const PetscInt    *found;
693662a38674SMatthew G. Knepley $
6937a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
69383a93e3b7SToby Isaac 
69393a93e3b7SToby 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
69403a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
69413a93e3b7SToby Isaac 
694262a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
694361e3bb9bSMatthew G Knepley @*/
694462a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6945e87bb0d3SMatthew G Knepley {
6946735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6947735aa83eSMatthew G Knepley 
6948e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6949e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6950e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6951e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
69523a93e3b7SToby Isaac   if (*cellSF) {
69533a93e3b7SToby Isaac     PetscMPIInt result;
69543a93e3b7SToby Isaac 
6955e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6956a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
69573a93e3b7SToby 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");
6958e0fc9d1bSMatthew G. Knepley   } else {
69593a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
69603a93e3b7SToby Isaac   }
6961b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
696247a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
696362a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
696447a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6965e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6966e87bb0d3SMatthew G Knepley }
696714f150ffSMatthew G. Knepley 
6968f4d763aaSMatthew G. Knepley /*@
6969f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6970f4d763aaSMatthew G. Knepley 
69718f700142SStefano Zampini   Collective on dm
69728f700142SStefano Zampini 
6973f4d763aaSMatthew G. Knepley   Input Parameter:
6974f4d763aaSMatthew G. Knepley . dm - The original DM
6975f4d763aaSMatthew G. Knepley 
6976f4d763aaSMatthew G. Knepley   Output Parameter:
6977f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6978f4d763aaSMatthew G. Knepley 
6979f4d763aaSMatthew G. Knepley   Level: intermediate
6980f4d763aaSMatthew G. Knepley 
6981e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6982f4d763aaSMatthew G. Knepley @*/
698314f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
698414f150ffSMatthew G. Knepley {
6985c26acbdeSMatthew G. Knepley   PetscSection   section;
69862d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
698714f150ffSMatthew G. Knepley   PetscErrorCode ierr;
698814f150ffSMatthew G. Knepley 
698914f150ffSMatthew G. Knepley   PetscFunctionBegin;
699014f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
699114f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
699292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6993c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6994127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
69952d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6996c26acbdeSMatthew G. Knepley     *odm = dm;
6997c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6998c26acbdeSMatthew G. Knepley   }
699914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7000c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
700114f150ffSMatthew G. Knepley     PetscSF      sf;
700214f150ffSMatthew G. Knepley 
700314f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7004e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
700514f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
700692fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
700714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
700814f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
700915b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7010e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
701114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
701214f150ffSMatthew G. Knepley   }
701314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
701414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
701514f150ffSMatthew G. Knepley }
7016f4d763aaSMatthew G. Knepley 
7017f4d763aaSMatthew G. Knepley /*@
7018cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7019f4d763aaSMatthew G. Knepley 
7020f4d763aaSMatthew G. Knepley   Input Parameter:
7021f4d763aaSMatthew G. Knepley . dm - The original DM
7022f4d763aaSMatthew G. Knepley 
7023cdb7a50dSMatthew G. Knepley   Output Parameters:
7024cdb7a50dSMatthew G. Knepley + num - The output sequence number
7025cdb7a50dSMatthew G. Knepley - val - The output sequence value
7026f4d763aaSMatthew G. Knepley 
7027f4d763aaSMatthew G. Knepley   Level: intermediate
7028f4d763aaSMatthew G. Knepley 
7029f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7030f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7031f4d763aaSMatthew G. Knepley 
7032f4d763aaSMatthew G. Knepley .seealso: VecView()
7033f4d763aaSMatthew G. Knepley @*/
7034cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7035f4d763aaSMatthew G. Knepley {
7036f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7037f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7038534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7039534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7040f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7041f4d763aaSMatthew G. Knepley }
7042f4d763aaSMatthew G. Knepley 
7043f4d763aaSMatthew G. Knepley /*@
7044cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7045f4d763aaSMatthew G. Knepley 
7046f4d763aaSMatthew G. Knepley   Input Parameters:
7047f4d763aaSMatthew G. Knepley + dm - The original DM
7048cdb7a50dSMatthew G. Knepley . num - The output sequence number
7049cdb7a50dSMatthew G. Knepley - val - The output sequence value
7050f4d763aaSMatthew G. Knepley 
7051f4d763aaSMatthew G. Knepley   Level: intermediate
7052f4d763aaSMatthew G. Knepley 
7053f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7054f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7055f4d763aaSMatthew G. Knepley 
7056f4d763aaSMatthew G. Knepley .seealso: VecView()
7057f4d763aaSMatthew G. Knepley @*/
7058cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7059f4d763aaSMatthew G. Knepley {
7060f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7061f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7062f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7063cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7064cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7065cdb7a50dSMatthew G. Knepley }
7066cdb7a50dSMatthew G. Knepley 
7067cdb7a50dSMatthew G. Knepley /*@C
7068cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7069cdb7a50dSMatthew G. Knepley 
7070cdb7a50dSMatthew G. Knepley   Input Parameters:
7071cdb7a50dSMatthew G. Knepley + dm   - The original DM
7072cdb7a50dSMatthew G. Knepley . name - The sequence name
7073cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7074cdb7a50dSMatthew G. Knepley 
7075cdb7a50dSMatthew G. Knepley   Output Parameter:
7076cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7077cdb7a50dSMatthew G. Knepley 
7078cdb7a50dSMatthew G. Knepley   Level: intermediate
7079cdb7a50dSMatthew G. Knepley 
7080cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7081cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7082cdb7a50dSMatthew G. Knepley 
7083cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7084cdb7a50dSMatthew G. Knepley @*/
7085cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7086cdb7a50dSMatthew G. Knepley {
7087cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7088cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7089cdb7a50dSMatthew G. Knepley 
7090cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7091cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7092cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7093534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
7094cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7095cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7096cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7097cdb7a50dSMatthew G. Knepley     PetscScalar value;
7098cdb7a50dSMatthew G. Knepley 
709939d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
71004aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7101cdb7a50dSMatthew G. Knepley #endif
7102cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7103f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7104f4d763aaSMatthew G. Knepley }
71058e4ac7eaSMatthew G. Knepley 
71068e4ac7eaSMatthew G. Knepley /*@
71078e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
71088e4ac7eaSMatthew G. Knepley 
71098e4ac7eaSMatthew G. Knepley   Not collective
71108e4ac7eaSMatthew G. Knepley 
71118e4ac7eaSMatthew G. Knepley   Input Parameter:
71128e4ac7eaSMatthew G. Knepley . dm - The DM
71138e4ac7eaSMatthew G. Knepley 
71148e4ac7eaSMatthew G. Knepley   Output Parameter:
71158e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
71168e4ac7eaSMatthew G. Knepley 
71178e4ac7eaSMatthew G. Knepley   Level: beginner
71188e4ac7eaSMatthew G. Knepley 
71198e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
71208e4ac7eaSMatthew G. Knepley @*/
71218e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
71228e4ac7eaSMatthew G. Knepley {
71238e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71248e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7125534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
71268e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
71278e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71288e4ac7eaSMatthew G. Knepley }
71298e4ac7eaSMatthew G. Knepley 
71308e4ac7eaSMatthew G. Knepley /*@
71315d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
71328e4ac7eaSMatthew G. Knepley 
71338e4ac7eaSMatthew G. Knepley   Collective on dm
71348e4ac7eaSMatthew G. Knepley 
71358e4ac7eaSMatthew G. Knepley   Input Parameters:
71368e4ac7eaSMatthew G. Knepley + dm - The DM
71378e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
71388e4ac7eaSMatthew G. Knepley 
71395d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
71405d3b26e6SMatthew G. Knepley 
71418e4ac7eaSMatthew G. Knepley   Level: beginner
71428e4ac7eaSMatthew G. Knepley 
71435d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
71448e4ac7eaSMatthew G. Knepley @*/
71458e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
71468e4ac7eaSMatthew G. Knepley {
71478e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71488e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71498833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
71508e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
71518e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71528e4ac7eaSMatthew G. Knepley }
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac 
7155c58f1c22SToby Isaac /*@C
7156c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7157c58f1c22SToby Isaac 
7158c58f1c22SToby Isaac   Not Collective
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac   Input Parameters:
7161c58f1c22SToby Isaac + dm   - The DM object
7162c58f1c22SToby Isaac - name - The label name
7163c58f1c22SToby Isaac 
7164c58f1c22SToby Isaac   Level: intermediate
7165c58f1c22SToby Isaac 
7166c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7167c58f1c22SToby Isaac @*/
7168c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7169c58f1c22SToby Isaac {
71705d80c0bfSVaclav Hapla   PetscBool      flg;
71715d80c0bfSVaclav Hapla   DMLabel        label;
7172c58f1c22SToby Isaac   PetscErrorCode ierr;
7173c58f1c22SToby Isaac 
7174c58f1c22SToby Isaac   PetscFunctionBegin;
7175c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7176c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
71775d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7178c58f1c22SToby Isaac   if (!flg) {
71795d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
71805d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
71815d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7182c58f1c22SToby Isaac   }
7183c58f1c22SToby Isaac   PetscFunctionReturn(0);
7184c58f1c22SToby Isaac }
7185c58f1c22SToby Isaac 
7186c58f1c22SToby Isaac /*@C
71870fdc7489SMatthew Knepley   DMCreateLabelAtIndex - Create a label of the given name at the iven index. If it already exists, move it to this index.
71880fdc7489SMatthew Knepley 
71890fdc7489SMatthew Knepley   Not Collective
71900fdc7489SMatthew Knepley 
71910fdc7489SMatthew Knepley   Input Parameters:
71920fdc7489SMatthew Knepley + dm   - The DM object
71930fdc7489SMatthew Knepley . l    - The index for the label
71940fdc7489SMatthew Knepley - name - The label name
71950fdc7489SMatthew Knepley 
71960fdc7489SMatthew Knepley   Level: intermediate
71970fdc7489SMatthew Knepley 
71980fdc7489SMatthew Knepley .seealso: DMCreateLabel(), DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
71990fdc7489SMatthew Knepley @*/
72000fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
72010fdc7489SMatthew Knepley {
72020fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
72030fdc7489SMatthew Knepley   DMLabel        label;
72040fdc7489SMatthew Knepley   PetscInt       Nl, m;
72050fdc7489SMatthew Knepley   PetscBool      flg, match;
72060fdc7489SMatthew Knepley   const char    *lname;
72070fdc7489SMatthew Knepley   PetscErrorCode ierr;
72080fdc7489SMatthew Knepley 
72090fdc7489SMatthew Knepley   PetscFunctionBegin;
72100fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72110fdc7489SMatthew Knepley   PetscValidCharPointer(name, 2);
72120fdc7489SMatthew Knepley   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
72130fdc7489SMatthew Knepley   if (!flg) {
72140fdc7489SMatthew Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
72150fdc7489SMatthew Knepley     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
72160fdc7489SMatthew Knepley     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
72170fdc7489SMatthew Knepley   }
72180fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
72190fdc7489SMatthew Knepley   if (l >= Nl) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %D must be in [0, %D)", l, Nl);
72200fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
72210fdc7489SMatthew Knepley     ierr = PetscObjectGetName((PetscObject) orig->label, &lname);CHKERRQ(ierr);
72220fdc7489SMatthew Knepley     ierr = PetscStrcmp(name, lname, &match);CHKERRQ(ierr);
72230fdc7489SMatthew Knepley     if (match) break;
72240fdc7489SMatthew Knepley   }
72250fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
72260fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
72270fdc7489SMatthew Knepley   else    prev->next = orig->next;
72280fdc7489SMatthew Knepley   if (!l) {
72290fdc7489SMatthew Knepley     orig->next = dm->labels;
72300fdc7489SMatthew Knepley     dm->labels = orig;
72310fdc7489SMatthew Knepley   } else {
72320fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
72330fdc7489SMatthew Knepley     orig->next = prev->next;
72340fdc7489SMatthew Knepley     prev->next = orig;
72350fdc7489SMatthew Knepley   }
72360fdc7489SMatthew Knepley   PetscFunctionReturn(0);
72370fdc7489SMatthew Knepley }
72380fdc7489SMatthew Knepley 
72390fdc7489SMatthew Knepley /*@C
7240c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7241c58f1c22SToby Isaac 
7242c58f1c22SToby Isaac   Not Collective
7243c58f1c22SToby Isaac 
7244c58f1c22SToby Isaac   Input Parameters:
7245c58f1c22SToby Isaac + dm   - The DM object
7246c58f1c22SToby Isaac . name - The label name
7247c58f1c22SToby Isaac - point - The mesh point
7248c58f1c22SToby Isaac 
7249c58f1c22SToby Isaac   Output Parameter:
7250c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7251c58f1c22SToby Isaac 
7252c58f1c22SToby Isaac   Level: beginner
7253c58f1c22SToby Isaac 
7254c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7255c58f1c22SToby Isaac @*/
7256c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7257c58f1c22SToby Isaac {
7258c58f1c22SToby Isaac   DMLabel        label;
7259c58f1c22SToby Isaac   PetscErrorCode ierr;
7260c58f1c22SToby Isaac 
7261c58f1c22SToby Isaac   PetscFunctionBegin;
7262c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7263c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7264c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
726513903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7266c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7267c58f1c22SToby Isaac   PetscFunctionReturn(0);
7268c58f1c22SToby Isaac }
7269c58f1c22SToby Isaac 
7270c58f1c22SToby Isaac /*@C
7271c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7272c58f1c22SToby Isaac 
7273c58f1c22SToby Isaac   Not Collective
7274c58f1c22SToby Isaac 
7275c58f1c22SToby Isaac   Input Parameters:
7276c58f1c22SToby Isaac + dm   - The DM object
7277c58f1c22SToby Isaac . name - The label name
7278c58f1c22SToby Isaac . point - The mesh point
7279c58f1c22SToby Isaac - value - The label value for this point
7280c58f1c22SToby Isaac 
7281c58f1c22SToby Isaac   Output Parameter:
7282c58f1c22SToby Isaac 
7283c58f1c22SToby Isaac   Level: beginner
7284c58f1c22SToby Isaac 
7285c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7286c58f1c22SToby Isaac @*/
7287c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7288c58f1c22SToby Isaac {
7289c58f1c22SToby Isaac   DMLabel        label;
7290c58f1c22SToby Isaac   PetscErrorCode ierr;
7291c58f1c22SToby Isaac 
7292c58f1c22SToby Isaac   PetscFunctionBegin;
7293c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7294c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7295c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7296c58f1c22SToby Isaac   if (!label) {
7297c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7298c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7299c58f1c22SToby Isaac   }
7300c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7301c58f1c22SToby Isaac   PetscFunctionReturn(0);
7302c58f1c22SToby Isaac }
7303c58f1c22SToby Isaac 
7304c58f1c22SToby Isaac /*@C
7305c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7306c58f1c22SToby Isaac 
7307c58f1c22SToby Isaac   Not Collective
7308c58f1c22SToby Isaac 
7309c58f1c22SToby Isaac   Input Parameters:
7310c58f1c22SToby Isaac + dm   - The DM object
7311c58f1c22SToby Isaac . name - The label name
7312c58f1c22SToby Isaac . point - The mesh point
7313c58f1c22SToby Isaac - value - The label value for this point
7314c58f1c22SToby Isaac 
7315c58f1c22SToby Isaac   Output Parameter:
7316c58f1c22SToby Isaac 
7317c58f1c22SToby Isaac   Level: beginner
7318c58f1c22SToby Isaac 
7319c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7320c58f1c22SToby Isaac @*/
7321c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7322c58f1c22SToby Isaac {
7323c58f1c22SToby Isaac   DMLabel        label;
7324c58f1c22SToby Isaac   PetscErrorCode ierr;
7325c58f1c22SToby Isaac 
7326c58f1c22SToby Isaac   PetscFunctionBegin;
7327c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7328c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7329c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7330c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7331c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7332c58f1c22SToby Isaac   PetscFunctionReturn(0);
7333c58f1c22SToby Isaac }
7334c58f1c22SToby Isaac 
7335c58f1c22SToby Isaac /*@C
7336c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7337c58f1c22SToby Isaac 
7338c58f1c22SToby Isaac   Not Collective
7339c58f1c22SToby Isaac 
7340c58f1c22SToby Isaac   Input Parameters:
7341c58f1c22SToby Isaac + dm   - The DM object
7342c58f1c22SToby Isaac - name - The label name
7343c58f1c22SToby Isaac 
7344c58f1c22SToby Isaac   Output Parameter:
7345c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7346c58f1c22SToby Isaac 
7347c58f1c22SToby Isaac   Level: beginner
7348c58f1c22SToby Isaac 
7349df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7350c58f1c22SToby Isaac @*/
7351c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7352c58f1c22SToby Isaac {
7353c58f1c22SToby Isaac   DMLabel        label;
7354c58f1c22SToby Isaac   PetscErrorCode ierr;
7355c58f1c22SToby Isaac 
7356c58f1c22SToby Isaac   PetscFunctionBegin;
7357c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7358c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7359534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7360c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7361c58f1c22SToby Isaac   *size = 0;
7362c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7363c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7364c58f1c22SToby Isaac   PetscFunctionReturn(0);
7365c58f1c22SToby Isaac }
7366c58f1c22SToby Isaac 
7367c58f1c22SToby Isaac /*@C
7368c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7369c58f1c22SToby Isaac 
7370c58f1c22SToby Isaac   Not Collective
7371c58f1c22SToby Isaac 
7372c58f1c22SToby Isaac   Input Parameters:
7373c58f1c22SToby Isaac + mesh - The DM object
7374c58f1c22SToby Isaac - name - The label name
7375c58f1c22SToby Isaac 
7376c58f1c22SToby Isaac   Output Parameter:
7377c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7378c58f1c22SToby Isaac 
7379c58f1c22SToby Isaac   Level: beginner
7380c58f1c22SToby Isaac 
7381c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7382c58f1c22SToby Isaac @*/
7383c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7384c58f1c22SToby Isaac {
7385c58f1c22SToby Isaac   DMLabel        label;
7386c58f1c22SToby Isaac   PetscErrorCode ierr;
7387c58f1c22SToby Isaac 
7388c58f1c22SToby Isaac   PetscFunctionBegin;
7389c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7390c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7391c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7392c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7393c58f1c22SToby Isaac   *ids = NULL;
7394dab2e251SBlaise Bourdin  if (label) {
7395c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7396dab2e251SBlaise Bourdin   } else {
7397dab2e251SBlaise Bourdin     /* returning an empty IS */
7398dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7399dab2e251SBlaise Bourdin   }
7400c58f1c22SToby Isaac   PetscFunctionReturn(0);
7401c58f1c22SToby Isaac }
7402c58f1c22SToby Isaac 
7403c58f1c22SToby Isaac /*@C
7404c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7405c58f1c22SToby Isaac 
7406c58f1c22SToby Isaac   Not Collective
7407c58f1c22SToby Isaac 
7408c58f1c22SToby Isaac   Input Parameters:
7409c58f1c22SToby Isaac + dm - The DM object
7410c58f1c22SToby Isaac . name - The label name
7411c58f1c22SToby Isaac - value - The stratum value
7412c58f1c22SToby Isaac 
7413c58f1c22SToby Isaac   Output Parameter:
7414c58f1c22SToby Isaac . size - The stratum size
7415c58f1c22SToby Isaac 
7416c58f1c22SToby Isaac   Level: beginner
7417c58f1c22SToby Isaac 
7418c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7419c58f1c22SToby Isaac @*/
7420c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7421c58f1c22SToby Isaac {
7422c58f1c22SToby Isaac   DMLabel        label;
7423c58f1c22SToby Isaac   PetscErrorCode ierr;
7424c58f1c22SToby Isaac 
7425c58f1c22SToby Isaac   PetscFunctionBegin;
7426c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7427c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7428534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7429c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7430c58f1c22SToby Isaac   *size = 0;
7431c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7432c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7433c58f1c22SToby Isaac   PetscFunctionReturn(0);
7434c58f1c22SToby Isaac }
7435c58f1c22SToby Isaac 
7436c58f1c22SToby Isaac /*@C
7437c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7438c58f1c22SToby Isaac 
7439c58f1c22SToby Isaac   Not Collective
7440c58f1c22SToby Isaac 
7441c58f1c22SToby Isaac   Input Parameters:
7442c58f1c22SToby Isaac + dm - The DM object
7443c58f1c22SToby Isaac . name - The label name
7444c58f1c22SToby Isaac - value - The stratum value
7445c58f1c22SToby Isaac 
7446c58f1c22SToby Isaac   Output Parameter:
7447c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7448c58f1c22SToby Isaac 
7449c58f1c22SToby Isaac   Level: beginner
7450c58f1c22SToby Isaac 
7451c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7452c58f1c22SToby Isaac @*/
7453c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7454c58f1c22SToby Isaac {
7455c58f1c22SToby Isaac   DMLabel        label;
7456c58f1c22SToby Isaac   PetscErrorCode ierr;
7457c58f1c22SToby Isaac 
7458c58f1c22SToby Isaac   PetscFunctionBegin;
7459c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7460c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7461c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7462c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7463c58f1c22SToby Isaac   *points = NULL;
7464c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7465c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7466c58f1c22SToby Isaac   PetscFunctionReturn(0);
7467c58f1c22SToby Isaac }
7468c58f1c22SToby Isaac 
74694de306b1SToby Isaac /*@C
74709044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
74714de306b1SToby Isaac 
74724de306b1SToby Isaac   Not Collective
74734de306b1SToby Isaac 
74744de306b1SToby Isaac   Input Parameters:
74754de306b1SToby Isaac + dm - The DM object
74764de306b1SToby Isaac . name - The label name
74774de306b1SToby Isaac . value - The stratum value
74784de306b1SToby Isaac - points - The stratum points
74794de306b1SToby Isaac 
74804de306b1SToby Isaac   Level: beginner
74814de306b1SToby Isaac 
74824de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
74834de306b1SToby Isaac @*/
74844de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
74854de306b1SToby Isaac {
74864de306b1SToby Isaac   DMLabel        label;
74874de306b1SToby Isaac   PetscErrorCode ierr;
74884de306b1SToby Isaac 
74894de306b1SToby Isaac   PetscFunctionBegin;
74904de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74914de306b1SToby Isaac   PetscValidCharPointer(name, 2);
74924de306b1SToby Isaac   PetscValidPointer(points, 4);
74934de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
74944de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
74954de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
74964de306b1SToby Isaac   PetscFunctionReturn(0);
74974de306b1SToby Isaac }
74984de306b1SToby Isaac 
7499c58f1c22SToby Isaac /*@C
7500c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7501c58f1c22SToby Isaac 
7502c58f1c22SToby Isaac   Not Collective
7503c58f1c22SToby Isaac 
7504c58f1c22SToby Isaac   Input Parameters:
7505c58f1c22SToby Isaac + dm   - The DM object
7506c58f1c22SToby Isaac . name - The label name
7507c58f1c22SToby Isaac - value - The label value for this point
7508c58f1c22SToby Isaac 
7509c58f1c22SToby Isaac   Output Parameter:
7510c58f1c22SToby Isaac 
7511c58f1c22SToby Isaac   Level: beginner
7512c58f1c22SToby Isaac 
7513c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7514c58f1c22SToby Isaac @*/
7515c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7516c58f1c22SToby Isaac {
7517c58f1c22SToby Isaac   DMLabel        label;
7518c58f1c22SToby Isaac   PetscErrorCode ierr;
7519c58f1c22SToby Isaac 
7520c58f1c22SToby Isaac   PetscFunctionBegin;
7521c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7522c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7523c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7524c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7525c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7526c58f1c22SToby Isaac   PetscFunctionReturn(0);
7527c58f1c22SToby Isaac }
7528c58f1c22SToby Isaac 
7529c58f1c22SToby Isaac /*@
7530c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7531c58f1c22SToby Isaac 
7532c58f1c22SToby Isaac   Not Collective
7533c58f1c22SToby Isaac 
7534c58f1c22SToby Isaac   Input Parameter:
7535c58f1c22SToby Isaac . dm   - The DM object
7536c58f1c22SToby Isaac 
7537c58f1c22SToby Isaac   Output Parameter:
7538c58f1c22SToby Isaac . numLabels - the number of Labels
7539c58f1c22SToby Isaac 
7540c58f1c22SToby Isaac   Level: intermediate
7541c58f1c22SToby Isaac 
7542c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7543c58f1c22SToby Isaac @*/
7544c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7545c58f1c22SToby Isaac {
75465d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7547c58f1c22SToby Isaac   PetscInt  n    = 0;
7548c58f1c22SToby Isaac 
7549c58f1c22SToby Isaac   PetscFunctionBegin;
7550c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7551534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7552c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7553c58f1c22SToby Isaac   *numLabels = n;
7554c58f1c22SToby Isaac   PetscFunctionReturn(0);
7555c58f1c22SToby Isaac }
7556c58f1c22SToby Isaac 
7557c58f1c22SToby Isaac /*@C
7558c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7559c58f1c22SToby Isaac 
7560c58f1c22SToby Isaac   Not Collective
7561c58f1c22SToby Isaac 
7562c58f1c22SToby Isaac   Input Parameters:
7563c58f1c22SToby Isaac + dm - The DM object
7564c58f1c22SToby Isaac - n  - the label number
7565c58f1c22SToby Isaac 
7566c58f1c22SToby Isaac   Output Parameter:
7567c58f1c22SToby Isaac . name - the label name
7568c58f1c22SToby Isaac 
7569c58f1c22SToby Isaac   Level: intermediate
7570c58f1c22SToby Isaac 
7571c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7572c58f1c22SToby Isaac @*/
7573c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7574c58f1c22SToby Isaac {
75755d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7576c58f1c22SToby Isaac   PetscInt       l    = 0;
7577d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7578c58f1c22SToby Isaac 
7579c58f1c22SToby Isaac   PetscFunctionBegin;
7580c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7581c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7582c58f1c22SToby Isaac   while (next) {
7583c58f1c22SToby Isaac     if (l == n) {
7584d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7585c58f1c22SToby Isaac       PetscFunctionReturn(0);
7586c58f1c22SToby Isaac     }
7587c58f1c22SToby Isaac     ++l;
7588c58f1c22SToby Isaac     next = next->next;
7589c58f1c22SToby Isaac   }
7590c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7591c58f1c22SToby Isaac }
7592c58f1c22SToby Isaac 
7593c58f1c22SToby Isaac /*@C
7594c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7595c58f1c22SToby Isaac 
7596c58f1c22SToby Isaac   Not Collective
7597c58f1c22SToby Isaac 
7598c58f1c22SToby Isaac   Input Parameters:
7599c58f1c22SToby Isaac + dm   - The DM object
7600c58f1c22SToby Isaac - name - The label name
7601c58f1c22SToby Isaac 
7602c58f1c22SToby Isaac   Output Parameter:
7603c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7604c58f1c22SToby Isaac 
7605c58f1c22SToby Isaac   Level: intermediate
7606c58f1c22SToby Isaac 
7607c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7608c58f1c22SToby Isaac @*/
7609c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7610c58f1c22SToby Isaac {
76115d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7612d67d17b1SMatthew G. Knepley   const char    *lname;
7613c58f1c22SToby Isaac   PetscErrorCode ierr;
7614c58f1c22SToby Isaac 
7615c58f1c22SToby Isaac   PetscFunctionBegin;
7616c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7617c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7618534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7619c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7620c58f1c22SToby Isaac   while (next) {
7621d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7622d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7623c58f1c22SToby Isaac     if (*hasLabel) break;
7624c58f1c22SToby Isaac     next = next->next;
7625c58f1c22SToby Isaac   }
7626c58f1c22SToby Isaac   PetscFunctionReturn(0);
7627c58f1c22SToby Isaac }
7628c58f1c22SToby Isaac 
7629c58f1c22SToby Isaac /*@C
7630c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7631c58f1c22SToby Isaac 
7632c58f1c22SToby Isaac   Not Collective
7633c58f1c22SToby Isaac 
7634c58f1c22SToby Isaac   Input Parameters:
7635c58f1c22SToby Isaac + dm   - The DM object
7636c58f1c22SToby Isaac - name - The label name
7637c58f1c22SToby Isaac 
7638c58f1c22SToby Isaac   Output Parameter:
7639c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7640c58f1c22SToby Isaac 
7641c58f1c22SToby Isaac   Level: intermediate
7642c58f1c22SToby Isaac 
7643c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7644c58f1c22SToby Isaac @*/
7645c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7646c58f1c22SToby Isaac {
76475d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7648c58f1c22SToby Isaac   PetscBool      hasLabel;
7649d67d17b1SMatthew G. Knepley   const char    *lname;
7650c58f1c22SToby Isaac   PetscErrorCode ierr;
7651c58f1c22SToby Isaac 
7652c58f1c22SToby Isaac   PetscFunctionBegin;
7653c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7654c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7655c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7656c58f1c22SToby Isaac   *label = NULL;
7657c58f1c22SToby Isaac   while (next) {
7658d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7659d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7660c58f1c22SToby Isaac     if (hasLabel) {
7661c58f1c22SToby Isaac       *label = next->label;
7662c58f1c22SToby Isaac       break;
7663c58f1c22SToby Isaac     }
7664c58f1c22SToby Isaac     next = next->next;
7665c58f1c22SToby Isaac   }
7666c58f1c22SToby Isaac   PetscFunctionReturn(0);
7667c58f1c22SToby Isaac }
7668c58f1c22SToby Isaac 
7669c58f1c22SToby Isaac /*@C
7670c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7671c58f1c22SToby Isaac 
7672c58f1c22SToby Isaac   Not Collective
7673c58f1c22SToby Isaac 
7674c58f1c22SToby Isaac   Input Parameters:
7675c58f1c22SToby Isaac + dm - The DM object
7676c58f1c22SToby Isaac - n  - the label number
7677c58f1c22SToby Isaac 
7678c58f1c22SToby Isaac   Output Parameter:
7679c58f1c22SToby Isaac . label - the label
7680c58f1c22SToby Isaac 
7681c58f1c22SToby Isaac   Level: intermediate
7682c58f1c22SToby Isaac 
7683c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7684c58f1c22SToby Isaac @*/
7685c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7686c58f1c22SToby Isaac {
76875d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7688c58f1c22SToby Isaac   PetscInt    l    = 0;
7689c58f1c22SToby Isaac 
7690c58f1c22SToby Isaac   PetscFunctionBegin;
7691c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7692c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7693c58f1c22SToby Isaac   while (next) {
7694c58f1c22SToby Isaac     if (l == n) {
7695c58f1c22SToby Isaac       *label = next->label;
7696c58f1c22SToby Isaac       PetscFunctionReturn(0);
7697c58f1c22SToby Isaac     }
7698c58f1c22SToby Isaac     ++l;
7699c58f1c22SToby Isaac     next = next->next;
7700c58f1c22SToby Isaac   }
7701c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7702c58f1c22SToby Isaac }
7703c58f1c22SToby Isaac 
7704c58f1c22SToby Isaac /*@C
7705c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7706c58f1c22SToby Isaac 
7707c58f1c22SToby Isaac   Not Collective
7708c58f1c22SToby Isaac 
7709c58f1c22SToby Isaac   Input Parameters:
7710c58f1c22SToby Isaac + dm   - The DM object
7711c58f1c22SToby Isaac - label - The DMLabel
7712c58f1c22SToby Isaac 
7713c58f1c22SToby Isaac   Level: developer
7714c58f1c22SToby Isaac 
7715c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7716c58f1c22SToby Isaac @*/
7717c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7718c58f1c22SToby Isaac {
77195d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7720c58f1c22SToby Isaac   PetscBool      hasLabel;
7721d67d17b1SMatthew G. Knepley   const char    *lname;
77225d80c0bfSVaclav Hapla   PetscBool      flg;
7723c58f1c22SToby Isaac   PetscErrorCode ierr;
7724c58f1c22SToby Isaac 
7725c58f1c22SToby Isaac   PetscFunctionBegin;
7726c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7727d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7728d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7729d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7730c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7731c58f1c22SToby Isaac   tmpLabel->label  = label;
7732c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
77335d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
77345d80c0bfSVaclav Hapla   *p = tmpLabel;
773508f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
77365d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
77375d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7738ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7739ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7740c58f1c22SToby Isaac   PetscFunctionReturn(0);
7741c58f1c22SToby Isaac }
7742c58f1c22SToby Isaac 
7743c58f1c22SToby Isaac /*@C
7744e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7745c58f1c22SToby Isaac 
7746c58f1c22SToby Isaac   Not Collective
7747c58f1c22SToby Isaac 
7748c58f1c22SToby Isaac   Input Parameters:
7749c58f1c22SToby Isaac + dm   - The DM object
7750c58f1c22SToby Isaac - name - The label name
7751c58f1c22SToby Isaac 
7752c58f1c22SToby Isaac   Output Parameter:
7753c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7754c58f1c22SToby Isaac 
7755c58f1c22SToby Isaac   Level: developer
7756c58f1c22SToby Isaac 
7757e5472504SVaclav Hapla   Notes:
7758e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7759e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7760e5472504SVaclav Hapla 
7761e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7762e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7763e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7764e5472504SVaclav Hapla 
7765e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7766c58f1c22SToby Isaac @*/
7767c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7768c58f1c22SToby Isaac {
776995d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7770c58f1c22SToby Isaac   PetscBool      hasLabel;
7771d67d17b1SMatthew G. Knepley   const char    *lname;
7772c58f1c22SToby Isaac   PetscErrorCode ierr;
7773c58f1c22SToby Isaac 
7774c58f1c22SToby Isaac   PetscFunctionBegin;
7775c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7776e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7777e5472504SVaclav Hapla   if (label) {
7778e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7779c58f1c22SToby Isaac     *label = NULL;
7780e5472504SVaclav Hapla   }
77815d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
778295d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7783d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7784c58f1c22SToby Isaac     if (hasLabel) {
778595d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7786c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
778795d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7788ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7789ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
779095d578d6SVaclav Hapla       if (label) *label = link->label;
779195d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
779295d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7793c58f1c22SToby Isaac       break;
7794c58f1c22SToby Isaac     }
7795c58f1c22SToby Isaac   }
7796c58f1c22SToby Isaac   PetscFunctionReturn(0);
7797c58f1c22SToby Isaac }
7798c58f1c22SToby Isaac 
7799306894acSVaclav Hapla /*@
7800306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7801306894acSVaclav Hapla 
7802306894acSVaclav Hapla   Not Collective
7803306894acSVaclav Hapla 
7804306894acSVaclav Hapla   Input Parameters:
7805306894acSVaclav Hapla + dm   - The DM object
7806306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7807306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7808306894acSVaclav Hapla 
7809306894acSVaclav Hapla   Level: developer
7810306894acSVaclav Hapla 
7811306894acSVaclav Hapla   Notes:
7812306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7813306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7814306894acSVaclav Hapla   *label nullified.
7815306894acSVaclav Hapla 
7816306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7817306894acSVaclav Hapla @*/
7818306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7819306894acSVaclav Hapla {
782043e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7821306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7822306894acSVaclav Hapla   PetscErrorCode ierr;
7823306894acSVaclav Hapla 
7824306894acSVaclav Hapla   PetscFunctionBegin;
7825306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7826306894acSVaclav Hapla   PetscValidPointer(label, 2);
7827f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7828306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7829306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
78305d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
783143e45a93SVaclav Hapla     if (*label == link->label) {
7832306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
783343e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7834306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7835ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
783643e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
783743e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
783843e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7839306894acSVaclav Hapla       break;
7840306894acSVaclav Hapla     }
7841306894acSVaclav Hapla   }
7842306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7843306894acSVaclav Hapla   PetscFunctionReturn(0);
7844306894acSVaclav Hapla }
7845306894acSVaclav Hapla 
7846c58f1c22SToby Isaac /*@C
7847c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7848c58f1c22SToby Isaac 
7849c58f1c22SToby Isaac   Not Collective
7850c58f1c22SToby Isaac 
7851c58f1c22SToby Isaac   Input Parameters:
7852c58f1c22SToby Isaac + dm   - The DM object
7853c58f1c22SToby Isaac - name - The label name
7854c58f1c22SToby Isaac 
7855c58f1c22SToby Isaac   Output Parameter:
7856c58f1c22SToby Isaac . output - The flag for output
7857c58f1c22SToby Isaac 
7858c58f1c22SToby Isaac   Level: developer
7859c58f1c22SToby Isaac 
7860c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7861c58f1c22SToby Isaac @*/
7862c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7863c58f1c22SToby Isaac {
78645d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7865d67d17b1SMatthew G. Knepley   const char    *lname;
7866c58f1c22SToby Isaac   PetscErrorCode ierr;
7867c58f1c22SToby Isaac 
7868c58f1c22SToby Isaac   PetscFunctionBegin;
7869c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7870c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7871c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7872c58f1c22SToby Isaac   while (next) {
7873c58f1c22SToby Isaac     PetscBool flg;
7874c58f1c22SToby Isaac 
7875d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7876d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7877c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7878c58f1c22SToby Isaac     next = next->next;
7879c58f1c22SToby Isaac   }
7880c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7881c58f1c22SToby Isaac }
7882c58f1c22SToby Isaac 
7883c58f1c22SToby Isaac /*@C
7884c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7885c58f1c22SToby Isaac 
7886c58f1c22SToby Isaac   Not Collective
7887c58f1c22SToby Isaac 
7888c58f1c22SToby Isaac   Input Parameters:
7889c58f1c22SToby Isaac + dm     - The DM object
7890c58f1c22SToby Isaac . name   - The label name
7891c58f1c22SToby Isaac - output - The flag for output
7892c58f1c22SToby Isaac 
7893c58f1c22SToby Isaac   Level: developer
7894c58f1c22SToby Isaac 
7895c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7896c58f1c22SToby Isaac @*/
7897c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7898c58f1c22SToby Isaac {
78995d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7900d67d17b1SMatthew G. Knepley   const char    *lname;
7901c58f1c22SToby Isaac   PetscErrorCode ierr;
7902c58f1c22SToby Isaac 
7903c58f1c22SToby Isaac   PetscFunctionBegin;
7904c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7905534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7906c58f1c22SToby Isaac   while (next) {
7907c58f1c22SToby Isaac     PetscBool flg;
7908c58f1c22SToby Isaac 
7909d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7910d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7911c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7912c58f1c22SToby Isaac     next = next->next;
7913c58f1c22SToby Isaac   }
7914c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7915c58f1c22SToby Isaac }
7916c58f1c22SToby Isaac 
7917c58f1c22SToby Isaac /*@
7918c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7919c58f1c22SToby Isaac 
7920d083f849SBarry Smith   Collective on dmA
7921c58f1c22SToby Isaac 
7922c58f1c22SToby Isaac   Input Parameter:
79235d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
79242e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
79255d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7926ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7927c58f1c22SToby Isaac 
7928c58f1c22SToby Isaac   Level: intermediate
7929c58f1c22SToby Isaac 
7930c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7931c58f1c22SToby Isaac 
79325d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7933c58f1c22SToby Isaac @*/
79345d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7935c58f1c22SToby Isaac {
7936c58f1c22SToby Isaac   DMLabel        label, labelNew;
7937c58f1c22SToby Isaac   const char    *name;
7938c58f1c22SToby Isaac   PetscBool      flg;
79395d80c0bfSVaclav Hapla   DMLabelLink    link;
79405d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7941c58f1c22SToby Isaac 
79425d80c0bfSVaclav Hapla   PetscFunctionBegin;
79435d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
79445d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
79455d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
79465d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
79475d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
79485d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
79495d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
79505d80c0bfSVaclav Hapla     label=link->label;
79515d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
79525d80c0bfSVaclav Hapla     if (!all) {
7953c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7954c58f1c22SToby Isaac       if (flg) continue;
79557d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
79567d5acc75SStefano Zampini       if (flg) continue;
7957ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7958ba2698f1SMatthew G. Knepley       if (flg) continue;
79595d80c0bfSVaclav Hapla     }
79605d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7961c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
79625d80c0bfSVaclav Hapla     } else {
79635d80c0bfSVaclav Hapla       labelNew = label;
79645d80c0bfSVaclav Hapla     }
7965c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
79665d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7967c58f1c22SToby Isaac   }
7968c58f1c22SToby Isaac   PetscFunctionReturn(0);
7969c58f1c22SToby Isaac }
79700fdc7489SMatthew Knepley /*
79710fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
79720fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
79730fdc7489SMatthew Knepley   (label, id) pair in the DM.
79740fdc7489SMatthew Knepley 
79750fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
79760fdc7489SMatthew Knepley   each label.
79770fdc7489SMatthew Knepley */
79780fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
79790fdc7489SMatthew Knepley {
79800fdc7489SMatthew Knepley   DMUniversalLabel ul;
79810fdc7489SMatthew Knepley   PetscBool       *active;
79820fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
79830fdc7489SMatthew Knepley   PetscErrorCode   ierr;
79840fdc7489SMatthew Knepley 
79850fdc7489SMatthew Knepley   PetscFunctionBegin;
79860fdc7489SMatthew Knepley   ierr = PetscMalloc1(1, &ul);CHKERRQ(ierr);
79870fdc7489SMatthew Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label);CHKERRQ(ierr);
79880fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
79890fdc7489SMatthew Knepley   ierr = PetscCalloc1(Nl, &active);CHKERRQ(ierr);
79900fdc7489SMatthew Knepley   ul->Nl = 0;
79910fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
79920fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
79930fdc7489SMatthew Knepley     const char *name;
79940fdc7489SMatthew Knepley 
79950fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
79960fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "depth", 6, &isdepth);CHKERRQ(ierr);
79970fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "celltype", 9, &iscelltype);CHKERRQ(ierr);
79980fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
79990fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
80000fdc7489SMatthew Knepley   }
80010fdc7489SMatthew Knepley   ierr = PetscCalloc5(ul->Nl, &ul->names, ul->Nl, &ul->indices, ul->Nl+1, &ul->offsets, ul->Nl+1, &ul->bits, ul->Nl, &ul->masks);CHKERRQ(ierr);
80020fdc7489SMatthew Knepley   ul->Nv = 0;
80030fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
80040fdc7489SMatthew Knepley     DMLabel     label;
80050fdc7489SMatthew Knepley     PetscInt    nv;
80060fdc7489SMatthew Knepley     const char *name;
80070fdc7489SMatthew Knepley 
80080fdc7489SMatthew Knepley     if (!active[l]) continue;
80090fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
80100fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
80110fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
80120fdc7489SMatthew Knepley     ierr = PetscStrallocpy(name, &ul->names[m]);CHKERRQ(ierr);
80130fdc7489SMatthew Knepley     ul->indices[m]   = l;
80140fdc7489SMatthew Knepley     ul->Nv          += nv;
80150fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
80160fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
80170fdc7489SMatthew Knepley     ++m;
80180fdc7489SMatthew Knepley   }
80190fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
80200fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
80210fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
80220fdc7489SMatthew Knepley   }
80230fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
80240fdc7489SMatthew Knepley     PetscInt b;
80250fdc7489SMatthew Knepley 
80260fdc7489SMatthew Knepley     ul->masks[l] = 0;
80270fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
80280fdc7489SMatthew Knepley   }
80290fdc7489SMatthew Knepley   ierr = PetscMalloc1(ul->Nv, &ul->values);CHKERRQ(ierr);
80300fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
80310fdc7489SMatthew Knepley     DMLabel         label;
80320fdc7489SMatthew Knepley     IS              valueIS;
80330fdc7489SMatthew Knepley     const PetscInt *varr;
80340fdc7489SMatthew Knepley     PetscInt        nv, v;
80350fdc7489SMatthew Knepley 
80360fdc7489SMatthew Knepley     if (!active[l]) continue;
80370fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
80380fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
80390fdc7489SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
80400fdc7489SMatthew Knepley     ierr = ISGetIndices(valueIS, &varr);CHKERRQ(ierr);
80410fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
80420fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
80430fdc7489SMatthew Knepley     }
80440fdc7489SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &varr);CHKERRQ(ierr);
80450fdc7489SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
80460fdc7489SMatthew Knepley     ierr = PetscSortInt(nv, &ul->values[ul->offsets[m]]);CHKERRQ(ierr);
80470fdc7489SMatthew Knepley     ++m;
80480fdc7489SMatthew Knepley   }
80490fdc7489SMatthew Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
80500fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
80510fdc7489SMatthew Knepley     PetscInt  uval = 0;
80520fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
80530fdc7489SMatthew Knepley 
80540fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
80550fdc7489SMatthew Knepley       DMLabel  label;
80560649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
80570fdc7489SMatthew Knepley 
80580fdc7489SMatthew Knepley       if (!active[l]) continue;
80590fdc7489SMatthew Knepley       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
80600fdc7489SMatthew Knepley       ierr = DMLabelGetValue(label, p, &val);CHKERRQ(ierr);
80610fdc7489SMatthew Knepley       ierr = DMLabelGetDefaultValue(label, &defval);CHKERRQ(ierr);
80620fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
80630649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
80640fdc7489SMatthew Knepley       marked = PETSC_TRUE;
80650fdc7489SMatthew Knepley       ierr = PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc);CHKERRQ(ierr);
80660fdc7489SMatthew Knepley       if (loc < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %D not found in compression array", val);
80670fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
80680fdc7489SMatthew Knepley       ++m;
80690fdc7489SMatthew Knepley     }
80700fdc7489SMatthew Knepley     if (marked) {ierr = DMLabelSetValue(ul->label, p, uval);CHKERRQ(ierr);}
80710fdc7489SMatthew Knepley   }
80720fdc7489SMatthew Knepley   ierr = PetscFree(active);CHKERRQ(ierr);
80730fdc7489SMatthew Knepley   *universal = ul;
80740fdc7489SMatthew Knepley   PetscFunctionReturn(0);
80750fdc7489SMatthew Knepley }
80760fdc7489SMatthew Knepley 
80770fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
80780fdc7489SMatthew Knepley {
80790fdc7489SMatthew Knepley   PetscInt       l;
80800fdc7489SMatthew Knepley   PetscErrorCode ierr;
80810fdc7489SMatthew Knepley 
80820fdc7489SMatthew Knepley   PetscFunctionBegin;
80830fdc7489SMatthew Knepley   for (l = 0; l < (*universal)->Nl; ++l) {ierr = PetscFree((*universal)->names[l]);CHKERRQ(ierr);}
80840fdc7489SMatthew Knepley   ierr = DMLabelDestroy(&(*universal)->label);CHKERRQ(ierr);
80850fdc7489SMatthew Knepley   ierr = PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks);CHKERRQ(ierr);
80860fdc7489SMatthew Knepley   ierr = PetscFree((*universal)->values);CHKERRQ(ierr);
80870fdc7489SMatthew Knepley   ierr = PetscFree(*universal);CHKERRQ(ierr);
80880fdc7489SMatthew Knepley   *universal = NULL;
80890fdc7489SMatthew Knepley   PetscFunctionReturn(0);
80900fdc7489SMatthew Knepley }
80910fdc7489SMatthew Knepley 
80920fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
80930fdc7489SMatthew Knepley {
80940fdc7489SMatthew Knepley   PetscFunctionBegin;
80950fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
80960fdc7489SMatthew Knepley   *ulabel = ul->label;
80970fdc7489SMatthew Knepley   PetscFunctionReturn(0);
80980fdc7489SMatthew Knepley }
80990fdc7489SMatthew Knepley 
81000fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
81010fdc7489SMatthew Knepley {
81020fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
81030fdc7489SMatthew Knepley   PetscErrorCode ierr;
81040fdc7489SMatthew Knepley 
81050fdc7489SMatthew Knepley   PetscFunctionBegin;
81060fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
81070fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
81080fdc7489SMatthew Knepley     if (preserveOrder) {ierr = DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]);CHKERRQ(ierr);}
81090fdc7489SMatthew Knepley     else               {ierr = DMCreateLabel(dm, ul->names[l]);CHKERRQ(ierr);}
81100fdc7489SMatthew Knepley   }
81110fdc7489SMatthew Knepley   if (preserveOrder) {
81120fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
81130fdc7489SMatthew Knepley       const char *name;
81140fdc7489SMatthew Knepley       PetscBool   match;
81150fdc7489SMatthew Knepley 
81160fdc7489SMatthew Knepley       ierr = DMGetLabelName(dm, ul->indices[l], &name);CHKERRQ(ierr);
81170fdc7489SMatthew Knepley       ierr = PetscStrcmp(name, ul->names[l], &match);CHKERRQ(ierr);
81180fdc7489SMatthew Knepley       if (!match) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %D name %s does not match new name %s", l, name, ul->names[l]);
81190fdc7489SMatthew Knepley     }
81200fdc7489SMatthew Knepley   }
81210fdc7489SMatthew Knepley   PetscFunctionReturn(0);
81220fdc7489SMatthew Knepley }
81230fdc7489SMatthew Knepley 
81240fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
81250fdc7489SMatthew Knepley {
81260fdc7489SMatthew Knepley   PetscInt       l;
81270fdc7489SMatthew Knepley   PetscErrorCode ierr;
81280fdc7489SMatthew Knepley 
81290fdc7489SMatthew Knepley   PetscFunctionBegin;
81300fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
81310fdc7489SMatthew Knepley     DMLabel  label;
81320fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
81330fdc7489SMatthew Knepley 
81340fdc7489SMatthew Knepley     if (lval) {
81350fdc7489SMatthew Knepley       if (useIndex) {ierr = DMGetLabelByNum(dm, ul->indices[l], &label);CHKERRQ(ierr);}
81360fdc7489SMatthew Knepley       else          {ierr = DMGetLabel(dm, ul->names[l], &label);CHKERRQ(ierr);}
81370fdc7489SMatthew Knepley       ierr = DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]);CHKERRQ(ierr);
81380fdc7489SMatthew Knepley     }
81390fdc7489SMatthew Knepley   }
81400fdc7489SMatthew Knepley   PetscFunctionReturn(0);
81410fdc7489SMatthew Knepley }
8142a8fb8f29SToby Isaac 
8143a8fb8f29SToby Isaac /*@
8144a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8145a8fb8f29SToby Isaac 
8146a8fb8f29SToby Isaac   Input Parameter:
8147a8fb8f29SToby Isaac . dm - The DM object
8148a8fb8f29SToby Isaac 
8149a8fb8f29SToby Isaac   Output Parameter:
8150a8fb8f29SToby Isaac . cdm - The coarse DM
8151a8fb8f29SToby Isaac 
8152a8fb8f29SToby Isaac   Level: intermediate
8153a8fb8f29SToby Isaac 
8154a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
8155a8fb8f29SToby Isaac @*/
8156a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8157a8fb8f29SToby Isaac {
8158a8fb8f29SToby Isaac   PetscFunctionBegin;
8159a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8160a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8161a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8162a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8163a8fb8f29SToby Isaac }
8164a8fb8f29SToby Isaac 
8165a8fb8f29SToby Isaac /*@
8166a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8167a8fb8f29SToby Isaac 
8168a8fb8f29SToby Isaac   Input Parameters:
8169a8fb8f29SToby Isaac + dm - The DM object
8170a8fb8f29SToby Isaac - cdm - The coarse DM
8171a8fb8f29SToby Isaac 
8172a8fb8f29SToby Isaac   Level: intermediate
8173a8fb8f29SToby Isaac 
8174a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
8175a8fb8f29SToby Isaac @*/
8176a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8177a8fb8f29SToby Isaac {
8178a8fb8f29SToby Isaac   PetscErrorCode ierr;
8179a8fb8f29SToby Isaac 
8180a8fb8f29SToby Isaac   PetscFunctionBegin;
8181a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8182a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
8183a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
8184a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
8185a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8186a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8187a8fb8f29SToby Isaac }
8188a8fb8f29SToby Isaac 
818988bdff64SToby Isaac /*@
819088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
819188bdff64SToby Isaac 
819288bdff64SToby Isaac   Input Parameter:
819388bdff64SToby Isaac . dm - The DM object
819488bdff64SToby Isaac 
819588bdff64SToby Isaac   Output Parameter:
819688bdff64SToby Isaac . fdm - The fine DM
819788bdff64SToby Isaac 
819888bdff64SToby Isaac   Level: intermediate
819988bdff64SToby Isaac 
820088bdff64SToby Isaac .seealso: DMSetFineDM()
820188bdff64SToby Isaac @*/
820288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
820388bdff64SToby Isaac {
820488bdff64SToby Isaac   PetscFunctionBegin;
820588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
820688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
820788bdff64SToby Isaac   *fdm = dm->fineMesh;
820888bdff64SToby Isaac   PetscFunctionReturn(0);
820988bdff64SToby Isaac }
821088bdff64SToby Isaac 
821188bdff64SToby Isaac /*@
821288bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
821388bdff64SToby Isaac 
821488bdff64SToby Isaac   Input Parameters:
821588bdff64SToby Isaac + dm - The DM object
821688bdff64SToby Isaac - fdm - The fine DM
821788bdff64SToby Isaac 
821888bdff64SToby Isaac   Level: intermediate
821988bdff64SToby Isaac 
822088bdff64SToby Isaac .seealso: DMGetFineDM()
822188bdff64SToby Isaac @*/
822288bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
822388bdff64SToby Isaac {
822488bdff64SToby Isaac   PetscErrorCode ierr;
822588bdff64SToby Isaac 
822688bdff64SToby Isaac   PetscFunctionBegin;
822788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
822888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
822988bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
823088bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
823188bdff64SToby Isaac   dm->fineMesh = fdm;
823288bdff64SToby Isaac   PetscFunctionReturn(0);
823388bdff64SToby Isaac }
823488bdff64SToby Isaac 
8235a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8236a6ba4734SToby Isaac 
8237a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
8238a6ba4734SToby Isaac {
8239e5e52638SMatthew G. Knepley   PetscInt       d;
8240a6ba4734SToby Isaac   PetscErrorCode ierr;
8241a6ba4734SToby Isaac 
8242a6ba4734SToby Isaac   PetscFunctionBegin;
8243e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
8244e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
8245e5e52638SMatthew G. Knepley   }
8246a6ba4734SToby Isaac   PetscFunctionReturn(0);
8247a6ba4734SToby Isaac }
8248a6ba4734SToby Isaac 
8249a6ba4734SToby Isaac /*@C
8250a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8251a6ba4734SToby Isaac 
8252783e2ec8SMatthew G. Knepley   Collective on dm
8253783e2ec8SMatthew G. Knepley 
8254a6ba4734SToby Isaac   Input Parameters:
82554c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
8256f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8257a6ba4734SToby Isaac . name        - The BC name
8258a6ba4734SToby Isaac . labelname   - The label defining constrained points
8259a6ba4734SToby Isaac . field       - The field to constrain
8260e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
8261a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8262a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
826356cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time deriative of the boundary values, or NULL
8264a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8265a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8266a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8267a6ba4734SToby Isaac 
8268a6ba4734SToby Isaac   Options Database Keys:
8269a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8270a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8271a6ba4734SToby Isaac 
827256cf3b9cSMatthew G. Knepley   Note:
827356cf3b9cSMatthew G. Knepley   Both bcFunc abd bcFunc_t will depend on the boundary condition type. If the type if DM_BC_ESSENTIAL, Then the calling sequence is:
827456cf3b9cSMatthew G. Knepley 
827556cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
827656cf3b9cSMatthew G. Knepley 
827756cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
827856cf3b9cSMatthew G. Knepley 
827956cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
828056cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
828156cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
828256cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
828356cf3b9cSMatthew G. Knepley 
828456cf3b9cSMatthew G. Knepley + dim - the spatial dimension
828556cf3b9cSMatthew G. Knepley . Nf - the number of fields
828656cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
828756cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
828856cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
828956cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
829056cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
829156cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
829256cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
829356cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
829456cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
829556cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
829656cf3b9cSMatthew G. Knepley . t - current time
829756cf3b9cSMatthew G. Knepley . x - coordinates of the current point
829856cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
829956cf3b9cSMatthew G. Knepley . constants - constant parameters
830056cf3b9cSMatthew G. Knepley - bcval - output values at the current point
830156cf3b9cSMatthew G. Knepley 
8302a6ba4734SToby Isaac   Level: developer
8303a6ba4734SToby Isaac 
830456cf3b9cSMatthew G. Knepley .seealso: DMGetBoundary(), PetscDSAddBoundary()
8305a6ba4734SToby Isaac @*/
830656cf3b9cSMatthew G. Knepley PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(void), void (*bcFunc_t)(void), PetscInt numids, const PetscInt *ids, void *ctx)
8307a6ba4734SToby Isaac {
8308e5e52638SMatthew G. Knepley   PetscDS        ds;
8309a6ba4734SToby Isaac   PetscErrorCode ierr;
8310a6ba4734SToby Isaac 
8311a6ba4734SToby Isaac   PetscFunctionBegin;
8312a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8313783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
8314783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
8315783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
8316783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
8317e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8318783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
831956cf3b9cSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, bcFunc_t, numids, ids, ctx);CHKERRQ(ierr);
8320a6ba4734SToby Isaac   PetscFunctionReturn(0);
8321a6ba4734SToby Isaac }
8322a6ba4734SToby Isaac 
8323a6ba4734SToby Isaac /*@
8324a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
8325a6ba4734SToby Isaac 
8326a6ba4734SToby Isaac   Input Parameters:
8327a6ba4734SToby Isaac . dm - The mesh object
8328a6ba4734SToby Isaac 
8329a6ba4734SToby Isaac   Output Parameters:
8330a6ba4734SToby Isaac . numBd - The number of BC
8331a6ba4734SToby Isaac 
8332a6ba4734SToby Isaac   Level: intermediate
8333a6ba4734SToby Isaac 
8334a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
8335a6ba4734SToby Isaac @*/
8336a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
8337a6ba4734SToby Isaac {
8338e5e52638SMatthew G. Knepley   PetscDS        ds;
833958ebd649SToby Isaac   PetscErrorCode ierr;
8340a6ba4734SToby Isaac 
8341a6ba4734SToby Isaac   PetscFunctionBegin;
8342a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8343e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8344e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
8345a6ba4734SToby Isaac   PetscFunctionReturn(0);
8346a6ba4734SToby Isaac }
8347a6ba4734SToby Isaac 
8348a6ba4734SToby Isaac /*@C
83491c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
8350a6ba4734SToby Isaac 
8351a6ba4734SToby Isaac   Input Parameters:
8352a6ba4734SToby Isaac + dm          - The mesh object
8353a6ba4734SToby Isaac - bd          - The BC number
8354a6ba4734SToby Isaac 
8355a6ba4734SToby Isaac   Output Parameters:
8356f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8357a6ba4734SToby Isaac . name        - The BC name
8358a6ba4734SToby Isaac . labelname   - The label defining constrained points
8359a6ba4734SToby Isaac . field       - The field to constrain
8360a6ba4734SToby Isaac . numcomps    - The number of constrained field components
8361a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8362a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
836356cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time derviative of the boundary values
8364a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8365a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8366a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8367a6ba4734SToby Isaac 
8368a6ba4734SToby Isaac   Options Database Keys:
8369a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8370a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8371a6ba4734SToby Isaac 
8372a6ba4734SToby Isaac   Level: developer
8373a6ba4734SToby Isaac 
8374a6ba4734SToby Isaac .seealso: DMAddBoundary()
8375a6ba4734SToby Isaac @*/
837656cf3b9cSMatthew G. Knepley PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(void), void (**func_t)(void), PetscInt *numids, const PetscInt **ids, void **ctx)
8377a6ba4734SToby Isaac {
8378e5e52638SMatthew G. Knepley   PetscDS        ds;
837958ebd649SToby Isaac   PetscErrorCode ierr;
8380a6ba4734SToby Isaac 
8381a6ba4734SToby Isaac   PetscFunctionBegin;
8382a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8383e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
838456cf3b9cSMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, func_t, numids, ids, ctx);CHKERRQ(ierr);
8385a6ba4734SToby Isaac   PetscFunctionReturn(0);
8386a6ba4734SToby Isaac }
8387a6ba4734SToby Isaac 
8388e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8389e6f8dbb6SToby Isaac {
8390e5e52638SMatthew G. Knepley   PetscDS        ds;
8391dff059c6SToby Isaac   DMBoundary    *lastnext;
8392e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8393e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8394e6f8dbb6SToby Isaac 
8395e6f8dbb6SToby Isaac   PetscFunctionBegin;
8396e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8397e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
839847a1f5adSToby Isaac   if (dm->boundary) {
839947a1f5adSToby Isaac     DMBoundary next = dm->boundary;
840047a1f5adSToby Isaac 
840147a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
840247a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
840347a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
840447a1f5adSToby Isaac     while (next) {
840547a1f5adSToby Isaac       DMBoundary b = next;
840647a1f5adSToby Isaac 
840747a1f5adSToby Isaac       next = b->next;
840847a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8409a6ba4734SToby Isaac     }
841047a1f5adSToby Isaac     dm->boundary = NULL;
8411a6ba4734SToby Isaac   }
841247a1f5adSToby Isaac 
8413dff059c6SToby Isaac   lastnext = &(dm->boundary);
8414e6f8dbb6SToby Isaac   while (dsbound) {
8415e6f8dbb6SToby Isaac     DMBoundary dmbound;
8416e6f8dbb6SToby Isaac 
8417e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8418e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
8419e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
8420994fe344SLisandro 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);}
842147a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8422dff059c6SToby Isaac     *lastnext = dmbound;
8423dff059c6SToby Isaac     lastnext = &(dmbound->next);
8424dff059c6SToby Isaac     dsbound = dsbound->next;
8425a6ba4734SToby Isaac   }
8426a6ba4734SToby Isaac   PetscFunctionReturn(0);
8427a6ba4734SToby Isaac }
8428a6ba4734SToby Isaac 
8429a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8430a6ba4734SToby Isaac {
8431b95f2879SToby Isaac   DMBoundary     b;
8432a6ba4734SToby Isaac   PetscErrorCode ierr;
8433a6ba4734SToby Isaac 
8434a6ba4734SToby Isaac   PetscFunctionBegin;
8435a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8436534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8437a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8438e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8439b95f2879SToby Isaac   b = dm->boundary;
8440a6ba4734SToby Isaac   while (b && !(*isBd)) {
8441e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8442e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
84433424c85cSToby Isaac 
84443424c85cSToby Isaac     if (label) {
8445a6ba4734SToby Isaac       PetscInt i;
8446a6ba4734SToby Isaac 
8447e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
8448e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
8449a6ba4734SToby Isaac       }
8450a6ba4734SToby Isaac     }
8451a6ba4734SToby Isaac     b = b->next;
8452a6ba4734SToby Isaac   }
8453a6ba4734SToby Isaac   PetscFunctionReturn(0);
8454a6ba4734SToby Isaac }
84554d6f44ffSToby Isaac 
84564d6f44ffSToby Isaac /*@C
8457a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8458a6e0b375SMatthew G. Knepley 
8459a6e0b375SMatthew G. Knepley   Collective on DM
84604d6f44ffSToby Isaac 
84614d6f44ffSToby Isaac   Input Parameters:
84624d6f44ffSToby Isaac + dm      - The DM
84630709b2feSToby Isaac . time    - The time
84644d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
84654d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
84664d6f44ffSToby Isaac - mode    - The insertion mode for values
84674d6f44ffSToby Isaac 
84684d6f44ffSToby Isaac   Output Parameter:
84694d6f44ffSToby Isaac . X - vector
84704d6f44ffSToby Isaac 
84714d6f44ffSToby Isaac    Calling sequence of func:
84720709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
84734d6f44ffSToby Isaac 
84744d6f44ffSToby Isaac +  dim - The spatial dimension
84758ec8862eSJed Brown .  time - The time at which to sample
84764d6f44ffSToby Isaac .  x   - The coordinates
84774d6f44ffSToby Isaac .  Nf  - The number of fields
84784d6f44ffSToby Isaac .  u   - The output field values
84794d6f44ffSToby Isaac -  ctx - optional user-defined function context
84804d6f44ffSToby Isaac 
84814d6f44ffSToby Isaac   Level: developer
84824d6f44ffSToby Isaac 
8483a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
84844d6f44ffSToby Isaac @*/
84850709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
84864d6f44ffSToby Isaac {
84874d6f44ffSToby Isaac   Vec            localX;
84884d6f44ffSToby Isaac   PetscErrorCode ierr;
84894d6f44ffSToby Isaac 
84904d6f44ffSToby Isaac   PetscFunctionBegin;
84914d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84924d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
84930709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
84944d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
84954d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
84964d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
84974d6f44ffSToby Isaac   PetscFunctionReturn(0);
84984d6f44ffSToby Isaac }
84994d6f44ffSToby Isaac 
8500a6e0b375SMatthew G. Knepley /*@C
8501a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8502a6e0b375SMatthew G. Knepley 
8503a6e0b375SMatthew G. Knepley   Not collective
8504a6e0b375SMatthew G. Knepley 
8505a6e0b375SMatthew G. Knepley   Input Parameters:
8506a6e0b375SMatthew G. Knepley + dm      - The DM
8507a6e0b375SMatthew G. Knepley . time    - The time
8508a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8509a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8510a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8511a6e0b375SMatthew G. Knepley 
8512a6e0b375SMatthew G. Knepley   Output Parameter:
8513a6e0b375SMatthew G. Knepley . localX - vector
8514a6e0b375SMatthew G. Knepley 
8515a6e0b375SMatthew G. Knepley    Calling sequence of func:
8516a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8517a6e0b375SMatthew G. Knepley 
8518a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8519a6e0b375SMatthew G. Knepley .  x   - The coordinates
8520a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8521a6e0b375SMatthew G. Knepley .  u   - The output field values
8522a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8523a6e0b375SMatthew G. Knepley 
8524a6e0b375SMatthew G. Knepley   Level: developer
8525a6e0b375SMatthew G. Knepley 
8526a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8527a6e0b375SMatthew G. Knepley @*/
85280709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
85294d6f44ffSToby Isaac {
85304d6f44ffSToby Isaac   PetscErrorCode ierr;
85314d6f44ffSToby Isaac 
85324d6f44ffSToby Isaac   PetscFunctionBegin;
85334d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85344d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
85350918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
85360709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
85374d6f44ffSToby Isaac   PetscFunctionReturn(0);
85384d6f44ffSToby Isaac }
85394d6f44ffSToby Isaac 
8540a6e0b375SMatthew G. Knepley /*@C
8541a6e0b375SMatthew 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.
8542a6e0b375SMatthew G. Knepley 
8543a6e0b375SMatthew G. Knepley   Collective on DM
8544a6e0b375SMatthew G. Knepley 
8545a6e0b375SMatthew G. Knepley   Input Parameters:
8546a6e0b375SMatthew G. Knepley + dm      - The DM
8547a6e0b375SMatthew G. Knepley . time    - The time
8548a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8549a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8550a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8551a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8552a6e0b375SMatthew G. Knepley 
8553a6e0b375SMatthew G. Knepley   Output Parameter:
8554a6e0b375SMatthew G. Knepley . X - vector
8555a6e0b375SMatthew G. Knepley 
8556a6e0b375SMatthew G. Knepley    Calling sequence of func:
8557a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8558a6e0b375SMatthew G. Knepley 
8559a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8560a6e0b375SMatthew G. Knepley .  x   - The coordinates
8561a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8562a6e0b375SMatthew G. Knepley .  u   - The output field values
8563a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8564a6e0b375SMatthew G. Knepley 
8565a6e0b375SMatthew G. Knepley   Level: developer
8566a6e0b375SMatthew G. Knepley 
8567a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8568a6e0b375SMatthew G. Knepley @*/
85692c53366bSMatthew 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)
85702c53366bSMatthew G. Knepley {
85712c53366bSMatthew G. Knepley   Vec            localX;
85722c53366bSMatthew G. Knepley   PetscErrorCode ierr;
85732c53366bSMatthew G. Knepley 
85742c53366bSMatthew G. Knepley   PetscFunctionBegin;
85752c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85762c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
85772c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
85782c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
85792c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
85802c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
85812c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
85822c53366bSMatthew G. Knepley }
85832c53366bSMatthew G. Knepley 
8584a6e0b375SMatthew G. Knepley /*@C
8585a6e0b375SMatthew 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.
8586a6e0b375SMatthew G. Knepley 
8587a6e0b375SMatthew G. Knepley   Not collective
8588a6e0b375SMatthew G. Knepley 
8589a6e0b375SMatthew G. Knepley   Input Parameters:
8590a6e0b375SMatthew G. Knepley + dm      - The DM
8591a6e0b375SMatthew G. Knepley . time    - The time
8592a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8593a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8594a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8595a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8596a6e0b375SMatthew G. Knepley 
8597a6e0b375SMatthew G. Knepley   Output Parameter:
8598a6e0b375SMatthew G. Knepley . localX - vector
8599a6e0b375SMatthew G. Knepley 
8600a6e0b375SMatthew G. Knepley    Calling sequence of func:
8601a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8602a6e0b375SMatthew G. Knepley 
8603a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8604a6e0b375SMatthew G. Knepley .  x   - The coordinates
8605a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8606a6e0b375SMatthew G. Knepley .  u   - The output field values
8607a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8608a6e0b375SMatthew G. Knepley 
8609a6e0b375SMatthew G. Knepley   Level: developer
8610a6e0b375SMatthew G. Knepley 
8611a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8612a6e0b375SMatthew G. Knepley @*/
86131c531cf8SMatthew 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)
86144d6f44ffSToby Isaac {
86154d6f44ffSToby Isaac   PetscErrorCode ierr;
86164d6f44ffSToby Isaac 
86174d6f44ffSToby Isaac   PetscFunctionBegin;
86184d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86194d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
86200918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
86211c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
86224d6f44ffSToby Isaac   PetscFunctionReturn(0);
86234d6f44ffSToby Isaac }
86242716604bSToby Isaac 
8625a6e0b375SMatthew G. Knepley /*@C
8626a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8627a6e0b375SMatthew G. Knepley 
8628a6e0b375SMatthew G. Knepley   Not collective
8629a6e0b375SMatthew G. Knepley 
8630a6e0b375SMatthew G. Knepley   Input Parameters:
8631a6e0b375SMatthew G. Knepley + dm      - The DM
8632a6e0b375SMatthew G. Knepley . time    - The time
8633a6e0b375SMatthew G. Knepley . localU  - The input field vector
8634a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8635a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8636a6e0b375SMatthew G. Knepley 
8637a6e0b375SMatthew G. Knepley   Output Parameter:
8638a6e0b375SMatthew G. Knepley . localX  - The output vector
8639a6e0b375SMatthew G. Knepley 
8640a6e0b375SMatthew G. Knepley    Calling sequence of func:
8641a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8642a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8643a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8644a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8645a6e0b375SMatthew G. Knepley 
8646a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8647a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8648a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8649a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8650a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8651a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8652a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8653a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8654a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8655a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8656a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8657a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8658a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8659a6e0b375SMatthew G. Knepley .  t            - The current time
8660a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8661a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8662a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8663a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8664a6e0b375SMatthew G. Knepley 
8665a6e0b375SMatthew 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.
8666a6e0b375SMatthew 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
8667a6e0b375SMatthew 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
8668a6e0b375SMatthew 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.
8669a6e0b375SMatthew G. Knepley 
8670a6e0b375SMatthew G. Knepley   Level: intermediate
8671a6e0b375SMatthew G. Knepley 
8672a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8673a6e0b375SMatthew G. Knepley @*/
86748c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
86758c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
86768c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
86778c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8678191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
86798c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
86808c6c5593SMatthew G. Knepley {
86818c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
86828c6c5593SMatthew G. Knepley 
86838c6c5593SMatthew G. Knepley   PetscFunctionBegin;
86848c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86858c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
86868c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
86870918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
86888c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
86898c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
86908c6c5593SMatthew G. Knepley }
86918c6c5593SMatthew G. Knepley 
8692a6e0b375SMatthew G. Knepley /*@C
8693a6e0b375SMatthew 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.
8694a6e0b375SMatthew G. Knepley 
8695a6e0b375SMatthew G. Knepley   Not collective
8696a6e0b375SMatthew G. Knepley 
8697a6e0b375SMatthew G. Knepley   Input Parameters:
8698a6e0b375SMatthew G. Knepley + dm      - The DM
8699a6e0b375SMatthew G. Knepley . time    - The time
8700a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8701a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8702a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8703a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8704a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8705a6e0b375SMatthew G. Knepley . localU  - The input field vector
8706a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8707a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8708a6e0b375SMatthew G. Knepley 
8709a6e0b375SMatthew G. Knepley   Output Parameter:
8710a6e0b375SMatthew G. Knepley . localX  - The output vector
8711a6e0b375SMatthew G. Knepley 
8712a6e0b375SMatthew G. Knepley    Calling sequence of func:
8713a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8714a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8715a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8716a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8717a6e0b375SMatthew G. Knepley 
8718a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8719a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8720a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8721a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8722a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8723a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8724a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8725a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8726a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8727a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8728a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8729a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8730a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8731a6e0b375SMatthew G. Knepley .  t            - The current time
8732a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8733a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8734a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8735a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8736a6e0b375SMatthew G. Knepley 
8737a6e0b375SMatthew 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.
8738a6e0b375SMatthew 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
8739a6e0b375SMatthew 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
8740a6e0b375SMatthew 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.
8741a6e0b375SMatthew G. Knepley 
8742a6e0b375SMatthew G. Knepley   Level: intermediate
8743a6e0b375SMatthew G. Knepley 
8744a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8745a6e0b375SMatthew G. Knepley @*/
87461c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
87478c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
87488c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
87498c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8750191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
87518c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
87528c6c5593SMatthew G. Knepley {
87538c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
87548c6c5593SMatthew G. Knepley 
87558c6c5593SMatthew G. Knepley   PetscFunctionBegin;
87568c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
87588c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8759ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
87601c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
87618c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
87628c6c5593SMatthew G. Knepley }
87638c6c5593SMatthew G. Knepley 
87642716604bSToby Isaac /*@C
8765ece3a9fcSMatthew 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.
8766ece3a9fcSMatthew G. Knepley 
8767ece3a9fcSMatthew G. Knepley   Not collective
8768ece3a9fcSMatthew G. Knepley 
8769ece3a9fcSMatthew G. Knepley   Input Parameters:
8770ece3a9fcSMatthew G. Knepley + dm      - The DM
8771ece3a9fcSMatthew G. Knepley . time    - The time
8772ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8773ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8774ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8775ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8776ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8777ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8778ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8779ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8780ece3a9fcSMatthew G. Knepley 
8781ece3a9fcSMatthew G. Knepley   Output Parameter:
8782ece3a9fcSMatthew G. Knepley . localX  - The output vector
8783ece3a9fcSMatthew G. Knepley 
8784ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8785ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8786ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8787ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8788ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8789ece3a9fcSMatthew G. Knepley 
8790ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8791ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8792ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8793ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8794ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8795ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8796ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8797ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8798ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8799ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8800ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8801ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8802ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8803ece3a9fcSMatthew G. Knepley .  t            - The current time
8804ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8805ece3a9fcSMatthew G. Knepley .  n            - The face normal
8806ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8807ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8808ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8809ece3a9fcSMatthew G. Knepley 
8810ece3a9fcSMatthew G. Knepley   Note:
8811ece3a9fcSMatthew 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.
8812ece3a9fcSMatthew 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
8813ece3a9fcSMatthew 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
8814ece3a9fcSMatthew 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.
8815ece3a9fcSMatthew G. Knepley 
8816ece3a9fcSMatthew G. Knepley   Level: intermediate
8817ece3a9fcSMatthew G. Knepley 
8818ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8819ece3a9fcSMatthew G. Knepley @*/
8820ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8821ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8822ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8823ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8824ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8825ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8826ece3a9fcSMatthew G. Knepley {
8827ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8828ece3a9fcSMatthew G. Knepley 
8829ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8830ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8831ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8832ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8833ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8834ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8835ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8836ece3a9fcSMatthew G. Knepley }
8837ece3a9fcSMatthew G. Knepley 
8838ece3a9fcSMatthew G. Knepley /*@C
88392716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
88402716604bSToby Isaac 
88412716604bSToby Isaac   Input Parameters:
88422716604bSToby Isaac + dm    - The DM
88430709b2feSToby Isaac . time  - The time
88442716604bSToby Isaac . funcs - The functions to evaluate for each field component
88452716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8846574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
88472716604bSToby Isaac 
88482716604bSToby Isaac   Output Parameter:
88492716604bSToby Isaac . diff - The diff ||u - u_h||_2
88502716604bSToby Isaac 
88512716604bSToby Isaac   Level: developer
88522716604bSToby Isaac 
88531189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
88542716604bSToby Isaac @*/
88550709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
88562716604bSToby Isaac {
88572716604bSToby Isaac   PetscErrorCode ierr;
88582716604bSToby Isaac 
88592716604bSToby Isaac   PetscFunctionBegin;
88602716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8861b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
88620918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
88630709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
88642716604bSToby Isaac   PetscFunctionReturn(0);
88652716604bSToby Isaac }
8866b698f381SToby Isaac 
8867b698f381SToby Isaac /*@C
8868b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8869b698f381SToby Isaac 
8870d083f849SBarry Smith   Collective on dm
8871d083f849SBarry Smith 
8872b698f381SToby Isaac   Input Parameters:
8873b698f381SToby Isaac + dm    - The DM
8874b698f381SToby Isaac , time  - The time
8875b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8876b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8877574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8878b698f381SToby Isaac - n     - The vector to project along
8879b698f381SToby Isaac 
8880b698f381SToby Isaac   Output Parameter:
8881b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8882b698f381SToby Isaac 
8883b698f381SToby Isaac   Level: developer
8884b698f381SToby Isaac 
8885b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8886b698f381SToby Isaac @*/
8887b698f381SToby 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)
8888b698f381SToby Isaac {
8889b698f381SToby Isaac   PetscErrorCode ierr;
8890b698f381SToby Isaac 
8891b698f381SToby Isaac   PetscFunctionBegin;
8892b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8893b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8894b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8895b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8896b698f381SToby Isaac   PetscFunctionReturn(0);
8897b698f381SToby Isaac }
8898b698f381SToby Isaac 
88992a16baeaSToby Isaac /*@C
89002a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
89012a16baeaSToby Isaac 
8902d083f849SBarry Smith   Collective on dm
8903d083f849SBarry Smith 
89042a16baeaSToby Isaac   Input Parameters:
89052a16baeaSToby Isaac + dm    - The DM
89062a16baeaSToby Isaac . time  - The time
89072a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
89082a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8909574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
89102a16baeaSToby Isaac 
89112a16baeaSToby Isaac   Output Parameter:
89122a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
89132a16baeaSToby Isaac 
89142a16baeaSToby Isaac   Level: developer
89152a16baeaSToby Isaac 
89161189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
89172a16baeaSToby Isaac @*/
89181189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
89192a16baeaSToby Isaac {
89202a16baeaSToby Isaac   PetscErrorCode ierr;
89212a16baeaSToby Isaac 
89222a16baeaSToby Isaac   PetscFunctionBegin;
89232a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89242a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
89250918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
89262a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
89272a16baeaSToby Isaac   PetscFunctionReturn(0);
89282a16baeaSToby Isaac }
89292a16baeaSToby Isaac 
8930df0b854cSToby Isaac /*@C
8931df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8932cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8933df0b854cSToby Isaac 
8934df0b854cSToby Isaac   Collective on dm
8935df0b854cSToby Isaac 
8936df0b854cSToby Isaac   Input parameters:
8937df0b854cSToby Isaac + dm - the pre-adaptation DM object
8938a1b0c543SToby Isaac - label - label with the flags
8939df0b854cSToby Isaac 
8940df0b854cSToby Isaac   Output parameters:
89410d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8942df0b854cSToby Isaac 
8943df0b854cSToby Isaac   Level: intermediate
89440d1cd5e0SMatthew G. Knepley 
89450d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8946df0b854cSToby Isaac @*/
89470d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8948df0b854cSToby Isaac {
8949df0b854cSToby Isaac   PetscErrorCode ierr;
8950df0b854cSToby Isaac 
8951df0b854cSToby Isaac   PetscFunctionBegin;
8952df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8953a1b0c543SToby Isaac   PetscValidPointer(label,2);
89540d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
89550d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
89566f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
89570d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
8958*a587d139SMark   if (*dmAdapt) {
8959*a587d139SMark     (*dmAdapt)->prealloc_only = dm->prealloc_only;  /* maybe this should go .... */
8960*a587d139SMark     ierr = PetscFree((*dmAdapt)->vectype);CHKERRQ(ierr);
8961*a587d139SMark     ierr = PetscStrallocpy(dm->vectype,(char**)&(*dmAdapt)->vectype);CHKERRQ(ierr);
8962*a587d139SMark     ierr = PetscFree((*dmAdapt)->mattype);CHKERRQ(ierr);
8963*a587d139SMark     ierr = PetscStrallocpy(dm->mattype,(char**)&(*dmAdapt)->mattype);CHKERRQ(ierr);
8964*a587d139SMark   }
89650d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
89660d1cd5e0SMatthew G. Knepley }
89670d1cd5e0SMatthew G. Knepley 
89680d1cd5e0SMatthew G. Knepley /*@C
89690d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
89700d1cd5e0SMatthew G. Knepley 
89710d1cd5e0SMatthew G. Knepley   Input Parameters:
89720d1cd5e0SMatthew G. Knepley + dm - The DM object
89730d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
89746f25b0d8SLisandro 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_".
89750d1cd5e0SMatthew G. Knepley 
89760d1cd5e0SMatthew G. Knepley   Output Parameter:
89770d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
89780d1cd5e0SMatthew G. Knepley 
89790d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
89800d1cd5e0SMatthew G. Knepley 
89810d1cd5e0SMatthew G. Knepley   Level: advanced
89820d1cd5e0SMatthew G. Knepley 
89830d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
89840d1cd5e0SMatthew G. Knepley @*/
89850d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
89860d1cd5e0SMatthew G. Knepley {
89870d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
89880d1cd5e0SMatthew G. Knepley 
89890d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
89900d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89910d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
89920d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
89930d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
89946f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
89956f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
89960d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8997df0b854cSToby Isaac   PetscFunctionReturn(0);
8998df0b854cSToby Isaac }
8999c4088d22SMatthew G. Knepley 
9000502a2867SDave May /*@C
9001502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9002502a2867SDave May 
9003502a2867SDave May  Not Collective
9004502a2867SDave May 
9005502a2867SDave May  Input Parameter:
9006502a2867SDave May .  dm    - The DM
9007502a2867SDave May 
90080a19bb7dSprj-  Output Parameters:
90090a19bb7dSprj- +  nranks - the number of neighbours
90100a19bb7dSprj- -  ranks - the neighbors ranks
9011502a2867SDave May 
9012502a2867SDave May  Notes:
9013502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9014502a2867SDave May 
9015502a2867SDave May  Level: beginner
9016502a2867SDave May 
9017dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
9018502a2867SDave May @*/
9019502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9020502a2867SDave May {
9021502a2867SDave May   PetscErrorCode ierr;
9022502a2867SDave May 
9023502a2867SDave May   PetscFunctionBegin;
9024502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90250918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
9026502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
9027502a2867SDave May   PetscFunctionReturn(0);
9028502a2867SDave May }
9029502a2867SDave May 
9030531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9031531c7667SBarry Smith 
9032531c7667SBarry Smith /*
9033531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9034531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9035531c7667SBarry Smith */
9036531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9037531c7667SBarry Smith {
9038531c7667SBarry Smith   PetscErrorCode ierr;
9039531c7667SBarry Smith 
9040531c7667SBarry Smith   PetscFunctionBegin;
9041531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9042531c7667SBarry Smith     Vec x1local;
9043531c7667SBarry Smith     DM  dm;
9044531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9045531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
9046531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
9047531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9048531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9049531c7667SBarry Smith     x1   = x1local;
9050531c7667SBarry Smith   }
9051531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
9052531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9053531c7667SBarry Smith     DM  dm;
9054531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9055531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
9056531c7667SBarry Smith   }
9057531c7667SBarry Smith   PetscFunctionReturn(0);
9058531c7667SBarry Smith }
9059531c7667SBarry Smith 
9060531c7667SBarry Smith /*@
9061531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9062531c7667SBarry Smith 
9063531c7667SBarry Smith     Input Parameter:
9064531c7667SBarry Smith .    coloring - the MatFDColoring object
9065531c7667SBarry Smith 
906695452b02SPatrick Sanan     Developer Notes:
906795452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9068531c7667SBarry Smith 
90691b266c99SBarry Smith     Level: advanced
90701b266c99SBarry Smith 
9071531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
9072531c7667SBarry Smith @*/
9073531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9074531c7667SBarry Smith {
9075531c7667SBarry Smith   PetscFunctionBegin;
9076531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9077531c7667SBarry Smith   PetscFunctionReturn(0);
9078531c7667SBarry Smith }
90798320bc6fSPatrick Sanan 
90808320bc6fSPatrick Sanan /*@
90818320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
90828320bc6fSPatrick Sanan 
90838320bc6fSPatrick Sanan     Collective
90848320bc6fSPatrick Sanan 
90858320bc6fSPatrick Sanan     Input Parameters:
9086a5bc1bf3SBarry Smith +    dm1 - the first DM
90878320bc6fSPatrick Sanan -    dm2 - the second DM
90888320bc6fSPatrick Sanan 
90898320bc6fSPatrick Sanan     Output Parameters:
90908320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
90918320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
90928320bc6fSPatrick Sanan 
90938320bc6fSPatrick Sanan     Notes:
90948320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
90953d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
90968320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
90973d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
90983d862458SPatrick Sanan     decomposition, but hold different data.
90998320bc6fSPatrick Sanan 
91008320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
91018320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
91028320bc6fSPatrick Sanan 
91038320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
91048320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
91058320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
91068320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
91078320bc6fSPatrick Sanan 
91088320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
91098320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
91108320bc6fSPatrick Sanan .vb
91118320bc6fSPatrick Sanan   ...
91128320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
91138320bc6fSPatrick Sanan   if (set && compatible)  {
91148320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
91158320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
91163d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
91178320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
91188320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
91198320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
91208320bc6fSPatrick Sanan       }
91218320bc6fSPatrick Sanan     }
91228320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
91238320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
91248320bc6fSPatrick Sanan   } else {
91258320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
91268320bc6fSPatrick Sanan   }
91278320bc6fSPatrick Sanan   ...
91288320bc6fSPatrick Sanan .ve
91298320bc6fSPatrick Sanan 
91308320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
91318320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
91328320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
91338320bc6fSPatrick Sanan     always check the "set" output parameter.
91348320bc6fSPatrick Sanan 
91358320bc6fSPatrick Sanan     A DM is always compatible with itself.
91368320bc6fSPatrick Sanan 
91378320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
91388320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
91398320bc6fSPatrick Sanan     incompatible.
91408320bc6fSPatrick Sanan 
91418320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
91428320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
91438320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
91448320bc6fSPatrick Sanan 
91458320bc6fSPatrick Sanan     Developer Notes:
91463d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
91473d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9148a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
91498320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
91508320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
91513d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
91523d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
91538320bc6fSPatrick Sanan 
91548320bc6fSPatrick Sanan     Level: advanced
91558320bc6fSPatrick Sanan 
91563d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
91578320bc6fSPatrick Sanan @*/
91588320bc6fSPatrick Sanan 
9159a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
91608320bc6fSPatrick Sanan {
91618320bc6fSPatrick Sanan   PetscErrorCode ierr;
91628320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
91638320bc6fSPatrick Sanan   DMType         type,type2;
91648320bc6fSPatrick Sanan   PetscBool      sameType;
91658320bc6fSPatrick Sanan 
91668320bc6fSPatrick Sanan   PetscFunctionBegin;
9167a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
91688320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
91698320bc6fSPatrick Sanan 
91708320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9171a5bc1bf3SBarry Smith   if (dm1 == dm2) {
91728320bc6fSPatrick Sanan     *set = PETSC_TRUE;
91738320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
91748320bc6fSPatrick Sanan     PetscFunctionReturn(0);
91758320bc6fSPatrick Sanan   }
91768320bc6fSPatrick Sanan 
91778320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
91788320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
91798320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
91808320bc6fSPatrick Sanan      determined by the implementation-specific logic */
9181a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
91828320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
91838320bc6fSPatrick Sanan     *set = PETSC_TRUE;
91848320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
91858320bc6fSPatrick Sanan     PetscFunctionReturn(0);
91868320bc6fSPatrick Sanan   }
91878320bc6fSPatrick Sanan 
91888320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9189a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
9190a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
9191b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
91928320bc6fSPatrick Sanan   }
91938320bc6fSPatrick Sanan 
9194a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
91958320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
9196a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
91978320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
91988320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
91998320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
9200a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
92018320bc6fSPatrick Sanan   } else {
92028320bc6fSPatrick Sanan     *set = PETSC_FALSE;
92038320bc6fSPatrick Sanan   }
92048320bc6fSPatrick Sanan   PetscFunctionReturn(0);
92058320bc6fSPatrick Sanan }
9206c0f0dcc3SMatthew G. Knepley 
9207c0f0dcc3SMatthew G. Knepley /*@C
9208c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9209c0f0dcc3SMatthew G. Knepley 
9210c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9211c0f0dcc3SMatthew G. Knepley 
9212c0f0dcc3SMatthew G. Knepley   Input Parameters:
9213c0f0dcc3SMatthew G. Knepley + DM - the DM
9214c0f0dcc3SMatthew G. Knepley . f - the monitor function
9215c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9216c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9217c0f0dcc3SMatthew G. Knepley 
9218c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9219c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9220c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9221c0f0dcc3SMatthew G. Knepley 
9222c0f0dcc3SMatthew G. Knepley   Notes:
9223c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9224c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9225c0f0dcc3SMatthew G. Knepley   order in which they were set.
9226c0f0dcc3SMatthew G. Knepley 
9227c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9228c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9229c0f0dcc3SMatthew G. Knepley 
9230c0f0dcc3SMatthew G. Knepley   Level: intermediate
9231c0f0dcc3SMatthew G. Knepley 
9232c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9233c0f0dcc3SMatthew G. Knepley @*/
9234c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9235c0f0dcc3SMatthew G. Knepley {
9236c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9237c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9238c0f0dcc3SMatthew G. Knepley 
9239c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9240c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9241c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9242c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9243c0f0dcc3SMatthew G. Knepley 
9244c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9245c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9246c0f0dcc3SMatthew G. Knepley   }
9247c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9248c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9249c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9250c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9251c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9252c0f0dcc3SMatthew G. Knepley }
9253c0f0dcc3SMatthew G. Knepley 
9254c0f0dcc3SMatthew G. Knepley /*@
9255c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9256c0f0dcc3SMatthew G. Knepley 
9257c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9258c0f0dcc3SMatthew G. Knepley 
9259c0f0dcc3SMatthew G. Knepley   Input Parameter:
9260c0f0dcc3SMatthew G. Knepley . dm - the DM
9261c0f0dcc3SMatthew G. Knepley 
9262c0f0dcc3SMatthew G. Knepley   Options Database Key:
9263c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9264c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9265c0f0dcc3SMatthew G. Knepley   set via the options database
9266c0f0dcc3SMatthew G. Knepley 
9267c0f0dcc3SMatthew G. Knepley   Notes:
9268c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9269c0f0dcc3SMatthew G. Knepley 
9270c0f0dcc3SMatthew G. Knepley   Level: intermediate
9271c0f0dcc3SMatthew G. Knepley 
9272c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9273c0f0dcc3SMatthew G. Knepley @*/
9274c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9275c0f0dcc3SMatthew G. Knepley {
9276c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9277c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9278c0f0dcc3SMatthew G. Knepley 
9279c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9280c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9281c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9282c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9283c0f0dcc3SMatthew G. Knepley   }
9284c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9285c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9286c0f0dcc3SMatthew G. Knepley }
9287c0f0dcc3SMatthew G. Knepley 
9288c0f0dcc3SMatthew G. Knepley /*@C
9289c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9290c0f0dcc3SMatthew G. Knepley 
9291c0f0dcc3SMatthew G. Knepley   Collective on DM
9292c0f0dcc3SMatthew G. Knepley 
9293c0f0dcc3SMatthew G. Knepley   Input Parameters:
9294c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9295c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9296c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9297c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9298c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9299c0f0dcc3SMatthew 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
9300c0f0dcc3SMatthew G. Knepley 
9301c0f0dcc3SMatthew G. Knepley   Output Parameter:
9302c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9303c0f0dcc3SMatthew G. Knepley 
9304c0f0dcc3SMatthew G. Knepley   Level: developer
9305c0f0dcc3SMatthew G. Knepley 
9306c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9307c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9308c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9309c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9310c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9311c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9312c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9313c0f0dcc3SMatthew G. Knepley @*/
9314c0f0dcc3SMatthew 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)
9315c0f0dcc3SMatthew G. Knepley {
9316c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9317c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9318c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9319c0f0dcc3SMatthew G. Knepley 
9320c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9321c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9322c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9323c0f0dcc3SMatthew G. Knepley   if (*flg) {
9324c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9325c0f0dcc3SMatthew G. Knepley 
9326c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9327c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9328c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9329c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9330c0f0dcc3SMatthew G. Knepley   }
9331c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9332c0f0dcc3SMatthew G. Knepley }
9333c0f0dcc3SMatthew G. Knepley 
9334c0f0dcc3SMatthew G. Knepley /*@
9335c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9336c0f0dcc3SMatthew G. Knepley 
9337c0f0dcc3SMatthew G. Knepley    Collective on DM
9338c0f0dcc3SMatthew G. Knepley 
9339c0f0dcc3SMatthew G. Knepley    Input Parameters:
9340c0f0dcc3SMatthew G. Knepley .  dm - The DM
9341c0f0dcc3SMatthew G. Knepley 
9342c0f0dcc3SMatthew G. Knepley    Level: developer
9343c0f0dcc3SMatthew G. Knepley 
9344c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9345c0f0dcc3SMatthew G. Knepley @*/
9346c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9347c0f0dcc3SMatthew G. Knepley {
9348c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9349c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9350c0f0dcc3SMatthew G. Knepley 
9351c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9352c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9353c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9354c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9355c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9356c0f0dcc3SMatthew G. Knepley   }
9357c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9358c0f0dcc3SMatthew G. Knepley }
9359