xref: /petsc/src/dm/interface/dm.c (revision a256111f674ccedc7ac1f921a377256b7c64af48)
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;
1331de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1341de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   if (dm->ops->clone) {
13638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13738221697SMatthew G. Knepley   }
1383f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
143be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
144be4c1c3eSMatthew G. Knepley     DM           ncdm;
145be4c1c3eSMatthew G. Knepley     PetscSection cs;
1465a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
147be4c1c3eSMatthew G. Knepley 
14892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
149be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1505a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1515a0206caSToby Isaac     if (pEndMax >= 0) {
152be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
15383bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
15492fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
155a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
156be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
157be4c1c3eSMatthew G. Knepley     }
158be4c1c3eSMatthew G. Knepley   }
159a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
160a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
16138221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16238221697SMatthew G. Knepley   if (coords) {
16338221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
16438221697SMatthew G. Knepley   } else {
16538221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16638221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16738221697SMatthew G. Knepley   }
16890b157c4SStefano Zampini   {
16990b157c4SStefano Zampini     PetscBool             isper;
170c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1715dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17290b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17390b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
174c6b900c6SMatthew G. Knepley   }
17534aa8a36SMatthew G. Knepley   {
17634aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17734aa8a36SMatthew G. Knepley 
17834aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
18034aa8a36SMatthew G. Knepley   }
18138221697SMatthew G. Knepley   PetscFunctionReturn(0);
18238221697SMatthew G. Knepley }
18338221697SMatthew G. Knepley 
1849a42bb27SBarry Smith /*@C
185564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1869a42bb27SBarry Smith 
187d083f849SBarry Smith    Logically Collective on da
1889a42bb27SBarry Smith 
1899a42bb27SBarry Smith    Input Parameter:
1909a42bb27SBarry Smith +  da - initial distributed array
191e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1929a42bb27SBarry Smith 
1939a42bb27SBarry Smith    Options Database:
194dd85299cSBarry Smith .   -dm_vec_type ctype
1959a42bb27SBarry Smith 
1969a42bb27SBarry Smith    Level: intermediate
1979a42bb27SBarry Smith 
198a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1999a42bb27SBarry Smith @*/
20019fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2019a42bb27SBarry Smith {
2029a42bb27SBarry Smith   PetscErrorCode ierr;
2039a42bb27SBarry Smith 
2049a42bb27SBarry Smith   PetscFunctionBegin;
2059a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2069a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2089a42bb27SBarry Smith   PetscFunctionReturn(0);
2099a42bb27SBarry Smith }
2109a42bb27SBarry Smith 
211c0dedaeaSBarry Smith /*@C
212c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
213c0dedaeaSBarry Smith 
214d083f849SBarry Smith    Logically Collective on da
215c0dedaeaSBarry Smith 
216c0dedaeaSBarry Smith    Input Parameter:
217c0dedaeaSBarry Smith .  da - initial distributed array
218c0dedaeaSBarry Smith 
219c0dedaeaSBarry Smith    Output Parameter:
220c0dedaeaSBarry Smith .  ctype - the vector type
221c0dedaeaSBarry Smith 
222c0dedaeaSBarry Smith    Level: intermediate
223c0dedaeaSBarry Smith 
224a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
225c0dedaeaSBarry Smith @*/
226c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
227c0dedaeaSBarry Smith {
228c0dedaeaSBarry Smith   PetscFunctionBegin;
229c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
230c0dedaeaSBarry Smith   *ctype = da->vectype;
231c0dedaeaSBarry Smith   PetscFunctionReturn(0);
232c0dedaeaSBarry Smith }
233c0dedaeaSBarry Smith 
2345f1ad066SMatthew G Knepley /*@
23534f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley   Not collective
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley   Input Parameter:
2405f1ad066SMatthew G Knepley . v - The Vec
2415f1ad066SMatthew G Knepley 
2425f1ad066SMatthew G Knepley   Output Parameter:
2435f1ad066SMatthew G Knepley . dm - The DM
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley   Level: intermediate
2465f1ad066SMatthew G Knepley 
2475f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2485f1ad066SMatthew G Knepley @*/
2495f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2505f1ad066SMatthew G Knepley {
2515f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley   PetscFunctionBegin;
2545f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2555f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2565f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2575f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2585f1ad066SMatthew G Knepley }
2595f1ad066SMatthew G Knepley 
2605f1ad066SMatthew G Knepley /*@
261d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2625f1ad066SMatthew G Knepley 
2635f1ad066SMatthew G Knepley   Not collective
2645f1ad066SMatthew G Knepley 
2655f1ad066SMatthew G Knepley   Input Parameters:
2665f1ad066SMatthew G Knepley + v - The Vec
2675f1ad066SMatthew G Knepley - dm - The DM
2685f1ad066SMatthew G Knepley 
269d9805387SMatthew 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.
270d9805387SMatthew G. Knepley 
2715f1ad066SMatthew G Knepley   Level: intermediate
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2745f1ad066SMatthew G Knepley @*/
2755f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2765f1ad066SMatthew G Knepley {
2775f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2785f1ad066SMatthew G Knepley 
2795f1ad066SMatthew G Knepley   PetscFunctionBegin;
2805f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
281d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2825f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2835f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2845f1ad066SMatthew G Knepley }
2855f1ad066SMatthew G Knepley 
286521d9a4cSLisandro Dalcin /*@C
2878f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2888f1509bcSBarry Smith 
289d083f849SBarry Smith    Logically Collective on dm
2908f1509bcSBarry Smith 
2918f1509bcSBarry Smith    Input Parameters:
2928f1509bcSBarry Smith +  dm - the DM context
2938f1509bcSBarry Smith -  ctype - the matrix type
2948f1509bcSBarry Smith 
2958f1509bcSBarry Smith    Options Database:
2968f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2978f1509bcSBarry Smith 
2988f1509bcSBarry Smith    Level: intermediate
2998f1509bcSBarry Smith 
3008f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3018f1509bcSBarry Smith           DMGetISColoringType()
3028f1509bcSBarry Smith @*/
3038f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3048f1509bcSBarry Smith {
3058f1509bcSBarry Smith   PetscFunctionBegin;
3068f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3078f1509bcSBarry Smith   dm->coloringtype = ctype;
3088f1509bcSBarry Smith   PetscFunctionReturn(0);
3098f1509bcSBarry Smith }
3108f1509bcSBarry Smith 
3118f1509bcSBarry Smith /*@C
3128f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
313521d9a4cSLisandro Dalcin 
314d083f849SBarry Smith    Logically Collective on dm
315521d9a4cSLisandro Dalcin 
316521d9a4cSLisandro Dalcin    Input Parameter:
3178f1509bcSBarry Smith .  dm - the DM context
3188f1509bcSBarry Smith 
3198f1509bcSBarry Smith    Output Parameter:
3208f1509bcSBarry Smith .  ctype - the matrix type
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith    Options Database:
3238f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith    Level: intermediate
3268f1509bcSBarry Smith 
3278f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3288f1509bcSBarry Smith           DMGetISColoringType()
3298f1509bcSBarry Smith @*/
3308f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3318f1509bcSBarry Smith {
3328f1509bcSBarry Smith   PetscFunctionBegin;
3338f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3348f1509bcSBarry Smith   *ctype = dm->coloringtype;
3358f1509bcSBarry Smith   PetscFunctionReturn(0);
3368f1509bcSBarry Smith }
3378f1509bcSBarry Smith 
3388f1509bcSBarry Smith /*@C
3398f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3408f1509bcSBarry Smith 
341d083f849SBarry Smith    Logically Collective on dm
3428f1509bcSBarry Smith 
3438f1509bcSBarry Smith    Input Parameters:
344521d9a4cSLisandro Dalcin +  dm - the DM context
345a2b5a043SBarry Smith -  ctype - the matrix type
346521d9a4cSLisandro Dalcin 
347521d9a4cSLisandro Dalcin    Options Database:
348521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
349521d9a4cSLisandro Dalcin 
350521d9a4cSLisandro Dalcin    Level: intermediate
351521d9a4cSLisandro Dalcin 
352a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
353521d9a4cSLisandro Dalcin @*/
35419fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
355521d9a4cSLisandro Dalcin {
356521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35788f0584fSBarry Smith 
358521d9a4cSLisandro Dalcin   PetscFunctionBegin;
359521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
360521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
36119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
362521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
363521d9a4cSLisandro Dalcin }
364521d9a4cSLisandro Dalcin 
365c0dedaeaSBarry Smith /*@C
366c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
367c0dedaeaSBarry Smith 
368d083f849SBarry Smith    Logically Collective on dm
369c0dedaeaSBarry Smith 
370c0dedaeaSBarry Smith    Input Parameter:
371c0dedaeaSBarry Smith .  dm - the DM context
372c0dedaeaSBarry Smith 
373c0dedaeaSBarry Smith    Output Parameter:
374c0dedaeaSBarry Smith .  ctype - the matrix type
375c0dedaeaSBarry Smith 
376c0dedaeaSBarry Smith    Options Database:
377c0dedaeaSBarry Smith .   -dm_mat_type ctype
378c0dedaeaSBarry Smith 
379c0dedaeaSBarry Smith    Level: intermediate
380c0dedaeaSBarry Smith 
381a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
382c0dedaeaSBarry Smith @*/
383c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
384c0dedaeaSBarry Smith {
385c0dedaeaSBarry Smith   PetscFunctionBegin;
386c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
387c0dedaeaSBarry Smith   *ctype = dm->mattype;
388c0dedaeaSBarry Smith   PetscFunctionReturn(0);
389c0dedaeaSBarry Smith }
390c0dedaeaSBarry Smith 
391c688c046SMatthew G Knepley /*@
39234f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
393c688c046SMatthew G Knepley 
394c688c046SMatthew G Knepley   Not collective
395c688c046SMatthew G Knepley 
396c688c046SMatthew G Knepley   Input Parameter:
397c688c046SMatthew G Knepley . A - The Mat
398c688c046SMatthew G Knepley 
399c688c046SMatthew G Knepley   Output Parameter:
400c688c046SMatthew G Knepley . dm - The DM
401c688c046SMatthew G Knepley 
402c688c046SMatthew G Knepley   Level: intermediate
403c688c046SMatthew G Knepley 
4048f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4058f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4068f1509bcSBarry Smith 
407c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
408c688c046SMatthew G Knepley @*/
409c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
410c688c046SMatthew G Knepley {
411c688c046SMatthew G Knepley   PetscErrorCode ierr;
412c688c046SMatthew G Knepley 
413c688c046SMatthew G Knepley   PetscFunctionBegin;
414c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
415c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
416c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
417c688c046SMatthew G Knepley   PetscFunctionReturn(0);
418c688c046SMatthew G Knepley }
419c688c046SMatthew G Knepley 
420c688c046SMatthew G Knepley /*@
421c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   Not collective
424c688c046SMatthew G Knepley 
425c688c046SMatthew G Knepley   Input Parameters:
426c688c046SMatthew G Knepley + A - The Mat
427c688c046SMatthew G Knepley - dm - The DM
428c688c046SMatthew G Knepley 
429c688c046SMatthew G Knepley   Level: intermediate
430c688c046SMatthew G Knepley 
4318f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4328f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4338f1509bcSBarry Smith 
4348f1509bcSBarry Smith 
435c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
436c688c046SMatthew G Knepley @*/
437c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
438c688c046SMatthew G Knepley {
439c688c046SMatthew G Knepley   PetscErrorCode ierr;
440c688c046SMatthew G Knepley 
441c688c046SMatthew G Knepley   PetscFunctionBegin;
442c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4438865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
444c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
445c688c046SMatthew G Knepley   PetscFunctionReturn(0);
446c688c046SMatthew G Knepley }
447c688c046SMatthew G Knepley 
4489a42bb27SBarry Smith /*@C
4499a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4506757b960SDave May    DM options in the database.
4519a42bb27SBarry Smith 
452d083f849SBarry Smith    Logically Collective on dm
4539a42bb27SBarry Smith 
4549a42bb27SBarry Smith    Input Parameter:
4558353ddbbSDave May +  da - the DM context
4569a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4579a42bb27SBarry Smith 
4589a42bb27SBarry Smith    Notes:
4599a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4609a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4619a42bb27SBarry Smith 
4629a42bb27SBarry Smith    Level: advanced
4639a42bb27SBarry Smith 
4649a42bb27SBarry Smith .seealso: DMSetFromOptions()
4659a42bb27SBarry Smith @*/
4667087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4679a42bb27SBarry Smith {
4689a42bb27SBarry Smith   PetscErrorCode ierr;
4699a42bb27SBarry Smith 
4709a42bb27SBarry Smith   PetscFunctionBegin;
4719a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4729a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
473691be533SLawrence Mitchell   if (dm->sf) {
474691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
475691be533SLawrence Mitchell   }
4761bb6d2a8SBarry Smith   if (dm->sectionSF) {
4771bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
478691be533SLawrence Mitchell   }
4799a42bb27SBarry Smith   PetscFunctionReturn(0);
4809a42bb27SBarry Smith }
4819a42bb27SBarry Smith 
48231697293SDave May /*@C
48331697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48431697293SDave May    DM options in the database.
48531697293SDave May 
486d083f849SBarry Smith    Logically Collective on dm
48731697293SDave May 
48831697293SDave May    Input Parameters:
48931697293SDave May +  dm - the DM context
49031697293SDave May -  prefix - the prefix string to prepend to all DM option requests
49131697293SDave May 
49231697293SDave May    Notes:
49331697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49431697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49531697293SDave May 
49631697293SDave May    Level: advanced
49731697293SDave May 
49831697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49931697293SDave May @*/
50031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
50131697293SDave May {
50231697293SDave May   PetscErrorCode ierr;
50331697293SDave May 
50431697293SDave May   PetscFunctionBegin;
50531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50631697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50731697293SDave May   PetscFunctionReturn(0);
50831697293SDave May }
50931697293SDave May 
51031697293SDave May /*@C
51131697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
51231697293SDave May    DM options in the database.
51331697293SDave May 
51431697293SDave May    Not Collective
51531697293SDave May 
51631697293SDave May    Input Parameters:
51731697293SDave May .  dm - the DM context
51831697293SDave May 
51931697293SDave May    Output Parameters:
52031697293SDave May .  prefix - pointer to the prefix string used is returned
52131697293SDave May 
52295452b02SPatrick Sanan    Notes:
52395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52431697293SDave May    sufficient length to hold the prefix.
52531697293SDave May 
52631697293SDave May    Level: advanced
52731697293SDave May 
52831697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52931697293SDave May @*/
53031697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
53131697293SDave May {
53231697293SDave May   PetscErrorCode ierr;
53331697293SDave May 
53431697293SDave May   PetscFunctionBegin;
53531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53631697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53731697293SDave May   PetscFunctionReturn(0);
53831697293SDave May }
53931697293SDave May 
54088bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
54188bdff64SToby Isaac {
5426eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
54388bdff64SToby Isaac   PetscErrorCode ierr;
54488bdff64SToby Isaac 
54588bdff64SToby Isaac   PetscFunctionBegin;
546aab5bcd8SJed Brown   *ncrefct = 0;
54788bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54888bdff64SToby Isaac     refct--;
54988bdff64SToby Isaac     if (recurseCoarse) {
55088bdff64SToby Isaac       PetscInt coarseCount;
55188bdff64SToby Isaac 
55288bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55388bdff64SToby Isaac       refct += coarseCount;
55488bdff64SToby Isaac     }
55588bdff64SToby Isaac   }
55688bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55788bdff64SToby Isaac     refct--;
55888bdff64SToby Isaac     if (recurseFine) {
55988bdff64SToby Isaac       PetscInt fineCount;
56088bdff64SToby Isaac 
56188bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56288bdff64SToby Isaac       refct += fineCount;
56388bdff64SToby Isaac     }
56488bdff64SToby Isaac   }
56588bdff64SToby Isaac   *ncrefct = refct;
56688bdff64SToby Isaac   PetscFunctionReturn(0);
56788bdff64SToby Isaac }
56888bdff64SToby Isaac 
569f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
570354557abSToby Isaac {
5715d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
572354557abSToby Isaac   PetscErrorCode ierr;
573354557abSToby Isaac 
574354557abSToby Isaac   PetscFunctionBegin;
575354557abSToby Isaac   /* destroy the labels */
576354557abSToby Isaac   while (next) {
577354557abSToby Isaac     DMLabelLink tmp = next->next;
578354557abSToby Isaac 
5795d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
580ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
581354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
582354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
583354557abSToby Isaac     next = tmp;
584354557abSToby Isaac   }
5855d80c0bfSVaclav Hapla   dm->labels = NULL;
586354557abSToby Isaac   PetscFunctionReturn(0);
587354557abSToby Isaac }
588354557abSToby Isaac 
58947c6ae99SBarry Smith /*@
5908472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59147c6ae99SBarry Smith 
592d083f849SBarry Smith     Collective on dm
59347c6ae99SBarry Smith 
59447c6ae99SBarry Smith     Input Parameter:
59547c6ae99SBarry Smith .   dm - the DM object to destroy
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith     Level: developer
59847c6ae99SBarry Smith 
599e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60047c6ae99SBarry Smith 
60147c6ae99SBarry Smith @*/
602fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60347c6ae99SBarry Smith {
6046eb26441SStefano Zampini   PetscInt       cnt;
605dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60647c6ae99SBarry Smith   PetscErrorCode ierr;
60747c6ae99SBarry Smith 
60847c6ae99SBarry Smith   PetscFunctionBegin;
6096bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6106bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61187e657c6SBarry Smith 
61288bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61388bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61488bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
615ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6166bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6176bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6186eb26441SStefano Zampini 
6196eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6206eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6216eb26441SStefano Zampini 
622f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6230298fd71SBarry Smith   (*dm)->namedglobal = NULL;
624f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6252348bcf4SPeter Brune     nnext = nlink->next;
6262348bcf4SPeter 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);
6272348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6282348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6292348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6302348bcf4SPeter Brune   }
631f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6320298fd71SBarry Smith   (*dm)->namedlocal = NULL;
633f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
634f490541aSPeter Brune     nnext = nlink->next;
635f490541aSPeter 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);
636f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
637f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
638f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
639f490541aSPeter Brune   }
6402348bcf4SPeter Brune 
641b17ce1afSJed Brown   /* Destroy the list of hooks */
642c833c3b5SJed Brown   {
643c833c3b5SJed Brown     DMCoarsenHookLink link,next;
644b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
645b17ce1afSJed Brown       next = link->next;
646b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
647b17ce1afSJed Brown     }
6480298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
649c833c3b5SJed Brown   }
650c833c3b5SJed Brown   {
651c833c3b5SJed Brown     DMRefineHookLink link,next;
652c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
653c833c3b5SJed Brown       next = link->next;
654c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
655c833c3b5SJed Brown     }
6560298fd71SBarry Smith     (*dm)->refinehook = NULL;
657c833c3b5SJed Brown   }
658be081cd6SPeter Brune   {
659be081cd6SPeter Brune     DMSubDomainHookLink link,next;
660be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
661be081cd6SPeter Brune       next = link->next;
662be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
663be081cd6SPeter Brune     }
6640298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
665be081cd6SPeter Brune   }
666baf369e7SPeter Brune   {
667baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
668baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
669baf369e7SPeter Brune       next = link->next;
670baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
671baf369e7SPeter Brune     }
6720298fd71SBarry Smith     (*dm)->gtolhook = NULL;
673baf369e7SPeter Brune   }
674d4d07f1eSToby Isaac   {
675d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
676d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
677d4d07f1eSToby Isaac       next = link->next;
678d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
679d4d07f1eSToby Isaac     }
680d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
681d4d07f1eSToby Isaac   }
682aa1993deSMatthew G Knepley   /* Destroy the work arrays */
683aa1993deSMatthew G Knepley   {
684aa1993deSMatthew G Knepley     DMWorkLink link,next;
685aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
686aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
687aa1993deSMatthew G Knepley       next = link->next;
688aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
689aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
690aa1993deSMatthew G Knepley     }
6910298fd71SBarry Smith     (*dm)->workin = NULL;
692aa1993deSMatthew G Knepley   }
693c58f1c22SToby Isaac   /* destroy the labels */
694f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
695f4cdcedcSVaclav Hapla   /* destroy the fields */
696e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
697f4cdcedcSVaclav Hapla   /* destroy the boundaries */
698e6f8dbb6SToby Isaac   {
699e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
700e6f8dbb6SToby Isaac     while (next) {
701e6f8dbb6SToby Isaac       DMBoundary b = next;
702e6f8dbb6SToby Isaac 
703e6f8dbb6SToby Isaac       next = b->next;
704e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
705e6f8dbb6SToby Isaac     }
706e6f8dbb6SToby Isaac   }
707b17ce1afSJed Brown 
70852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
70952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
71052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
71152536dc3SBarry Smith 
7121a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7131a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7141a266240SBarry Smith   }
71571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7166bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7176bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
718073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
71988ed4aceSMatthew G Knepley 
7201bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7211bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7228b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
723fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
724fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
72588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7261bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
727736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
728736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7298e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
730736995cdSBlaise Bourdin     }
731736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
732736995cdSBlaise Bourdin   }
73388bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
73488bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
73588bdff64SToby Isaac   }
7366eb26441SStefano Zampini 
737a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
73888bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
73988bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
74088bdff64SToby Isaac   }
74188bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
742f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7436636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7446636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7456636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
746412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
747412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
748412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
749ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
750ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
751ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7526636e97aSMatthew G Knepley 
753e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
75414f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
755e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
756e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
757732e2eb9SMatthew G Knepley 
758ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7596bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
760ed3c66a1SDave May   }
761c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
762435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
763732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
76447c6ae99SBarry Smith   PetscFunctionReturn(0);
76547c6ae99SBarry Smith }
76647c6ae99SBarry Smith 
767d7bf68aeSBarry Smith /*@
768d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
769d7bf68aeSBarry Smith 
770d083f849SBarry Smith     Collective on dm
771d7bf68aeSBarry Smith 
772d7bf68aeSBarry Smith     Input Parameter:
773d7bf68aeSBarry Smith .   dm - the DM object to setup
774d7bf68aeSBarry Smith 
775d7bf68aeSBarry Smith     Level: developer
776d7bf68aeSBarry Smith 
777e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
778d7bf68aeSBarry Smith 
779d7bf68aeSBarry Smith @*/
7807087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
781d7bf68aeSBarry Smith {
782d7bf68aeSBarry Smith   PetscErrorCode ierr;
783d7bf68aeSBarry Smith 
784d7bf68aeSBarry Smith   PetscFunctionBegin;
785171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7868387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
787d7bf68aeSBarry Smith   if (dm->ops->setup) {
788d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
789d7bf68aeSBarry Smith   }
7908387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
791d7bf68aeSBarry Smith   PetscFunctionReturn(0);
792d7bf68aeSBarry Smith }
793d7bf68aeSBarry Smith 
794d7bf68aeSBarry Smith /*@
795d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
796d7bf68aeSBarry Smith 
797d083f849SBarry Smith     Collective on dm
798d7bf68aeSBarry Smith 
799d7bf68aeSBarry Smith     Input Parameter:
800d7bf68aeSBarry Smith .   dm - the DM object to set options for
801d7bf68aeSBarry Smith 
802732e2eb9SMatthew G Knepley     Options Database:
8034164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8044164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8054164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8068f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
807732e2eb9SMatthew G Knepley 
808384a6580SVaclav Hapla     DMPLEX Specific Checks
809384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
810384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
811384a6580SVaclav 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()
812384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
813384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
814384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
815384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
816d7bf68aeSBarry Smith 
81795eb5ee5SVaclav Hapla     Level: intermediate
81895eb5ee5SVaclav Hapla 
81995eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
82095eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
821d7bf68aeSBarry Smith 
822d7bf68aeSBarry Smith @*/
8237087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
824d7bf68aeSBarry Smith {
8257781c08eSBarry Smith   char           typeName[256];
826ca266f36SBarry Smith   PetscBool      flg;
827d7bf68aeSBarry Smith   PetscErrorCode ierr;
828d7bf68aeSBarry Smith 
829d7bf68aeSBarry Smith   PetscFunctionBegin;
830171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83149be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
83249be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8331bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8343194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8350298fd71SBarry 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);
836a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
837f9ba7244SBarry Smith   if (flg) {
838f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
839f9ba7244SBarry Smith   }
840a264d7a6SBarry 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);
841073dac72SJed Brown   if (flg) {
842521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
843073dac72SJed Brown   }
8448f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
845f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
846e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
847f9ba7244SBarry Smith   }
848f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8490633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
85082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
851d7bf68aeSBarry Smith   PetscFunctionReturn(0);
852d7bf68aeSBarry Smith }
853d7bf68aeSBarry Smith 
854fc9bc008SSatish Balay /*@C
855fe2efc57SMark    DMViewFromOptions - View from Options
856fe2efc57SMark 
857fe2efc57SMark    Collective on DM
858fe2efc57SMark 
859fe2efc57SMark    Input Parameters:
860fe2efc57SMark +  dm - the DM object
861736c3998SJose E. Roman .  obj - Optional object
862736c3998SJose E. Roman -  name - command line option
863fe2efc57SMark 
864fe2efc57SMark    Level: intermediate
865fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
866fe2efc57SMark @*/
867fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
868fe2efc57SMark {
869fe2efc57SMark   PetscErrorCode ierr;
870fe2efc57SMark 
871fe2efc57SMark   PetscFunctionBegin;
872fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
873fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
874fe2efc57SMark   PetscFunctionReturn(0);
875fe2efc57SMark }
876fe2efc57SMark 
877fe2efc57SMark /*@C
878224748a4SBarry Smith     DMView - Views a DM
87947c6ae99SBarry Smith 
880d083f849SBarry Smith     Collective on dm
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith     Input Parameter:
88347c6ae99SBarry Smith +   dm - the DM object to view
88447c6ae99SBarry Smith -   v - the viewer
88547c6ae99SBarry Smith 
886224748a4SBarry Smith     Level: beginner
88747c6ae99SBarry Smith 
888e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith @*/
8917087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
89247c6ae99SBarry Smith {
89347c6ae99SBarry Smith   PetscErrorCode    ierr;
89432c0f0efSBarry Smith   PetscBool         isbinary;
89576a8abe0SBarry Smith   PetscMPIInt       size;
89676a8abe0SBarry Smith   PetscViewerFormat format;
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith   PetscFunctionBegin;
899171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9003014e516SBarry Smith   if (!v) {
901ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9023014e516SBarry Smith   }
903b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
90474903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
90574903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
90674903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
90774903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
90874903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
90974903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
91074903a4fSStefano Zampini      in an error here */
91174903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9128bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
913b1b135c8SBarry Smith 
91476a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
91576a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
91676a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
91798c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
91832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
91932c0f0efSBarry Smith   if (isbinary) {
92055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
92132c0f0efSBarry Smith     char     type[256];
92232c0f0efSBarry Smith 
923f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
92432c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
925f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
92632c0f0efSBarry Smith   }
9270c010503SBarry Smith   if (dm->ops->view) {
9280c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
92947c6ae99SBarry Smith   }
93047c6ae99SBarry Smith   PetscFunctionReturn(0);
93147c6ae99SBarry Smith }
93247c6ae99SBarry Smith 
93347c6ae99SBarry Smith /*@
9348472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
93547c6ae99SBarry Smith 
936d083f849SBarry Smith     Collective on dm
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith     Input Parameter:
93947c6ae99SBarry Smith .   dm - the DM object
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith     Output Parameter:
94247c6ae99SBarry Smith .   vec - the global vector
94347c6ae99SBarry Smith 
944073dac72SJed Brown     Level: beginner
94547c6ae99SBarry Smith 
946ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith @*/
9497087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
95047c6ae99SBarry Smith {
95147c6ae99SBarry Smith   PetscErrorCode ierr;
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith   PetscFunctionBegin;
954171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
955b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
956b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
95747c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
95876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
959c6b011d8SStefano Zampini     DM vdm;
960c6b011d8SStefano Zampini 
961c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
962c6b011d8SStefano 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);
963c6b011d8SStefano Zampini   }
96447c6ae99SBarry Smith   PetscFunctionReturn(0);
96547c6ae99SBarry Smith }
96647c6ae99SBarry Smith 
96747c6ae99SBarry Smith /*@
9688472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith     Not Collective
97147c6ae99SBarry Smith 
97247c6ae99SBarry Smith     Input Parameter:
97347c6ae99SBarry Smith .   dm - the DM object
97447c6ae99SBarry Smith 
97547c6ae99SBarry Smith     Output Parameter:
97647c6ae99SBarry Smith .   vec - the local vector
97747c6ae99SBarry Smith 
978073dac72SJed Brown     Level: beginner
97947c6ae99SBarry Smith 
980ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
98147c6ae99SBarry Smith 
98247c6ae99SBarry Smith @*/
9837087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
98447c6ae99SBarry Smith {
98547c6ae99SBarry Smith   PetscErrorCode ierr;
98647c6ae99SBarry Smith 
98747c6ae99SBarry Smith   PetscFunctionBegin;
988171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
989b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
990b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
99147c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
99276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
993c6b011d8SStefano Zampini     DM vdm;
994c6b011d8SStefano Zampini 
995c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
996c6b011d8SStefano 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);
997c6b011d8SStefano Zampini   }
99847c6ae99SBarry Smith   PetscFunctionReturn(0);
99947c6ae99SBarry Smith }
100047c6ae99SBarry Smith 
10011411c6eeSJed Brown /*@
10021411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10031411c6eeSJed Brown 
1004d083f849SBarry Smith    Collective on dm
10051411c6eeSJed Brown 
10061411c6eeSJed Brown    Input Parameter:
10071411c6eeSJed Brown .  dm - the DM that provides the mapping
10081411c6eeSJed Brown 
10091411c6eeSJed Brown    Output Parameter:
10101411c6eeSJed Brown .  ltog - the mapping
10111411c6eeSJed Brown 
10121411c6eeSJed Brown    Level: intermediate
10131411c6eeSJed Brown 
10141411c6eeSJed Brown    Notes:
10151411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10161411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10171411c6eeSJed Brown 
1018fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10191411c6eeSJed Brown @*/
10207087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10211411c6eeSJed Brown {
10220be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10231411c6eeSJed Brown   PetscErrorCode ierr;
10241411c6eeSJed Brown 
10251411c6eeSJed Brown   PetscFunctionBegin;
10261411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10271411c6eeSJed Brown   PetscValidPointer(ltog,2);
10281411c6eeSJed Brown   if (!dm->ltogmap) {
102937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
103037d0c07bSMatthew G Knepley 
103192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
103237d0c07bSMatthew G Knepley     if (section) {
1033a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
103437d0c07bSMatthew G Knepley       PetscInt       *ltog;
1035ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
103637d0c07bSMatthew G Knepley 
1037e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1039ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1040ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
104137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1042a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
104337d0c07bSMatthew G Knepley 
104437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
104537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1046a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1047a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
104837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10491a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10501a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10511a7dc684SMatthew G. Knepley         if (dof) {
1052b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1053b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10541a7dc684SMatthew G. Knepley         }
105537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1056a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1057a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
105837d0c07bSMatthew G Knepley         }
105937d0c07bSMatthew G Knepley       }
1060bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10610be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10620be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10630be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10640be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10657591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10667591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1067ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1068ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1069ccf3bd66SMatthew G. Knepley         n /= bs;
1070ccf3bd66SMatthew G. Knepley       }
1071ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10723bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
107337d0c07bSMatthew G Knepley     } else {
1074b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1075184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10761411c6eeSJed Brown     }
107737d0c07bSMatthew G Knepley   }
10781411c6eeSJed Brown   *ltog = dm->ltogmap;
10791411c6eeSJed Brown   PetscFunctionReturn(0);
10801411c6eeSJed Brown }
10811411c6eeSJed Brown 
10821411c6eeSJed Brown /*@
10831411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10841411c6eeSJed Brown 
10851411c6eeSJed Brown    Not Collective
10861411c6eeSJed Brown 
10871411c6eeSJed Brown    Input Parameter:
10881411c6eeSJed Brown .  dm - the DM with block structure
10891411c6eeSJed Brown 
10901411c6eeSJed Brown    Output Parameter:
10911411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10921411c6eeSJed Brown 
10931411c6eeSJed Brown    Level: intermediate
10941411c6eeSJed Brown 
1095fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10961411c6eeSJed Brown @*/
10977087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10981411c6eeSJed Brown {
10991411c6eeSJed Brown   PetscFunctionBegin;
11001411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1101534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11021411c6eeSJed 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");
11031411c6eeSJed Brown   *bs = dm->bs;
11041411c6eeSJed Brown   PetscFunctionReturn(0);
11051411c6eeSJed Brown }
11061411c6eeSJed Brown 
110747c6ae99SBarry Smith /*@
11088472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
110947c6ae99SBarry Smith 
1110a5bc1bf3SBarry Smith     Collective on dmc
111147c6ae99SBarry Smith 
111247c6ae99SBarry Smith     Input Parameter:
1113a5bc1bf3SBarry Smith +   dmc - the DM object
1114a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
111547c6ae99SBarry Smith 
111647c6ae99SBarry Smith     Output Parameter:
111747c6ae99SBarry Smith +  mat - the interpolation
111847c6ae99SBarry Smith -  vec - the scaling (optional)
111947c6ae99SBarry Smith 
112047c6ae99SBarry Smith     Level: developer
112147c6ae99SBarry Smith 
112295452b02SPatrick Sanan     Notes:
112395452b02SPatrick 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
112485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1125d52bd9f3SBarry Smith 
11261f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1127d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
112885afcc9aSBarry Smith 
112985afcc9aSBarry Smith 
11302ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
113147c6ae99SBarry Smith 
113247c6ae99SBarry Smith @*/
1133a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
113447c6ae99SBarry Smith {
113547c6ae99SBarry Smith   PetscErrorCode ierr;
113647c6ae99SBarry Smith 
113747c6ae99SBarry Smith   PetscFunctionBegin;
1138a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1139a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1140c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1141a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1142a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1143a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1144a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
114547c6ae99SBarry Smith   PetscFunctionReturn(0);
114647c6ae99SBarry Smith }
114747c6ae99SBarry Smith 
11483ad4599aSBarry Smith /*@
1149d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11502ed6491fSPatrick Sanan 
11512ed6491fSPatrick Sanan   Input Parameters:
11522ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11532ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11542ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11552ed6491fSPatrick Sanan 
11562ed6491fSPatrick Sanan   Output Parameter:
11572ed6491fSPatrick Sanan .    scale - the scaled vector
11582ed6491fSPatrick Sanan 
11592ed6491fSPatrick Sanan   Level: developer
11602ed6491fSPatrick Sanan 
11612ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
11622ed6491fSPatrick Sanan 
11632ed6491fSPatrick Sanan @*/
11642ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11652ed6491fSPatrick Sanan {
11662ed6491fSPatrick Sanan   PetscErrorCode ierr;
11672ed6491fSPatrick Sanan   Vec            fine;
11682ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11692ed6491fSPatrick Sanan 
11702ed6491fSPatrick Sanan   PetscFunctionBegin;
11712ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
11722ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
11732ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
11742ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
11752ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
11762ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
11772ed6491fSPatrick Sanan   PetscFunctionReturn(0);
11782ed6491fSPatrick Sanan }
11792ed6491fSPatrick Sanan 
11802ed6491fSPatrick Sanan /*@
11813ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11823ad4599aSBarry Smith 
1183a5bc1bf3SBarry Smith     Collective on dmc
11843ad4599aSBarry Smith 
11853ad4599aSBarry Smith     Input Parameter:
1186a5bc1bf3SBarry Smith +   dmc - the DM object
1187a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
11883ad4599aSBarry Smith 
11893ad4599aSBarry Smith     Output Parameter:
11903ad4599aSBarry Smith .  mat - the restriction
11913ad4599aSBarry Smith 
11923ad4599aSBarry Smith 
11933ad4599aSBarry Smith     Level: developer
11943ad4599aSBarry Smith 
119595452b02SPatrick Sanan     Notes:
119695452b02SPatrick 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
11973ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11983ad4599aSBarry Smith 
11993ad4599aSBarry Smith 
12003ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12013ad4599aSBarry Smith 
12023ad4599aSBarry Smith @*/
1203a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12043ad4599aSBarry Smith {
12053ad4599aSBarry Smith   PetscErrorCode ierr;
12063ad4599aSBarry Smith 
12073ad4599aSBarry Smith   PetscFunctionBegin;
1208a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1209a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12105a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1211a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1212a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1213a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1214a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12153ad4599aSBarry Smith   PetscFunctionReturn(0);
12163ad4599aSBarry Smith }
12173ad4599aSBarry Smith 
121847c6ae99SBarry Smith /*@
12198472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
122047c6ae99SBarry Smith 
1221a5bc1bf3SBarry Smith     Collective on dac
122247c6ae99SBarry Smith 
122347c6ae99SBarry Smith     Input Parameter:
1224a5bc1bf3SBarry Smith +   dac - the DM object
1225a5bc1bf3SBarry Smith -   daf - the second, finer DM object
122647c6ae99SBarry Smith 
122747c6ae99SBarry Smith     Output Parameter:
12286dbf9973SLawrence Mitchell .   mat - the injection
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith     Level: developer
123147c6ae99SBarry Smith 
123295452b02SPatrick Sanan    Notes:
123395452b02SPatrick 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
123485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
123585afcc9aSBarry Smith 
1236e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
123747c6ae99SBarry Smith 
123847c6ae99SBarry Smith @*/
1239a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
124047c6ae99SBarry Smith {
124147c6ae99SBarry Smith   PetscErrorCode ierr;
124247c6ae99SBarry Smith 
124347c6ae99SBarry Smith   PetscFunctionBegin;
1244a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1245a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12465a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1247a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1248a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1249a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1250a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
125147c6ae99SBarry Smith   PetscFunctionReturn(0);
125247c6ae99SBarry Smith }
125347c6ae99SBarry Smith 
1254b412c318SBarry Smith /*@
1255bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1256bd041c0cSMatthew G. Knepley 
1257a5bc1bf3SBarry Smith   Collective on dac
1258bd041c0cSMatthew G. Knepley 
1259bd041c0cSMatthew G. Knepley   Input Parameter:
1260a5bc1bf3SBarry Smith + dac - the DM object
1261a5bc1bf3SBarry Smith - daf - the second, finer DM object
1262bd041c0cSMatthew G. Knepley 
1263bd041c0cSMatthew G. Knepley   Output Parameter:
1264bd041c0cSMatthew G. Knepley . mat - the interpolation
1265bd041c0cSMatthew G. Knepley 
1266bd041c0cSMatthew G. Knepley   Level: developer
1267bd041c0cSMatthew G. Knepley 
1268bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1269bd041c0cSMatthew G. Knepley @*/
1270a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1271bd041c0cSMatthew G. Knepley {
1272bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1273bd041c0cSMatthew G. Knepley 
1274bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1275a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1276a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
12775a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1278a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1279a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1280bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1281bd041c0cSMatthew G. Knepley }
1282bd041c0cSMatthew G. Knepley 
1283bd041c0cSMatthew G. Knepley /*@
1284b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
128547c6ae99SBarry Smith 
1286d083f849SBarry Smith     Collective on dm
128747c6ae99SBarry Smith 
128847c6ae99SBarry Smith     Input Parameter:
128947c6ae99SBarry Smith +   dm - the DM object
12905bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
129147c6ae99SBarry Smith 
129247c6ae99SBarry Smith     Output Parameter:
129347c6ae99SBarry Smith .   coloring - the coloring
129447c6ae99SBarry Smith 
1295ec5066bdSBarry Smith     Notes:
1296ec5066bdSBarry 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
1297ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1298ec5066bdSBarry Smith 
1299ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1300ec5066bdSBarry Smith 
130147c6ae99SBarry Smith     Level: developer
130247c6ae99SBarry Smith 
1303ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
130447c6ae99SBarry Smith 
1305aab9d709SJed Brown @*/
1306b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
130747c6ae99SBarry Smith {
130847c6ae99SBarry Smith   PetscErrorCode ierr;
130947c6ae99SBarry Smith 
131047c6ae99SBarry Smith   PetscFunctionBegin;
1311171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13125a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1313b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1314b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
131547c6ae99SBarry Smith   PetscFunctionReturn(0);
131647c6ae99SBarry Smith }
131747c6ae99SBarry Smith 
1318b412c318SBarry Smith /*@
13198472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
132047c6ae99SBarry Smith 
1321d083f849SBarry Smith     Collective on dm
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith     Input Parameter:
1324b412c318SBarry Smith .   dm - the DM object
132547c6ae99SBarry Smith 
132647c6ae99SBarry Smith     Output Parameter:
132747c6ae99SBarry Smith .   mat - the empty Jacobian
132847c6ae99SBarry Smith 
1329073dac72SJed Brown     Level: beginner
133047c6ae99SBarry Smith 
133195452b02SPatrick Sanan     Notes:
133295452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
133394013140SBarry Smith        do not need to do it yourself.
133494013140SBarry Smith 
133594013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13367889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
133794013140SBarry Smith 
133894013140SBarry 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
133994013140SBarry Smith        internally by PETSc.
134094013140SBarry Smith 
134194013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1342aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
134394013140SBarry Smith 
1344b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
134547c6ae99SBarry Smith 
1346aab9d709SJed Brown @*/
1347b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
134847c6ae99SBarry Smith {
134947c6ae99SBarry Smith   PetscErrorCode ierr;
135047c6ae99SBarry Smith 
135147c6ae99SBarry Smith   PetscFunctionBegin;
1352171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1353c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1354b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13555a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1356fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1357b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
135876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1359c6b011d8SStefano Zampini     DM mdm;
1360c6b011d8SStefano Zampini 
1361c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1362c6b011d8SStefano 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);
1363c6b011d8SStefano Zampini   }
1364e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1365e5e52638SMatthew G. Knepley   if (dm->Nf) {
1366e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1367649ef022SMatthew Knepley     PetscInt     Nf, f;
1368e571a35bSMatthew G. Knepley 
1369e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1370649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1371649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1372649ef022SMatthew Knepley         ierr = (*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1373e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1374e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1375649ef022SMatthew Knepley         break;
1376e571a35bSMatthew G. Knepley       }
1377649ef022SMatthew Knepley     }
1378649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1379649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1380649ef022SMatthew Knepley         ierr = (*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1381e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1382e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1383e571a35bSMatthew G. Knepley       }
1384e571a35bSMatthew G. Knepley     }
1385e571a35bSMatthew G. Knepley   }
1386fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
138747c6ae99SBarry Smith   PetscFunctionReturn(0);
138847c6ae99SBarry Smith }
138947c6ae99SBarry Smith 
1390732e2eb9SMatthew G Knepley /*@
1391950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1392732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1393732e2eb9SMatthew G Knepley 
1394d083f849SBarry Smith   Logically Collective on dm
1395732e2eb9SMatthew G Knepley 
1396732e2eb9SMatthew G Knepley   Input Parameter:
1397732e2eb9SMatthew G Knepley + dm - the DM
1398732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1399732e2eb9SMatthew G Knepley 
1400732e2eb9SMatthew G Knepley   Level: developer
1401b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1402732e2eb9SMatthew G Knepley @*/
1403732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1404732e2eb9SMatthew G Knepley {
1405732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1406732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1407732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1408732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1409732e2eb9SMatthew G Knepley }
1410732e2eb9SMatthew G Knepley 
1411b06ff27eSHong Zhang /*@
1412b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1413b06ff27eSHong Zhang     but the array for values will not be allocated.
1414b06ff27eSHong Zhang 
1415d083f849SBarry Smith   Logically Collective on dm
1416b06ff27eSHong Zhang 
1417b06ff27eSHong Zhang   Input Parameter:
1418b06ff27eSHong Zhang + dm - the DM
1419b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1420b06ff27eSHong Zhang 
1421b06ff27eSHong Zhang   Level: developer
1422b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1423b06ff27eSHong Zhang @*/
1424b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1425b06ff27eSHong Zhang {
1426b06ff27eSHong Zhang   PetscFunctionBegin;
1427b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1428b06ff27eSHong Zhang   dm->structure_only = only;
1429b06ff27eSHong Zhang   PetscFunctionReturn(0);
1430b06ff27eSHong Zhang }
1431b06ff27eSHong Zhang 
1432a89ea682SMatthew G Knepley /*@C
1433aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1434a89ea682SMatthew G Knepley 
1435a89ea682SMatthew G Knepley   Not Collective
1436a89ea682SMatthew G Knepley 
1437a89ea682SMatthew G Knepley   Input Parameters:
1438a89ea682SMatthew G Knepley + dm - the DM object
1439aa1993deSMatthew G Knepley . count - The minium size
144069291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1441a89ea682SMatthew G Knepley 
1442a89ea682SMatthew G Knepley   Output Parameter:
1443a89ea682SMatthew G Knepley . array - the work array
1444a89ea682SMatthew G Knepley 
1445a89ea682SMatthew G Knepley   Level: developer
1446a89ea682SMatthew G Knepley 
1447a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1448a89ea682SMatthew G Knepley @*/
144969291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1450a89ea682SMatthew G Knepley {
1451a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1452aa1993deSMatthew G Knepley   DMWorkLink     link;
145369291d52SBarry Smith   PetscMPIInt    dsize;
1454a89ea682SMatthew G Knepley 
1455a89ea682SMatthew G Knepley   PetscFunctionBegin;
1456a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1457aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1458aa1993deSMatthew G Knepley   if (dm->workin) {
1459aa1993deSMatthew G Knepley     link       = dm->workin;
1460aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1461aa1993deSMatthew G Knepley   } else {
1462b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1463a89ea682SMatthew G Knepley   }
146469291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14655056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1466aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1467854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1468854ce69bSBarry Smith     link->bytes = dsize*count;
1469aa1993deSMatthew G Knepley   }
1470aa1993deSMatthew G Knepley   link->next   = dm->workout;
1471aa1993deSMatthew G Knepley   dm->workout  = link;
147200d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
147300d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
147400d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
147500d952a4SJed Brown #endif
1476aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1477a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1478a89ea682SMatthew G Knepley }
1479a89ea682SMatthew G Knepley 
1480aa1993deSMatthew G Knepley /*@C
1481aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1482aa1993deSMatthew G Knepley 
1483aa1993deSMatthew G Knepley   Not Collective
1484aa1993deSMatthew G Knepley 
1485aa1993deSMatthew G Knepley   Input Parameters:
1486aa1993deSMatthew G Knepley + dm - the DM object
1487aa1993deSMatthew G Knepley . count - The minium size
148869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1489aa1993deSMatthew G Knepley 
1490aa1993deSMatthew G Knepley   Output Parameter:
1491aa1993deSMatthew G Knepley . array - the work array
1492aa1993deSMatthew G Knepley 
1493aa1993deSMatthew G Knepley   Level: developer
1494aa1993deSMatthew G Knepley 
149595452b02SPatrick Sanan   Developer Notes:
149695452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1497aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1498aa1993deSMatthew G Knepley @*/
149969291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1500aa1993deSMatthew G Knepley {
1501aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1502aa1993deSMatthew G Knepley 
1503aa1993deSMatthew G Knepley   PetscFunctionBegin;
1504aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1505aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1506aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1507aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1508aa1993deSMatthew G Knepley       *p           = link->next;
1509aa1993deSMatthew G Knepley       link->next   = dm->workin;
1510aa1993deSMatthew G Knepley       dm->workin   = link;
15110298fd71SBarry Smith       *(void**)mem = NULL;
1512aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1513aa1993deSMatthew G Knepley     }
1514aa1993deSMatthew G Knepley   }
1515aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1516aa1993deSMatthew G Knepley }
1517e7c4fc90SDmitry Karpeev 
15188cda7954SMatthew G. Knepley /*@C
15198cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
15208cda7954SMatthew G. Knepley 
15218cda7954SMatthew G. Knepley   Logically collective on DM
15228cda7954SMatthew G. Knepley 
15238cda7954SMatthew G. Knepley   Input Parameters:
15248cda7954SMatthew G. Knepley + dm     - The DM
15258cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15268cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
15278cda7954SMatthew G. Knepley 
15288cda7954SMatthew G. Knepley   Notes:
15298cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15308cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15318cda7954SMatthew G. Knepley $ dm        - The present DM
15328cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15338cda7954SMatthew G. Knepley $ field     - The field number in dm
15348cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15358cda7954SMatthew G. Knepley 
15368cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15378cda7954SMatthew G. Knepley 
15388cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15398cda7954SMatthew G. Knepley */
15408cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1541435a35e8SMatthew G Knepley {
1542435a35e8SMatthew G Knepley   PetscFunctionBegin;
1543435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
154482f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1545435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1546435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1547435a35e8SMatthew G Knepley }
1548435a35e8SMatthew G Knepley 
15498cda7954SMatthew G. Knepley /*@C
15508cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
15518cda7954SMatthew G. Knepley 
15528cda7954SMatthew G. Knepley   Not collective
15538cda7954SMatthew G. Knepley 
15548cda7954SMatthew G. Knepley   Input Parameters:
15558cda7954SMatthew G. Knepley + dm     - The DM
15568cda7954SMatthew G. Knepley - field  - The field number for the nullspace
15578cda7954SMatthew G. Knepley 
15588cda7954SMatthew G. Knepley   Output Parameter:
15598cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
15608cda7954SMatthew G. Knepley 
15618cda7954SMatthew G. Knepley   Notes:
15628cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15638cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15648cda7954SMatthew G. Knepley $ dm        - The present DM
15658cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15668cda7954SMatthew G. Knepley $ field     - The field number in dm
15678cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15688cda7954SMatthew G. Knepley 
15698cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15708cda7954SMatthew G. Knepley 
15718cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15728cda7954SMatthew G. Knepley */
15738cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
15740a50eb56SMatthew G. Knepley {
15750a50eb56SMatthew G. Knepley   PetscFunctionBegin;
15760a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1577f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
15780a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15790a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15800a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15810a50eb56SMatthew G. Knepley }
15820a50eb56SMatthew G. Knepley 
15838cda7954SMatthew G. Knepley /*@C
15848cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
15858cda7954SMatthew G. Knepley 
15868cda7954SMatthew G. Knepley   Logically collective on DM
15878cda7954SMatthew G. Knepley 
15888cda7954SMatthew G. Knepley   Input Parameters:
15898cda7954SMatthew G. Knepley + dm     - The DM
15908cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15918cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
15928cda7954SMatthew G. Knepley 
15938cda7954SMatthew G. Knepley   Notes:
15948cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15958cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15968cda7954SMatthew G. Knepley $ dm        - The present DM
15978cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15988cda7954SMatthew G. Knepley $ field     - The field number in dm
15998cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16008cda7954SMatthew G. Knepley 
16018cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16028cda7954SMatthew G. Knepley 
16038cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16048cda7954SMatthew G. Knepley */
16058cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1606f9d4088aSMatthew G. Knepley {
1607f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1608f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1609f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1610f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1611f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1612f9d4088aSMatthew G. Knepley }
1613f9d4088aSMatthew G. Knepley 
16148cda7954SMatthew G. Knepley /*@C
16158cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
16168cda7954SMatthew G. Knepley 
16178cda7954SMatthew G. Knepley   Not collective
16188cda7954SMatthew G. Knepley 
16198cda7954SMatthew G. Knepley   Input Parameters:
16208cda7954SMatthew G. Knepley + dm     - The DM
16218cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16228cda7954SMatthew G. Knepley 
16238cda7954SMatthew G. Knepley   Output Parameter:
16248cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
16258cda7954SMatthew G. Knepley 
16268cda7954SMatthew G. Knepley   Notes:
16278cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16288cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16298cda7954SMatthew G. Knepley $ dm        - The present DM
16308cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16318cda7954SMatthew G. Knepley $ field     - The field number in dm
16328cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16338cda7954SMatthew G. Knepley 
16348cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16358cda7954SMatthew G. Knepley 
16368cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16378cda7954SMatthew G. Knepley */
16388cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1639f9d4088aSMatthew G. Knepley {
1640f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1641f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1642f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1643f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1644f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1645f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1646f9d4088aSMatthew G. Knepley }
1647f9d4088aSMatthew G. Knepley 
16484f3b5142SJed Brown /*@C
16494d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
16504d343eeaSMatthew G Knepley 
16514d343eeaSMatthew G Knepley   Not collective
16524d343eeaSMatthew G Knepley 
16534d343eeaSMatthew G Knepley   Input Parameter:
16544d343eeaSMatthew G Knepley . dm - the DM object
16554d343eeaSMatthew G Knepley 
16564d343eeaSMatthew G Knepley   Output Parameters:
16570298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
16580298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
16590298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
16604d343eeaSMatthew G Knepley 
16614d343eeaSMatthew G Knepley   Level: intermediate
16624d343eeaSMatthew G Knepley 
166321c9b008SJed Brown   Notes:
166421c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
166521c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
166621c9b008SJed Brown   PetscFree().
166721c9b008SJed Brown 
16684d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
16694d343eeaSMatthew G Knepley @*/
167037d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
16714d343eeaSMatthew G Knepley {
167237d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
16734d343eeaSMatthew G Knepley   PetscErrorCode ierr;
16744d343eeaSMatthew G Knepley 
16754d343eeaSMatthew G Knepley   PetscFunctionBegin;
16764d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
167769ca1f37SDmitry Karpeev   if (numFields) {
1678534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
167969ca1f37SDmitry Karpeev     *numFields = 0;
168069ca1f37SDmitry Karpeev   }
168137d0c07bSMatthew G Knepley   if (fieldNames) {
168237d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
16830298fd71SBarry Smith     *fieldNames = NULL;
168469ca1f37SDmitry Karpeev   }
168569ca1f37SDmitry Karpeev   if (fields) {
168669ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
16870298fd71SBarry Smith     *fields = NULL;
168869ca1f37SDmitry Karpeev   }
168992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
169037d0c07bSMatthew G Knepley   if (section) {
16913a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
169237d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
169337d0c07bSMatthew G Knepley 
1694e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
169537d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
16963a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
169737d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
169837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
169937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
17003a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
170137d0c07bSMatthew G Knepley     }
170237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
170337d0c07bSMatthew G Knepley       PetscInt gdof;
170437d0c07bSMatthew G Knepley 
170537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
170637d0c07bSMatthew G Knepley       if (gdof > 0) {
170737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
17083a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
170937d0c07bSMatthew G Knepley 
171037d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
171137d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
17123a544194SStefano Zampini           fpdof = fdof-fcdof;
17133a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
17143a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
17153a544194SStefano Zampini             fieldNc[f] = 1;
17163a544194SStefano Zampini           }
17173a544194SStefano Zampini           fieldSizes[f] += fpdof;
171837d0c07bSMatthew G Knepley         }
171937d0c07bSMatthew G Knepley       }
172037d0c07bSMatthew G Knepley     }
172137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1722785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
172337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
172437d0c07bSMatthew G Knepley     }
172537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
172637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
172737d0c07bSMatthew G Knepley 
172837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
172937d0c07bSMatthew G Knepley       if (gdof > 0) {
173037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
173137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
173237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
173337d0c07bSMatthew G Knepley 
173437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
173537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
173637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
173737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
173837d0c07bSMatthew G Knepley           }
173937d0c07bSMatthew G Knepley         }
174037d0c07bSMatthew G Knepley       }
174137d0c07bSMatthew G Knepley     }
17428865f1eaSKarl Rupp     if (numFields) *numFields = nF;
174337d0c07bSMatthew G Knepley     if (fieldNames) {
1744785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
174537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
174637d0c07bSMatthew G Knepley         const char *fieldName;
174737d0c07bSMatthew G Knepley 
174837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
174937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
175037d0c07bSMatthew G Knepley       }
175137d0c07bSMatthew G Knepley     }
175237d0c07bSMatthew G Knepley     if (fields) {
1753785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
175437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
17553a544194SStefano Zampini         PetscInt bs, in[2], out[2];
17563a544194SStefano Zampini 
175782f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
17583a544194SStefano Zampini         in[0] = -fieldNc[f];
17593a544194SStefano Zampini         in[1] = fieldNc[f];
17603a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
17613a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
17623a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
176337d0c07bSMatthew G Knepley       }
176437d0c07bSMatthew G Knepley     }
17653a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
17668865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
17678865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
176869ca1f37SDmitry Karpeev   }
17694d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
17704d343eeaSMatthew G Knepley }
17714d343eeaSMatthew G Knepley 
177216621825SDmitry Karpeev 
177316621825SDmitry Karpeev /*@C
177416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
177516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
177616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1777e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1778e7c4fc90SDmitry Karpeev 
1779e7c4fc90SDmitry Karpeev   Not collective
1780e7c4fc90SDmitry Karpeev 
1781e7c4fc90SDmitry Karpeev   Input Parameter:
1782e7c4fc90SDmitry Karpeev . dm - the DM object
1783e7c4fc90SDmitry Karpeev 
1784e7c4fc90SDmitry Karpeev   Output Parameters:
17850298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
17860298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
17870298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
17880298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1789e7c4fc90SDmitry Karpeev 
1790e7c4fc90SDmitry Karpeev   Level: intermediate
1791e7c4fc90SDmitry Karpeev 
1792e7c4fc90SDmitry Karpeev   Notes:
1793e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1794e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1795e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1796e7c4fc90SDmitry Karpeev 
1797e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1798e7c4fc90SDmitry Karpeev @*/
179916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1800e7c4fc90SDmitry Karpeev {
1801e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1802e7c4fc90SDmitry Karpeev 
1803e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1804e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18058865f1eaSKarl Rupp   if (len) {
1806534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
18078865f1eaSKarl Rupp     *len = 0;
18088865f1eaSKarl Rupp   }
18098865f1eaSKarl Rupp   if (namelist) {
18108865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1811ea78f98cSLisandro Dalcin     *namelist = NULL;
18128865f1eaSKarl Rupp   }
18138865f1eaSKarl Rupp   if (islist) {
18148865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1815ea78f98cSLisandro Dalcin     *islist = NULL;
18168865f1eaSKarl Rupp   }
18178865f1eaSKarl Rupp   if (dmlist) {
18188865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1819ea78f98cSLisandro Dalcin     *dmlist = NULL;
18208865f1eaSKarl Rupp   }
1821f3f0edfdSDmitry Karpeev   /*
1822f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1823f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1824f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1825f3f0edfdSDmitry Karpeev    */
1826ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
182716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1828435a35e8SMatthew G Knepley     PetscSection section;
1829435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1830435a35e8SMatthew G Knepley 
183192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1832435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1833435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1834f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
183503dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
183603dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
183703dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1838435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1839435a35e8SMatthew G Knepley         const char *fieldName;
1840435a35e8SMatthew G Knepley 
184103dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
184203dc3394SMatthew G. Knepley         if (namelist) {
1843435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1844435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1845435a35e8SMatthew G Knepley         }
184603dc3394SMatthew G. Knepley       }
1847435a35e8SMatthew G Knepley     } else {
184869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1849e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
18500298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1851e7c4fc90SDmitry Karpeev     }
18528865f1eaSKarl Rupp   } else {
185316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
185416621825SDmitry Karpeev   }
185516621825SDmitry Karpeev   PetscFunctionReturn(0);
185616621825SDmitry Karpeev }
185716621825SDmitry Karpeev 
1858564cec59SMatthew G. Knepley /*@
1859435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1860435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1861435a35e8SMatthew G Knepley 
1862435a35e8SMatthew G Knepley   Not collective
1863435a35e8SMatthew G Knepley 
1864435a35e8SMatthew G Knepley   Input Parameters:
18652adcc780SMatthew G. Knepley + dm        - The DM object
18662adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
18672adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1868435a35e8SMatthew G Knepley 
1869435a35e8SMatthew G Knepley   Output Parameters:
18702adcc780SMatthew G. Knepley + is - The global indices for the subproblem
18712adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1872435a35e8SMatthew G Knepley 
18735d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
18745d3b26e6SMatthew G. Knepley 
1875435a35e8SMatthew G Knepley   Level: intermediate
1876435a35e8SMatthew G Knepley 
18775d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1878435a35e8SMatthew G Knepley @*/
187937bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1880435a35e8SMatthew G Knepley {
1881435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1882435a35e8SMatthew G Knepley 
1883435a35e8SMatthew G Knepley   PetscFunctionBegin;
1884435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1885435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
18868865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
18878865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1888b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1889435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1890435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1891435a35e8SMatthew G Knepley }
1892435a35e8SMatthew G Knepley 
18932adcc780SMatthew G. Knepley /*@C
18942adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
18952adcc780SMatthew G. Knepley 
18962adcc780SMatthew G. Knepley   Not collective
18972adcc780SMatthew G. Knepley 
18982adcc780SMatthew G. Knepley   Input Parameter:
18992adcc780SMatthew G. Knepley + dms - The DM objects
19002adcc780SMatthew G. Knepley - len - The number of DMs
19012adcc780SMatthew G. Knepley 
19022adcc780SMatthew G. Knepley   Output Parameters:
1903a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
19042adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19052adcc780SMatthew G. Knepley 
19065d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19075d3b26e6SMatthew G. Knepley 
19082adcc780SMatthew G. Knepley   Level: intermediate
19092adcc780SMatthew G. Knepley 
19105d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
19112adcc780SMatthew G. Knepley @*/
19122adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
19132adcc780SMatthew G. Knepley {
19142adcc780SMatthew G. Knepley   PetscInt       i;
19152adcc780SMatthew G. Knepley   PetscErrorCode ierr;
19162adcc780SMatthew G. Knepley 
19172adcc780SMatthew G. Knepley   PetscFunctionBegin;
19182adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
19192adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
19202adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1921a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
19222adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
19232adcc780SMatthew G. Knepley   if (len) {
1924b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1925b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1926b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
19272adcc780SMatthew G. Knepley   }
19282adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
19292adcc780SMatthew G. Knepley }
19302adcc780SMatthew G. Knepley 
193116621825SDmitry Karpeev 
193216621825SDmitry Karpeev /*@C
19338d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
19348d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
19358d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
19368d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
19378d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
193816621825SDmitry Karpeev 
193916621825SDmitry Karpeev   Not collective
194016621825SDmitry Karpeev 
194116621825SDmitry Karpeev   Input Parameter:
194216621825SDmitry Karpeev . dm - the DM object
194316621825SDmitry Karpeev 
194416621825SDmitry Karpeev   Output Parameters:
19450298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
19460298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
19470298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
19480298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
19490298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
195016621825SDmitry Karpeev 
195116621825SDmitry Karpeev   Level: intermediate
195216621825SDmitry Karpeev 
195316621825SDmitry Karpeev   Notes:
195416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
195516621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
195616621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
195716621825SDmitry Karpeev 
1958245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
195916621825SDmitry Karpeev @*/
19608d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
196116621825SDmitry Karpeev {
196216621825SDmitry Karpeev   PetscErrorCode      ierr;
1963be081cd6SPeter Brune   DMSubDomainHookLink link;
1964be081cd6SPeter Brune   PetscInt            i,l;
196516621825SDmitry Karpeev 
196616621825SDmitry Karpeev   PetscFunctionBegin;
196716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
196814a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
19690298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
19700298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
19710298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
19720298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1973f3f0edfdSDmitry Karpeev   /*
1974f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1975f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1976f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1977f3f0edfdSDmitry Karpeev    */
1978ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
197916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1980be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
198114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1982f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1983be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1984be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1985be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1986be081cd6SPeter Brune         }
1987648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1988e7c4fc90SDmitry Karpeev       }
198914a18fd3SPeter Brune     }
199014a18fd3SPeter Brune     if (len) *len = l;
199114a18fd3SPeter Brune   }
1992e30e807fSPeter Brune   PetscFunctionReturn(0);
1993e30e807fSPeter Brune }
1994e30e807fSPeter Brune 
1995e30e807fSPeter Brune 
1996e30e807fSPeter Brune /*@C
1997e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1998e30e807fSPeter Brune 
1999e30e807fSPeter Brune   Not collective
2000e30e807fSPeter Brune 
2001e30e807fSPeter Brune   Input Parameters:
2002e30e807fSPeter Brune + dm - the DM object
2003e30e807fSPeter Brune . n  - the number of subdomain scatters
2004e30e807fSPeter Brune - subdms - the local subdomains
2005e30e807fSPeter Brune 
2006e30e807fSPeter Brune   Output Parameters:
2007e30e807fSPeter Brune + n     - the number of scatters returned
2008e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2009e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2010e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2011e30e807fSPeter Brune 
201295452b02SPatrick Sanan   Notes:
201395452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2014e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2015e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2016e30e807fSPeter Brune   solution and residual data.
2017e30e807fSPeter Brune 
2018e30e807fSPeter Brune   Level: developer
2019e30e807fSPeter Brune 
2020e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2021e30e807fSPeter Brune @*/
2022e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2023e30e807fSPeter Brune {
2024e30e807fSPeter Brune   PetscErrorCode ierr;
2025e30e807fSPeter Brune 
2026e30e807fSPeter Brune   PetscFunctionBegin;
2027e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2028e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2029b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2030e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2031e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2032e7c4fc90SDmitry Karpeev }
2033e7c4fc90SDmitry Karpeev 
203447c6ae99SBarry Smith /*@
203547c6ae99SBarry Smith   DMRefine - Refines a DM object
203647c6ae99SBarry Smith 
2037d083f849SBarry Smith   Collective on dm
203847c6ae99SBarry Smith 
203947c6ae99SBarry Smith   Input Parameter:
204047c6ae99SBarry Smith + dm   - the DM object
204191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
204247c6ae99SBarry Smith 
204347c6ae99SBarry Smith   Output Parameter:
20440298fd71SBarry Smith . dmf - the refined DM, or NULL
2045ae0a1c52SMatthew G Knepley 
2046412e9a14SMatthew G. Knepley   Options Dtabase Keys:
2047412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2048412e9a14SMatthew G. Knepley 
20490298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
205047c6ae99SBarry Smith 
205147c6ae99SBarry Smith   Level: developer
205247c6ae99SBarry Smith 
2053e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
205447c6ae99SBarry Smith @*/
20557087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
205647c6ae99SBarry Smith {
205747c6ae99SBarry Smith   PetscErrorCode   ierr;
2058c833c3b5SJed Brown   DMRefineHookLink link;
205947c6ae99SBarry Smith 
206047c6ae99SBarry Smith   PetscFunctionBegin;
2061732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2062b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
20631ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
206447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
20654057135bSMatthew G Knepley   if (*dmf) {
206643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
20678865f1eaSKarl Rupp 
20688cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
20698865f1eaSKarl Rupp 
2070644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
20710598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2072656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
20738865f1eaSKarl Rupp 
2074e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2075c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
20768865f1eaSKarl Rupp       if (link->refinehook) {
20778865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
20788865f1eaSKarl Rupp       }
2079c833c3b5SJed Brown     }
2080c833c3b5SJed Brown   }
20811ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2082c833c3b5SJed Brown   PetscFunctionReturn(0);
2083c833c3b5SJed Brown }
2084c833c3b5SJed Brown 
2085bb9467b5SJed Brown /*@C
2086c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2087c833c3b5SJed Brown 
2088c833c3b5SJed Brown    Logically Collective
2089c833c3b5SJed Brown 
2090c833c3b5SJed Brown    Input Arguments:
2091c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2092c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2093c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20940298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2095c833c3b5SJed Brown 
2096c833c3b5SJed Brown    Calling sequence of refinehook:
2097c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2098c833c3b5SJed Brown 
2099c833c3b5SJed Brown +  coarse - coarse level DM
2100c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2101c833c3b5SJed Brown -  ctx - optional user-defined function context
2102c833c3b5SJed Brown 
2103c833c3b5SJed Brown    Calling sequence for interphook:
2104c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2105c833c3b5SJed Brown 
2106c833c3b5SJed Brown +  coarse - coarse level DM
2107c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2108c833c3b5SJed Brown .  fine - fine level DM to update
2109c833c3b5SJed Brown -  ctx - optional user-defined function context
2110c833c3b5SJed Brown 
2111c833c3b5SJed Brown    Level: advanced
2112c833c3b5SJed Brown 
2113c833c3b5SJed Brown    Notes:
2114c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2115c833c3b5SJed Brown 
2116c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2117c833c3b5SJed Brown 
2118bb9467b5SJed Brown    This function is currently not available from Fortran.
2119bb9467b5SJed Brown 
2120c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2121c833c3b5SJed Brown @*/
2122c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2123c833c3b5SJed Brown {
2124c833c3b5SJed Brown   PetscErrorCode   ierr;
2125c833c3b5SJed Brown   DMRefineHookLink link,*p;
2126c833c3b5SJed Brown 
2127c833c3b5SJed Brown   PetscFunctionBegin;
2128c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21293d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
21303d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
21313d8e3701SJed Brown   }
213295dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2133c833c3b5SJed Brown   link->refinehook = refinehook;
2134c833c3b5SJed Brown   link->interphook = interphook;
2135c833c3b5SJed Brown   link->ctx        = ctx;
21360298fd71SBarry Smith   link->next       = NULL;
2137c833c3b5SJed Brown   *p               = link;
2138c833c3b5SJed Brown   PetscFunctionReturn(0);
2139c833c3b5SJed Brown }
2140c833c3b5SJed Brown 
21413d8e3701SJed Brown /*@C
21423d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
21433d8e3701SJed Brown 
21443d8e3701SJed Brown    Logically Collective
21453d8e3701SJed Brown 
21463d8e3701SJed Brown    Input Arguments:
21473d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
21483d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
21493d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21503d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
21513d8e3701SJed Brown 
21523d8e3701SJed Brown    Level: advanced
21533d8e3701SJed Brown 
21543d8e3701SJed Brown    Notes:
21553d8e3701SJed Brown    This function does nothing if the hook is not in the list.
21563d8e3701SJed Brown 
21573d8e3701SJed Brown    This function is currently not available from Fortran.
21583d8e3701SJed Brown 
21593d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
21603d8e3701SJed Brown @*/
21613d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
21623d8e3701SJed Brown {
21633d8e3701SJed Brown   PetscErrorCode   ierr;
21643d8e3701SJed Brown   DMRefineHookLink link,*p;
21653d8e3701SJed Brown 
21663d8e3701SJed Brown   PetscFunctionBegin;
21673d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21683d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
21693d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
21703d8e3701SJed Brown       link = *p;
21713d8e3701SJed Brown       *p = link->next;
21723d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
21733d8e3701SJed Brown       break;
21743d8e3701SJed Brown     }
21753d8e3701SJed Brown   }
21763d8e3701SJed Brown   PetscFunctionReturn(0);
21773d8e3701SJed Brown }
21783d8e3701SJed Brown 
2179c833c3b5SJed Brown /*@
2180c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2181c833c3b5SJed Brown 
2182c833c3b5SJed Brown    Collective if any hooks are
2183c833c3b5SJed Brown 
2184c833c3b5SJed Brown    Input Arguments:
2185c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2186e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2187c833c3b5SJed Brown -  fine - finer DM to update
2188c833c3b5SJed Brown 
2189c833c3b5SJed Brown    Level: developer
2190c833c3b5SJed Brown 
2191c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2192c833c3b5SJed Brown @*/
2193c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2194c833c3b5SJed Brown {
2195c833c3b5SJed Brown   PetscErrorCode   ierr;
2196c833c3b5SJed Brown   DMRefineHookLink link;
2197c833c3b5SJed Brown 
2198c833c3b5SJed Brown   PetscFunctionBegin;
2199c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22008865f1eaSKarl Rupp     if (link->interphook) {
22018865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
22028865f1eaSKarl Rupp     }
22034057135bSMatthew G Knepley   }
220447c6ae99SBarry Smith   PetscFunctionReturn(0);
220547c6ae99SBarry Smith }
220647c6ae99SBarry Smith 
2207eb3f98d2SBarry Smith /*@
2208aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2209eb3f98d2SBarry Smith 
2210eb3f98d2SBarry Smith     Not Collective
2211eb3f98d2SBarry Smith 
2212eb3f98d2SBarry Smith     Input Parameter:
2213eb3f98d2SBarry Smith .   dm - the DM object
2214eb3f98d2SBarry Smith 
2215eb3f98d2SBarry Smith     Output Parameter:
2216eb3f98d2SBarry Smith .   level - number of refinements
2217eb3f98d2SBarry Smith 
2218eb3f98d2SBarry Smith     Level: developer
2219eb3f98d2SBarry Smith 
22206a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2221eb3f98d2SBarry Smith 
2222eb3f98d2SBarry Smith @*/
2223eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2224eb3f98d2SBarry Smith {
2225eb3f98d2SBarry Smith   PetscFunctionBegin;
2226eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2227eb3f98d2SBarry Smith   *level = dm->levelup;
2228eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2229eb3f98d2SBarry Smith }
2230eb3f98d2SBarry Smith 
2231fef3a512SBarry Smith /*@
2232aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2233fef3a512SBarry Smith 
2234fef3a512SBarry Smith     Not Collective
2235fef3a512SBarry Smith 
2236fef3a512SBarry Smith     Input Parameter:
2237fef3a512SBarry Smith +   dm - the DM object
2238fef3a512SBarry Smith -   level - number of refinements
2239fef3a512SBarry Smith 
2240fef3a512SBarry Smith     Level: advanced
2241fef3a512SBarry Smith 
224295452b02SPatrick Sanan     Notes:
224395452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2244fef3a512SBarry Smith 
2245fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2246fef3a512SBarry Smith 
2247fef3a512SBarry Smith @*/
2248fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2249fef3a512SBarry Smith {
2250fef3a512SBarry Smith   PetscFunctionBegin;
2251fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2252fef3a512SBarry Smith   dm->levelup = level;
2253fef3a512SBarry Smith   PetscFunctionReturn(0);
2254fef3a512SBarry Smith }
2255fef3a512SBarry Smith 
2256ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2257ca3d3a14SMatthew G. Knepley {
2258ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2259ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2260ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2261ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2262ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2263ca3d3a14SMatthew G. Knepley }
2264ca3d3a14SMatthew G. Knepley 
2265ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2266ca3d3a14SMatthew G. Knepley {
2267ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2268ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2269ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2270ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2271ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2272ca3d3a14SMatthew G. Knepley }
2273ca3d3a14SMatthew G. Knepley 
2274ca3d3a14SMatthew G. Knepley /*@
2275c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2276ca3d3a14SMatthew G. Knepley 
2277ca3d3a14SMatthew G. Knepley   Input Parameter:
2278ca3d3a14SMatthew G. Knepley . dm - The DM
2279ca3d3a14SMatthew G. Knepley 
2280ca3d3a14SMatthew G. Knepley   Output Parameter:
2281ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2282ca3d3a14SMatthew G. Knepley 
2283ca3d3a14SMatthew G. Knepley   Level: developer
2284ca3d3a14SMatthew G. Knepley 
2285436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2286ca3d3a14SMatthew G. Knepley @*/
2287ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2288ca3d3a14SMatthew G. Knepley {
2289ca3d3a14SMatthew G. Knepley   Vec            tv;
2290ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2291ca3d3a14SMatthew G. Knepley 
2292ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2293ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2294534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2295ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2296ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2297ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2298ca3d3a14SMatthew G. Knepley }
2299ca3d3a14SMatthew G. Knepley 
2300ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2301ca3d3a14SMatthew G. Knepley {
2302ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2303ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2304ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2305ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2306ca3d3a14SMatthew G. Knepley 
2307ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2308ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
230992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2310ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2311ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2312ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
231392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2314ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2315ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2316ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2317ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2318ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2319ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2320ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2321ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2322ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2323ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2324ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2325ca3d3a14SMatthew G. Knepley     }
2326ca3d3a14SMatthew G. Knepley   }
2327ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2328ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2329ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2330ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2331ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2332ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2333ca3d3a14SMatthew G. Knepley       if (dof) {
2334ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2335ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2336ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2337ca3d3a14SMatthew G. Knepley 
2338ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2339ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2340ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2341580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2342ca3d3a14SMatthew G. Knepley       }
2343ca3d3a14SMatthew G. Knepley     }
2344ca3d3a14SMatthew G. Knepley   }
2345ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2346ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2347ca3d3a14SMatthew G. Knepley }
2348ca3d3a14SMatthew G. Knepley 
2349ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2350ca3d3a14SMatthew G. Knepley {
2351ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2352ca3d3a14SMatthew G. Knepley 
2353ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2354ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2355ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2356ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2357ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2358ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2359ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2360ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2361ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2362ca3d3a14SMatthew G. Knepley }
2363ca3d3a14SMatthew G. Knepley 
2364bb9467b5SJed Brown /*@C
2365baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2366baf369e7SPeter Brune 
2367baf369e7SPeter Brune    Logically Collective
2368baf369e7SPeter Brune 
2369baf369e7SPeter Brune    Input Arguments:
2370baf369e7SPeter Brune +  dm - the DM
2371baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2372baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
23730298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2374baf369e7SPeter Brune 
2375baf369e7SPeter Brune    Calling sequence for beginhook:
2376baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2377baf369e7SPeter Brune 
2378baf369e7SPeter Brune +  dm - global DM
2379baf369e7SPeter Brune .  g - global vector
2380baf369e7SPeter Brune .  mode - mode
2381baf369e7SPeter Brune .  l - local vector
2382baf369e7SPeter Brune -  ctx - optional user-defined function context
2383baf369e7SPeter Brune 
2384baf369e7SPeter Brune 
2385baf369e7SPeter Brune    Calling sequence for endhook:
2386ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2387baf369e7SPeter Brune 
2388baf369e7SPeter Brune +  global - global DM
2389baf369e7SPeter Brune -  ctx - optional user-defined function context
2390baf369e7SPeter Brune 
2391baf369e7SPeter Brune    Level: advanced
2392baf369e7SPeter Brune 
2393baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2394baf369e7SPeter Brune @*/
2395baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2396baf369e7SPeter Brune {
2397baf369e7SPeter Brune   PetscErrorCode          ierr;
2398baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2399baf369e7SPeter Brune 
2400baf369e7SPeter Brune   PetscFunctionBegin;
2401baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2402baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
240395dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2404baf369e7SPeter Brune   link->beginhook = beginhook;
2405baf369e7SPeter Brune   link->endhook   = endhook;
2406baf369e7SPeter Brune   link->ctx       = ctx;
24070298fd71SBarry Smith   link->next      = NULL;
2408baf369e7SPeter Brune   *p              = link;
2409baf369e7SPeter Brune   PetscFunctionReturn(0);
2410baf369e7SPeter Brune }
2411baf369e7SPeter Brune 
24124c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
24134c274da1SToby Isaac {
24144c274da1SToby Isaac   Mat cMat;
24154c274da1SToby Isaac   Vec cVec;
24164c274da1SToby Isaac   PetscSection section, cSec;
24174c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24184c274da1SToby Isaac   PetscErrorCode ierr;
24194c274da1SToby Isaac 
24204c274da1SToby Isaac   PetscFunctionBegin;
24214c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24224c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24234c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
24245db9a05bSToby Isaac     PetscInt nRows;
24255db9a05bSToby Isaac 
24265db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24275db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
242892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
24297711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24304c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
24314c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24324c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24334c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24344c274da1SToby Isaac       if (dof) {
24354c274da1SToby Isaac         PetscScalar *vals;
24364c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
24374c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
24384c274da1SToby Isaac       }
24394c274da1SToby Isaac     }
24404c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24414c274da1SToby Isaac   }
24424c274da1SToby Isaac   PetscFunctionReturn(0);
24434c274da1SToby Isaac }
24444c274da1SToby Isaac 
244547c6ae99SBarry Smith /*@
244601729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
244701729b5cSPatrick Sanan 
2448d083f849SBarry Smith     Neighbor-wise Collective on dm
244901729b5cSPatrick Sanan 
245001729b5cSPatrick Sanan     Input Parameters:
245101729b5cSPatrick Sanan +   dm - the DM object
245201729b5cSPatrick Sanan .   g - the global vector
245301729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
245401729b5cSPatrick Sanan -   l - the local vector
245501729b5cSPatrick Sanan 
245601729b5cSPatrick Sanan     Notes:
245701729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
245801729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
245901729b5cSPatrick Sanan 
246001729b5cSPatrick Sanan     Level: beginner
246101729b5cSPatrick Sanan 
246201729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
246301729b5cSPatrick Sanan 
246401729b5cSPatrick Sanan @*/
246501729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
246601729b5cSPatrick Sanan {
246701729b5cSPatrick Sanan   PetscErrorCode ierr;
246801729b5cSPatrick Sanan 
246901729b5cSPatrick Sanan   PetscFunctionBegin;
247001729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
247101729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
247201729b5cSPatrick Sanan   PetscFunctionReturn(0);
247301729b5cSPatrick Sanan }
247401729b5cSPatrick Sanan 
247501729b5cSPatrick Sanan /*@
247647c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
247747c6ae99SBarry Smith 
2478d083f849SBarry Smith     Neighbor-wise Collective on dm
247947c6ae99SBarry Smith 
248047c6ae99SBarry Smith     Input Parameters:
248147c6ae99SBarry Smith +   dm - the DM object
248247c6ae99SBarry Smith .   g - the global vector
248347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
248447c6ae99SBarry Smith -   l - the local vector
248547c6ae99SBarry Smith 
248601729b5cSPatrick Sanan     Level: intermediate
248747c6ae99SBarry Smith 
248801729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
248947c6ae99SBarry Smith 
249047c6ae99SBarry Smith @*/
24917087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
249247c6ae99SBarry Smith {
24937128ae9fSMatthew G Knepley   PetscSF                 sf;
249447c6ae99SBarry Smith   PetscErrorCode          ierr;
2495baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
249647c6ae99SBarry Smith 
2497d0295fc0SJunchao Zhang 
249847c6ae99SBarry Smith   PetscFunctionBegin;
2499171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2500baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
25018865f1eaSKarl Rupp     if (link->beginhook) {
25028865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
25038865f1eaSKarl Rupp     }
2504baf369e7SPeter Brune   }
25051bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
25067128ae9fSMatthew G Knepley   if (sf) {
2507ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2508ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2509d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
25107128ae9fSMatthew G Knepley 
251182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2512*a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2513*a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2514d0295fc0SJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray);CHKERRQ(ierr);
2515*a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2516*a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
25177128ae9fSMatthew G Knepley   } else {
251833907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2519843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
25207128ae9fSMatthew G Knepley   }
252147c6ae99SBarry Smith   PetscFunctionReturn(0);
252247c6ae99SBarry Smith }
252347c6ae99SBarry Smith 
252447c6ae99SBarry Smith /*@
252547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
252647c6ae99SBarry Smith 
2527d083f849SBarry Smith     Neighbor-wise Collective on dm
252847c6ae99SBarry Smith 
252947c6ae99SBarry Smith     Input Parameters:
253047c6ae99SBarry Smith +   dm - the DM object
253147c6ae99SBarry Smith .   g - the global vector
253247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
253347c6ae99SBarry Smith -   l - the local vector
253447c6ae99SBarry Smith 
253501729b5cSPatrick Sanan     Level: intermediate
253647c6ae99SBarry Smith 
253701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
253847c6ae99SBarry Smith 
253947c6ae99SBarry Smith @*/
25407087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
254147c6ae99SBarry Smith {
25427128ae9fSMatthew G Knepley   PetscSF                 sf;
254347c6ae99SBarry Smith   PetscErrorCode          ierr;
2544ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2545ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2546ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2547baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2548d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
254947c6ae99SBarry Smith 
255047c6ae99SBarry Smith   PetscFunctionBegin;
2551171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25521bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2553ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
25547128ae9fSMatthew G Knepley   if (sf) {
255582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25567128ae9fSMatthew G Knepley 
2557*a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2558*a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
25597128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2560*a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2561*a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
2562ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
25637128ae9fSMatthew G Knepley   } else {
256433907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2565843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
25667128ae9fSMatthew G Knepley   }
25674c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2568baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2569baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2570baf369e7SPeter Brune   }
257147c6ae99SBarry Smith   PetscFunctionReturn(0);
257247c6ae99SBarry Smith }
257347c6ae99SBarry Smith 
2574d4d07f1eSToby Isaac /*@C
2575d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2576d4d07f1eSToby Isaac 
2577d4d07f1eSToby Isaac    Logically Collective
2578d4d07f1eSToby Isaac 
2579d4d07f1eSToby Isaac    Input Arguments:
2580d4d07f1eSToby Isaac +  dm - the DM
2581d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2582d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2583d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2584d4d07f1eSToby Isaac 
2585d4d07f1eSToby Isaac    Calling sequence for beginhook:
2586d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2587d4d07f1eSToby Isaac 
2588d4d07f1eSToby Isaac +  dm - global DM
2589d4d07f1eSToby Isaac .  l - local vector
2590d4d07f1eSToby Isaac .  mode - mode
2591d4d07f1eSToby Isaac .  g - global vector
2592d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2593d4d07f1eSToby Isaac 
2594d4d07f1eSToby Isaac 
2595d4d07f1eSToby Isaac    Calling sequence for endhook:
2596d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2597d4d07f1eSToby Isaac 
2598d4d07f1eSToby Isaac +  global - global DM
2599d4d07f1eSToby Isaac .  l - local vector
2600d4d07f1eSToby Isaac .  mode - mode
2601d4d07f1eSToby Isaac .  g - global vector
2602d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2603d4d07f1eSToby Isaac 
2604d4d07f1eSToby Isaac    Level: advanced
2605d4d07f1eSToby Isaac 
2606d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2607d4d07f1eSToby Isaac @*/
2608d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2609d4d07f1eSToby Isaac {
2610d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2611d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2612d4d07f1eSToby Isaac 
2613d4d07f1eSToby Isaac   PetscFunctionBegin;
2614d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2615d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
261695dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2617d4d07f1eSToby Isaac   link->beginhook = beginhook;
2618d4d07f1eSToby Isaac   link->endhook   = endhook;
2619d4d07f1eSToby Isaac   link->ctx       = ctx;
2620d4d07f1eSToby Isaac   link->next      = NULL;
2621d4d07f1eSToby Isaac   *p              = link;
2622d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2623d4d07f1eSToby Isaac }
2624d4d07f1eSToby Isaac 
26254c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
26264c274da1SToby Isaac {
26274c274da1SToby Isaac   Mat cMat;
26284c274da1SToby Isaac   Vec cVec;
26294c274da1SToby Isaac   PetscSection section, cSec;
26304c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
26314c274da1SToby Isaac   PetscErrorCode ierr;
26324c274da1SToby Isaac 
26334c274da1SToby Isaac   PetscFunctionBegin;
26344c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26354c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
26364c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
26375db9a05bSToby Isaac     PetscInt nRows;
26385db9a05bSToby Isaac 
26395db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
26405db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
264192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
26427711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
26434c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
26444c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
26454c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
26464c274da1SToby Isaac       if (dof) {
26474c274da1SToby Isaac         PetscInt d;
26484c274da1SToby Isaac         PetscScalar *vals;
26494c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
26504c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
26514c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
26524c274da1SToby Isaac          * we just extracted */
26534c274da1SToby Isaac         for (d = 0; d < dof; d++) {
26544c274da1SToby Isaac           vals[d] = 0.;
26554c274da1SToby Isaac         }
26564c274da1SToby Isaac       }
26574c274da1SToby Isaac     }
26584c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
26594c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
26604c274da1SToby Isaac   }
26614c274da1SToby Isaac   PetscFunctionReturn(0);
26624c274da1SToby Isaac }
266301729b5cSPatrick Sanan /*@
266401729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
266501729b5cSPatrick Sanan 
2666d083f849SBarry Smith     Neighbor-wise Collective on dm
266701729b5cSPatrick Sanan 
266801729b5cSPatrick Sanan     Input Parameters:
266901729b5cSPatrick Sanan +   dm - the DM object
267001729b5cSPatrick Sanan .   l - the local vector
267101729b5cSPatrick 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.
267201729b5cSPatrick Sanan -   g - the global vector
267301729b5cSPatrick Sanan 
267401729b5cSPatrick Sanan     Notes:
267501729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
267601729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
267701729b5cSPatrick Sanan 
267801729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
267901729b5cSPatrick 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.
268001729b5cSPatrick Sanan 
268101729b5cSPatrick Sanan     Level: beginner
268201729b5cSPatrick Sanan 
268301729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
268401729b5cSPatrick Sanan 
268501729b5cSPatrick Sanan @*/
268601729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
268701729b5cSPatrick Sanan {
268801729b5cSPatrick Sanan   PetscErrorCode ierr;
268901729b5cSPatrick Sanan 
269001729b5cSPatrick Sanan   PetscFunctionBegin;
269101729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
269201729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
269301729b5cSPatrick Sanan   PetscFunctionReturn(0);
269401729b5cSPatrick Sanan }
26954c274da1SToby Isaac 
269647c6ae99SBarry Smith /*@
269701729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
26989a42bb27SBarry Smith 
2699d083f849SBarry Smith     Neighbor-wise Collective on dm
27009a42bb27SBarry Smith 
27019a42bb27SBarry Smith     Input Parameters:
27029a42bb27SBarry Smith +   dm - the DM object
2703f6813fd5SJed Brown .   l - the local vector
27041eb28f2eSBarry 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.
27051eb28f2eSBarry Smith -   g - the global vector
27069a42bb27SBarry Smith 
270795452b02SPatrick Sanan     Notes:
270895452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
270984330215SMatthew 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.
27109a42bb27SBarry Smith 
271101729b5cSPatrick Sanan     Level: intermediate
27129a42bb27SBarry Smith 
271301729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
27149a42bb27SBarry Smith 
27159a42bb27SBarry Smith @*/
27167087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
27179a42bb27SBarry Smith {
27187128ae9fSMatthew G Knepley   PetscSF                 sf;
271984330215SMatthew G. Knepley   PetscSection            s, gs;
2720d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2721ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2722ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2723ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2724fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
272584330215SMatthew G. Knepley   PetscErrorCode          ierr;
2726d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
27279a42bb27SBarry Smith 
27289a42bb27SBarry Smith   PetscFunctionBegin;
2729171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2730d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2731d4d07f1eSToby Isaac     if (link->beginhook) {
2732d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2733d4d07f1eSToby Isaac     }
2734d4d07f1eSToby Isaac   }
27354c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
27361bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
273792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27387128ae9fSMatthew G Knepley   switch (mode) {
27397128ae9fSMatthew G Knepley   case INSERT_VALUES:
27407128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2741304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
274284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27437128ae9fSMatthew G Knepley   case ADD_VALUES:
27447128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2745304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
274684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27477128ae9fSMatthew G Knepley   default:
274882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27497128ae9fSMatthew G Knepley   }
2750ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2751ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2752ca3d3a14SMatthew G. Knepley     if (transform) {
2753ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2754ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2755ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2756ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2757fa88e482SJed Brown     } else if (isInsert) {
2758ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2759fa88e482SJed Brown     } else {
2760*a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2761fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2762ca3d3a14SMatthew G. Knepley     }
2763fa88e482SJed Brown     if (s && isInsert) {
27647128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2765fa88e482SJed Brown     } else {
2766*a256111fSJunchao Zhang       ierr = VecGetArrayAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2767fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2768fa88e482SJed Brown     }
2769ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2770d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
277184330215SMatthew G. Knepley     } else if (s && isInsert) {
277284330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
277384330215SMatthew G. Knepley 
2774e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
277584330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
277684330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
277784330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2778b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
277984330215SMatthew G. Knepley 
278084330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
278103442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
278284330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2783b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
278484330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
278584330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2786b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
278703442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2788b3b16f48SMatthew 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);
2789b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2790b3b16f48SMatthew G. Knepley         if (!gcdof) {
279184330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2792b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2793b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
279484330215SMatthew G. Knepley           const PetscInt *cdofs;
279584330215SMatthew G. Knepley           PetscInt        cind = 0;
279684330215SMatthew G. Knepley 
279784330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2798b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
279984330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2800b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
280184330215SMatthew G. Knepley           }
2802b3b16f48SMatthew 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);
280384330215SMatthew G. Knepley       }
2804ca3d3a14SMatthew G. Knepley     }
2805fa88e482SJed Brown     if (g_inplace) {
2806*a256111fSJunchao Zhang       ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
2807fa88e482SJed Brown     } else {
28087128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2809fa88e482SJed Brown     }
2810ca3d3a14SMatthew G. Knepley     if (transform) {
2811ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2812ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2813fa88e482SJed Brown     } else if (l_inplace) {
2814*a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2815ca3d3a14SMatthew G. Knepley     } else {
2816ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2817ca3d3a14SMatthew G. Knepley     }
28187128ae9fSMatthew G Knepley   } else {
2819b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2820843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
28217128ae9fSMatthew G Knepley   }
28229a42bb27SBarry Smith   PetscFunctionReturn(0);
28239a42bb27SBarry Smith }
28249a42bb27SBarry Smith 
28259a42bb27SBarry Smith /*@
28269a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
282747c6ae99SBarry Smith 
2828d083f849SBarry Smith     Neighbor-wise Collective on dm
282947c6ae99SBarry Smith 
283047c6ae99SBarry Smith     Input Parameters:
283147c6ae99SBarry Smith +   dm - the DM object
2832f6813fd5SJed Brown .   l - the local vector
283347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2834f6813fd5SJed Brown -   g - the global vector
283547c6ae99SBarry Smith 
283601729b5cSPatrick Sanan     Level: intermediate
283747c6ae99SBarry Smith 
2838e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
283947c6ae99SBarry Smith 
284047c6ae99SBarry Smith @*/
28417087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
284247c6ae99SBarry Smith {
28437128ae9fSMatthew G Knepley   PetscSF                 sf;
284484330215SMatthew G. Knepley   PetscSection            s;
2845d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2846ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
284784330215SMatthew G. Knepley   PetscErrorCode          ierr;
284847c6ae99SBarry Smith 
284947c6ae99SBarry Smith   PetscFunctionBegin;
2850171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28511bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
285292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
28537128ae9fSMatthew G Knepley   switch (mode) {
28547128ae9fSMatthew G Knepley   case INSERT_VALUES:
28557128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
285684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28577128ae9fSMatthew G Knepley   case ADD_VALUES:
28587128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
285984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28607128ae9fSMatthew G Knepley   default:
286182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
28627128ae9fSMatthew G Knepley   }
286384330215SMatthew G. Knepley   if (sf && !isInsert) {
2864ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2865ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2866ca3d3a14SMatthew G. Knepley     Vec                tmpl;
286784330215SMatthew G. Knepley 
2868ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2869ca3d3a14SMatthew G. Knepley     if (transform) {
2870ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2871ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2872ca3d3a14SMatthew G. Knepley     } else {
2873*a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
2874ca3d3a14SMatthew G. Knepley     }
2875*a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(g, &gArray, NULL);CHKERRQ(ierr);
2876a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2877ca3d3a14SMatthew G. Knepley     if (transform) {
2878ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2879ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2880ca3d3a14SMatthew G. Knepley     } else {
2881*a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2882ca3d3a14SMatthew G. Knepley     }
2883*a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
288484330215SMatthew G. Knepley   } else if (s && isInsert) {
28857128ae9fSMatthew G Knepley   } else {
2886b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2887843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
28887128ae9fSMatthew G Knepley   }
2889d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2890d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2891d4d07f1eSToby Isaac   }
289247c6ae99SBarry Smith   PetscFunctionReturn(0);
289347c6ae99SBarry Smith }
289447c6ae99SBarry Smith 
2895f089877aSRichard Tran Mills /*@
2896bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2897bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2898d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2899f089877aSRichard Tran Mills 
2900d083f849SBarry Smith    Neighbor-wise Collective on dm
2901f089877aSRichard Tran Mills 
2902f089877aSRichard Tran Mills    Input Parameters:
2903f089877aSRichard Tran Mills +  dm - the DM object
2904bc0a1609SRichard Tran Mills .  g - the original local vector
2905bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2906f089877aSRichard Tran Mills 
2907bc0a1609SRichard Tran Mills    Output Parameter:
2908bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2909f089877aSRichard Tran Mills 
2910f089877aSRichard Tran Mills    Level: intermediate
2911f089877aSRichard Tran Mills 
2912bc0a1609SRichard Tran Mills    Notes:
2913bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2914bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2915bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2916bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2917bc0a1609SRichard Tran Mills 
2918bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2919f089877aSRichard Tran Mills 
2920f089877aSRichard Tran Mills @*/
2921f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2922f089877aSRichard Tran Mills {
2923f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2924f089877aSRichard Tran Mills 
2925f089877aSRichard Tran Mills   PetscFunctionBegin;
2926f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2927bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2928f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2929f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2930f089877aSRichard Tran Mills }
2931f089877aSRichard Tran Mills 
2932f089877aSRichard Tran Mills /*@
2933bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2934bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2935d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2936f089877aSRichard Tran Mills 
2937d083f849SBarry Smith    Neighbor-wise Collective on dm
2938f089877aSRichard Tran Mills 
2939f089877aSRichard Tran Mills    Input Parameters:
2940bc0a1609SRichard Tran Mills +  da - the DM object
2941bc0a1609SRichard Tran Mills .  g - the original local vector
2942bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2943f089877aSRichard Tran Mills 
2944bc0a1609SRichard Tran Mills    Output Parameter:
2945bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2946f089877aSRichard Tran Mills 
2947f089877aSRichard Tran Mills    Level: intermediate
2948f089877aSRichard Tran Mills 
2949bc0a1609SRichard Tran Mills    Notes:
2950bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2951bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2952bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2953bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2954bc0a1609SRichard Tran Mills 
2955bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2956f089877aSRichard Tran Mills 
2957f089877aSRichard Tran Mills @*/
2958f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2959f089877aSRichard Tran Mills {
2960f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2961f089877aSRichard Tran Mills 
2962f089877aSRichard Tran Mills   PetscFunctionBegin;
2963f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2964bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2965f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2966f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2967f089877aSRichard Tran Mills }
2968f089877aSRichard Tran Mills 
2969f089877aSRichard Tran Mills 
297047c6ae99SBarry Smith /*@
297147c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
297247c6ae99SBarry Smith 
2973d083f849SBarry Smith     Collective on dm
297447c6ae99SBarry Smith 
297547c6ae99SBarry Smith     Input Parameter:
297647c6ae99SBarry Smith +   dm - the DM object
297791d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
297847c6ae99SBarry Smith 
297947c6ae99SBarry Smith     Output Parameter:
298047c6ae99SBarry Smith .   dmc - the coarsened DM
298147c6ae99SBarry Smith 
298247c6ae99SBarry Smith     Level: developer
298347c6ae99SBarry Smith 
2984e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
298547c6ae99SBarry Smith 
298647c6ae99SBarry Smith @*/
29877087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
298847c6ae99SBarry Smith {
298947c6ae99SBarry Smith   PetscErrorCode    ierr;
2990b17ce1afSJed Brown   DMCoarsenHookLink link;
299147c6ae99SBarry Smith 
299247c6ae99SBarry Smith   PetscFunctionBegin;
2993171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2994b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
299547a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
299647c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2997b9d85ea2SLisandro Dalcin   if (*dmc) {
2998a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
299943842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
30008cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
3001644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
30020598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3003656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3004e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3005b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3006b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3007b17ce1afSJed Brown     }
3008b9d85ea2SLisandro Dalcin   }
300947a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3010b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3011b17ce1afSJed Brown   PetscFunctionReturn(0);
3012b17ce1afSJed Brown }
3013b17ce1afSJed Brown 
3014bb9467b5SJed Brown /*@C
3015b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3016b17ce1afSJed Brown 
3017b17ce1afSJed Brown    Logically Collective
3018b17ce1afSJed Brown 
3019b17ce1afSJed Brown    Input Arguments:
3020b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3021b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3022b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
30230298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3024b17ce1afSJed Brown 
3025b17ce1afSJed Brown    Calling sequence of coarsenhook:
3026b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3027b17ce1afSJed Brown 
3028b17ce1afSJed Brown +  fine - fine level DM
3029b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3030b17ce1afSJed Brown -  ctx - optional user-defined function context
3031b17ce1afSJed Brown 
3032b17ce1afSJed Brown    Calling sequence for restricthook:
3033c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3034b17ce1afSJed Brown 
3035b17ce1afSJed Brown +  fine - fine level DM
3036b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3037c833c3b5SJed Brown .  rscale - scaling vector for restriction
3038c833c3b5SJed Brown .  inject - matrix restricting by injection
3039b17ce1afSJed Brown .  coarse - coarse level DM to update
3040b17ce1afSJed Brown -  ctx - optional user-defined function context
3041b17ce1afSJed Brown 
3042b17ce1afSJed Brown    Level: advanced
3043b17ce1afSJed Brown 
3044b17ce1afSJed Brown    Notes:
3045b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3046b17ce1afSJed Brown 
3047b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3048b17ce1afSJed Brown 
3049b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3050b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3051b17ce1afSJed Brown 
3052bb9467b5SJed Brown    This function is currently not available from Fortran.
3053bb9467b5SJed Brown 
3054dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3055b17ce1afSJed Brown @*/
3056b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3057b17ce1afSJed Brown {
3058b17ce1afSJed Brown   PetscErrorCode    ierr;
3059b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3060b17ce1afSJed Brown 
3061b17ce1afSJed Brown   PetscFunctionBegin;
3062b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
30631e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
30641e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
30651e3d8eccSJed Brown   }
306695dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3067b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3068b17ce1afSJed Brown   link->restricthook = restricthook;
3069b17ce1afSJed Brown   link->ctx          = ctx;
30700298fd71SBarry Smith   link->next         = NULL;
3071b17ce1afSJed Brown   *p                 = link;
3072b17ce1afSJed Brown   PetscFunctionReturn(0);
3073b17ce1afSJed Brown }
3074b17ce1afSJed Brown 
3075dc822a44SJed Brown /*@C
3076dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3077dc822a44SJed Brown 
3078dc822a44SJed Brown    Logically Collective
3079dc822a44SJed Brown 
3080dc822a44SJed Brown    Input Arguments:
3081dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3082dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3083dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3084dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3085dc822a44SJed Brown 
3086dc822a44SJed Brown    Level: advanced
3087dc822a44SJed Brown 
3088dc822a44SJed Brown    Notes:
3089dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3090dc822a44SJed Brown 
3091dc822a44SJed Brown    This function is currently not available from Fortran.
3092dc822a44SJed Brown 
3093dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3094dc822a44SJed Brown @*/
3095dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3096dc822a44SJed Brown {
3097dc822a44SJed Brown   PetscErrorCode    ierr;
3098dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3099dc822a44SJed Brown 
3100dc822a44SJed Brown   PetscFunctionBegin;
3101dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3102dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3103dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3104dc822a44SJed Brown       link = *p;
3105dc822a44SJed Brown       *p = link->next;
3106dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3107dc822a44SJed Brown       break;
3108dc822a44SJed Brown     }
3109dc822a44SJed Brown   }
3110dc822a44SJed Brown   PetscFunctionReturn(0);
3111dc822a44SJed Brown }
3112dc822a44SJed Brown 
3113dc822a44SJed Brown 
3114b17ce1afSJed Brown /*@
3115b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3116b17ce1afSJed Brown 
3117b17ce1afSJed Brown    Collective if any hooks are
3118b17ce1afSJed Brown 
3119b17ce1afSJed Brown    Input Arguments:
3120b17ce1afSJed Brown +  fine - finer DM to use as a base
3121b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3122e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3123b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3124e91eccc2SStefano Zampini -  coarse - coarser DM to update
3125b17ce1afSJed Brown 
3126b17ce1afSJed Brown    Level: developer
3127b17ce1afSJed Brown 
3128b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3129b17ce1afSJed Brown @*/
3130b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3131b17ce1afSJed Brown {
3132b17ce1afSJed Brown   PetscErrorCode    ierr;
3133b17ce1afSJed Brown   DMCoarsenHookLink link;
3134b17ce1afSJed Brown 
3135b17ce1afSJed Brown   PetscFunctionBegin;
3136b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
31378865f1eaSKarl Rupp     if (link->restricthook) {
31388865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
31398865f1eaSKarl Rupp     }
3140b17ce1afSJed Brown   }
314147c6ae99SBarry Smith   PetscFunctionReturn(0);
314247c6ae99SBarry Smith }
314347c6ae99SBarry Smith 
3144bb9467b5SJed Brown /*@C
3145be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
31465dbd56e3SPeter Brune 
3147d083f849SBarry Smith    Logically Collective on global
31485dbd56e3SPeter Brune 
31495dbd56e3SPeter Brune    Input Arguments:
31505dbd56e3SPeter Brune +  global - global DM
3151ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
31525dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
31530298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
31545dbd56e3SPeter Brune 
3155ec4806b8SPeter Brune 
3156ec4806b8SPeter Brune    Calling sequence for ddhook:
3157ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3158ec4806b8SPeter Brune 
3159ec4806b8SPeter Brune +  global - global DM
3160ec4806b8SPeter Brune .  block  - block DM
3161ec4806b8SPeter Brune -  ctx - optional user-defined function context
3162ec4806b8SPeter Brune 
31635dbd56e3SPeter Brune    Calling sequence for restricthook:
3164ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
31655dbd56e3SPeter Brune 
31665dbd56e3SPeter Brune +  global - global DM
31675dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
31685dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3169ec4806b8SPeter Brune .  block  - block DM
31705dbd56e3SPeter Brune -  ctx - optional user-defined function context
31715dbd56e3SPeter Brune 
31725dbd56e3SPeter Brune    Level: advanced
31735dbd56e3SPeter Brune 
31745dbd56e3SPeter Brune    Notes:
3175ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
31765dbd56e3SPeter Brune 
31775dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
31785dbd56e3SPeter Brune 
31795dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3180ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
31815dbd56e3SPeter Brune 
3182bb9467b5SJed Brown    This function is currently not available from Fortran.
3183bb9467b5SJed Brown 
31845dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
31855dbd56e3SPeter Brune @*/
3186be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
31875dbd56e3SPeter Brune {
31885dbd56e3SPeter Brune   PetscErrorCode      ierr;
3189be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
31905dbd56e3SPeter Brune 
31915dbd56e3SPeter Brune   PetscFunctionBegin;
31925dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3193b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3194b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3195b3a6b972SJed Brown   }
319695dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
31975dbd56e3SPeter Brune   link->restricthook = restricthook;
3198be081cd6SPeter Brune   link->ddhook       = ddhook;
31995dbd56e3SPeter Brune   link->ctx          = ctx;
32000298fd71SBarry Smith   link->next         = NULL;
32015dbd56e3SPeter Brune   *p                 = link;
32025dbd56e3SPeter Brune   PetscFunctionReturn(0);
32035dbd56e3SPeter Brune }
32045dbd56e3SPeter Brune 
3205b3a6b972SJed Brown /*@C
3206b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3207b3a6b972SJed Brown 
3208b3a6b972SJed Brown    Logically Collective
3209b3a6b972SJed Brown 
3210b3a6b972SJed Brown    Input Arguments:
3211b3a6b972SJed Brown +  global - global DM
3212b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3213b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3214b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3215b3a6b972SJed Brown 
3216b3a6b972SJed Brown    Level: advanced
3217b3a6b972SJed Brown 
3218b3a6b972SJed Brown    Notes:
3219b3a6b972SJed Brown 
3220b3a6b972SJed Brown    This function is currently not available from Fortran.
3221b3a6b972SJed Brown 
3222b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3223b3a6b972SJed Brown @*/
3224b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3225b3a6b972SJed Brown {
3226b3a6b972SJed Brown   PetscErrorCode      ierr;
3227b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3228b3a6b972SJed Brown 
3229b3a6b972SJed Brown   PetscFunctionBegin;
3230b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3231b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3232b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3233b3a6b972SJed Brown       link = *p;
3234b3a6b972SJed Brown       *p = link->next;
3235b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3236b3a6b972SJed Brown       break;
3237b3a6b972SJed Brown     }
3238b3a6b972SJed Brown   }
3239b3a6b972SJed Brown   PetscFunctionReturn(0);
3240b3a6b972SJed Brown }
3241b3a6b972SJed Brown 
32425dbd56e3SPeter Brune /*@
3243be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
32445dbd56e3SPeter Brune 
32455dbd56e3SPeter Brune    Collective if any hooks are
32465dbd56e3SPeter Brune 
32475dbd56e3SPeter Brune    Input Arguments:
32485dbd56e3SPeter Brune +  fine - finer DM to use as a base
3249be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3250be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
32515dbd56e3SPeter Brune -  coarse - coarer DM to update
32525dbd56e3SPeter Brune 
32535dbd56e3SPeter Brune    Level: developer
32545dbd56e3SPeter Brune 
32555dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
32565dbd56e3SPeter Brune @*/
3257be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
32585dbd56e3SPeter Brune {
32595dbd56e3SPeter Brune   PetscErrorCode      ierr;
3260be081cd6SPeter Brune   DMSubDomainHookLink link;
32615dbd56e3SPeter Brune 
32625dbd56e3SPeter Brune   PetscFunctionBegin;
3263be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
32648865f1eaSKarl Rupp     if (link->restricthook) {
32658865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
32668865f1eaSKarl Rupp     }
32675dbd56e3SPeter Brune   }
32685dbd56e3SPeter Brune   PetscFunctionReturn(0);
32695dbd56e3SPeter Brune }
32705dbd56e3SPeter Brune 
32715fe1f584SPeter Brune /*@
32726a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
32735fe1f584SPeter Brune 
32745fe1f584SPeter Brune     Not Collective
32755fe1f584SPeter Brune 
32765fe1f584SPeter Brune     Input Parameter:
32775fe1f584SPeter Brune .   dm - the DM object
32785fe1f584SPeter Brune 
32795fe1f584SPeter Brune     Output Parameter:
32806a7d9d85SPeter Brune .   level - number of coarsenings
32815fe1f584SPeter Brune 
32825fe1f584SPeter Brune     Level: developer
32835fe1f584SPeter Brune 
32846a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
32855fe1f584SPeter Brune 
32865fe1f584SPeter Brune @*/
32875fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
32885fe1f584SPeter Brune {
32895fe1f584SPeter Brune   PetscFunctionBegin;
32905fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3291b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
32925fe1f584SPeter Brune   *level = dm->leveldown;
32935fe1f584SPeter Brune   PetscFunctionReturn(0);
32945fe1f584SPeter Brune }
32955fe1f584SPeter Brune 
32969a64c4a8SMatthew G. Knepley /*@
32979a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
32989a64c4a8SMatthew G. Knepley 
32999a64c4a8SMatthew G. Knepley     Not Collective
33009a64c4a8SMatthew G. Knepley 
33019a64c4a8SMatthew G. Knepley     Input Parameters:
33029a64c4a8SMatthew G. Knepley +   dm - the DM object
33039a64c4a8SMatthew G. Knepley -   level - number of coarsenings
33049a64c4a8SMatthew G. Knepley 
33059a64c4a8SMatthew G. Knepley     Level: developer
33069a64c4a8SMatthew G. Knepley 
33079a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
33089a64c4a8SMatthew G. Knepley @*/
33099a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
33109a64c4a8SMatthew G. Knepley {
33119a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
33129a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33139a64c4a8SMatthew G. Knepley   dm->leveldown = level;
33149a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
33159a64c4a8SMatthew G. Knepley }
33169a64c4a8SMatthew G. Knepley 
33175fe1f584SPeter Brune 
33185fe1f584SPeter Brune 
331947c6ae99SBarry Smith /*@C
332047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
332147c6ae99SBarry Smith 
3322d083f849SBarry Smith     Collective on dm
332347c6ae99SBarry Smith 
332447c6ae99SBarry Smith     Input Parameter:
332547c6ae99SBarry Smith +   dm - the DM object
332647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
332747c6ae99SBarry Smith 
332847c6ae99SBarry Smith     Output Parameter:
332947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
333047c6ae99SBarry Smith 
333147c6ae99SBarry Smith     Level: developer
333247c6ae99SBarry Smith 
3333e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
333447c6ae99SBarry Smith 
333547c6ae99SBarry Smith @*/
33367087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
333747c6ae99SBarry Smith {
333847c6ae99SBarry Smith   PetscErrorCode ierr;
333947c6ae99SBarry Smith 
334047c6ae99SBarry Smith   PetscFunctionBegin;
3341171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3342ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
334347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3344b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
334547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
334647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
334747c6ae99SBarry Smith   } else if (dm->ops->refine) {
334847c6ae99SBarry Smith     PetscInt i;
334947c6ae99SBarry Smith 
3350ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
335147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3352ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
335347c6ae99SBarry Smith     }
3354ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
335547c6ae99SBarry Smith   PetscFunctionReturn(0);
335647c6ae99SBarry Smith }
335747c6ae99SBarry Smith 
335847c6ae99SBarry Smith /*@C
335947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
336047c6ae99SBarry Smith 
3361d083f849SBarry Smith     Collective on dm
336247c6ae99SBarry Smith 
336347c6ae99SBarry Smith     Input Parameter:
336447c6ae99SBarry Smith +   dm - the DM object
336547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
336647c6ae99SBarry Smith 
336747c6ae99SBarry Smith     Output Parameter:
336847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
336947c6ae99SBarry Smith 
337047c6ae99SBarry Smith     Level: developer
337147c6ae99SBarry Smith 
3372e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
337347c6ae99SBarry Smith 
337447c6ae99SBarry Smith @*/
33757087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
337647c6ae99SBarry Smith {
337747c6ae99SBarry Smith   PetscErrorCode ierr;
337847c6ae99SBarry Smith 
337947c6ae99SBarry Smith   PetscFunctionBegin;
3380171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3381ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
338247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
338347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
338447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
338547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
338647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
338747c6ae99SBarry Smith     PetscInt i;
338847c6ae99SBarry Smith 
3389ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
339047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3391ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
339247c6ae99SBarry Smith     }
3393ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
339447c6ae99SBarry Smith   PetscFunctionReturn(0);
339547c6ae99SBarry Smith }
339647c6ae99SBarry Smith 
33971a266240SBarry Smith /*@C
33981a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
33991a266240SBarry Smith 
34001a266240SBarry Smith     Not Collective
34011a266240SBarry Smith 
34021a266240SBarry Smith     Input Parameters:
34031a266240SBarry Smith +   dm - the DM object
34041a266240SBarry Smith -   destroy - the destroy function
34051a266240SBarry Smith 
34061a266240SBarry Smith     Level: intermediate
34071a266240SBarry Smith 
3408e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
34091a266240SBarry Smith 
3410f07f9ceaSJed Brown @*/
34111a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
34121a266240SBarry Smith {
34131a266240SBarry Smith   PetscFunctionBegin;
3414171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34151a266240SBarry Smith   dm->ctxdestroy = destroy;
34161a266240SBarry Smith   PetscFunctionReturn(0);
34171a266240SBarry Smith }
34181a266240SBarry Smith 
3419b07ff414SBarry Smith /*@
34201b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
342147c6ae99SBarry Smith 
342247c6ae99SBarry Smith     Not Collective
342347c6ae99SBarry Smith 
342447c6ae99SBarry Smith     Input Parameters:
342547c6ae99SBarry Smith +   dm - the DM object
342647c6ae99SBarry Smith -   ctx - the user context
342747c6ae99SBarry Smith 
342847c6ae99SBarry Smith     Level: intermediate
342947c6ae99SBarry Smith 
3430e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
343147c6ae99SBarry Smith 
343247c6ae99SBarry Smith @*/
34331b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
343447c6ae99SBarry Smith {
343547c6ae99SBarry Smith   PetscFunctionBegin;
3436171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
343747c6ae99SBarry Smith   dm->ctx = ctx;
343847c6ae99SBarry Smith   PetscFunctionReturn(0);
343947c6ae99SBarry Smith }
344047c6ae99SBarry Smith 
344147c6ae99SBarry Smith /*@
34421b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
344347c6ae99SBarry Smith 
344447c6ae99SBarry Smith     Not Collective
344547c6ae99SBarry Smith 
344647c6ae99SBarry Smith     Input Parameter:
344747c6ae99SBarry Smith .   dm - the DM object
344847c6ae99SBarry Smith 
344947c6ae99SBarry Smith     Output Parameter:
345047c6ae99SBarry Smith .   ctx - the user context
345147c6ae99SBarry Smith 
345247c6ae99SBarry Smith     Level: intermediate
345347c6ae99SBarry Smith 
3454e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
345547c6ae99SBarry Smith 
345647c6ae99SBarry Smith @*/
34571b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
345847c6ae99SBarry Smith {
345947c6ae99SBarry Smith   PetscFunctionBegin;
3460171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34611b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
346247c6ae99SBarry Smith   PetscFunctionReturn(0);
346347c6ae99SBarry Smith }
346447c6ae99SBarry Smith 
346508da532bSDmitry Karpeev /*@C
3466df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
346708da532bSDmitry Karpeev 
3468d083f849SBarry Smith     Logically Collective on dm
346908da532bSDmitry Karpeev 
347008da532bSDmitry Karpeev     Input Parameter:
347108da532bSDmitry Karpeev +   dm - the DM object
34720298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
347308da532bSDmitry Karpeev 
347408da532bSDmitry Karpeev     Level: intermediate
347508da532bSDmitry Karpeev 
3476835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
347708da532bSDmitry Karpeev          DMSetJacobian()
347808da532bSDmitry Karpeev 
347908da532bSDmitry Karpeev @*/
348008da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
348108da532bSDmitry Karpeev {
348208da532bSDmitry Karpeev   PetscFunctionBegin;
34835a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
348408da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
348508da532bSDmitry Karpeev   PetscFunctionReturn(0);
348608da532bSDmitry Karpeev }
348708da532bSDmitry Karpeev 
348808da532bSDmitry Karpeev /*@
348908da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
349008da532bSDmitry Karpeev 
349108da532bSDmitry Karpeev     Not Collective
349208da532bSDmitry Karpeev 
349308da532bSDmitry Karpeev     Input Parameter:
349408da532bSDmitry Karpeev .   dm - the DM object to destroy
349508da532bSDmitry Karpeev 
349608da532bSDmitry Karpeev     Output Parameter:
349708da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
349808da532bSDmitry Karpeev 
349908da532bSDmitry Karpeev     Level: developer
350008da532bSDmitry Karpeev 
350174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
350208da532bSDmitry Karpeev 
350308da532bSDmitry Karpeev @*/
350408da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
350508da532bSDmitry Karpeev {
350608da532bSDmitry Karpeev   PetscFunctionBegin;
35075a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3508534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
350908da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
351008da532bSDmitry Karpeev   PetscFunctionReturn(0);
351108da532bSDmitry Karpeev }
351208da532bSDmitry Karpeev 
351308da532bSDmitry Karpeev /*@C
351408da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
351508da532bSDmitry Karpeev 
3516d083f849SBarry Smith     Logically Collective on dm
351708da532bSDmitry Karpeev 
351808da532bSDmitry Karpeev     Input Parameters:
3519907376e6SBarry Smith .   dm - the DM object
352008da532bSDmitry Karpeev 
352108da532bSDmitry Karpeev     Output parameters:
352208da532bSDmitry Karpeev +   xl - lower bound
352308da532bSDmitry Karpeev -   xu - upper bound
352408da532bSDmitry Karpeev 
3525907376e6SBarry Smith     Level: advanced
3526907376e6SBarry Smith 
352795452b02SPatrick Sanan     Notes:
352895452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
352908da532bSDmitry Karpeev 
353074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
353108da532bSDmitry Karpeev 
353208da532bSDmitry Karpeev @*/
353308da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
353408da532bSDmitry Karpeev {
353508da532bSDmitry Karpeev   PetscErrorCode ierr;
35365fd66863SKarl Rupp 
353708da532bSDmitry Karpeev   PetscFunctionBegin;
35385a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
353908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
35405a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3541b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
354208da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
354308da532bSDmitry Karpeev   PetscFunctionReturn(0);
354408da532bSDmitry Karpeev }
354508da532bSDmitry Karpeev 
3546b0ae01b7SPeter Brune /*@
3547b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3548b0ae01b7SPeter Brune 
3549b0ae01b7SPeter Brune     Not Collective
3550b0ae01b7SPeter Brune 
3551b0ae01b7SPeter Brune     Input Parameter:
3552b0ae01b7SPeter Brune .   dm - the DM object
3553b0ae01b7SPeter Brune 
3554b0ae01b7SPeter Brune     Output Parameter:
3555b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3556b0ae01b7SPeter Brune 
3557b0ae01b7SPeter Brune     Level: developer
3558b0ae01b7SPeter Brune 
35591565f0a7SPatrick Sanan .seealso DMCreateColoring()
3560b0ae01b7SPeter Brune 
3561b0ae01b7SPeter Brune @*/
3562b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3563b0ae01b7SPeter Brune {
3564b0ae01b7SPeter Brune   PetscFunctionBegin;
35655a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3566534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3567b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3568b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3569b0ae01b7SPeter Brune }
3570b0ae01b7SPeter Brune 
35713ad4599aSBarry Smith /*@
35723ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
35733ad4599aSBarry Smith 
35743ad4599aSBarry Smith     Not Collective
35753ad4599aSBarry Smith 
35763ad4599aSBarry Smith     Input Parameter:
35773ad4599aSBarry Smith .   dm - the DM object
35783ad4599aSBarry Smith 
35793ad4599aSBarry Smith     Output Parameter:
35803ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
35813ad4599aSBarry Smith 
35823ad4599aSBarry Smith     Level: developer
35833ad4599aSBarry Smith 
35841565f0a7SPatrick Sanan .seealso DMCreateRestriction()
35853ad4599aSBarry Smith 
35863ad4599aSBarry Smith @*/
35873ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
35883ad4599aSBarry Smith {
35893ad4599aSBarry Smith   PetscFunctionBegin;
35905a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3591534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
35923ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
35933ad4599aSBarry Smith   PetscFunctionReturn(0);
35943ad4599aSBarry Smith }
35953ad4599aSBarry Smith 
3596a7058e45SLawrence Mitchell 
3597a7058e45SLawrence Mitchell /*@
3598a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3599a7058e45SLawrence Mitchell 
3600a7058e45SLawrence Mitchell     Not Collective
3601a7058e45SLawrence Mitchell 
3602a7058e45SLawrence Mitchell     Input Parameter:
3603a7058e45SLawrence Mitchell .   dm - the DM object
3604a7058e45SLawrence Mitchell 
3605a7058e45SLawrence Mitchell     Output Parameter:
3606a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3607a7058e45SLawrence Mitchell 
3608a7058e45SLawrence Mitchell     Level: developer
3609a7058e45SLawrence Mitchell 
36101565f0a7SPatrick Sanan .seealso DMCreateInjection()
3611a7058e45SLawrence Mitchell 
3612a7058e45SLawrence Mitchell @*/
3613a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3614a7058e45SLawrence Mitchell {
36154a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
36165a84ad33SLisandro Dalcin 
3617a7058e45SLawrence Mitchell   PetscFunctionBegin;
36185a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3619534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
36205a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
36214a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
36225a84ad33SLisandro Dalcin   } else {
36235a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
36245a84ad33SLisandro Dalcin   }
3625a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3626a7058e45SLawrence Mitchell }
3627a7058e45SLawrence Mitchell 
36280298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3629264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3630264ace61SBarry Smith 
3631264ace61SBarry Smith /*@C
3632264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3633264ace61SBarry Smith 
3634d083f849SBarry Smith   Collective on dm
3635264ace61SBarry Smith 
3636264ace61SBarry Smith   Input Parameters:
3637264ace61SBarry Smith + dm     - The DM object
3638264ace61SBarry Smith - method - The name of the DM type
3639264ace61SBarry Smith 
3640264ace61SBarry Smith   Options Database Key:
3641264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3642264ace61SBarry Smith 
3643264ace61SBarry Smith   Notes:
3644e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3645264ace61SBarry Smith 
3646264ace61SBarry Smith   Level: intermediate
3647264ace61SBarry Smith 
3648264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3649264ace61SBarry Smith @*/
365019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3651264ace61SBarry Smith {
3652264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3653264ace61SBarry Smith   PetscBool      match;
3654264ace61SBarry Smith   PetscErrorCode ierr;
3655264ace61SBarry Smith 
3656264ace61SBarry Smith   PetscFunctionBegin;
3657264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3658251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3659264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3660264ace61SBarry Smith 
36610f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
36621c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3663ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3664264ace61SBarry Smith 
3665264ace61SBarry Smith   if (dm->ops->destroy) {
3666264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3667264ace61SBarry Smith   }
3668d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3669264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3670d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3671264ace61SBarry Smith   PetscFunctionReturn(0);
3672264ace61SBarry Smith }
3673264ace61SBarry Smith 
3674264ace61SBarry Smith /*@C
3675264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3676264ace61SBarry Smith 
3677264ace61SBarry Smith   Not Collective
3678264ace61SBarry Smith 
3679264ace61SBarry Smith   Input Parameter:
3680264ace61SBarry Smith . dm  - The DM
3681264ace61SBarry Smith 
3682264ace61SBarry Smith   Output Parameter:
3683264ace61SBarry Smith . type - The DM type name
3684264ace61SBarry Smith 
3685264ace61SBarry Smith   Level: intermediate
3686264ace61SBarry Smith 
3687264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3688264ace61SBarry Smith @*/
368919fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3690264ace61SBarry Smith {
3691264ace61SBarry Smith   PetscErrorCode ierr;
3692264ace61SBarry Smith 
3693264ace61SBarry Smith   PetscFunctionBegin;
3694264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3695c959eef4SJed Brown   PetscValidPointer(type,2);
3696607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3697264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3698264ace61SBarry Smith   PetscFunctionReturn(0);
3699264ace61SBarry Smith }
3700264ace61SBarry Smith 
370167a56275SMatthew G Knepley /*@C
370267a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
370367a56275SMatthew G Knepley 
3704d083f849SBarry Smith   Collective on dm
370567a56275SMatthew G Knepley 
370667a56275SMatthew G Knepley   Input Parameters:
370767a56275SMatthew G Knepley + dm - the DM
370867a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
370967a56275SMatthew G Knepley 
371067a56275SMatthew G Knepley   Output Parameter:
371167a56275SMatthew G Knepley . M - pointer to new DM
371267a56275SMatthew G Knepley 
371367a56275SMatthew G Knepley   Notes:
371467a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
371567a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
371667a56275SMatthew G Knepley   of the input DM.
371767a56275SMatthew G Knepley 
371867a56275SMatthew G Knepley   Level: intermediate
371967a56275SMatthew G Knepley 
372067a56275SMatthew G Knepley .seealso: DMCreate()
372167a56275SMatthew G Knepley @*/
372219fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
372367a56275SMatthew G Knepley {
372467a56275SMatthew G Knepley   DM             B;
372567a56275SMatthew G Knepley   char           convname[256];
3726c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
372767a56275SMatthew G Knepley   PetscErrorCode ierr;
372867a56275SMatthew G Knepley 
372967a56275SMatthew G Knepley   PetscFunctionBegin;
373067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
373167a56275SMatthew G Knepley   PetscValidType(dm,1);
373267a56275SMatthew G Knepley   PetscValidPointer(M,3);
3733251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3734c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3735c067b6caSMatthew G. Knepley   if (sametype) {
3736c067b6caSMatthew G. Knepley     *M   = dm;
3737c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3738c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3739c067b6caSMatthew G. Knepley   } else {
37400298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
374167a56275SMatthew G Knepley 
374267a56275SMatthew G Knepley     /*
374367a56275SMatthew G Knepley        Order of precedence:
374467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
374567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
374667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
374767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
374867a56275SMatthew G Knepley        5) Use a really basic converter.
374967a56275SMatthew G Knepley     */
375067a56275SMatthew G Knepley 
375167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3752a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3753a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3754a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3755a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3756a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
37570005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
375867a56275SMatthew G Knepley     if (conv) goto foundconv;
375967a56275SMatthew G Knepley 
376067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
376182f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
376267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3763a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3764a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3765a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3766a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3767a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
37680005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
376967a56275SMatthew G Knepley     if (conv) {
3770fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
377167a56275SMatthew G Knepley       goto foundconv;
377267a56275SMatthew G Knepley     }
377367a56275SMatthew G Knepley 
377467a56275SMatthew G Knepley #if 0
377567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
377667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3777fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
377867a56275SMatthew G Knepley     if (conv) goto foundconv;
377967a56275SMatthew G Knepley 
378067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
378167a56275SMatthew G Knepley     if (dm->ops->convert) {
378267a56275SMatthew G Knepley       conv = dm->ops->convert;
378367a56275SMatthew G Knepley     }
378467a56275SMatthew G Knepley     if (conv) goto foundconv;
378567a56275SMatthew G Knepley #endif
378667a56275SMatthew G Knepley 
378767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
378882f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
378967a56275SMatthew G Knepley 
379067a56275SMatthew G Knepley foundconv:
379167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
379267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
379312fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
379490b157c4SStefano Zampini     {
379590b157c4SStefano Zampini       PetscBool             isper;
379612fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
379712fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
379890b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
379990b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
3800c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
380112fa691eSMatthew G. Knepley     }
380267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
380367a56275SMatthew G Knepley   }
380467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
380567a56275SMatthew G Knepley   PetscFunctionReturn(0);
380667a56275SMatthew G Knepley }
3807264ace61SBarry Smith 
3808264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3809264ace61SBarry Smith 
3810264ace61SBarry Smith /*@C
38111c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
38121c84c290SBarry Smith 
38131c84c290SBarry Smith   Not Collective
38141c84c290SBarry Smith 
38151c84c290SBarry Smith   Input Parameters:
38161c84c290SBarry Smith + name        - The name of a new user-defined creation routine
38171c84c290SBarry Smith - create_func - The creation routine itself
38181c84c290SBarry Smith 
38191c84c290SBarry Smith   Notes:
38201c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
38211c84c290SBarry Smith 
38221c84c290SBarry Smith 
38231c84c290SBarry Smith   Sample usage:
38241c84c290SBarry Smith .vb
3825bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
38261c84c290SBarry Smith .ve
38271c84c290SBarry Smith 
38281c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
38291c84c290SBarry Smith .vb
38301c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
38311c84c290SBarry Smith     DMSetType(DM,"my_da");
38321c84c290SBarry Smith .ve
38331c84c290SBarry Smith    or at runtime via the option
38341c84c290SBarry Smith .vb
38351c84c290SBarry Smith     -da_type my_da
38361c84c290SBarry Smith .ve
3837264ace61SBarry Smith 
3838264ace61SBarry Smith   Level: advanced
38391c84c290SBarry Smith 
3840bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
38411c84c290SBarry Smith 
3842264ace61SBarry Smith @*/
3843bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3844264ace61SBarry Smith {
3845264ace61SBarry Smith   PetscErrorCode ierr;
3846264ace61SBarry Smith 
3847264ace61SBarry Smith   PetscFunctionBegin;
38481d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3849a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3850264ace61SBarry Smith   PetscFunctionReturn(0);
3851264ace61SBarry Smith }
3852264ace61SBarry Smith 
3853b859378eSBarry Smith /*@C
385455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3855b859378eSBarry Smith 
3856d083f849SBarry Smith   Collective on viewer
3857b859378eSBarry Smith 
3858b859378eSBarry Smith   Input Parameters:
3859b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3860b859378eSBarry Smith            some related function before a call to DMLoad().
3861b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3862b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3863b859378eSBarry Smith 
3864b859378eSBarry Smith    Level: intermediate
3865b859378eSBarry Smith 
3866b859378eSBarry Smith   Notes:
386755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3868b859378eSBarry Smith 
3869b859378eSBarry Smith   Notes for advanced users:
3870b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3871b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3872b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3873b859378eSBarry Smith   format is
3874b859378eSBarry Smith .vb
3875b859378eSBarry Smith      has not yet been determined
3876b859378eSBarry Smith .ve
3877b859378eSBarry Smith 
3878b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3879b859378eSBarry Smith @*/
3880b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3881b859378eSBarry Smith {
38829331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3883b859378eSBarry Smith   PetscErrorCode ierr;
3884b859378eSBarry Smith 
3885b859378eSBarry Smith   PetscFunctionBegin;
3886b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3887b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3888fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
388932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
38909331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
389158cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
38929331c7a4SMatthew G. Knepley   if (isbinary) {
38939331c7a4SMatthew G. Knepley     PetscInt classid;
38949331c7a4SMatthew G. Knepley     char     type[256];
3895b859378eSBarry Smith 
3896060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
38979200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3898060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
389932c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
39009331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
39019331c7a4SMatthew G. Knepley   } else if (ishdf5) {
39029331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
39039331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
390458cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3905b859378eSBarry Smith   PetscFunctionReturn(0);
3906b859378eSBarry Smith }
3907b859378eSBarry Smith 
3908b2e4378dSMatthew G. Knepley /*@
3909b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3910b2e4378dSMatthew G. Knepley 
3911b2e4378dSMatthew G. Knepley   Not collective
3912b2e4378dSMatthew G. Knepley 
3913b2e4378dSMatthew G. Knepley   Input Parameter:
3914b2e4378dSMatthew G. Knepley . dm - the DM
3915b2e4378dSMatthew G. Knepley 
3916b2e4378dSMatthew G. Knepley   Output Parameters:
3917b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3918b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3919b2e4378dSMatthew G. Knepley 
3920b2e4378dSMatthew G. Knepley   Level: beginner
3921b2e4378dSMatthew G. Knepley 
3922b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3923b2e4378dSMatthew G. Knepley 
3924b2e4378dSMatthew G. Knepley 
3925b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3926b2e4378dSMatthew G. Knepley @*/
3927b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3928b2e4378dSMatthew G. Knepley {
3929b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3930b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3931b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3932b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3933b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3934b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3935b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3936b2e4378dSMatthew G. Knepley 
3937b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3938b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3939b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3940b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3941b2e4378dSMatthew G. Knepley   if (coords) {
3942b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3943b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3944b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3945b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3946b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3947b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3948b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3949b2e4378dSMatthew G. Knepley       }
3950b2e4378dSMatthew G. Knepley     }
3951b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3952b2e4378dSMatthew G. Knepley   } else {
3953b2e4378dSMatthew G. Knepley     PetscBool isda;
3954b2e4378dSMatthew G. Knepley 
3955b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3956b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3957b2e4378dSMatthew G. Knepley   }
3958b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3959b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3960b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3961b2e4378dSMatthew G. Knepley }
3962b2e4378dSMatthew G. Knepley 
3963b2e4378dSMatthew G. Knepley /*@
3964b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3965b2e4378dSMatthew G. Knepley 
3966b2e4378dSMatthew G. Knepley   Collective
3967b2e4378dSMatthew G. Knepley 
3968b2e4378dSMatthew G. Knepley   Input Parameter:
3969b2e4378dSMatthew G. Knepley . dm - the DM
3970b2e4378dSMatthew G. Knepley 
3971b2e4378dSMatthew G. Knepley   Output Parameters:
3972b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3973b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3974b2e4378dSMatthew G. Knepley 
3975b2e4378dSMatthew G. Knepley   Level: beginner
3976b2e4378dSMatthew G. Knepley 
3977b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3978b2e4378dSMatthew G. Knepley @*/
3979b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3980b2e4378dSMatthew G. Knepley {
3981b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
39826ce308c4SMatthew G. Knepley   PetscInt       cdim;
39836ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3984b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3985b2e4378dSMatthew G. Knepley 
3986b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3987b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3988b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3989b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3990b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3991b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3992b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3993b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3994b2e4378dSMatthew G. Knepley }
3995b2e4378dSMatthew G. Knepley 
39967da65231SMatthew G Knepley /******************************** FEM Support **********************************/
39977da65231SMatthew G Knepley 
3998a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3999a6dfd86eSKarl Rupp {
40001d47ebbbSSatish Balay   PetscInt       f;
40011b30c384SMatthew G Knepley   PetscErrorCode ierr;
40021b30c384SMatthew G Knepley 
40037da65231SMatthew G Knepley   PetscFunctionBegin;
400474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
40051d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
400657622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
40077da65231SMatthew G Knepley   }
40087da65231SMatthew G Knepley   PetscFunctionReturn(0);
40097da65231SMatthew G Knepley }
40107da65231SMatthew G Knepley 
4011a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4012a6dfd86eSKarl Rupp {
40131b30c384SMatthew G Knepley   PetscInt       f, g;
40147da65231SMatthew G Knepley   PetscErrorCode ierr;
40157da65231SMatthew G Knepley 
40167da65231SMatthew G Knepley   PetscFunctionBegin;
401774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
40181d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
401974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
40201d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4021e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
40227da65231SMatthew G Knepley     }
402374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
40247da65231SMatthew G Knepley   }
40257da65231SMatthew G Knepley   PetscFunctionReturn(0);
40267da65231SMatthew G Knepley }
4027e7c4fc90SDmitry Karpeev 
40286113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4029e759306cSMatthew G. Knepley {
40300c5b8624SToby Isaac   PetscInt          localSize, bs;
40310c5b8624SToby Isaac   PetscMPIInt       size;
40320c5b8624SToby Isaac   Vec               x, xglob;
40330c5b8624SToby Isaac   const PetscScalar *xarray;
4034e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4035e759306cSMatthew G. Knepley 
4036e759306cSMatthew G. Knepley   PetscFunctionBegin;
40379852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
4038e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4039e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
40406113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
40410c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
40420c5b8624SToby Isaac   if (size > 1) {
40430c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
40440c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
40450c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
40460c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
40470c5b8624SToby Isaac   } else {
40480c5b8624SToby Isaac     xglob = x;
40490c5b8624SToby Isaac   }
40500c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
40510c5b8624SToby Isaac   if (size > 1) {
40520c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
40530c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
40540c5b8624SToby Isaac   }
4055e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4056e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4057e759306cSMatthew G. Knepley }
4058e759306cSMatthew G. Knepley 
405988ed4aceSMatthew G Knepley /*@
40601bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4061061576a5SJed Brown 
4062061576a5SJed Brown   Input Parameter:
4063061576a5SJed Brown . dm - The DM
4064061576a5SJed Brown 
4065061576a5SJed Brown   Output Parameter:
4066061576a5SJed Brown . section - The PetscSection
4067061576a5SJed Brown 
4068061576a5SJed Brown   Options Database Keys:
4069061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4070061576a5SJed Brown 
4071061576a5SJed Brown   Level: advanced
4072061576a5SJed Brown 
4073061576a5SJed Brown   Notes:
4074061576a5SJed Brown   Use DMGetLocalSection() in new code.
4075061576a5SJed Brown 
4076061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4077061576a5SJed Brown 
4078061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4079061576a5SJed Brown @*/
4080061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4081061576a5SJed Brown {
4082061576a5SJed Brown   PetscErrorCode ierr;
4083061576a5SJed Brown 
4084061576a5SJed Brown   PetscFunctionBegin;
4085061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4086061576a5SJed Brown   PetscFunctionReturn(0);
4087061576a5SJed Brown }
4088061576a5SJed Brown 
4089061576a5SJed Brown /*@
4090061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
409188ed4aceSMatthew G Knepley 
409288ed4aceSMatthew G Knepley   Input Parameter:
409388ed4aceSMatthew G Knepley . dm - The DM
409488ed4aceSMatthew G Knepley 
409588ed4aceSMatthew G Knepley   Output Parameter:
409688ed4aceSMatthew G Knepley . section - The PetscSection
409788ed4aceSMatthew G Knepley 
4098e5893cccSMatthew G. Knepley   Options Database Keys:
4099e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4100e5893cccSMatthew G. Knepley 
410188ed4aceSMatthew G Knepley   Level: intermediate
410288ed4aceSMatthew G Knepley 
410388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
410488ed4aceSMatthew G Knepley 
4105061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
410688ed4aceSMatthew G Knepley @*/
4107061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
41080adebc6cSBarry Smith {
4109fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4110fd59a867SMatthew G. Knepley 
411188ed4aceSMatthew G Knepley   PetscFunctionBegin;
411288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
411388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41141bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4115e5e52638SMatthew G. Knepley     PetscInt d;
4116e5e52638SMatthew G. Knepley 
4117e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
41181bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
41191bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
41202f0f8703SMatthew G. Knepley   }
41211bb6d2a8SBarry Smith   *section = dm->localSection;
412288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
412388ed4aceSMatthew G Knepley }
412488ed4aceSMatthew G Knepley 
412588ed4aceSMatthew G Knepley /*@
41261bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4127061576a5SJed Brown 
4128061576a5SJed Brown   Input Parameters:
4129061576a5SJed Brown + dm - The DM
4130061576a5SJed Brown - section - The PetscSection
4131061576a5SJed Brown 
4132061576a5SJed Brown   Level: advanced
4133061576a5SJed Brown 
4134061576a5SJed Brown   Notes:
4135061576a5SJed Brown   Use DMSetLocalSection() in new code.
4136061576a5SJed Brown 
4137061576a5SJed Brown   Any existing Section will be destroyed
4138061576a5SJed Brown 
4139061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4140061576a5SJed Brown @*/
4141061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4142061576a5SJed Brown {
4143061576a5SJed Brown   PetscErrorCode ierr;
4144061576a5SJed Brown 
4145061576a5SJed Brown   PetscFunctionBegin;
4146061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4147061576a5SJed Brown   PetscFunctionReturn(0);
4148061576a5SJed Brown }
4149061576a5SJed Brown 
4150061576a5SJed Brown /*@
4151061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
415288ed4aceSMatthew G Knepley 
415388ed4aceSMatthew G Knepley   Input Parameters:
415488ed4aceSMatthew G Knepley + dm - The DM
415588ed4aceSMatthew G Knepley - section - The PetscSection
415688ed4aceSMatthew G Knepley 
415788ed4aceSMatthew G Knepley   Level: intermediate
415888ed4aceSMatthew G Knepley 
415988ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
416088ed4aceSMatthew G Knepley 
4161061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
416288ed4aceSMatthew G Knepley @*/
4163061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
41640adebc6cSBarry Smith {
4165c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4166af122d2aSMatthew G Knepley   PetscInt       f;
416788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
416888ed4aceSMatthew G Knepley 
416988ed4aceSMatthew G Knepley   PetscFunctionBegin;
417088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4171b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
41721d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41731bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
41741bb6d2a8SBarry Smith   dm->localSection = section;
41751bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4176af122d2aSMatthew G Knepley   if (numFields) {
4177af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4178af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
41790f21e855SMatthew G. Knepley       PetscObject disc;
4180af122d2aSMatthew G Knepley       const char *name;
4181af122d2aSMatthew G Knepley 
41821bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
418344a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
41840f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4185af122d2aSMatthew G Knepley     }
4186af122d2aSMatthew G Knepley   }
4187e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
41881bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
418988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
419088ed4aceSMatthew G Knepley }
419188ed4aceSMatthew G Knepley 
41929435951eSToby Isaac /*@
4193b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
41949435951eSToby Isaac 
4195e228b242SToby Isaac   not collective
4196e228b242SToby Isaac 
41979435951eSToby Isaac   Input Parameter:
41989435951eSToby Isaac . dm - The DM
41999435951eSToby Isaac 
42009435951eSToby Isaac   Output Parameter:
42019435951eSToby 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.
42029435951eSToby 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.
42039435951eSToby Isaac 
42049435951eSToby Isaac   Level: advanced
42059435951eSToby Isaac 
42069435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
42079435951eSToby Isaac 
42089435951eSToby Isaac .seealso: DMSetDefaultConstraints()
42099435951eSToby Isaac @*/
42109435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
42119435951eSToby Isaac {
42129435951eSToby Isaac   PetscErrorCode ierr;
42139435951eSToby Isaac 
42149435951eSToby Isaac   PetscFunctionBegin;
42159435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42169435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
421745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
421845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
42199435951eSToby Isaac   PetscFunctionReturn(0);
42209435951eSToby Isaac }
42219435951eSToby Isaac 
42229435951eSToby Isaac /*@
4223b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
42249435951eSToby Isaac 
42259435951eSToby 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().
42269435951eSToby Isaac 
42279435951eSToby 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.
42289435951eSToby Isaac 
4229e228b242SToby Isaac   collective on dm
4230e228b242SToby Isaac 
42319435951eSToby Isaac   Input Parameters:
42329435951eSToby Isaac + dm - The DM
4233e228b242SToby 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).
4234e228b242SToby 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).
42359435951eSToby Isaac 
42369435951eSToby Isaac   Level: advanced
42379435951eSToby Isaac 
42389435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
42399435951eSToby Isaac 
42409435951eSToby Isaac .seealso: DMGetDefaultConstraints()
42419435951eSToby Isaac @*/
42429435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
42439435951eSToby Isaac {
4244e228b242SToby Isaac   PetscMPIInt result;
42459435951eSToby Isaac   PetscErrorCode ierr;
42469435951eSToby Isaac 
42479435951eSToby Isaac   PetscFunctionBegin;
42489435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4249e228b242SToby Isaac   if (section) {
4250e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4251e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4252f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4253e228b242SToby Isaac   }
4254e228b242SToby Isaac   if (mat) {
4255e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4256e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4257f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4258e228b242SToby Isaac   }
42599435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42609435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
42619435951eSToby Isaac   dm->defaultConstraintSection = section;
42629435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
42639435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
42649435951eSToby Isaac   dm->defaultConstraintMat = mat;
42659435951eSToby Isaac   PetscFunctionReturn(0);
42669435951eSToby Isaac }
42679435951eSToby Isaac 
4268497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4269507e4973SMatthew G. Knepley /*
4270507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4271507e4973SMatthew G. Knepley 
4272507e4973SMatthew G. Knepley   Input Parameters:
4273507e4973SMatthew G. Knepley + dm - The DM
4274507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4275507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4276507e4973SMatthew G. Knepley 
4277507e4973SMatthew G. Knepley   Level: intermediate
4278507e4973SMatthew G. Knepley 
42791bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4280507e4973SMatthew G. Knepley */
4281f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4282507e4973SMatthew G. Knepley {
4283507e4973SMatthew G. Knepley   MPI_Comm        comm;
4284507e4973SMatthew G. Knepley   PetscLayout     layout;
4285507e4973SMatthew G. Knepley   const PetscInt *ranges;
4286507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4287507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4288507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4289507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4290507e4973SMatthew G. Knepley 
4291507e4973SMatthew G. Knepley   PetscFunctionBegin;
4292507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4293507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4294507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4295507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4296507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4297507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4298507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4299507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4300507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4301507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4302507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4303507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4304f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4305507e4973SMatthew G. Knepley 
4306507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4307507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4308507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4309507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4310507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4311507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4312507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4313507e4973SMatthew 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;}
4314507e4973SMatthew 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;}
4315507e4973SMatthew G. Knepley     if (gdof < 0) {
4316507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4317507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4318507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4319507e4973SMatthew G. Knepley 
4320507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4321507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4322507e4973SMatthew 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;}
4323507e4973SMatthew G. Knepley       }
4324507e4973SMatthew G. Knepley     }
4325507e4973SMatthew G. Knepley   }
4326507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4327507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4328b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4329507e4973SMatthew G. Knepley   if (!gvalid) {
4330507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4331507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4332507e4973SMatthew G. Knepley   }
4333507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4334507e4973SMatthew G. Knepley }
4335f741bcd2SMatthew G. Knepley #endif
4336507e4973SMatthew G. Knepley 
433788ed4aceSMatthew G Knepley /*@
4338e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
433988ed4aceSMatthew G Knepley 
4340d083f849SBarry Smith   Collective on dm
43418b1ab98fSJed Brown 
434288ed4aceSMatthew G Knepley   Input Parameter:
434388ed4aceSMatthew G Knepley . dm - The DM
434488ed4aceSMatthew G Knepley 
434588ed4aceSMatthew G Knepley   Output Parameter:
434688ed4aceSMatthew G Knepley . section - The PetscSection
434788ed4aceSMatthew G Knepley 
434888ed4aceSMatthew G Knepley   Level: intermediate
434988ed4aceSMatthew G Knepley 
435088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
435188ed4aceSMatthew G Knepley 
435292fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
435388ed4aceSMatthew G Knepley @*/
4354e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
43550adebc6cSBarry Smith {
435688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
435788ed4aceSMatthew G Knepley 
435888ed4aceSMatthew G Knepley   PetscFunctionBegin;
435988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
436088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
43611bb6d2a8SBarry Smith   if (!dm->globalSection) {
4362fd59a867SMatthew G. Knepley     PetscSection s;
4363fd59a867SMatthew G. Knepley 
436492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4365fd59a867SMatthew 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");
436633907cc2SStefano 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");
43671bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4368cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
43691bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
43701bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
437188ed4aceSMatthew G Knepley   }
43721bb6d2a8SBarry Smith   *section = dm->globalSection;
437388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
437488ed4aceSMatthew G Knepley }
437588ed4aceSMatthew G Knepley 
4376b21d0597SMatthew G Knepley /*@
4377e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4378b21d0597SMatthew G Knepley 
4379b21d0597SMatthew G Knepley   Input Parameters:
4380b21d0597SMatthew G Knepley + dm - The DM
43815080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4382b21d0597SMatthew G Knepley 
4383b21d0597SMatthew G Knepley   Level: intermediate
4384b21d0597SMatthew G Knepley 
4385b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4386b21d0597SMatthew G Knepley 
438792fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4388b21d0597SMatthew G Knepley @*/
4389e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
43900adebc6cSBarry Smith {
4391b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4392b21d0597SMatthew G Knepley 
4393b21d0597SMatthew G Knepley   PetscFunctionBegin;
4394b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43955080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43961d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
43971bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
43981bb6d2a8SBarry Smith   dm->globalSection = section;
4399497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
44001bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4401507e4973SMatthew G. Knepley #endif
4402b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4403b21d0597SMatthew G Knepley }
4404b21d0597SMatthew G Knepley 
440588ed4aceSMatthew G Knepley /*@
44061bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
440788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
440888ed4aceSMatthew G Knepley 
440988ed4aceSMatthew G Knepley   Input Parameter:
441088ed4aceSMatthew G Knepley . dm - The DM
441188ed4aceSMatthew G Knepley 
441288ed4aceSMatthew G Knepley   Output Parameter:
441388ed4aceSMatthew G Knepley . sf - The PetscSF
441488ed4aceSMatthew G Knepley 
441588ed4aceSMatthew G Knepley   Level: intermediate
441688ed4aceSMatthew G Knepley 
441788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
441888ed4aceSMatthew G Knepley 
44191bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
442088ed4aceSMatthew G Knepley @*/
44211bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
44220adebc6cSBarry Smith {
442388ed4aceSMatthew G Knepley   PetscInt       nroots;
442488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
442588ed4aceSMatthew G Knepley 
442688ed4aceSMatthew G Knepley   PetscFunctionBegin;
442788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
442888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
44291bb6d2a8SBarry Smith   if (!dm->sectionSF) {
44301bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
443133907cc2SStefano Zampini   }
44321bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
443388ed4aceSMatthew G Knepley   if (nroots < 0) {
443488ed4aceSMatthew G Knepley     PetscSection section, gSection;
443588ed4aceSMatthew G Knepley 
443692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
443731ea6d37SMatthew G Knepley     if (section) {
4438e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
44391bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
444031ea6d37SMatthew G Knepley     } else {
44410298fd71SBarry Smith       *sf = NULL;
444231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
444331ea6d37SMatthew G Knepley     }
444488ed4aceSMatthew G Knepley   }
44451bb6d2a8SBarry Smith   *sf = dm->sectionSF;
444688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
444788ed4aceSMatthew G Knepley }
444888ed4aceSMatthew G Knepley 
444988ed4aceSMatthew G Knepley /*@
44501bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
445188ed4aceSMatthew G Knepley 
445288ed4aceSMatthew G Knepley   Input Parameters:
445388ed4aceSMatthew G Knepley + dm - The DM
445488ed4aceSMatthew G Knepley - sf - The PetscSF
445588ed4aceSMatthew G Knepley 
445688ed4aceSMatthew G Knepley   Level: intermediate
445788ed4aceSMatthew G Knepley 
445888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
445988ed4aceSMatthew G Knepley 
44601bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
446188ed4aceSMatthew G Knepley @*/
44621bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
44630adebc6cSBarry Smith {
446488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
446588ed4aceSMatthew G Knepley 
446688ed4aceSMatthew G Knepley   PetscFunctionBegin;
446788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4468b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
446933907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
44701bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
44711bb6d2a8SBarry Smith   dm->sectionSF = sf;
447288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
447388ed4aceSMatthew G Knepley }
447488ed4aceSMatthew G Knepley 
447588ed4aceSMatthew G Knepley /*@C
44761bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
447788ed4aceSMatthew G Knepley   describing the data layout.
447888ed4aceSMatthew G Knepley 
447988ed4aceSMatthew G Knepley   Input Parameters:
448088ed4aceSMatthew G Knepley + dm - The DM
448188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
448288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
448388ed4aceSMatthew G Knepley 
44841bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
448588ed4aceSMatthew G Knepley 
44861bb6d2a8SBarry Smith   Level: developer
44871bb6d2a8SBarry Smith 
44881bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
44891bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
44901bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
44911bb6d2a8SBarry Smith 
44921bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
449388ed4aceSMatthew G Knepley @*/
44941bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
449588ed4aceSMatthew G Knepley {
449682f516ccSBarry Smith   MPI_Comm       comm;
449788ed4aceSMatthew G Knepley   PetscLayout    layout;
449888ed4aceSMatthew G Knepley   const PetscInt *ranges;
449988ed4aceSMatthew G Knepley   PetscInt       *local;
450088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4501ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
450288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
450388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
450488ed4aceSMatthew G Knepley 
450588ed4aceSMatthew G Knepley   PetscFunctionBegin;
450688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4507367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
450888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
450988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
451088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
451188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
451288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
451388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
451488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
451588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
451688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4517ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
45186636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
451988ed4aceSMatthew G Knepley 
45206636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
45216636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4522235fbf56SMatthew G. Knepley     if (gcdof > (gdof < 0 ? -(gdof+1) : gdof)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d has %d constraints > %d dof", p, gcdof, (gdof < 0 ? -(gdof+1) : gdof));
45236636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
452488ed4aceSMatthew G Knepley   }
4525785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4526785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
452788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
45281f588964SMatthew G Knepley     const PetscInt *cind;
45296636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
453088ed4aceSMatthew G Knepley 
453188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
453288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
453388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
453488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
453588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
45366636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
453788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
45386636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
45396636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
45406636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4541057b4bcdSMatthew G Knepley       if (gsize != dof) SETERRQ4(comm, PETSC_ERR_ARG_WRONG, "Global dof %d for point %d is neither the constrained size %d, nor the unconstrained %d", gsize, p, dof-cdof, dof);
45426636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
45436636e97aSMatthew G Knepley     }
454488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
454588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
454688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
454788ed4aceSMatthew G Knepley     }
454888ed4aceSMatthew G Knepley     if (gdof < 0) {
45496636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
455088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
455188ed4aceSMatthew G Knepley 
455205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
455331d3f06eSJed Brown         if (r < 0) r = -(r+2);
455405376888SMatthew G. Knepley         if ((r < 0) || (r >= size)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d mapped to invalid process %d (%d, %d)", p, r, gdof, goff);
455588ed4aceSMatthew G Knepley         remote[l].rank  = r;
455688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
455788ed4aceSMatthew G Knepley       }
455888ed4aceSMatthew G Knepley     } else {
45596636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
456088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
456188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
456288ed4aceSMatthew G Knepley       }
456388ed4aceSMatthew G Knepley     }
456488ed4aceSMatthew G Knepley   }
45656636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
456688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
45671bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
456888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
456988ed4aceSMatthew G Knepley }
4570af122d2aSMatthew G Knepley 
4571b21d0597SMatthew G Knepley /*@
4572b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4573b21d0597SMatthew G Knepley 
4574b21d0597SMatthew G Knepley   Input Parameter:
4575b21d0597SMatthew G Knepley . dm - The DM
4576b21d0597SMatthew G Knepley 
4577b21d0597SMatthew G Knepley   Output Parameter:
4578b21d0597SMatthew G Knepley . sf - The PetscSF
4579b21d0597SMatthew G Knepley 
4580b21d0597SMatthew G Knepley   Level: intermediate
4581b21d0597SMatthew G Knepley 
4582b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4583b21d0597SMatthew G Knepley 
45841bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4585b21d0597SMatthew G Knepley @*/
45860adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
45870adebc6cSBarry Smith {
4588b21d0597SMatthew G Knepley   PetscFunctionBegin;
4589b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4590b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4591b21d0597SMatthew G Knepley   *sf = dm->sf;
4592b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4593b21d0597SMatthew G Knepley }
4594b21d0597SMatthew G Knepley 
4595057b4bcdSMatthew G Knepley /*@
4596057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4597057b4bcdSMatthew G Knepley 
4598057b4bcdSMatthew G Knepley   Input Parameters:
4599057b4bcdSMatthew G Knepley + dm - The DM
4600057b4bcdSMatthew G Knepley - sf - The PetscSF
4601057b4bcdSMatthew G Knepley 
4602057b4bcdSMatthew G Knepley   Level: intermediate
4603057b4bcdSMatthew G Knepley 
46041bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4605057b4bcdSMatthew G Knepley @*/
46060adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
46070adebc6cSBarry Smith {
4608057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4609057b4bcdSMatthew G Knepley 
4610057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4611057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4612b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4613057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
461433907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4615057b4bcdSMatthew G Knepley   dm->sf = sf;
4616057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4617057b4bcdSMatthew G Knepley }
4618057b4bcdSMatthew G Knepley 
461934aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
462034aa8a36SMatthew G. Knepley {
462134aa8a36SMatthew G. Knepley   PetscClassId   id;
462234aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
462334aa8a36SMatthew G. Knepley 
462434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
462534aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
462634aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
462734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
462834aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
462934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
463017c1d62eSMatthew G. Knepley   } else {
463117c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
463234aa8a36SMatthew G. Knepley   }
463334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
463434aa8a36SMatthew G. Knepley }
463534aa8a36SMatthew G. Knepley 
463644a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
463744a7f3ddSMatthew G. Knepley {
463844a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
463944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
464044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
464144a7f3ddSMatthew G. Knepley 
464244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
464344a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
464444a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
464544a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
464644a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
464744a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
464844a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
464944a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
465044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
465144a7f3ddSMatthew G. Knepley }
465244a7f3ddSMatthew G. Knepley 
465344a7f3ddSMatthew G. Knepley /*@
465444a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
465544a7f3ddSMatthew G. Knepley 
4656d083f849SBarry Smith   Logically collective on dm
465744a7f3ddSMatthew G. Knepley 
465844a7f3ddSMatthew G. Knepley   Input Parameter:
465944a7f3ddSMatthew G. Knepley . dm - The DM
466044a7f3ddSMatthew G. Knepley 
466144a7f3ddSMatthew G. Knepley   Level: intermediate
466244a7f3ddSMatthew G. Knepley 
466344a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
466444a7f3ddSMatthew G. Knepley @*/
466544a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
466644a7f3ddSMatthew G. Knepley {
466744a7f3ddSMatthew G. Knepley   PetscInt       f;
466844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
466944a7f3ddSMatthew G. Knepley 
467044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
467144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
467244a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
467344a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
467444a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
467544a7f3ddSMatthew G. Knepley   }
467644a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
467744a7f3ddSMatthew G. Knepley   dm->fields = NULL;
467844a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
467944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
468044a7f3ddSMatthew G. Knepley }
468144a7f3ddSMatthew G. Knepley 
4682689b5837SMatthew G. Knepley /*@
4683689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4684689b5837SMatthew G. Knepley 
4685689b5837SMatthew G. Knepley   Not collective
4686689b5837SMatthew G. Knepley 
4687689b5837SMatthew G. Knepley   Input Parameter:
4688689b5837SMatthew G. Knepley . dm - The DM
4689689b5837SMatthew G. Knepley 
4690689b5837SMatthew G. Knepley   Output Parameter:
4691689b5837SMatthew G. Knepley . Nf - The number of fields
4692689b5837SMatthew G. Knepley 
4693689b5837SMatthew G. Knepley   Level: intermediate
4694689b5837SMatthew G. Knepley 
4695689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4696689b5837SMatthew G. Knepley @*/
46970f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
46980f21e855SMatthew G. Knepley {
46990f21e855SMatthew G. Knepley   PetscFunctionBegin;
47000f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4701534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
470244a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4703af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4704af122d2aSMatthew G Knepley }
4705af122d2aSMatthew G Knepley 
4706689b5837SMatthew G. Knepley /*@
4707689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4708689b5837SMatthew G. Knepley 
4709d083f849SBarry Smith   Logically collective on dm
4710689b5837SMatthew G. Knepley 
4711689b5837SMatthew G. Knepley   Input Parameters:
4712689b5837SMatthew G. Knepley + dm - The DM
4713689b5837SMatthew G. Knepley - Nf - The number of fields
4714689b5837SMatthew G. Knepley 
4715689b5837SMatthew G. Knepley   Level: intermediate
4716689b5837SMatthew G. Knepley 
4717689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4718689b5837SMatthew G. Knepley @*/
4719af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4720af122d2aSMatthew G Knepley {
47210f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4722af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4723af122d2aSMatthew G Knepley 
4724af122d2aSMatthew G Knepley   PetscFunctionBegin;
4725af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
472644a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
47270f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
47280f21e855SMatthew G. Knepley     PetscContainer obj;
47290f21e855SMatthew G. Knepley 
47300f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
473144a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
47320f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4733af122d2aSMatthew G Knepley   }
4734af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4735af122d2aSMatthew G Knepley }
4736af122d2aSMatthew G Knepley 
4737c1929be8SMatthew G. Knepley /*@
4738c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4739c1929be8SMatthew G. Knepley 
4740c1929be8SMatthew G. Knepley   Not collective
4741c1929be8SMatthew G. Knepley 
4742c1929be8SMatthew G. Knepley   Input Parameters:
4743c1929be8SMatthew G. Knepley + dm - The DM
4744c1929be8SMatthew G. Knepley - f  - The field number
4745c1929be8SMatthew G. Knepley 
474644a7f3ddSMatthew G. Knepley   Output Parameters:
474744a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
474844a7f3ddSMatthew G. Knepley - field - The discretization object
4749c1929be8SMatthew G. Knepley 
475044a7f3ddSMatthew G. Knepley   Level: intermediate
4751c1929be8SMatthew G. Knepley 
475244a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4753c1929be8SMatthew G. Knepley @*/
475444a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4755af122d2aSMatthew G Knepley {
4756af122d2aSMatthew G Knepley   PetscFunctionBegin;
4757af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
475844a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
475944a7f3ddSMatthew 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);
476044a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
476144a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4762decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4763decb47aaSMatthew G. Knepley }
4764decb47aaSMatthew G. Knepley 
4765083401c6SMatthew G. Knepley /* Does not clear the DS */
4766083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4767083401c6SMatthew G. Knepley {
4768083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4769083401c6SMatthew G. Knepley 
4770083401c6SMatthew G. Knepley   PetscFunctionBegin;
4771083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4772083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4773083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4774083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4775083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4776083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4777083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4778083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4779083401c6SMatthew G. Knepley }
4780083401c6SMatthew G. Knepley 
4781c1929be8SMatthew G. Knepley /*@
4782c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4783c1929be8SMatthew G. Knepley 
4784d083f849SBarry Smith   Logically collective on dm
4785c1929be8SMatthew G. Knepley 
4786c1929be8SMatthew G. Knepley   Input Parameters:
4787c1929be8SMatthew G. Knepley + dm    - The DM
4788c1929be8SMatthew G. Knepley . f     - The field number
478944a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4790c1929be8SMatthew G. Knepley - field - The discretization object
4791c1929be8SMatthew G. Knepley 
479244a7f3ddSMatthew G. Knepley   Level: intermediate
4793c1929be8SMatthew G. Knepley 
479444a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4795c1929be8SMatthew G. Knepley @*/
479644a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4797decb47aaSMatthew G. Knepley {
4798decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4799decb47aaSMatthew G. Knepley 
4800decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4801decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4802e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
480344a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4804e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4805083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
480634aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
48072df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
480844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
480944a7f3ddSMatthew G. Knepley }
481044a7f3ddSMatthew G. Knepley 
481144a7f3ddSMatthew G. Knepley /*@
481244a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
481344a7f3ddSMatthew G. Knepley 
4814d083f849SBarry Smith   Logically collective on dm
481544a7f3ddSMatthew G. Knepley 
481644a7f3ddSMatthew G. Knepley   Input Parameters:
481744a7f3ddSMatthew G. Knepley + dm    - The DM
481844a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
481944a7f3ddSMatthew G. Knepley - field - The discretization object
482044a7f3ddSMatthew G. Knepley 
482144a7f3ddSMatthew G. Knepley   Level: intermediate
482244a7f3ddSMatthew G. Knepley 
482344a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
482444a7f3ddSMatthew G. Knepley @*/
482544a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
482644a7f3ddSMatthew G. Knepley {
482744a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
482844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
482944a7f3ddSMatthew G. Knepley 
483044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
483144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
483244a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
483344a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
483444a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
483544a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
483644a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
483744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
483844a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
483934aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
48402df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4841af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4842af122d2aSMatthew G Knepley }
48436636e97aSMatthew G Knepley 
4844e5e52638SMatthew G. Knepley /*@
4845e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4846e5e52638SMatthew G. Knepley 
4847d083f849SBarry Smith   Collective on dm
4848e5e52638SMatthew G. Knepley 
4849e5e52638SMatthew G. Knepley   Input Parameter:
4850e5e52638SMatthew G. Knepley . dm - The DM
4851e5e52638SMatthew G. Knepley 
4852e5e52638SMatthew G. Knepley   Output Parameter:
4853e5e52638SMatthew G. Knepley . newdm - The DM
4854e5e52638SMatthew G. Knepley 
4855e5e52638SMatthew G. Knepley   Level: advanced
4856e5e52638SMatthew G. Knepley 
4857e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4858e5e52638SMatthew G. Knepley @*/
4859e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4860e5e52638SMatthew G. Knepley {
4861e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4862e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4863e5e52638SMatthew G. Knepley 
4864e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4865e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4866e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4867e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4868e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4869e5e52638SMatthew G. Knepley     DMLabel     label;
4870e5e52638SMatthew G. Knepley     PetscObject field;
487134aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4872e5e52638SMatthew G. Knepley 
4873e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4874e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
487534aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
487634aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
487734aa8a36SMatthew G. Knepley   }
487834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
487934aa8a36SMatthew G. Knepley }
488034aa8a36SMatthew G. Knepley 
488134aa8a36SMatthew G. Knepley /*@
488234aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
488334aa8a36SMatthew G. Knepley 
488434aa8a36SMatthew G. Knepley   Not collective
488534aa8a36SMatthew G. Knepley 
488634aa8a36SMatthew G. Knepley   Input Parameters:
488734aa8a36SMatthew G. Knepley + dm - The DM object
488834aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
488934aa8a36SMatthew G. Knepley 
489034aa8a36SMatthew G. Knepley   Output Parameter:
489134aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
489234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
489334aa8a36SMatthew G. Knepley 
489434aa8a36SMatthew G. Knepley   Notes:
489534aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
489634aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
489734aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4898979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
489934aa8a36SMatthew G. Knepley 
490034aa8a36SMatthew G. Knepley   Level: developer
490134aa8a36SMatthew G. Knepley 
490234aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
490334aa8a36SMatthew G. Knepley @*/
490434aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
490534aa8a36SMatthew G. Knepley {
490634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
490734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4908534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4909534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
491034aa8a36SMatthew G. Knepley   if (f < 0) {
491134aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
491234aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
491334aa8a36SMatthew G. Knepley   } else {
491434aa8a36SMatthew G. Knepley     PetscInt       Nf;
491534aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
491634aa8a36SMatthew G. Knepley 
491734aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
491834aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
491934aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
492034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
492134aa8a36SMatthew G. Knepley   }
492234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
492334aa8a36SMatthew G. Knepley }
492434aa8a36SMatthew G. Knepley 
492534aa8a36SMatthew G. Knepley /*@
492634aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
492734aa8a36SMatthew G. Knepley 
492834aa8a36SMatthew G. Knepley   Not collective
492934aa8a36SMatthew G. Knepley 
493034aa8a36SMatthew G. Knepley   Input Parameters:
493134aa8a36SMatthew G. Knepley + dm         - The DM object
493234aa8a36SMatthew G. Knepley . f          - The field number
493334aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
493434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
493534aa8a36SMatthew G. Knepley 
493634aa8a36SMatthew G. Knepley   Notes:
493734aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
493834aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
493934aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4940979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
494134aa8a36SMatthew G. Knepley 
494234aa8a36SMatthew G. Knepley   Level: developer
494334aa8a36SMatthew G. Knepley 
494434aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
494534aa8a36SMatthew G. Knepley @*/
494634aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
494734aa8a36SMatthew G. Knepley {
494834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
494934aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
495034aa8a36SMatthew G. Knepley   if (f < 0) {
495134aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
495234aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
495334aa8a36SMatthew G. Knepley   } else {
495434aa8a36SMatthew G. Knepley     PetscInt       Nf;
495534aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
495634aa8a36SMatthew G. Knepley 
495734aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
495834aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
495934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
496034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4961e5e52638SMatthew G. Knepley   }
4962e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4963e5e52638SMatthew G. Knepley }
4964e5e52638SMatthew G. Knepley 
4965b0441da4SMatthew G. Knepley /*@
4966b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4967b0441da4SMatthew G. Knepley 
4968b0441da4SMatthew G. Knepley   Not collective
4969b0441da4SMatthew G. Knepley 
4970b0441da4SMatthew G. Knepley   Input Parameters:
4971b0441da4SMatthew G. Knepley . dm - The DM object
4972b0441da4SMatthew G. Knepley 
4973b0441da4SMatthew G. Knepley   Output Parameter:
4974b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4975b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4976b0441da4SMatthew G. Knepley 
4977b0441da4SMatthew G. Knepley   Notes:
4978b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4979b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4980b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4981b0441da4SMatthew G. Knepley 
4982b0441da4SMatthew G. Knepley   Level: developer
4983b0441da4SMatthew G. Knepley 
4984b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4985b0441da4SMatthew G. Knepley @*/
4986b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4987b0441da4SMatthew G. Knepley {
4988b0441da4SMatthew G. Knepley   PetscInt       Nf;
4989b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4990b0441da4SMatthew G. Knepley 
4991b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4992b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4993534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4994534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4995b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4996b0441da4SMatthew G. Knepley   if (!Nf) {
4997b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4998b0441da4SMatthew G. Knepley   } else {
4999b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5000b0441da4SMatthew G. Knepley   }
5001b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5002b0441da4SMatthew G. Knepley }
5003b0441da4SMatthew G. Knepley 
5004b0441da4SMatthew G. Knepley /*@
5005b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5006b0441da4SMatthew G. Knepley 
5007b0441da4SMatthew G. Knepley   Not collective
5008b0441da4SMatthew G. Knepley 
5009b0441da4SMatthew G. Knepley   Input Parameters:
5010b0441da4SMatthew G. Knepley + dm         - The DM object
5011b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5012b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5013b0441da4SMatthew G. Knepley 
5014b0441da4SMatthew G. Knepley   Notes:
5015b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5016b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5017b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5018b0441da4SMatthew G. Knepley 
5019b0441da4SMatthew G. Knepley   Level: developer
5020b0441da4SMatthew G. Knepley 
5021b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5022b0441da4SMatthew G. Knepley @*/
5023b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5024b0441da4SMatthew G. Knepley {
5025b0441da4SMatthew G. Knepley   PetscInt       Nf;
5026b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5027b0441da4SMatthew G. Knepley 
5028b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5029b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5030b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5031b0441da4SMatthew G. Knepley   if (!Nf) {
5032b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5033b0441da4SMatthew G. Knepley   } else {
5034b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5035e5e52638SMatthew G. Knepley   }
5036e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5037e5e52638SMatthew G. Knepley }
5038e5e52638SMatthew G. Knepley 
5039783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
5040783e2ec8SMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, const char labelname[])
5041783e2ec8SMatthew G. Knepley {
5042783e2ec8SMatthew G. Knepley   DMLabel        label;
5043783e2ec8SMatthew G. Knepley   PetscObject    obj;
5044783e2ec8SMatthew G. Knepley   PetscClassId   id;
5045783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5046783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5047783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5048783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5049783e2ec8SMatthew G. Knepley 
5050783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5051783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5052783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5053783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5054783e2ec8SMatthew G. Knepley   ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
5055783e2ec8SMatthew G. Knepley   if (isFE && label) {
5056783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5057783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5058783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
5059783e2ec8SMatthew G. Knepley       const char *lname;
5060783e2ec8SMatthew G. Knepley 
506156cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, &lname, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5062783e2ec8SMatthew G. Knepley       ierr = PetscStrcmp(lname, labelname, &duplicate);CHKERRQ(ierr);
5063783e2ec8SMatthew G. Knepley       if (duplicate) break;
5064783e2ec8SMatthew G. Knepley     }
5065783e2ec8SMatthew G. Knepley     if (!duplicate) {
5066783e2ec8SMatthew G. Knepley       DM plex;
5067783e2ec8SMatthew G. Knepley 
5068783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5069783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5070783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5071783e2ec8SMatthew G. Knepley     }
5072783e2ec8SMatthew G. Knepley   }
5073783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5074783e2ec8SMatthew G. Knepley }
5075783e2ec8SMatthew G. Knepley 
5076e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5077e5e52638SMatthew G. Knepley {
5078e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5079e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5080e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5081e5e52638SMatthew G. Knepley 
5082e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5083e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5084e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5085e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5086b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5087e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5088e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5089e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5090e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5091e5e52638SMatthew G. Knepley }
5092e5e52638SMatthew G. Knepley 
5093e5e52638SMatthew G. Knepley /*@
5094e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5095e5e52638SMatthew G. Knepley 
5096e5e52638SMatthew G. Knepley   Not collective
5097e5e52638SMatthew G. Knepley 
5098e5e52638SMatthew G. Knepley   Input Parameter:
5099e5e52638SMatthew G. Knepley . dm - The DM
5100e5e52638SMatthew G. Knepley 
5101e5e52638SMatthew G. Knepley   Output Parameter:
5102e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5103e5e52638SMatthew G. Knepley 
5104e5e52638SMatthew G. Knepley   Level: intermediate
5105e5e52638SMatthew G. Knepley 
5106e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5107e5e52638SMatthew G. Knepley @*/
5108e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5109e5e52638SMatthew G. Knepley {
5110e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5111e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5112534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5113e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5114e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5115e5e52638SMatthew G. Knepley }
5116e5e52638SMatthew G. Knepley 
5117e5e52638SMatthew G. Knepley /*@
5118e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5119e5e52638SMatthew G. Knepley 
5120d083f849SBarry Smith   Logically collective on dm
5121e5e52638SMatthew G. Knepley 
5122e5e52638SMatthew G. Knepley   Input Parameter:
5123e5e52638SMatthew G. Knepley . dm - The DM
5124e5e52638SMatthew G. Knepley 
5125e5e52638SMatthew G. Knepley   Level: intermediate
5126e5e52638SMatthew G. Knepley 
5127e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5128e5e52638SMatthew G. Knepley @*/
5129e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5130e5e52638SMatthew G. Knepley {
5131e5e52638SMatthew G. Knepley   PetscInt       s;
5132e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5133e5e52638SMatthew G. Knepley 
5134e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5135e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5136e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5137e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5138e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5139b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5140e5e52638SMatthew G. Knepley   }
5141e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5142e5e52638SMatthew G. Knepley   dm->probs = NULL;
5143e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5144e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5145e5e52638SMatthew G. Knepley }
5146e5e52638SMatthew G. Knepley 
5147e5e52638SMatthew G. Knepley /*@
5148e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5149e5e52638SMatthew G. Knepley 
5150e5e52638SMatthew G. Knepley   Not collective
5151e5e52638SMatthew G. Knepley 
5152e5e52638SMatthew G. Knepley   Input Parameter:
5153e5e52638SMatthew G. Knepley . dm    - The DM
5154e5e52638SMatthew G. Knepley 
5155e5e52638SMatthew G. Knepley   Output Parameter:
5156e5e52638SMatthew G. Knepley . prob - The default PetscDS
5157e5e52638SMatthew G. Knepley 
5158e5e52638SMatthew G. Knepley   Level: intermediate
5159e5e52638SMatthew G. Knepley 
5160e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5161e5e52638SMatthew G. Knepley @*/
5162e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5163e5e52638SMatthew G. Knepley {
5164b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5165b0143b4dSMatthew G. Knepley 
5166e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5167e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5168e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5169b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5170b0143b4dSMatthew G. Knepley     PetscDS ds;
5171b0143b4dSMatthew G. Knepley 
5172b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5173b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5174b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5175b0143b4dSMatthew G. Knepley   }
5176b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5177e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5178e5e52638SMatthew G. Knepley }
5179e5e52638SMatthew G. Knepley 
5180e5e52638SMatthew G. Knepley /*@
5181e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5182e5e52638SMatthew G. Knepley 
5183e5e52638SMatthew G. Knepley   Not collective
5184e5e52638SMatthew G. Knepley 
5185e5e52638SMatthew G. Knepley   Input Parameters:
5186e5e52638SMatthew G. Knepley + dm    - The DM
5187e5e52638SMatthew G. Knepley - point - Cell for the DS
5188e5e52638SMatthew G. Knepley 
5189e5e52638SMatthew G. Knepley   Output Parameter:
5190e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5191e5e52638SMatthew G. Knepley 
5192e5e52638SMatthew G. Knepley   Level: developer
5193e5e52638SMatthew G. Knepley 
5194b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5195e5e52638SMatthew G. Knepley @*/
5196e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5197e5e52638SMatthew G. Knepley {
5198e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5199e5e52638SMatthew G. Knepley   PetscInt       s;
5200e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5201e5e52638SMatthew G. Knepley 
5202e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5203e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5204e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5205360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5206e5e52638SMatthew G. Knepley   *prob = NULL;
5207e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5208e5e52638SMatthew G. Knepley     PetscInt val;
5209e5e52638SMatthew G. Knepley 
5210e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5211e5e52638SMatthew G. Knepley     else {
5212e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5213e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5214e5e52638SMatthew G. Knepley     }
5215e5e52638SMatthew G. Knepley   }
5216e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5217e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5218e5e52638SMatthew G. Knepley }
5219e5e52638SMatthew G. Knepley 
5220e5e52638SMatthew G. Knepley /*@
5221e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5222e5e52638SMatthew G. Knepley 
5223e5e52638SMatthew G. Knepley   Not collective
5224e5e52638SMatthew G. Knepley 
5225e5e52638SMatthew G. Knepley   Input Parameters:
5226e5e52638SMatthew G. Knepley + dm    - The DM
5227e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5228e5e52638SMatthew G. Knepley 
5229b3cf3223SMatthew G. Knepley   Output Parameters:
5230b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5231b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5232e5e52638SMatthew G. Knepley 
5233e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5234e5e52638SMatthew G. Knepley 
5235e5e52638SMatthew G. Knepley   Level: advanced
5236e5e52638SMatthew G. Knepley 
5237e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5238e5e52638SMatthew G. Knepley @*/
5239b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5240e5e52638SMatthew G. Knepley {
5241e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5242e5e52638SMatthew G. Knepley 
5243e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5244e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5245e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5246b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5247b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5248e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5249b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5250b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5251b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5252b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5253b3cf3223SMatthew G. Knepley     }
5254e5e52638SMatthew G. Knepley   }
52552df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5256e5e52638SMatthew G. Knepley }
5257e5e52638SMatthew G. Knepley 
5258e5e52638SMatthew G. Knepley /*@
5259083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5260083401c6SMatthew G. Knepley 
5261083401c6SMatthew G. Knepley   Collective on dm
5262083401c6SMatthew G. Knepley 
5263083401c6SMatthew G. Knepley   Input Parameters:
5264083401c6SMatthew G. Knepley + dm     - The DM
5265083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5266083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5267083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5268083401c6SMatthew G. Knepley 
5269083401c6SMatthew 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,
5270083401c6SMatthew G. Knepley   the fields argument is ignored.
5271083401c6SMatthew G. Knepley 
5272083401c6SMatthew G. Knepley   Level: advanced
5273083401c6SMatthew G. Knepley 
5274083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5275083401c6SMatthew G. Knepley @*/
5276083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5277083401c6SMatthew G. Knepley {
5278083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5279083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5280083401c6SMatthew G. Knepley 
5281083401c6SMatthew G. Knepley   PetscFunctionBegin;
5282083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5283083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5284083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5285083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5286083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5287083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5288083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5289083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5290083401c6SMatthew G. Knepley     }
5291083401c6SMatthew G. Knepley   }
5292083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5293083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5294083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5295083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5296083401c6SMatthew G. Knepley   if (!label) {
5297083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5298083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5299083401c6SMatthew G. Knepley     Nds = 0;
5300083401c6SMatthew G. Knepley   }
5301083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5302083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5303083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5304083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5305083401c6SMatthew G. Knepley }
5306083401c6SMatthew G. Knepley 
5307083401c6SMatthew G. Knepley /*@
5308e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5309e5e52638SMatthew G. Knepley 
5310e5e52638SMatthew G. Knepley   Not collective
5311e5e52638SMatthew G. Knepley 
5312e5e52638SMatthew G. Knepley   Input Parameters:
5313e5e52638SMatthew G. Knepley + dm  - The DM
5314e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5315e5e52638SMatthew G. Knepley 
5316e5e52638SMatthew G. Knepley   Output Parameters:
5317b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5318b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5319083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5320e5e52638SMatthew G. Knepley 
5321e5e52638SMatthew G. Knepley   Level: advanced
5322e5e52638SMatthew G. Knepley 
5323e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5324e5e52638SMatthew G. Knepley @*/
5325b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5326e5e52638SMatthew G. Knepley {
5327e5e52638SMatthew G. Knepley   PetscInt       Nds;
5328e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5329e5e52638SMatthew G. Knepley 
5330e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5331e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5332e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5333e5e52638SMatthew 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);
5334e5e52638SMatthew G. Knepley   if (label) {
5335e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5336e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5337e5e52638SMatthew G. Knepley   }
5338b3cf3223SMatthew G. Knepley   if (fields) {
5339b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5340b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5341b3cf3223SMatthew G. Knepley   }
5342e5e52638SMatthew G. Knepley   if (ds) {
5343b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5344e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5345e5e52638SMatthew G. Knepley   }
5346e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5347e5e52638SMatthew G. Knepley }
5348e5e52638SMatthew G. Knepley 
5349e5e52638SMatthew G. Knepley /*@
5350083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5351e5e52638SMatthew G. Knepley 
5352083401c6SMatthew G. Knepley   Not collective
5353e5e52638SMatthew G. Knepley 
5354e5e52638SMatthew G. Knepley   Input Parameters:
5355e5e52638SMatthew G. Knepley + dm     - The DM
5356083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5357083401c6SMatthew G. Knepley . label  - The region label, or NULL
5358083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5359083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5360e5e52638SMatthew G. Knepley 
5361e5e52638SMatthew G. Knepley   Level: advanced
5362e5e52638SMatthew G. Knepley 
5363083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5364e5e52638SMatthew G. Knepley @*/
5365083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5366e5e52638SMatthew G. Knepley {
5367083401c6SMatthew G. Knepley   PetscInt       Nds;
5368e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5369e5e52638SMatthew G. Knepley 
5370e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5371e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5372083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5373083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5374083401c6SMatthew 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);
5375e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5376083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5377083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5378083401c6SMatthew G. Knepley   if (fields) {
5379083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5380b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5381083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5382083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5383e5e52638SMatthew G. Knepley   }
5384083401c6SMatthew G. Knepley   if (ds) {
5385083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5386083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5387083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5388083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5389083401c6SMatthew G. Knepley   }
5390e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5391e5e52638SMatthew G. Knepley }
5392e5e52638SMatthew G. Knepley 
5393e5e52638SMatthew G. Knepley /*@
53941d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
53951d3af9e0SMatthew G. Knepley 
53961d3af9e0SMatthew G. Knepley   Not collective
53971d3af9e0SMatthew G. Knepley 
53981d3af9e0SMatthew G. Knepley   Input Parameters:
53991d3af9e0SMatthew G. Knepley + dm  - The DM
54001d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
54011d3af9e0SMatthew G. Knepley 
54021d3af9e0SMatthew G. Knepley   Output Parameter:
54031d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
54041d3af9e0SMatthew G. Knepley 
54051d3af9e0SMatthew G. Knepley   Level: advanced
54061d3af9e0SMatthew G. Knepley 
54071d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
54081d3af9e0SMatthew G. Knepley @*/
54091d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
54101d3af9e0SMatthew G. Knepley {
54111d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
54121d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
54131d3af9e0SMatthew G. Knepley 
54141d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
54151d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54161d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
54171d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
54181d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
54191d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
54201d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
54211d3af9e0SMatthew G. Knepley   else          *num = n;
54221d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
54231d3af9e0SMatthew G. Knepley }
54241d3af9e0SMatthew G. Knepley 
54251d3af9e0SMatthew G. Knepley /*@
5426e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5427e5e52638SMatthew G. Knepley 
5428d083f849SBarry Smith   Collective on dm
5429e5e52638SMatthew G. Knepley 
5430e5e52638SMatthew G. Knepley   Input Parameter:
5431e5e52638SMatthew G. Knepley . dm - The DM
5432e5e52638SMatthew G. Knepley 
5433e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5434e5e52638SMatthew G. Knepley 
5435e5e52638SMatthew G. Knepley   Level: intermediate
5436e5e52638SMatthew G. Knepley 
5437e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5438e5e52638SMatthew G. Knepley @*/
5439e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5440e5e52638SMatthew G. Knepley {
5441e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5442083401c6SMatthew G. Knepley   PetscDS        dsDef;
5443083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5444083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5445e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5446e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5447e5e52638SMatthew G. Knepley 
5448e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5449e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5450e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5451e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5452083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5453083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5454083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5455083401c6SMatthew G. Knepley   Nl   = 0;
5456083401c6SMatthew G. Knepley   Ndef = 0;
5457083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5458083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5459083401c6SMatthew G. Knepley     PetscInt l;
5460083401c6SMatthew G. Knepley 
5461083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5462083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5463083401c6SMatthew G. Knepley     if (l < Nl) continue;
5464083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5465083401c6SMatthew G. Knepley   }
5466083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5467083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5468083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5469b3cf3223SMatthew G. Knepley     IS        fields;
5470b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5471b3cf3223SMatthew G. Knepley 
5472b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5473083401c6SMatthew G. Knepley     if (nf) {
5474b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5475b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
547688f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
547788f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
547888f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
547988f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
548088f0c812SMatthew G. Knepley 
5481083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5482083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5483083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5484b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
54852df9ee95SMatthew G. Knepley     }
5486083401c6SMatthew G. Knepley   }
5487083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5488083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5489083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5490083401c6SMatthew G. Knepley   if (Ndef && Nl) {
54910122748bSMatthew G. Knepley     DM              plex;
5492083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5493083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5494083401c6SMatthew G. Knepley     PetscInt       *fields;
5495083401c6SMatthew G. Knepley     const PetscInt *cells;
5496083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
54970122748bSMatthew G. Knepley 
54980122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
54990122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5500083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5501083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5502083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5503083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5504083401c6SMatthew G. Knepley       IS      pointIS;
5505083401c6SMatthew G. Knepley 
5506083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5507083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5508083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5509083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5510083401c6SMatthew G. Knepley     }
5511083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5512083401c6SMatthew G. Knepley 
5513083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5514083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5515083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5516083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5517083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5518083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5519083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5520083401c6SMatthew G. Knepley 
5521083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5522083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5523083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5524083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5525083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5526083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5527083401c6SMatthew G. Knepley 
5528083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5529083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5530083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5531083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5532083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5533083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
55340122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5535083401c6SMatthew G. Knepley   }
5536083401c6SMatthew G. Knepley   /* Create label DSes
5537083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5538083401c6SMatthew G. Knepley   */
5539083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5540083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5541083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5542083401c6SMatthew G. Knepley     PetscDS   ds;
5543083401c6SMatthew G. Knepley     IS        fields;
5544083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5545083401c6SMatthew G. Knepley 
5546083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5547083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5548083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5549083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5550083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5551083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5552083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5553083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5554083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5555083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5556083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5557083401c6SMatthew G. Knepley     {
5558083401c6SMatthew G. Knepley       DMPolytopeType ct;
5559083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5560083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
55610122748bSMatthew G. Knepley 
5562e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5563665f567fSMatthew G. Knepley       if (lStart >= 0) {
5564412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5565412e9a14SMatthew G. Knepley         switch (ct) {
5566412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5567412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5568412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5569412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5570083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5571083401c6SMatthew G. Knepley           default: break;
5572412e9a14SMatthew G. Knepley         }
5573665f567fSMatthew G. Knepley       }
5574083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5575083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5576e5e52638SMatthew G. Knepley     }
5577083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5578e5e52638SMatthew G. Knepley   }
5579083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5580e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5581083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5582083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5583083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5584083401c6SMatthew G. Knepley     const PetscInt *fld;
5585083401c6SMatthew G. Knepley     PetscInt        nf;
5586e5e52638SMatthew G. Knepley 
5587083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5588083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5589083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5590083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5591083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5592e5e52638SMatthew G. Knepley       PetscClassId id;
5593e5e52638SMatthew G. Knepley 
5594083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5595083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5596083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5597083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5598083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5599e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5600e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5601e5e52638SMatthew G. Knepley     }
5602083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5603e5e52638SMatthew G. Knepley   }
5604e5e52638SMatthew G. Knepley   /* Setup DSes */
5605e5e52638SMatthew G. Knepley   if (doSetup) {
5606e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5607e5e52638SMatthew G. Knepley   }
5608e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5609e5e52638SMatthew G. Knepley }
5610e5e52638SMatthew G. Knepley 
5611e5e52638SMatthew G. Knepley /*@
56127f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
56137f96f943SMatthew G. Knepley 
5614f2cacb80SMatthew G. Knepley   Collective on DM
5615f2cacb80SMatthew G. Knepley 
56167f96f943SMatthew G. Knepley   Input Parameters:
56177f96f943SMatthew G. Knepley + dm   - The DM
56187f96f943SMatthew G. Knepley - time - The time
56197f96f943SMatthew G. Knepley 
56207f96f943SMatthew G. Knepley   Output Parameters:
5621f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5622f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
56237f96f943SMatthew G. Knepley 
56247f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
56257f96f943SMatthew G. Knepley 
56267f96f943SMatthew G. Knepley   Level: developer
56277f96f943SMatthew G. Knepley 
56287f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
56297f96f943SMatthew G. Knepley @*/
5630f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
56317f96f943SMatthew G. Knepley {
56327f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
56337f96f943SMatthew G. Knepley   void            **ectxs;
56347f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
56357f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
56367f96f943SMatthew G. Knepley 
56377f96f943SMatthew G. Knepley   PetscFunctionBegin;
5638f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5639f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5640f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
56417f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
56427f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
56437f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
56447f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
56457f96f943SMatthew G. Knepley     PetscDS         ds;
56467f96f943SMatthew G. Knepley     DMLabel         label;
56477f96f943SMatthew G. Knepley     IS              fieldIS;
56487f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
56497f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
56507f96f943SMatthew G. Knepley 
56517f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
56527f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
56537f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
56547f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
56557f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5656f2cacb80SMatthew G. Knepley     if (u) {
56577f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
56587f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
56597f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
56607f96f943SMatthew G. Knepley       }
56617f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
56627f96f943SMatthew G. Knepley       if (label) {
56637f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56647f96f943SMatthew G. Knepley       } else {
56657f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56667f96f943SMatthew G. Knepley       }
56677f96f943SMatthew G. Knepley     }
5668f2cacb80SMatthew G. Knepley     if (u_t) {
5669f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5670f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5671f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5672f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5673f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5674f2cacb80SMatthew G. Knepley       }
5675f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5676f2cacb80SMatthew G. Knepley       if (label) {
5677f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5678f2cacb80SMatthew G. Knepley       } else {
5679f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5680f2cacb80SMatthew G. Knepley       }
5681f2cacb80SMatthew G. Knepley     }
5682f2cacb80SMatthew G. Knepley   }
5683f2cacb80SMatthew G. Knepley   if (u) {
56847f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
56857f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5686f2cacb80SMatthew G. Knepley   }
5687f2cacb80SMatthew G. Knepley   if (u_t) {
5688f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5689f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5690f2cacb80SMatthew G. Knepley   }
56917f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
56927f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
56937f96f943SMatthew G. Knepley }
56947f96f943SMatthew G. Knepley 
56957f96f943SMatthew G. Knepley /*@
5696e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5697e5e52638SMatthew G. Knepley 
5698d083f849SBarry Smith   Collective on dm
5699e5e52638SMatthew G. Knepley 
5700e5e52638SMatthew G. Knepley   Input Parameter:
5701e5e52638SMatthew G. Knepley . dm - The DM
5702e5e52638SMatthew G. Knepley 
5703e5e52638SMatthew G. Knepley   Output Parameter:
5704e5e52638SMatthew G. Knepley . newdm - The DM
5705e5e52638SMatthew G. Knepley 
5706e5e52638SMatthew G. Knepley   Level: advanced
5707e5e52638SMatthew G. Knepley 
5708e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5709e5e52638SMatthew G. Knepley @*/
5710e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5711e5e52638SMatthew G. Knepley {
5712e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5713e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5714e5e52638SMatthew G. Knepley 
5715e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5716e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5717e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5718e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5719e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5720e5e52638SMatthew G. Knepley     DMLabel  label;
5721b3cf3223SMatthew G. Knepley     IS       fields;
5722e5e52638SMatthew G. Knepley     PetscDS  ds;
5723783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5724e5e52638SMatthew G. Knepley 
5725b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5726b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5727783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5728783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5729783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5730783e2ec8SMatthew G. Knepley       PetscInt    field;
5731783e2ec8SMatthew G. Knepley 
5732783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
573356cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5734783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5735783e2ec8SMatthew G. Knepley     }
5736e5e52638SMatthew G. Knepley   }
5737e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5738e5e52638SMatthew G. Knepley }
5739e5e52638SMatthew G. Knepley 
5740e5e52638SMatthew G. Knepley /*@
5741e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5742e5e52638SMatthew G. Knepley 
5743d083f849SBarry Smith   Collective on dm
5744e5e52638SMatthew G. Knepley 
5745e5e52638SMatthew G. Knepley   Input Parameter:
5746e5e52638SMatthew G. Knepley . dm - The DM
5747e5e52638SMatthew G. Knepley 
5748e5e52638SMatthew G. Knepley   Output Parameter:
5749e5e52638SMatthew G. Knepley . newdm - The DM
5750e5e52638SMatthew G. Knepley 
5751e5e52638SMatthew G. Knepley   Level: advanced
5752e5e52638SMatthew G. Knepley 
5753e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5754e5e52638SMatthew G. Knepley @*/
5755e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5756e5e52638SMatthew G. Knepley {
5757e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5758e5e52638SMatthew G. Knepley 
5759e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5760e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5761e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5762e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5763e5e52638SMatthew G. Knepley }
5764e5e52638SMatthew G. Knepley 
5765b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5766b64e0483SPeter Brune {
5767b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5768b64e0483SPeter Brune   PetscErrorCode ierr;
5769b64e0483SPeter Brune   Vec coords,ccoords;
57706dbf9973SLawrence Mitchell   Mat inject;
5771b64e0483SPeter Brune   PetscFunctionBegin;
5772b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5773b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5774b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5775b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5776b64e0483SPeter Brune   if (coords && !ccoords) {
5777b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
57786668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
57796dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
57802adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
57816dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5782b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5783b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5784b64e0483SPeter Brune   }
5785b64e0483SPeter Brune   PetscFunctionReturn(0);
5786b64e0483SPeter Brune }
5787b64e0483SPeter Brune 
578803dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
578903dadc2fSPeter Brune {
579003dadc2fSPeter Brune   DM dm_coord,subdm_coord;
579103dadc2fSPeter Brune   PetscErrorCode ierr;
579203dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
579303dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
579403dadc2fSPeter Brune   PetscFunctionBegin;
579503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
579603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
579703dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
579803dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
579903dadc2fSPeter Brune   if (coords && !ccoords) {
580003dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
58016668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
580203dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
580324640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
580403dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
580503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
580603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
58071ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
580803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
580903dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
581003dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
581103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
581203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
581303dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
581403dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
581503dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
581603dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
581703dadc2fSPeter Brune   }
581803dadc2fSPeter Brune   PetscFunctionReturn(0);
581903dadc2fSPeter Brune }
582003dadc2fSPeter Brune 
5821c73cfb54SMatthew G. Knepley /*@
5822c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5823c73cfb54SMatthew G. Knepley 
5824c73cfb54SMatthew G. Knepley   Not collective
5825c73cfb54SMatthew G. Knepley 
5826c73cfb54SMatthew G. Knepley   Input Parameter:
5827c73cfb54SMatthew G. Knepley . dm - The DM
5828c73cfb54SMatthew G. Knepley 
5829c73cfb54SMatthew G. Knepley   Output Parameter:
5830c73cfb54SMatthew G. Knepley . dim - The topological dimension
5831c73cfb54SMatthew G. Knepley 
5832c73cfb54SMatthew G. Knepley   Level: beginner
5833c73cfb54SMatthew G. Knepley 
5834c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5835c73cfb54SMatthew G. Knepley @*/
5836c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5837c73cfb54SMatthew G. Knepley {
5838c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5839c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5840534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5841c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5842c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5843c73cfb54SMatthew G. Knepley }
5844c73cfb54SMatthew G. Knepley 
5845c73cfb54SMatthew G. Knepley /*@
5846c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5847c73cfb54SMatthew G. Knepley 
5848c73cfb54SMatthew G. Knepley   Collective on dm
5849c73cfb54SMatthew G. Knepley 
5850c73cfb54SMatthew G. Knepley   Input Parameters:
5851c73cfb54SMatthew G. Knepley + dm - The DM
5852c73cfb54SMatthew G. Knepley - dim - The topological dimension
5853c73cfb54SMatthew G. Knepley 
5854c73cfb54SMatthew G. Knepley   Level: beginner
5855c73cfb54SMatthew G. Knepley 
5856c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5857c73cfb54SMatthew G. Knepley @*/
5858c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5859c73cfb54SMatthew G. Knepley {
5860e5e52638SMatthew G. Knepley   PetscDS        ds;
5861f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5862f17e8794SMatthew G. Knepley 
5863c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5864c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5865c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5866c73cfb54SMatthew G. Knepley   dm->dim = dim;
5867e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5868e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5869c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5870c73cfb54SMatthew G. Knepley }
5871c73cfb54SMatthew G. Knepley 
5872793f3fe5SMatthew G. Knepley /*@
5873793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5874793f3fe5SMatthew G. Knepley 
5875d083f849SBarry Smith   Collective on dm
5876793f3fe5SMatthew G. Knepley 
5877793f3fe5SMatthew G. Knepley   Input Parameters:
5878793f3fe5SMatthew G. Knepley + dm - the DM
5879793f3fe5SMatthew G. Knepley - dim - the dimension
5880793f3fe5SMatthew G. Knepley 
5881793f3fe5SMatthew G. Knepley   Output Parameters:
5882793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5883aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5884793f3fe5SMatthew G. Knepley 
5885793f3fe5SMatthew G. Knepley   Note:
5886793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5887a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5888793f3fe5SMatthew G. Knepley   then the interval is empty.
5889793f3fe5SMatthew G. Knepley 
5890793f3fe5SMatthew G. Knepley   Level: intermediate
5891793f3fe5SMatthew G. Knepley 
5892793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5893793f3fe5SMatthew G. Knepley @*/
5894793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5895793f3fe5SMatthew G. Knepley {
5896793f3fe5SMatthew G. Knepley   PetscInt       d;
5897793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5898793f3fe5SMatthew G. Knepley 
5899793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5900793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5901793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5902793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5903b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5904793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5905793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5906793f3fe5SMatthew G. Knepley }
5907793f3fe5SMatthew G. Knepley 
59086636e97aSMatthew G Knepley /*@
59096636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
59106636e97aSMatthew G Knepley 
5911d083f849SBarry Smith   Collective on dm
59126636e97aSMatthew G Knepley 
59136636e97aSMatthew G Knepley   Input Parameters:
59146636e97aSMatthew G Knepley + dm - the DM
59156636e97aSMatthew G Knepley - c - coordinate vector
59166636e97aSMatthew G Knepley 
59172dd40e9bSPatrick Sanan   Notes:
59182dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
59192dd40e9bSPatrick Sanan 
59202dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59216636e97aSMatthew G Knepley 
59226636e97aSMatthew G Knepley   Level: intermediate
59236636e97aSMatthew G Knepley 
59242dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59256636e97aSMatthew G Knepley @*/
59266636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
59276636e97aSMatthew G Knepley {
59286636e97aSMatthew G Knepley   PetscErrorCode ierr;
59296636e97aSMatthew G Knepley 
59306636e97aSMatthew G Knepley   PetscFunctionBegin;
59316636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59326636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59336636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59346636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59356636e97aSMatthew G Knepley   dm->coordinates = c;
59366636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5937b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
593803dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
59396636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59406636e97aSMatthew G Knepley }
59416636e97aSMatthew G Knepley 
59426636e97aSMatthew G Knepley /*@
59436636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
59446636e97aSMatthew G Knepley 
59457058e716SVaclav Hapla   Not collective
59466636e97aSMatthew G Knepley 
59476636e97aSMatthew G Knepley    Input Parameters:
59486636e97aSMatthew G Knepley +  dm - the DM
59496636e97aSMatthew G Knepley -  c - coordinate vector
59506636e97aSMatthew G Knepley 
59512dd40e9bSPatrick Sanan   Notes:
59526636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
59536636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
59546636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
59556636e97aSMatthew G Knepley 
59562dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59572dd40e9bSPatrick Sanan 
59586636e97aSMatthew G Knepley   Level: intermediate
59596636e97aSMatthew G Knepley 
59606636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
59616636e97aSMatthew G Knepley @*/
59626636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
59636636e97aSMatthew G Knepley {
59646636e97aSMatthew G Knepley   PetscErrorCode ierr;
59656636e97aSMatthew G Knepley 
59666636e97aSMatthew G Knepley   PetscFunctionBegin;
59676636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59686636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59696636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59706636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
59718865f1eaSKarl Rupp 
59726636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
59738865f1eaSKarl Rupp 
59746636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59756636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59766636e97aSMatthew G Knepley }
59776636e97aSMatthew G Knepley 
59786636e97aSMatthew G Knepley /*@
59796636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
59806636e97aSMatthew G Knepley 
5981d083f849SBarry Smith   Collective on dm
59826636e97aSMatthew G Knepley 
59836636e97aSMatthew G Knepley   Input Parameter:
59846636e97aSMatthew G Knepley . dm - the DM
59856636e97aSMatthew G Knepley 
59866636e97aSMatthew G Knepley   Output Parameter:
59876636e97aSMatthew G Knepley . c - global coordinate vector
59886636e97aSMatthew G Knepley 
59896636e97aSMatthew G Knepley   Note:
59906636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
59916636e97aSMatthew G Knepley 
59926636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
59936636e97aSMatthew G Knepley 
59946636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59956636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59966636e97aSMatthew G Knepley 
59976636e97aSMatthew G Knepley   Level: intermediate
59986636e97aSMatthew G Knepley 
59996636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
60006636e97aSMatthew G Knepley @*/
60016636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
60026636e97aSMatthew G Knepley {
60036636e97aSMatthew G Knepley   PetscErrorCode ierr;
60046636e97aSMatthew G Knepley 
60056636e97aSMatthew G Knepley   PetscFunctionBegin;
60066636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60076636e97aSMatthew G Knepley   PetscValidPointer(c,2);
60081f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
60090298fd71SBarry Smith     DM        cdm = NULL;
60101970a576SMatthew G. Knepley     PetscBool localized;
60116636e97aSMatthew G Knepley 
60126636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
60136636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
60141970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
60151970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
60161970a576SMatthew G. Knepley     if (localized) {
60171970a576SMatthew G. Knepley       PetscInt cdim;
60181970a576SMatthew G. Knepley 
60191970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
60201970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60211970a576SMatthew G. Knepley     }
60226636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
60236636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60246636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60256636e97aSMatthew G Knepley   }
60266636e97aSMatthew G Knepley   *c = dm->coordinates;
60276636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60286636e97aSMatthew G Knepley }
60296636e97aSMatthew G Knepley 
60306636e97aSMatthew G Knepley /*@
603181e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
603281e9a530SVaclav Hapla 
6033d083f849SBarry Smith   Collective on dm
603481e9a530SVaclav Hapla 
603581e9a530SVaclav Hapla   Input Parameter:
603681e9a530SVaclav Hapla . dm - the DM
603781e9a530SVaclav Hapla 
603881e9a530SVaclav Hapla   Level: advanced
603981e9a530SVaclav Hapla 
604081e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
604181e9a530SVaclav Hapla @*/
604281e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
604381e9a530SVaclav Hapla {
604481e9a530SVaclav Hapla   PetscErrorCode ierr;
604581e9a530SVaclav Hapla 
604681e9a530SVaclav Hapla   PetscFunctionBegin;
604781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
604881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
60491970a576SMatthew G. Knepley     DM        cdm = NULL;
60501970a576SMatthew G. Knepley     PetscBool localized;
60511970a576SMatthew G. Knepley 
605281e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
605381e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
60541970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
60551970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
60561970a576SMatthew G. Knepley     if (localized) {
60571970a576SMatthew G. Knepley       PetscInt cdim;
60581970a576SMatthew G. Knepley 
60591970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
60601970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60611970a576SMatthew G. Knepley     }
606281e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
606381e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
606481e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
606581e9a530SVaclav Hapla   }
606681e9a530SVaclav Hapla   PetscFunctionReturn(0);
606781e9a530SVaclav Hapla }
606881e9a530SVaclav Hapla 
606981e9a530SVaclav Hapla /*@
60706636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
60716636e97aSMatthew G Knepley 
6072d083f849SBarry Smith   Collective on dm
60736636e97aSMatthew G Knepley 
60746636e97aSMatthew G Knepley   Input Parameter:
60756636e97aSMatthew G Knepley . dm - the DM
60766636e97aSMatthew G Knepley 
60776636e97aSMatthew G Knepley   Output Parameter:
60786636e97aSMatthew G Knepley . c - coordinate vector
60796636e97aSMatthew G Knepley 
60806636e97aSMatthew G Knepley   Note:
60816636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
60826636e97aSMatthew G Knepley 
60836636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
60846636e97aSMatthew G Knepley 
60856636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
60866636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
60876636e97aSMatthew G Knepley 
60886636e97aSMatthew G Knepley   Level: intermediate
60896636e97aSMatthew G Knepley 
609081e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
60916636e97aSMatthew G Knepley @*/
60926636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
60936636e97aSMatthew G Knepley {
60946636e97aSMatthew G Knepley   PetscErrorCode ierr;
60956636e97aSMatthew G Knepley 
60966636e97aSMatthew G Knepley   PetscFunctionBegin;
60976636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60986636e97aSMatthew G Knepley   PetscValidPointer(c,2);
609981e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
61006636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
61016636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61026636e97aSMatthew G Knepley }
61036636e97aSMatthew G Knepley 
610481e9a530SVaclav Hapla /*@
610581e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
610681e9a530SVaclav Hapla 
610781e9a530SVaclav Hapla   Not collective
610881e9a530SVaclav Hapla 
610981e9a530SVaclav Hapla   Input Parameter:
611081e9a530SVaclav Hapla . dm - the DM
611181e9a530SVaclav Hapla 
611281e9a530SVaclav Hapla   Output Parameter:
611381e9a530SVaclav Hapla . c - coordinate vector
611481e9a530SVaclav Hapla 
611581e9a530SVaclav Hapla   Level: advanced
611681e9a530SVaclav Hapla 
611781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
611881e9a530SVaclav Hapla @*/
611981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
612081e9a530SVaclav Hapla {
612181e9a530SVaclav Hapla   PetscFunctionBegin;
612281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
612381e9a530SVaclav Hapla   PetscValidPointer(c,2);
612481e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
61256636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
61266636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61276636e97aSMatthew G Knepley }
61286636e97aSMatthew G Knepley 
61292db98f8dSVaclav Hapla /*@
61302db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
61312db98f8dSVaclav Hapla 
61322db98f8dSVaclav Hapla   Not collective
61332db98f8dSVaclav Hapla 
61342db98f8dSVaclav Hapla   Input Parameter:
61352db98f8dSVaclav Hapla + dm - the DM
61362db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
61372db98f8dSVaclav Hapla 
61382db98f8dSVaclav Hapla   Output Parameter:
61392db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
61402db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
61412db98f8dSVaclav Hapla 
61422db98f8dSVaclav Hapla   Note:
61432db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
61442db98f8dSVaclav Hapla 
61452db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
61462db98f8dSVaclav Hapla 
61472db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
61482db98f8dSVaclav Hapla 
61492db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
61502db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
61512db98f8dSVaclav Hapla 
61522db98f8dSVaclav Hapla   Level: advanced
61532db98f8dSVaclav Hapla 
61542db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
61552db98f8dSVaclav Hapla @*/
61562db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
61572db98f8dSVaclav Hapla {
61582db98f8dSVaclav Hapla   PetscSection        cs, newcs;
61592db98f8dSVaclav Hapla   Vec                 coords;
61602db98f8dSVaclav Hapla   const PetscScalar   *arr;
61612db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
61622db98f8dSVaclav Hapla   PetscInt            n;
61632db98f8dSVaclav Hapla   PetscErrorCode      ierr;
61642db98f8dSVaclav Hapla 
61652db98f8dSVaclav Hapla   PetscFunctionBegin;
61662db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61672db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
61682db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
61692db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
61702db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
61711bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
61721bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
61732db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
61742db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
61752db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
61762db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
61772db98f8dSVaclav Hapla   if (pCoord) {
61782db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
61792db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
61802db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
61812db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6182ad9ac99dSVaclav Hapla   } else {
6183ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
61842db98f8dSVaclav Hapla   }
6185ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6186ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
61872db98f8dSVaclav Hapla   PetscFunctionReturn(0);
61882db98f8dSVaclav Hapla }
61892db98f8dSVaclav Hapla 
6190f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6191f19dbd58SToby Isaac {
6192f19dbd58SToby Isaac   PetscErrorCode ierr;
6193f19dbd58SToby Isaac 
6194f19dbd58SToby Isaac   PetscFunctionBegin;
6195f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6196f19dbd58SToby Isaac   PetscValidPointer(field,2);
6197f19dbd58SToby Isaac   if (!dm->coordinateField) {
6198f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6199f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6200f19dbd58SToby Isaac     }
6201f19dbd58SToby Isaac   }
6202f19dbd58SToby Isaac   *field = dm->coordinateField;
6203f19dbd58SToby Isaac   PetscFunctionReturn(0);
6204f19dbd58SToby Isaac }
6205f19dbd58SToby Isaac 
6206f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6207f19dbd58SToby Isaac {
6208f19dbd58SToby Isaac   PetscErrorCode ierr;
6209f19dbd58SToby Isaac 
6210f19dbd58SToby Isaac   PetscFunctionBegin;
6211f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6212f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6213c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6214f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6215f19dbd58SToby Isaac   dm->coordinateField = field;
6216f19dbd58SToby Isaac   PetscFunctionReturn(0);
6217f19dbd58SToby Isaac }
6218f19dbd58SToby Isaac 
62196636e97aSMatthew G Knepley /*@
62201cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
62216636e97aSMatthew G Knepley 
6222d083f849SBarry Smith   Collective on dm
62236636e97aSMatthew G Knepley 
62246636e97aSMatthew G Knepley   Input Parameter:
62256636e97aSMatthew G Knepley . dm - the DM
62266636e97aSMatthew G Knepley 
62276636e97aSMatthew G Knepley   Output Parameter:
62286636e97aSMatthew G Knepley . cdm - coordinate DM
62296636e97aSMatthew G Knepley 
62306636e97aSMatthew G Knepley   Level: intermediate
62316636e97aSMatthew G Knepley 
62321cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62336636e97aSMatthew G Knepley @*/
62346636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
62356636e97aSMatthew G Knepley {
62366636e97aSMatthew G Knepley   PetscErrorCode ierr;
62376636e97aSMatthew G Knepley 
62386636e97aSMatthew G Knepley   PetscFunctionBegin;
62396636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62406636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
62416636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6242308f8a94SToby Isaac     DM cdm;
6243308f8a94SToby Isaac 
624482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6245308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6246308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6247308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6248308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6249308f8a94SToby Isaac     dm->coordinateDM = cdm;
62506636e97aSMatthew G Knepley   }
62516636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
62526636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62536636e97aSMatthew G Knepley }
6254e87bb0d3SMatthew G Knepley 
62551cfe2091SMatthew G. Knepley /*@
62561cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
62571cfe2091SMatthew G. Knepley 
6258d083f849SBarry Smith   Logically Collective on dm
62591cfe2091SMatthew G. Knepley 
62601cfe2091SMatthew G. Knepley   Input Parameters:
62611cfe2091SMatthew G. Knepley + dm - the DM
62621cfe2091SMatthew G. Knepley - cdm - coordinate DM
62631cfe2091SMatthew G. Knepley 
62641cfe2091SMatthew G. Knepley   Level: intermediate
62651cfe2091SMatthew G. Knepley 
62661cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62671cfe2091SMatthew G. Knepley @*/
62681cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
62691cfe2091SMatthew G. Knepley {
62701cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
62711cfe2091SMatthew G. Knepley 
62721cfe2091SMatthew G. Knepley   PetscFunctionBegin;
62731cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62741cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6275f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
62761cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
62771cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
62781cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
62791cfe2091SMatthew G. Knepley }
62801cfe2091SMatthew G. Knepley 
628146e270d4SMatthew G. Knepley /*@
628246e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
628346e270d4SMatthew G. Knepley 
628446e270d4SMatthew G. Knepley   Not Collective
628546e270d4SMatthew G. Knepley 
628646e270d4SMatthew G. Knepley   Input Parameter:
628746e270d4SMatthew G. Knepley . dm - The DM object
628846e270d4SMatthew G. Knepley 
628946e270d4SMatthew G. Knepley   Output Parameter:
629046e270d4SMatthew G. Knepley . dim - The embedding dimension
629146e270d4SMatthew G. Knepley 
629246e270d4SMatthew G. Knepley   Level: intermediate
629346e270d4SMatthew G. Knepley 
629492fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
629546e270d4SMatthew G. Knepley @*/
629646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
629746e270d4SMatthew G. Knepley {
629846e270d4SMatthew G. Knepley   PetscFunctionBegin;
629946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6300534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
63019a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
63029a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
63039a9a41abSToby Isaac   }
630446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
630546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
630646e270d4SMatthew G. Knepley }
630746e270d4SMatthew G. Knepley 
630846e270d4SMatthew G. Knepley /*@
630946e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
631046e270d4SMatthew G. Knepley 
631146e270d4SMatthew G. Knepley   Not Collective
631246e270d4SMatthew G. Knepley 
631346e270d4SMatthew G. Knepley   Input Parameters:
631446e270d4SMatthew G. Knepley + dm  - The DM object
631546e270d4SMatthew G. Knepley - dim - The embedding dimension
631646e270d4SMatthew G. Knepley 
631746e270d4SMatthew G. Knepley   Level: intermediate
631846e270d4SMatthew G. Knepley 
631992fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
632046e270d4SMatthew G. Knepley @*/
632146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
632246e270d4SMatthew G. Knepley {
6323e5e52638SMatthew G. Knepley   PetscDS        ds;
6324f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6325f17e8794SMatthew G. Knepley 
632646e270d4SMatthew G. Knepley   PetscFunctionBegin;
632746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
632846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6329e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6330e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
633146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
633246e270d4SMatthew G. Knepley }
633346e270d4SMatthew G. Knepley 
6334e8abe2deSMatthew G. Knepley /*@
6335e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6336e8abe2deSMatthew G. Knepley 
6337d083f849SBarry Smith   Collective on dm
6338e8abe2deSMatthew G. Knepley 
6339e8abe2deSMatthew G. Knepley   Input Parameter:
6340e8abe2deSMatthew G. Knepley . dm - The DM object
6341e8abe2deSMatthew G. Knepley 
6342e8abe2deSMatthew G. Knepley   Output Parameter:
6343e8abe2deSMatthew G. Knepley . section - The PetscSection object
6344e8abe2deSMatthew G. Knepley 
6345e8abe2deSMatthew G. Knepley   Level: intermediate
6346e8abe2deSMatthew G. Knepley 
634792fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6348e8abe2deSMatthew G. Knepley @*/
6349e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6350e8abe2deSMatthew G. Knepley {
6351e8abe2deSMatthew G. Knepley   DM             cdm;
6352e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6353e8abe2deSMatthew G. Knepley 
6354e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6355e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6356e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6357e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
635892fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6359e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6360e8abe2deSMatthew G. Knepley }
6361e8abe2deSMatthew G. Knepley 
6362e8abe2deSMatthew G. Knepley /*@
6363e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6364e8abe2deSMatthew G. Knepley 
6365e8abe2deSMatthew G. Knepley   Not Collective
6366e8abe2deSMatthew G. Knepley 
6367e8abe2deSMatthew G. Knepley   Input Parameters:
6368e8abe2deSMatthew G. Knepley + dm      - The DM object
636946e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6370e8abe2deSMatthew G. Knepley - section - The PetscSection object
6371e8abe2deSMatthew G. Knepley 
6372e8abe2deSMatthew G. Knepley   Level: intermediate
6373e8abe2deSMatthew G. Knepley 
637492fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6375e8abe2deSMatthew G. Knepley @*/
637646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6377e8abe2deSMatthew G. Knepley {
6378e8abe2deSMatthew G. Knepley   DM             cdm;
6379e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6380e8abe2deSMatthew G. Knepley 
6381e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6382e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
638346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6384e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
638592fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
638646e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
63874c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
638846e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
638946e270d4SMatthew G. Knepley 
639046e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
639146e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
639246e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
639346e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
639446e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
639546e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
639646e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
639746e270d4SMatthew G. Knepley     }
6398ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
639946e270d4SMatthew G. Knepley   }
6400e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6401e8abe2deSMatthew G. Knepley }
6402e8abe2deSMatthew G. Knepley 
6403d864a3eaSLisandro Dalcin /*@
6404d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6405d864a3eaSLisandro Dalcin 
6406d864a3eaSLisandro Dalcin   Input Parameters:
6407d864a3eaSLisandro Dalcin + dm      - The DM object
6408d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6409d864a3eaSLisandro Dalcin 
6410d864a3eaSLisandro Dalcin   Level: intermediate
6411d864a3eaSLisandro Dalcin 
6412d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6413d864a3eaSLisandro Dalcin @*/
6414d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6415d864a3eaSLisandro Dalcin {
6416d864a3eaSLisandro Dalcin   PetscObject    discOld;
6417d864a3eaSLisandro Dalcin   PetscClassId   classid;
6418d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6419d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6420d864a3eaSLisandro Dalcin   Mat            matInterp;
6421d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6422d864a3eaSLisandro Dalcin 
6423d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6424d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6425d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6426d864a3eaSLisandro Dalcin 
6427d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6428d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6429d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6430d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
643129ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
643229ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
643329ad44c5SMatthew G. Knepley       PetscFE        feLinear;
643429ad44c5SMatthew G. Knepley       DMPolytopeType ct;
643529ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
643629ad44c5SMatthew G. Knepley       PetscBool      simplex;
643729ad44c5SMatthew G. Knepley 
643829ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
643929ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
644029ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
644129ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
644229ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
644329ad44c5SMatthew G. Knepley       switch (ct) {
644429ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
644529ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
644629ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
644729ad44c5SMatthew G. Knepley         default: break;
644829ad44c5SMatthew G. Knepley       }
644929ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
645029ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
645129ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
645229ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
645329ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
645429ad44c5SMatthew G. Knepley     } else {
645529ad44c5SMatthew G. Knepley       const char *discname;
645629ad44c5SMatthew G. Knepley 
645729ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
645829ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
645929ad44c5SMatthew G. Knepley     }
646029ad44c5SMatthew G. Knepley   }
6461d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6462d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6463d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6464d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6465d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6466d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6467d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6468d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6469d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6470d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6471d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6472d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6473d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6474d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6475d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6476d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6477d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6478d864a3eaSLisandro Dalcin }
6479d864a3eaSLisandro Dalcin 
64805dc8c3f7SMatthew G. Knepley /*@C
648190b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
64825dc8c3f7SMatthew G. Knepley 
64835dc8c3f7SMatthew G. Knepley   Input Parameters:
648490b157c4SStefano Zampini . dm      - The DM object
648590b157c4SStefano Zampini 
648690b157c4SStefano Zampini   Output Parameters:
648790b157c4SStefano Zampini + per     - Whether the DM is periodic or not
64885dc8c3f7SMatthew 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
64895dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
64905dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
64915dc8c3f7SMatthew G. Knepley 
64925dc8c3f7SMatthew G. Knepley   Level: developer
64935dc8c3f7SMatthew G. Knepley 
64945dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
64955dc8c3f7SMatthew G. Knepley @*/
649690b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6497c6b900c6SMatthew G. Knepley {
6498c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6499c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
650090b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6501c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6502c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
65035dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6504c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6505c6b900c6SMatthew G. Knepley }
6506c6b900c6SMatthew G. Knepley 
65075dc8c3f7SMatthew G. Knepley /*@C
65085dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
65095dc8c3f7SMatthew G. Knepley 
65105dc8c3f7SMatthew G. Knepley   Input Parameters:
65115dc8c3f7SMatthew G. Knepley + dm      - The DM object
6512db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6513db2bf62eSStefano 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.
65145dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
65155dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
65165dc8c3f7SMatthew G. Knepley 
6517db2bf62eSStefano 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.
6518db2bf62eSStefano Zampini 
65195dc8c3f7SMatthew G. Knepley   Level: developer
65205dc8c3f7SMatthew G. Knepley 
65215dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
65225dc8c3f7SMatthew G. Knepley @*/
652390b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6524c6b900c6SMatthew G. Knepley {
6525c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6526c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6527c6b900c6SMatthew G. Knepley 
6528c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6529c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
653090b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6531412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6532412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6533412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
65345dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
653590b157c4SStefano Zampini   if (maxCell) {
6536412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6537412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6538db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6539db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6540412e9a14SMatthew G. Knepley   }
6541db2bf62eSStefano Zampini 
6542412e9a14SMatthew G. Knepley   if (L) {
6543412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6544412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6545412e9a14SMatthew G. Knepley   }
6546412e9a14SMatthew G. Knepley   if (bd) {
6547412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6548412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
654990b157c4SStefano Zampini   }
6550072d7d67SStefano Zampini   dm->periodic = per;
6551c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6552c6b900c6SMatthew G. Knepley }
6553c6b900c6SMatthew G. Knepley 
65542e17dfb7SMatthew G. Knepley /*@
65552e17dfb7SMatthew 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.
65562e17dfb7SMatthew G. Knepley 
65572e17dfb7SMatthew G. Knepley   Input Parameters:
65582e17dfb7SMatthew G. Knepley + dm     - The DM
655965da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
656065da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
65612e17dfb7SMatthew G. Knepley 
65622e17dfb7SMatthew G. Knepley   Output Parameter:
65632e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65642e17dfb7SMatthew G. Knepley 
65652e17dfb7SMatthew G. Knepley   Level: developer
65662e17dfb7SMatthew G. Knepley 
65672e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
65682e17dfb7SMatthew G. Knepley @*/
656965da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
65702e17dfb7SMatthew G. Knepley {
65712e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
65722e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
65732e17dfb7SMatthew G. Knepley 
65742e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65752e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
65762e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65772e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
65782e17dfb7SMatthew G. Knepley   } else {
657965da65dcSMatthew G. Knepley     if (endpoint) {
658065da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6581da3333bfSMatthew 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)) {
6582da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
658365da65dcSMatthew G. Knepley         } else {
6584da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
658565da65dcSMatthew G. Knepley         }
658665da65dcSMatthew G. Knepley       }
658765da65dcSMatthew G. Knepley     } else {
65882e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
65891118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
65902e17dfb7SMatthew G. Knepley       }
65912e17dfb7SMatthew G. Knepley     }
659265da65dcSMatthew G. Knepley   }
65932e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65942e17dfb7SMatthew G. Knepley }
65952e17dfb7SMatthew G. Knepley 
65962e17dfb7SMatthew G. Knepley /*
65972e17dfb7SMatthew 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.
65982e17dfb7SMatthew G. Knepley 
65992e17dfb7SMatthew G. Knepley   Input Parameters:
66002e17dfb7SMatthew G. Knepley + dm     - The DM
66012e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
66022e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
66032e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
66042e17dfb7SMatthew G. Knepley 
66052e17dfb7SMatthew G. Knepley   Output Parameter:
66062e17dfb7SMatthew G. Knepley . out - The localized coordinate point
66072e17dfb7SMatthew G. Knepley 
66082e17dfb7SMatthew G. Knepley   Level: developer
66092e17dfb7SMatthew G. Knepley 
66102e17dfb7SMatthew 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
66112e17dfb7SMatthew G. Knepley 
66122e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
66132e17dfb7SMatthew G. Knepley */
66142e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
66152e17dfb7SMatthew G. Knepley {
66162e17dfb7SMatthew G. Knepley   PetscInt d;
66172e17dfb7SMatthew G. Knepley 
66182e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66192e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66202e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
66212e17dfb7SMatthew G. Knepley   } else {
66222e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6623908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
66242e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
66252e17dfb7SMatthew G. Knepley       } else {
66262e17dfb7SMatthew G. Knepley         out[d] = in[d];
66272e17dfb7SMatthew G. Knepley       }
66282e17dfb7SMatthew G. Knepley     }
66292e17dfb7SMatthew G. Knepley   }
66302e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66312e17dfb7SMatthew G. Knepley }
6632a5801f52SStefano Zampini 
66332e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
66342e17dfb7SMatthew G. Knepley {
66352e17dfb7SMatthew G. Knepley   PetscInt d;
66362e17dfb7SMatthew G. Knepley 
66372e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66382e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66392e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
66402e17dfb7SMatthew G. Knepley   } else {
66412e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6642908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
66432e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
66442e17dfb7SMatthew G. Knepley       } else {
66452e17dfb7SMatthew G. Knepley         out[d] = in[d];
66462e17dfb7SMatthew G. Knepley       }
66472e17dfb7SMatthew G. Knepley     }
66482e17dfb7SMatthew G. Knepley   }
66492e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66502e17dfb7SMatthew G. Knepley }
66512e17dfb7SMatthew G. Knepley 
66522e17dfb7SMatthew G. Knepley /*
66532e17dfb7SMatthew 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.
66542e17dfb7SMatthew G. Knepley 
66552e17dfb7SMatthew G. Knepley   Input Parameters:
66562e17dfb7SMatthew G. Knepley + dm     - The DM
66572e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
66582e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
66592e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
66602e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
66612e17dfb7SMatthew G. Knepley 
66622e17dfb7SMatthew G. Knepley   Output Parameter:
66632e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
66642e17dfb7SMatthew G. Knepley 
66652e17dfb7SMatthew G. Knepley   Level: developer
66662e17dfb7SMatthew G. Knepley 
66672e17dfb7SMatthew 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
66682e17dfb7SMatthew G. Knepley 
66692e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
66702e17dfb7SMatthew G. Knepley */
66712e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
66722e17dfb7SMatthew G. Knepley {
66732e17dfb7SMatthew G. Knepley   PetscInt d;
66742e17dfb7SMatthew G. Knepley 
66752e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66762e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66772e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
66782e17dfb7SMatthew G. Knepley   } else {
66792e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6680412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6681412e9a14SMatthew G. Knepley 
6682412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6683412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6684412e9a14SMatthew G. Knepley 
6685412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6686412e9a14SMatthew 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]));
6687412e9a14SMatthew G. Knepley         out[d] += newCoord;
66882e17dfb7SMatthew G. Knepley       } else {
66892e17dfb7SMatthew G. Knepley         out[d] += in[d];
66902e17dfb7SMatthew G. Knepley       }
66912e17dfb7SMatthew G. Knepley     }
66922e17dfb7SMatthew G. Knepley   }
66932e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66942e17dfb7SMatthew G. Knepley }
66952e17dfb7SMatthew G. Knepley 
669636447a5eSToby Isaac /*@
66978f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
66988f700142SStefano Zampini 
66998f700142SStefano Zampini   Not collective
670036447a5eSToby Isaac 
670136447a5eSToby Isaac   Input Parameter:
670236447a5eSToby Isaac . dm - The DM
670336447a5eSToby Isaac 
670436447a5eSToby Isaac   Output Parameter:
670536447a5eSToby Isaac   areLocalized - True if localized
670636447a5eSToby Isaac 
670736447a5eSToby Isaac   Level: developer
670836447a5eSToby Isaac 
67098f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
671036447a5eSToby Isaac @*/
67118f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
671236447a5eSToby Isaac {
671336447a5eSToby Isaac   DM             cdm;
671436447a5eSToby Isaac   PetscSection   coordSection;
671546a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
671646a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
671736447a5eSToby Isaac   PetscErrorCode ierr;
671836447a5eSToby Isaac 
671936447a5eSToby Isaac   PetscFunctionBegin;
672036447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6721534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
67228b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
672346a3a80fSLisandro Dalcin 
672436447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
672536447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
672646a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
67279f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
672846a3a80fSLisandro Dalcin 
67299f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
673046a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
673136447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
673236447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
673346a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
673446a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
673536447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
673646a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
673736447a5eSToby Isaac   }
67388f700142SStefano Zampini   *areLocalized = alreadyLocalized;
673936447a5eSToby Isaac   PetscFunctionReturn(0);
674036447a5eSToby Isaac }
674136447a5eSToby Isaac 
67428f700142SStefano Zampini /*@
67438f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
67448f700142SStefano Zampini 
67458f700142SStefano Zampini   Collective on dm
67468f700142SStefano Zampini 
67478f700142SStefano Zampini   Input Parameter:
67488f700142SStefano Zampini . dm - The DM
67498f700142SStefano Zampini 
67508f700142SStefano Zampini   Output Parameter:
67518f700142SStefano Zampini   areLocalized - True if localized
67528f700142SStefano Zampini 
67538f700142SStefano Zampini   Level: developer
67548f700142SStefano Zampini 
67558f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
67568f700142SStefano Zampini @*/
67578f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
67588f700142SStefano Zampini {
67598f700142SStefano Zampini   PetscBool      localized;
67608f700142SStefano Zampini   PetscErrorCode ierr;
67618f700142SStefano Zampini 
67628f700142SStefano Zampini   PetscFunctionBegin;
67638f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6764534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
67658f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
67668f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
67678f700142SStefano Zampini   PetscFunctionReturn(0);
67688f700142SStefano Zampini }
676936447a5eSToby Isaac 
67702e17dfb7SMatthew G. Knepley /*@
6771492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
67722e17dfb7SMatthew G. Knepley 
67738f700142SStefano Zampini   Collective on dm
67748f700142SStefano Zampini 
67752e17dfb7SMatthew G. Knepley   Input Parameter:
67762e17dfb7SMatthew G. Knepley . dm - The DM
67772e17dfb7SMatthew G. Knepley 
67782e17dfb7SMatthew G. Knepley   Level: developer
67792e17dfb7SMatthew G. Knepley 
67808f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
67812e17dfb7SMatthew G. Knepley @*/
67822e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
67832e17dfb7SMatthew G. Knepley {
67842e17dfb7SMatthew G. Knepley   DM             cdm;
67852e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
67862e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
67873e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
67883e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6789e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
67903e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
67913e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
67922e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
67932e17dfb7SMatthew G. Knepley 
67942e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67952e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
679692c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6797f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6798f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6799f7cbd40bSStefano Zampini 
68002e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
68012e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
68022e17dfb7SMatthew G. Knepley   {
68032e17dfb7SMatthew G. Knepley     PetscBool isplex;
68042e17dfb7SMatthew G. Knepley 
68052e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
68062e17dfb7SMatthew G. Knepley     if (isplex) {
68072e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
68083e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
680969291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
68103e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
68113e922f36SToby Isaac       newStart = vStart;
68123e922f36SToby Isaac       newEnd   = vEnd;
68133e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
68143e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
68153e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
68163e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
68173e922f36SToby Isaac       }
68182e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
68192e17dfb7SMatthew G. Knepley   }
68202e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
682143eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
68222e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
68233e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6824e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
68253e922f36SToby Isaac 
68262e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
68272e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
68282e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
68292e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
68303e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
68313e922f36SToby Isaac 
683269291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68333e922f36SToby Isaac   localized = &anchor[bs];
68343e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
68353e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68363e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68373e922f36SToby Isaac 
68383e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
68393e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
68403e922f36SToby Isaac       PetscInt     b;
68413e922f36SToby Isaac 
68423e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
68433e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68443e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68453e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
68463e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
68473e922f36SToby Isaac         for (b = 0; b < bs; b++) {
68483e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
68493e922f36SToby Isaac         }
68503e922f36SToby Isaac         if (b < bs) break;
68513e922f36SToby Isaac       }
68523e922f36SToby Isaac       if (d < dof/bs) {
68533e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
68543e922f36SToby Isaac           PetscInt cdof;
68553e922f36SToby Isaac 
68563e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
68573e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
68583e922f36SToby Isaac         }
68593e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
68603e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
68613e922f36SToby Isaac       }
68623e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68633e922f36SToby Isaac     }
68643e922f36SToby Isaac   }
68653e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
68663e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
686769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68683e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
686969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
68703e922f36SToby Isaac     PetscFunctionReturn(0);
68713e922f36SToby Isaac   }
68722e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68732e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68742e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
68752e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
68762e17dfb7SMatthew G. Knepley   }
68772e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
68782e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6879c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
68802e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
68812e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
68822e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
68832e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6884c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68852e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
68862e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68872e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68882e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
68892e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
68902e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
68912e17dfb7SMatthew G. Knepley   }
68923e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68933e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68943e922f36SToby Isaac 
68952e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
68962e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
68973e922f36SToby Isaac       PetscInt     b, cdof;
68982e17dfb7SMatthew G. Knepley 
68993e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
69003e922f36SToby Isaac       if (!cdof) continue;
69012e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
69022e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
69032e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
69042e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
69052e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
69062e17dfb7SMatthew G. Knepley     }
69073e922f36SToby Isaac   }
690869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
690969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6910c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
69112e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
69122e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
69132e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
69142e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
69152e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
69162e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69172e17dfb7SMatthew G. Knepley }
69182e17dfb7SMatthew G. Knepley 
6919e87bb0d3SMatthew G Knepley /*@
69203a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6921e87bb0d3SMatthew G Knepley 
6922d083f849SBarry Smith   Collective on v (see explanation below)
6923e87bb0d3SMatthew G Knepley 
6924e87bb0d3SMatthew G Knepley   Input Parameters:
6925e87bb0d3SMatthew G Knepley + dm - The DM
69263a93e3b7SToby Isaac . v - The Vec of points
692762a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
69283a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6929e87bb0d3SMatthew G Knepley 
693061e3bb9bSMatthew G Knepley   Output Parameter:
693162a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
693262a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
69333a93e3b7SToby Isaac 
6934e87bb0d3SMatthew G Knepley 
6935e87bb0d3SMatthew G Knepley   Level: developer
693661e3bb9bSMatthew G Knepley 
693762a38674SMatthew G. Knepley   Notes:
69383a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
693962a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
69403a93e3b7SToby Isaac 
69413a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
694262a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
69433a93e3b7SToby Isaac 
69443a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
69453a93e3b7SToby Isaac 
694662a38674SMatthew G. Knepley $    const PetscSFNode *cells;
694762a38674SMatthew G. Knepley $    PetscInt           nFound;
6948a6216909SToby Isaac $    const PetscInt    *found;
694962a38674SMatthew G. Knepley $
6950a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
69513a93e3b7SToby Isaac 
69523a93e3b7SToby 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
69533a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
69543a93e3b7SToby Isaac 
695562a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
695661e3bb9bSMatthew G Knepley @*/
695762a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6958e87bb0d3SMatthew G Knepley {
6959735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6960735aa83eSMatthew G Knepley 
6961e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6962e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6963e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6964e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
69653a93e3b7SToby Isaac   if (*cellSF) {
69663a93e3b7SToby Isaac     PetscMPIInt result;
69673a93e3b7SToby Isaac 
6968e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6969a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
69703a93e3b7SToby 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");
6971e0fc9d1bSMatthew G. Knepley   } else {
69723a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
69733a93e3b7SToby Isaac   }
6974b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
697547a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
697662a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
697747a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6978e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6979e87bb0d3SMatthew G Knepley }
698014f150ffSMatthew G. Knepley 
6981f4d763aaSMatthew G. Knepley /*@
6982f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6983f4d763aaSMatthew G. Knepley 
69848f700142SStefano Zampini   Collective on dm
69858f700142SStefano Zampini 
6986f4d763aaSMatthew G. Knepley   Input Parameter:
6987f4d763aaSMatthew G. Knepley . dm - The original DM
6988f4d763aaSMatthew G. Knepley 
6989f4d763aaSMatthew G. Knepley   Output Parameter:
6990f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6991f4d763aaSMatthew G. Knepley 
6992f4d763aaSMatthew G. Knepley   Level: intermediate
6993f4d763aaSMatthew G. Knepley 
6994e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6995f4d763aaSMatthew G. Knepley @*/
699614f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
699714f150ffSMatthew G. Knepley {
6998c26acbdeSMatthew G. Knepley   PetscSection   section;
69992d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
700014f150ffSMatthew G. Knepley   PetscErrorCode ierr;
700114f150ffSMatthew G. Knepley 
700214f150ffSMatthew G. Knepley   PetscFunctionBegin;
700314f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
700414f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
700592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7006c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
7007127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
70082d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7009c26acbdeSMatthew G. Knepley     *odm = dm;
7010c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7011c26acbdeSMatthew G. Knepley   }
701214f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7013c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
701414f150ffSMatthew G. Knepley     PetscSF      sf;
701514f150ffSMatthew G. Knepley 
701614f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7017e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
701814f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
701992fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
702014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
702114f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
702215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7023e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
702414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
702514f150ffSMatthew G. Knepley   }
702614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
702714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
702814f150ffSMatthew G. Knepley }
7029f4d763aaSMatthew G. Knepley 
7030f4d763aaSMatthew G. Knepley /*@
7031cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7032f4d763aaSMatthew G. Knepley 
7033f4d763aaSMatthew G. Knepley   Input Parameter:
7034f4d763aaSMatthew G. Knepley . dm - The original DM
7035f4d763aaSMatthew G. Knepley 
7036cdb7a50dSMatthew G. Knepley   Output Parameters:
7037cdb7a50dSMatthew G. Knepley + num - The output sequence number
7038cdb7a50dSMatthew G. Knepley - val - The output sequence value
7039f4d763aaSMatthew G. Knepley 
7040f4d763aaSMatthew G. Knepley   Level: intermediate
7041f4d763aaSMatthew G. Knepley 
7042f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7043f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7044f4d763aaSMatthew G. Knepley 
7045f4d763aaSMatthew G. Knepley .seealso: VecView()
7046f4d763aaSMatthew G. Knepley @*/
7047cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7048f4d763aaSMatthew G. Knepley {
7049f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7050f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7051534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7052534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7053f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7054f4d763aaSMatthew G. Knepley }
7055f4d763aaSMatthew G. Knepley 
7056f4d763aaSMatthew G. Knepley /*@
7057cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7058f4d763aaSMatthew G. Knepley 
7059f4d763aaSMatthew G. Knepley   Input Parameters:
7060f4d763aaSMatthew G. Knepley + dm - The original DM
7061cdb7a50dSMatthew G. Knepley . num - The output sequence number
7062cdb7a50dSMatthew G. Knepley - val - The output sequence value
7063f4d763aaSMatthew G. Knepley 
7064f4d763aaSMatthew G. Knepley   Level: intermediate
7065f4d763aaSMatthew G. Knepley 
7066f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7067f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7068f4d763aaSMatthew G. Knepley 
7069f4d763aaSMatthew G. Knepley .seealso: VecView()
7070f4d763aaSMatthew G. Knepley @*/
7071cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7072f4d763aaSMatthew G. Knepley {
7073f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7074f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7075f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7076cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7077cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7078cdb7a50dSMatthew G. Knepley }
7079cdb7a50dSMatthew G. Knepley 
7080cdb7a50dSMatthew G. Knepley /*@C
7081cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7082cdb7a50dSMatthew G. Knepley 
7083cdb7a50dSMatthew G. Knepley   Input Parameters:
7084cdb7a50dSMatthew G. Knepley + dm   - The original DM
7085cdb7a50dSMatthew G. Knepley . name - The sequence name
7086cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7087cdb7a50dSMatthew G. Knepley 
7088cdb7a50dSMatthew G. Knepley   Output Parameter:
7089cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7090cdb7a50dSMatthew G. Knepley 
7091cdb7a50dSMatthew G. Knepley   Level: intermediate
7092cdb7a50dSMatthew G. Knepley 
7093cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7094cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7095cdb7a50dSMatthew G. Knepley 
7096cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7097cdb7a50dSMatthew G. Knepley @*/
7098cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7099cdb7a50dSMatthew G. Knepley {
7100cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7101cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7102cdb7a50dSMatthew G. Knepley 
7103cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7104cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7105cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7106534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
7107cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7108cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7109cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7110cdb7a50dSMatthew G. Knepley     PetscScalar value;
7111cdb7a50dSMatthew G. Knepley 
711239d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
71134aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7114cdb7a50dSMatthew G. Knepley #endif
7115cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7116f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7117f4d763aaSMatthew G. Knepley }
71188e4ac7eaSMatthew G. Knepley 
71198e4ac7eaSMatthew G. Knepley /*@
71208e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
71218e4ac7eaSMatthew G. Knepley 
71228e4ac7eaSMatthew G. Knepley   Not collective
71238e4ac7eaSMatthew G. Knepley 
71248e4ac7eaSMatthew G. Knepley   Input Parameter:
71258e4ac7eaSMatthew G. Knepley . dm - The DM
71268e4ac7eaSMatthew G. Knepley 
71278e4ac7eaSMatthew G. Knepley   Output Parameter:
71288e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
71298e4ac7eaSMatthew G. Knepley 
71308e4ac7eaSMatthew G. Knepley   Level: beginner
71318e4ac7eaSMatthew G. Knepley 
71328e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
71338e4ac7eaSMatthew G. Knepley @*/
71348e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
71358e4ac7eaSMatthew G. Knepley {
71368e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71378e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7138534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
71398e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
71408e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71418e4ac7eaSMatthew G. Knepley }
71428e4ac7eaSMatthew G. Knepley 
71438e4ac7eaSMatthew G. Knepley /*@
71445d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
71458e4ac7eaSMatthew G. Knepley 
71468e4ac7eaSMatthew G. Knepley   Collective on dm
71478e4ac7eaSMatthew G. Knepley 
71488e4ac7eaSMatthew G. Knepley   Input Parameters:
71498e4ac7eaSMatthew G. Knepley + dm - The DM
71508e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
71518e4ac7eaSMatthew G. Knepley 
71525d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
71535d3b26e6SMatthew G. Knepley 
71548e4ac7eaSMatthew G. Knepley   Level: beginner
71558e4ac7eaSMatthew G. Knepley 
71565d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
71578e4ac7eaSMatthew G. Knepley @*/
71588e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
71598e4ac7eaSMatthew G. Knepley {
71608e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71618e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71628833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
71638e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
71648e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71658e4ac7eaSMatthew G. Knepley }
7166c58f1c22SToby Isaac 
7167c58f1c22SToby Isaac 
7168c58f1c22SToby Isaac /*@C
7169c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7170c58f1c22SToby Isaac 
7171c58f1c22SToby Isaac   Not Collective
7172c58f1c22SToby Isaac 
7173c58f1c22SToby Isaac   Input Parameters:
7174c58f1c22SToby Isaac + dm   - The DM object
7175c58f1c22SToby Isaac - name - The label name
7176c58f1c22SToby Isaac 
7177c58f1c22SToby Isaac   Level: intermediate
7178c58f1c22SToby Isaac 
7179c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7180c58f1c22SToby Isaac @*/
7181c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7182c58f1c22SToby Isaac {
71835d80c0bfSVaclav Hapla   PetscBool      flg;
71845d80c0bfSVaclav Hapla   DMLabel        label;
7185c58f1c22SToby Isaac   PetscErrorCode ierr;
7186c58f1c22SToby Isaac 
7187c58f1c22SToby Isaac   PetscFunctionBegin;
7188c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7189c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
71905d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7191c58f1c22SToby Isaac   if (!flg) {
71925d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
71935d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
71945d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7195c58f1c22SToby Isaac   }
7196c58f1c22SToby Isaac   PetscFunctionReturn(0);
7197c58f1c22SToby Isaac }
7198c58f1c22SToby Isaac 
7199c58f1c22SToby Isaac /*@C
7200c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7201c58f1c22SToby Isaac 
7202c58f1c22SToby Isaac   Not Collective
7203c58f1c22SToby Isaac 
7204c58f1c22SToby Isaac   Input Parameters:
7205c58f1c22SToby Isaac + dm   - The DM object
7206c58f1c22SToby Isaac . name - The label name
7207c58f1c22SToby Isaac - point - The mesh point
7208c58f1c22SToby Isaac 
7209c58f1c22SToby Isaac   Output Parameter:
7210c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7211c58f1c22SToby Isaac 
7212c58f1c22SToby Isaac   Level: beginner
7213c58f1c22SToby Isaac 
7214c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7215c58f1c22SToby Isaac @*/
7216c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7217c58f1c22SToby Isaac {
7218c58f1c22SToby Isaac   DMLabel        label;
7219c58f1c22SToby Isaac   PetscErrorCode ierr;
7220c58f1c22SToby Isaac 
7221c58f1c22SToby Isaac   PetscFunctionBegin;
7222c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7223c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7224c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
722513903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7226c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7227c58f1c22SToby Isaac   PetscFunctionReturn(0);
7228c58f1c22SToby Isaac }
7229c58f1c22SToby Isaac 
7230c58f1c22SToby Isaac /*@C
7231c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7232c58f1c22SToby Isaac 
7233c58f1c22SToby Isaac   Not Collective
7234c58f1c22SToby Isaac 
7235c58f1c22SToby Isaac   Input Parameters:
7236c58f1c22SToby Isaac + dm   - The DM object
7237c58f1c22SToby Isaac . name - The label name
7238c58f1c22SToby Isaac . point - The mesh point
7239c58f1c22SToby Isaac - value - The label value for this point
7240c58f1c22SToby Isaac 
7241c58f1c22SToby Isaac   Output Parameter:
7242c58f1c22SToby Isaac 
7243c58f1c22SToby Isaac   Level: beginner
7244c58f1c22SToby Isaac 
7245c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7246c58f1c22SToby Isaac @*/
7247c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7248c58f1c22SToby Isaac {
7249c58f1c22SToby Isaac   DMLabel        label;
7250c58f1c22SToby Isaac   PetscErrorCode ierr;
7251c58f1c22SToby Isaac 
7252c58f1c22SToby Isaac   PetscFunctionBegin;
7253c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7254c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7255c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7256c58f1c22SToby Isaac   if (!label) {
7257c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7258c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7259c58f1c22SToby Isaac   }
7260c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7261c58f1c22SToby Isaac   PetscFunctionReturn(0);
7262c58f1c22SToby Isaac }
7263c58f1c22SToby Isaac 
7264c58f1c22SToby Isaac /*@C
7265c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7266c58f1c22SToby Isaac 
7267c58f1c22SToby Isaac   Not Collective
7268c58f1c22SToby Isaac 
7269c58f1c22SToby Isaac   Input Parameters:
7270c58f1c22SToby Isaac + dm   - The DM object
7271c58f1c22SToby Isaac . name - The label name
7272c58f1c22SToby Isaac . point - The mesh point
7273c58f1c22SToby Isaac - value - The label value for this point
7274c58f1c22SToby Isaac 
7275c58f1c22SToby Isaac   Output Parameter:
7276c58f1c22SToby Isaac 
7277c58f1c22SToby Isaac   Level: beginner
7278c58f1c22SToby Isaac 
7279c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7280c58f1c22SToby Isaac @*/
7281c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7282c58f1c22SToby Isaac {
7283c58f1c22SToby Isaac   DMLabel        label;
7284c58f1c22SToby Isaac   PetscErrorCode ierr;
7285c58f1c22SToby Isaac 
7286c58f1c22SToby Isaac   PetscFunctionBegin;
7287c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7288c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7289c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7290c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7291c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7292c58f1c22SToby Isaac   PetscFunctionReturn(0);
7293c58f1c22SToby Isaac }
7294c58f1c22SToby Isaac 
7295c58f1c22SToby Isaac /*@C
7296c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7297c58f1c22SToby Isaac 
7298c58f1c22SToby Isaac   Not Collective
7299c58f1c22SToby Isaac 
7300c58f1c22SToby Isaac   Input Parameters:
7301c58f1c22SToby Isaac + dm   - The DM object
7302c58f1c22SToby Isaac - name - The label name
7303c58f1c22SToby Isaac 
7304c58f1c22SToby Isaac   Output Parameter:
7305c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7306c58f1c22SToby Isaac 
7307c58f1c22SToby Isaac   Level: beginner
7308c58f1c22SToby Isaac 
7309df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7310c58f1c22SToby Isaac @*/
7311c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7312c58f1c22SToby Isaac {
7313c58f1c22SToby Isaac   DMLabel        label;
7314c58f1c22SToby Isaac   PetscErrorCode ierr;
7315c58f1c22SToby Isaac 
7316c58f1c22SToby Isaac   PetscFunctionBegin;
7317c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7318c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7319534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7320c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7321c58f1c22SToby Isaac   *size = 0;
7322c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7323c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7324c58f1c22SToby Isaac   PetscFunctionReturn(0);
7325c58f1c22SToby Isaac }
7326c58f1c22SToby Isaac 
7327c58f1c22SToby Isaac /*@C
7328c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7329c58f1c22SToby Isaac 
7330c58f1c22SToby Isaac   Not Collective
7331c58f1c22SToby Isaac 
7332c58f1c22SToby Isaac   Input Parameters:
7333c58f1c22SToby Isaac + mesh - The DM object
7334c58f1c22SToby Isaac - name - The label name
7335c58f1c22SToby Isaac 
7336c58f1c22SToby Isaac   Output Parameter:
7337c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7338c58f1c22SToby Isaac 
7339c58f1c22SToby Isaac   Level: beginner
7340c58f1c22SToby Isaac 
7341c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7342c58f1c22SToby Isaac @*/
7343c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7344c58f1c22SToby Isaac {
7345c58f1c22SToby Isaac   DMLabel        label;
7346c58f1c22SToby Isaac   PetscErrorCode ierr;
7347c58f1c22SToby Isaac 
7348c58f1c22SToby Isaac   PetscFunctionBegin;
7349c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7350c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7351c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7352c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7353c58f1c22SToby Isaac   *ids = NULL;
7354dab2e251SBlaise Bourdin  if (label) {
7355c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7356dab2e251SBlaise Bourdin   } else {
7357dab2e251SBlaise Bourdin     /* returning an empty IS */
7358dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7359dab2e251SBlaise Bourdin   }
7360c58f1c22SToby Isaac   PetscFunctionReturn(0);
7361c58f1c22SToby Isaac }
7362c58f1c22SToby Isaac 
7363c58f1c22SToby Isaac /*@C
7364c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7365c58f1c22SToby Isaac 
7366c58f1c22SToby Isaac   Not Collective
7367c58f1c22SToby Isaac 
7368c58f1c22SToby Isaac   Input Parameters:
7369c58f1c22SToby Isaac + dm - The DM object
7370c58f1c22SToby Isaac . name - The label name
7371c58f1c22SToby Isaac - value - The stratum value
7372c58f1c22SToby Isaac 
7373c58f1c22SToby Isaac   Output Parameter:
7374c58f1c22SToby Isaac . size - The stratum size
7375c58f1c22SToby Isaac 
7376c58f1c22SToby Isaac   Level: beginner
7377c58f1c22SToby Isaac 
7378c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7379c58f1c22SToby Isaac @*/
7380c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7381c58f1c22SToby Isaac {
7382c58f1c22SToby Isaac   DMLabel        label;
7383c58f1c22SToby Isaac   PetscErrorCode ierr;
7384c58f1c22SToby Isaac 
7385c58f1c22SToby Isaac   PetscFunctionBegin;
7386c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7387c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7388534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7389c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7390c58f1c22SToby Isaac   *size = 0;
7391c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7392c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7393c58f1c22SToby Isaac   PetscFunctionReturn(0);
7394c58f1c22SToby Isaac }
7395c58f1c22SToby Isaac 
7396c58f1c22SToby Isaac /*@C
7397c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7398c58f1c22SToby Isaac 
7399c58f1c22SToby Isaac   Not Collective
7400c58f1c22SToby Isaac 
7401c58f1c22SToby Isaac   Input Parameters:
7402c58f1c22SToby Isaac + dm - The DM object
7403c58f1c22SToby Isaac . name - The label name
7404c58f1c22SToby Isaac - value - The stratum value
7405c58f1c22SToby Isaac 
7406c58f1c22SToby Isaac   Output Parameter:
7407c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7408c58f1c22SToby Isaac 
7409c58f1c22SToby Isaac   Level: beginner
7410c58f1c22SToby Isaac 
7411c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7412c58f1c22SToby Isaac @*/
7413c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7414c58f1c22SToby Isaac {
7415c58f1c22SToby Isaac   DMLabel        label;
7416c58f1c22SToby Isaac   PetscErrorCode ierr;
7417c58f1c22SToby Isaac 
7418c58f1c22SToby Isaac   PetscFunctionBegin;
7419c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7420c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7421c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7422c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7423c58f1c22SToby Isaac   *points = NULL;
7424c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7425c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7426c58f1c22SToby Isaac   PetscFunctionReturn(0);
7427c58f1c22SToby Isaac }
7428c58f1c22SToby Isaac 
74294de306b1SToby Isaac /*@C
74309044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
74314de306b1SToby Isaac 
74324de306b1SToby Isaac   Not Collective
74334de306b1SToby Isaac 
74344de306b1SToby Isaac   Input Parameters:
74354de306b1SToby Isaac + dm - The DM object
74364de306b1SToby Isaac . name - The label name
74374de306b1SToby Isaac . value - The stratum value
74384de306b1SToby Isaac - points - The stratum points
74394de306b1SToby Isaac 
74404de306b1SToby Isaac   Level: beginner
74414de306b1SToby Isaac 
74424de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
74434de306b1SToby Isaac @*/
74444de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
74454de306b1SToby Isaac {
74464de306b1SToby Isaac   DMLabel        label;
74474de306b1SToby Isaac   PetscErrorCode ierr;
74484de306b1SToby Isaac 
74494de306b1SToby Isaac   PetscFunctionBegin;
74504de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74514de306b1SToby Isaac   PetscValidCharPointer(name, 2);
74524de306b1SToby Isaac   PetscValidPointer(points, 4);
74534de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
74544de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
74554de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
74564de306b1SToby Isaac   PetscFunctionReturn(0);
74574de306b1SToby Isaac }
74584de306b1SToby Isaac 
7459c58f1c22SToby Isaac /*@C
7460c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7461c58f1c22SToby Isaac 
7462c58f1c22SToby Isaac   Not Collective
7463c58f1c22SToby Isaac 
7464c58f1c22SToby Isaac   Input Parameters:
7465c58f1c22SToby Isaac + dm   - The DM object
7466c58f1c22SToby Isaac . name - The label name
7467c58f1c22SToby Isaac - value - The label value for this point
7468c58f1c22SToby Isaac 
7469c58f1c22SToby Isaac   Output Parameter:
7470c58f1c22SToby Isaac 
7471c58f1c22SToby Isaac   Level: beginner
7472c58f1c22SToby Isaac 
7473c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7474c58f1c22SToby Isaac @*/
7475c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7476c58f1c22SToby Isaac {
7477c58f1c22SToby Isaac   DMLabel        label;
7478c58f1c22SToby Isaac   PetscErrorCode ierr;
7479c58f1c22SToby Isaac 
7480c58f1c22SToby Isaac   PetscFunctionBegin;
7481c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7482c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7483c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7484c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7485c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7486c58f1c22SToby Isaac   PetscFunctionReturn(0);
7487c58f1c22SToby Isaac }
7488c58f1c22SToby Isaac 
7489c58f1c22SToby Isaac /*@
7490c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7491c58f1c22SToby Isaac 
7492c58f1c22SToby Isaac   Not Collective
7493c58f1c22SToby Isaac 
7494c58f1c22SToby Isaac   Input Parameter:
7495c58f1c22SToby Isaac . dm   - The DM object
7496c58f1c22SToby Isaac 
7497c58f1c22SToby Isaac   Output Parameter:
7498c58f1c22SToby Isaac . numLabels - the number of Labels
7499c58f1c22SToby Isaac 
7500c58f1c22SToby Isaac   Level: intermediate
7501c58f1c22SToby Isaac 
7502c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7503c58f1c22SToby Isaac @*/
7504c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7505c58f1c22SToby Isaac {
75065d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7507c58f1c22SToby Isaac   PetscInt  n    = 0;
7508c58f1c22SToby Isaac 
7509c58f1c22SToby Isaac   PetscFunctionBegin;
7510c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7511534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7512c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7513c58f1c22SToby Isaac   *numLabels = n;
7514c58f1c22SToby Isaac   PetscFunctionReturn(0);
7515c58f1c22SToby Isaac }
7516c58f1c22SToby Isaac 
7517c58f1c22SToby Isaac /*@C
7518c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7519c58f1c22SToby Isaac 
7520c58f1c22SToby Isaac   Not Collective
7521c58f1c22SToby Isaac 
7522c58f1c22SToby Isaac   Input Parameters:
7523c58f1c22SToby Isaac + dm - The DM object
7524c58f1c22SToby Isaac - n  - the label number
7525c58f1c22SToby Isaac 
7526c58f1c22SToby Isaac   Output Parameter:
7527c58f1c22SToby Isaac . name - the label name
7528c58f1c22SToby Isaac 
7529c58f1c22SToby Isaac   Level: intermediate
7530c58f1c22SToby Isaac 
7531c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7532c58f1c22SToby Isaac @*/
7533c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7534c58f1c22SToby Isaac {
75355d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7536c58f1c22SToby Isaac   PetscInt       l    = 0;
7537d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7538c58f1c22SToby Isaac 
7539c58f1c22SToby Isaac   PetscFunctionBegin;
7540c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7541c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7542c58f1c22SToby Isaac   while (next) {
7543c58f1c22SToby Isaac     if (l == n) {
7544d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7545c58f1c22SToby Isaac       PetscFunctionReturn(0);
7546c58f1c22SToby Isaac     }
7547c58f1c22SToby Isaac     ++l;
7548c58f1c22SToby Isaac     next = next->next;
7549c58f1c22SToby Isaac   }
7550c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7551c58f1c22SToby Isaac }
7552c58f1c22SToby Isaac 
7553c58f1c22SToby Isaac /*@C
7554c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7555c58f1c22SToby Isaac 
7556c58f1c22SToby Isaac   Not Collective
7557c58f1c22SToby Isaac 
7558c58f1c22SToby Isaac   Input Parameters:
7559c58f1c22SToby Isaac + dm   - The DM object
7560c58f1c22SToby Isaac - name - The label name
7561c58f1c22SToby Isaac 
7562c58f1c22SToby Isaac   Output Parameter:
7563c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7564c58f1c22SToby Isaac 
7565c58f1c22SToby Isaac   Level: intermediate
7566c58f1c22SToby Isaac 
7567c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7568c58f1c22SToby Isaac @*/
7569c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7570c58f1c22SToby Isaac {
75715d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7572d67d17b1SMatthew G. Knepley   const char    *lname;
7573c58f1c22SToby Isaac   PetscErrorCode ierr;
7574c58f1c22SToby Isaac 
7575c58f1c22SToby Isaac   PetscFunctionBegin;
7576c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7577c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7578534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7579c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7580c58f1c22SToby Isaac   while (next) {
7581d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7582d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7583c58f1c22SToby Isaac     if (*hasLabel) break;
7584c58f1c22SToby Isaac     next = next->next;
7585c58f1c22SToby Isaac   }
7586c58f1c22SToby Isaac   PetscFunctionReturn(0);
7587c58f1c22SToby Isaac }
7588c58f1c22SToby Isaac 
7589c58f1c22SToby Isaac /*@C
7590c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7591c58f1c22SToby Isaac 
7592c58f1c22SToby Isaac   Not Collective
7593c58f1c22SToby Isaac 
7594c58f1c22SToby Isaac   Input Parameters:
7595c58f1c22SToby Isaac + dm   - The DM object
7596c58f1c22SToby Isaac - name - The label name
7597c58f1c22SToby Isaac 
7598c58f1c22SToby Isaac   Output Parameter:
7599c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7600c58f1c22SToby Isaac 
7601c58f1c22SToby Isaac   Level: intermediate
7602c58f1c22SToby Isaac 
7603c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7604c58f1c22SToby Isaac @*/
7605c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7606c58f1c22SToby Isaac {
76075d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7608c58f1c22SToby Isaac   PetscBool      hasLabel;
7609d67d17b1SMatthew G. Knepley   const char    *lname;
7610c58f1c22SToby Isaac   PetscErrorCode ierr;
7611c58f1c22SToby Isaac 
7612c58f1c22SToby Isaac   PetscFunctionBegin;
7613c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7614c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7615c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7616c58f1c22SToby Isaac   *label = NULL;
7617c58f1c22SToby Isaac   while (next) {
7618d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7619d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7620c58f1c22SToby Isaac     if (hasLabel) {
7621c58f1c22SToby Isaac       *label = next->label;
7622c58f1c22SToby Isaac       break;
7623c58f1c22SToby Isaac     }
7624c58f1c22SToby Isaac     next = next->next;
7625c58f1c22SToby Isaac   }
7626c58f1c22SToby Isaac   PetscFunctionReturn(0);
7627c58f1c22SToby Isaac }
7628c58f1c22SToby Isaac 
7629c58f1c22SToby Isaac /*@C
7630c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7631c58f1c22SToby Isaac 
7632c58f1c22SToby Isaac   Not Collective
7633c58f1c22SToby Isaac 
7634c58f1c22SToby Isaac   Input Parameters:
7635c58f1c22SToby Isaac + dm - The DM object
7636c58f1c22SToby Isaac - n  - the label number
7637c58f1c22SToby Isaac 
7638c58f1c22SToby Isaac   Output Parameter:
7639c58f1c22SToby Isaac . label - the label
7640c58f1c22SToby Isaac 
7641c58f1c22SToby Isaac   Level: intermediate
7642c58f1c22SToby Isaac 
7643c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7644c58f1c22SToby Isaac @*/
7645c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7646c58f1c22SToby Isaac {
76475d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7648c58f1c22SToby Isaac   PetscInt    l    = 0;
7649c58f1c22SToby Isaac 
7650c58f1c22SToby Isaac   PetscFunctionBegin;
7651c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7652c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7653c58f1c22SToby Isaac   while (next) {
7654c58f1c22SToby Isaac     if (l == n) {
7655c58f1c22SToby Isaac       *label = next->label;
7656c58f1c22SToby Isaac       PetscFunctionReturn(0);
7657c58f1c22SToby Isaac     }
7658c58f1c22SToby Isaac     ++l;
7659c58f1c22SToby Isaac     next = next->next;
7660c58f1c22SToby Isaac   }
7661c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7662c58f1c22SToby Isaac }
7663c58f1c22SToby Isaac 
7664c58f1c22SToby Isaac /*@C
7665c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7666c58f1c22SToby Isaac 
7667c58f1c22SToby Isaac   Not Collective
7668c58f1c22SToby Isaac 
7669c58f1c22SToby Isaac   Input Parameters:
7670c58f1c22SToby Isaac + dm   - The DM object
7671c58f1c22SToby Isaac - label - The DMLabel
7672c58f1c22SToby Isaac 
7673c58f1c22SToby Isaac   Level: developer
7674c58f1c22SToby Isaac 
7675c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7676c58f1c22SToby Isaac @*/
7677c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7678c58f1c22SToby Isaac {
76795d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7680c58f1c22SToby Isaac   PetscBool      hasLabel;
7681d67d17b1SMatthew G. Knepley   const char    *lname;
76825d80c0bfSVaclav Hapla   PetscBool      flg;
7683c58f1c22SToby Isaac   PetscErrorCode ierr;
7684c58f1c22SToby Isaac 
7685c58f1c22SToby Isaac   PetscFunctionBegin;
7686c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7687d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7688d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7689d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7690c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7691c58f1c22SToby Isaac   tmpLabel->label  = label;
7692c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
76935d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
76945d80c0bfSVaclav Hapla   *p = tmpLabel;
769508f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
76965d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
76975d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7698ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7699ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7700c58f1c22SToby Isaac   PetscFunctionReturn(0);
7701c58f1c22SToby Isaac }
7702c58f1c22SToby Isaac 
7703c58f1c22SToby Isaac /*@C
7704e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7705c58f1c22SToby Isaac 
7706c58f1c22SToby Isaac   Not Collective
7707c58f1c22SToby Isaac 
7708c58f1c22SToby Isaac   Input Parameters:
7709c58f1c22SToby Isaac + dm   - The DM object
7710c58f1c22SToby Isaac - name - The label name
7711c58f1c22SToby Isaac 
7712c58f1c22SToby Isaac   Output Parameter:
7713c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7714c58f1c22SToby Isaac 
7715c58f1c22SToby Isaac   Level: developer
7716c58f1c22SToby Isaac 
7717e5472504SVaclav Hapla   Notes:
7718e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7719e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7720e5472504SVaclav Hapla 
7721e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7722e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7723e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7724e5472504SVaclav Hapla 
7725e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7726c58f1c22SToby Isaac @*/
7727c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7728c58f1c22SToby Isaac {
772995d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7730c58f1c22SToby Isaac   PetscBool      hasLabel;
7731d67d17b1SMatthew G. Knepley   const char    *lname;
7732c58f1c22SToby Isaac   PetscErrorCode ierr;
7733c58f1c22SToby Isaac 
7734c58f1c22SToby Isaac   PetscFunctionBegin;
7735c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7736e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7737e5472504SVaclav Hapla   if (label) {
7738e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7739c58f1c22SToby Isaac     *label = NULL;
7740e5472504SVaclav Hapla   }
77415d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
774295d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7743d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7744c58f1c22SToby Isaac     if (hasLabel) {
774595d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7746c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
774795d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7748ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7749ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
775095d578d6SVaclav Hapla       if (label) *label = link->label;
775195d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
775295d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7753c58f1c22SToby Isaac       break;
7754c58f1c22SToby Isaac     }
7755c58f1c22SToby Isaac   }
7756c58f1c22SToby Isaac   PetscFunctionReturn(0);
7757c58f1c22SToby Isaac }
7758c58f1c22SToby Isaac 
7759306894acSVaclav Hapla /*@
7760306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7761306894acSVaclav Hapla 
7762306894acSVaclav Hapla   Not Collective
7763306894acSVaclav Hapla 
7764306894acSVaclav Hapla   Input Parameters:
7765306894acSVaclav Hapla + dm   - The DM object
7766306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7767306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7768306894acSVaclav Hapla 
7769306894acSVaclav Hapla   Level: developer
7770306894acSVaclav Hapla 
7771306894acSVaclav Hapla   Notes:
7772306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7773306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7774306894acSVaclav Hapla   *label nullified.
7775306894acSVaclav Hapla 
7776306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7777306894acSVaclav Hapla @*/
7778306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7779306894acSVaclav Hapla {
778043e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7781306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7782306894acSVaclav Hapla   PetscErrorCode ierr;
7783306894acSVaclav Hapla 
7784306894acSVaclav Hapla   PetscFunctionBegin;
7785306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7786306894acSVaclav Hapla   PetscValidPointer(label, 2);
7787f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7788306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7789306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
77905d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
779143e45a93SVaclav Hapla     if (*label == link->label) {
7792306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
779343e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7794306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7795ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
779643e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
779743e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
779843e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7799306894acSVaclav Hapla       break;
7800306894acSVaclav Hapla     }
7801306894acSVaclav Hapla   }
7802306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7803306894acSVaclav Hapla   PetscFunctionReturn(0);
7804306894acSVaclav Hapla }
7805306894acSVaclav Hapla 
7806c58f1c22SToby Isaac /*@C
7807c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7808c58f1c22SToby Isaac 
7809c58f1c22SToby Isaac   Not Collective
7810c58f1c22SToby Isaac 
7811c58f1c22SToby Isaac   Input Parameters:
7812c58f1c22SToby Isaac + dm   - The DM object
7813c58f1c22SToby Isaac - name - The label name
7814c58f1c22SToby Isaac 
7815c58f1c22SToby Isaac   Output Parameter:
7816c58f1c22SToby Isaac . output - The flag for output
7817c58f1c22SToby Isaac 
7818c58f1c22SToby Isaac   Level: developer
7819c58f1c22SToby Isaac 
7820c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7821c58f1c22SToby Isaac @*/
7822c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7823c58f1c22SToby Isaac {
78245d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7825d67d17b1SMatthew G. Knepley   const char    *lname;
7826c58f1c22SToby Isaac   PetscErrorCode ierr;
7827c58f1c22SToby Isaac 
7828c58f1c22SToby Isaac   PetscFunctionBegin;
7829c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7830c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7831c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7832c58f1c22SToby Isaac   while (next) {
7833c58f1c22SToby Isaac     PetscBool flg;
7834c58f1c22SToby Isaac 
7835d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7836d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7837c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7838c58f1c22SToby Isaac     next = next->next;
7839c58f1c22SToby Isaac   }
7840c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7841c58f1c22SToby Isaac }
7842c58f1c22SToby Isaac 
7843c58f1c22SToby Isaac /*@C
7844c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7845c58f1c22SToby Isaac 
7846c58f1c22SToby Isaac   Not Collective
7847c58f1c22SToby Isaac 
7848c58f1c22SToby Isaac   Input Parameters:
7849c58f1c22SToby Isaac + dm     - The DM object
7850c58f1c22SToby Isaac . name   - The label name
7851c58f1c22SToby Isaac - output - The flag for output
7852c58f1c22SToby Isaac 
7853c58f1c22SToby Isaac   Level: developer
7854c58f1c22SToby Isaac 
7855c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7856c58f1c22SToby Isaac @*/
7857c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7858c58f1c22SToby Isaac {
78595d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7860d67d17b1SMatthew G. Knepley   const char    *lname;
7861c58f1c22SToby Isaac   PetscErrorCode ierr;
7862c58f1c22SToby Isaac 
7863c58f1c22SToby Isaac   PetscFunctionBegin;
7864c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7865534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7866c58f1c22SToby Isaac   while (next) {
7867c58f1c22SToby Isaac     PetscBool flg;
7868c58f1c22SToby Isaac 
7869d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7870d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7871c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7872c58f1c22SToby Isaac     next = next->next;
7873c58f1c22SToby Isaac   }
7874c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7875c58f1c22SToby Isaac }
7876c58f1c22SToby Isaac 
7877c58f1c22SToby Isaac /*@
7878c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7879c58f1c22SToby Isaac 
7880d083f849SBarry Smith   Collective on dmA
7881c58f1c22SToby Isaac 
7882c58f1c22SToby Isaac   Input Parameter:
78835d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
78842e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
78855d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7886ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7887c58f1c22SToby Isaac 
7888c58f1c22SToby Isaac   Level: intermediate
7889c58f1c22SToby Isaac 
7890c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7891c58f1c22SToby Isaac 
78925d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7893c58f1c22SToby Isaac @*/
78945d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7895c58f1c22SToby Isaac {
7896c58f1c22SToby Isaac   DMLabel        label, labelNew;
7897c58f1c22SToby Isaac   const char    *name;
7898c58f1c22SToby Isaac   PetscBool      flg;
78995d80c0bfSVaclav Hapla   DMLabelLink    link;
79005d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7901c58f1c22SToby Isaac 
79025d80c0bfSVaclav Hapla   PetscFunctionBegin;
79035d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
79045d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
79055d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
79065d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
79075d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
79085d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
79095d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
79105d80c0bfSVaclav Hapla     label=link->label;
79115d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
79125d80c0bfSVaclav Hapla     if (!all) {
7913c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7914c58f1c22SToby Isaac       if (flg) continue;
79157d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
79167d5acc75SStefano Zampini       if (flg) continue;
7917ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7918ba2698f1SMatthew G. Knepley       if (flg) continue;
79195d80c0bfSVaclav Hapla     }
79205d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7921c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
79225d80c0bfSVaclav Hapla     } else {
79235d80c0bfSVaclav Hapla       labelNew = label;
79245d80c0bfSVaclav Hapla     }
7925c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
79265d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7927c58f1c22SToby Isaac   }
7928c58f1c22SToby Isaac   PetscFunctionReturn(0);
7929c58f1c22SToby Isaac }
7930a8fb8f29SToby Isaac 
7931a8fb8f29SToby Isaac /*@
7932a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7933a8fb8f29SToby Isaac 
7934a8fb8f29SToby Isaac   Input Parameter:
7935a8fb8f29SToby Isaac . dm - The DM object
7936a8fb8f29SToby Isaac 
7937a8fb8f29SToby Isaac   Output Parameter:
7938a8fb8f29SToby Isaac . cdm - The coarse DM
7939a8fb8f29SToby Isaac 
7940a8fb8f29SToby Isaac   Level: intermediate
7941a8fb8f29SToby Isaac 
7942a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7943a8fb8f29SToby Isaac @*/
7944a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7945a8fb8f29SToby Isaac {
7946a8fb8f29SToby Isaac   PetscFunctionBegin;
7947a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7948a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7949a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7950a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7951a8fb8f29SToby Isaac }
7952a8fb8f29SToby Isaac 
7953a8fb8f29SToby Isaac /*@
7954a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7955a8fb8f29SToby Isaac 
7956a8fb8f29SToby Isaac   Input Parameters:
7957a8fb8f29SToby Isaac + dm - The DM object
7958a8fb8f29SToby Isaac - cdm - The coarse DM
7959a8fb8f29SToby Isaac 
7960a8fb8f29SToby Isaac   Level: intermediate
7961a8fb8f29SToby Isaac 
7962a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7963a8fb8f29SToby Isaac @*/
7964a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7965a8fb8f29SToby Isaac {
7966a8fb8f29SToby Isaac   PetscErrorCode ierr;
7967a8fb8f29SToby Isaac 
7968a8fb8f29SToby Isaac   PetscFunctionBegin;
7969a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7970a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7971a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7972a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7973a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7974a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7975a8fb8f29SToby Isaac }
7976a8fb8f29SToby Isaac 
797788bdff64SToby Isaac /*@
797888bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
797988bdff64SToby Isaac 
798088bdff64SToby Isaac   Input Parameter:
798188bdff64SToby Isaac . dm - The DM object
798288bdff64SToby Isaac 
798388bdff64SToby Isaac   Output Parameter:
798488bdff64SToby Isaac . fdm - The fine DM
798588bdff64SToby Isaac 
798688bdff64SToby Isaac   Level: intermediate
798788bdff64SToby Isaac 
798888bdff64SToby Isaac .seealso: DMSetFineDM()
798988bdff64SToby Isaac @*/
799088bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
799188bdff64SToby Isaac {
799288bdff64SToby Isaac   PetscFunctionBegin;
799388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
799488bdff64SToby Isaac   PetscValidPointer(fdm, 2);
799588bdff64SToby Isaac   *fdm = dm->fineMesh;
799688bdff64SToby Isaac   PetscFunctionReturn(0);
799788bdff64SToby Isaac }
799888bdff64SToby Isaac 
799988bdff64SToby Isaac /*@
800088bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
800188bdff64SToby Isaac 
800288bdff64SToby Isaac   Input Parameters:
800388bdff64SToby Isaac + dm - The DM object
800488bdff64SToby Isaac - fdm - The fine DM
800588bdff64SToby Isaac 
800688bdff64SToby Isaac   Level: intermediate
800788bdff64SToby Isaac 
800888bdff64SToby Isaac .seealso: DMGetFineDM()
800988bdff64SToby Isaac @*/
801088bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
801188bdff64SToby Isaac {
801288bdff64SToby Isaac   PetscErrorCode ierr;
801388bdff64SToby Isaac 
801488bdff64SToby Isaac   PetscFunctionBegin;
801588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
801688bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
801788bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
801888bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
801988bdff64SToby Isaac   dm->fineMesh = fdm;
802088bdff64SToby Isaac   PetscFunctionReturn(0);
802188bdff64SToby Isaac }
802288bdff64SToby Isaac 
8023a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8024a6ba4734SToby Isaac 
8025a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
8026a6ba4734SToby Isaac {
8027e5e52638SMatthew G. Knepley   PetscInt       d;
8028a6ba4734SToby Isaac   PetscErrorCode ierr;
8029a6ba4734SToby Isaac 
8030a6ba4734SToby Isaac   PetscFunctionBegin;
8031e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
8032e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
8033e5e52638SMatthew G. Knepley   }
8034a6ba4734SToby Isaac   PetscFunctionReturn(0);
8035a6ba4734SToby Isaac }
8036a6ba4734SToby Isaac 
8037a6ba4734SToby Isaac /*@C
8038a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8039a6ba4734SToby Isaac 
8040783e2ec8SMatthew G. Knepley   Collective on dm
8041783e2ec8SMatthew G. Knepley 
8042a6ba4734SToby Isaac   Input Parameters:
80434c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
8044f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8045a6ba4734SToby Isaac . name        - The BC name
8046a6ba4734SToby Isaac . labelname   - The label defining constrained points
8047a6ba4734SToby Isaac . field       - The field to constrain
8048e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
8049a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8050a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
805156cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time deriative of the boundary values, or NULL
8052a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8053a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8054a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8055a6ba4734SToby Isaac 
8056a6ba4734SToby Isaac   Options Database Keys:
8057a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8058a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8059a6ba4734SToby Isaac 
806056cf3b9cSMatthew G. Knepley   Note:
806156cf3b9cSMatthew 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:
806256cf3b9cSMatthew G. Knepley 
806356cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
806456cf3b9cSMatthew G. Knepley 
806556cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
806656cf3b9cSMatthew G. Knepley 
806756cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
806856cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
806956cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
807056cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
807156cf3b9cSMatthew G. Knepley 
807256cf3b9cSMatthew G. Knepley + dim - the spatial dimension
807356cf3b9cSMatthew G. Knepley . Nf - the number of fields
807456cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
807556cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
807656cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
807756cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
807856cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
807956cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
808056cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
808156cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
808256cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
808356cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
808456cf3b9cSMatthew G. Knepley . t - current time
808556cf3b9cSMatthew G. Knepley . x - coordinates of the current point
808656cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
808756cf3b9cSMatthew G. Knepley . constants - constant parameters
808856cf3b9cSMatthew G. Knepley - bcval - output values at the current point
808956cf3b9cSMatthew G. Knepley 
8090a6ba4734SToby Isaac   Level: developer
8091a6ba4734SToby Isaac 
809256cf3b9cSMatthew G. Knepley .seealso: DMGetBoundary(), PetscDSAddBoundary()
8093a6ba4734SToby Isaac @*/
809456cf3b9cSMatthew 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)
8095a6ba4734SToby Isaac {
8096e5e52638SMatthew G. Knepley   PetscDS        ds;
8097a6ba4734SToby Isaac   PetscErrorCode ierr;
8098a6ba4734SToby Isaac 
8099a6ba4734SToby Isaac   PetscFunctionBegin;
8100a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8101783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
8102783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
8103783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
8104783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
8105e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8106783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
810756cf3b9cSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, bcFunc_t, numids, ids, ctx);CHKERRQ(ierr);
8108a6ba4734SToby Isaac   PetscFunctionReturn(0);
8109a6ba4734SToby Isaac }
8110a6ba4734SToby Isaac 
8111a6ba4734SToby Isaac /*@
8112a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
8113a6ba4734SToby Isaac 
8114a6ba4734SToby Isaac   Input Parameters:
8115a6ba4734SToby Isaac . dm - The mesh object
8116a6ba4734SToby Isaac 
8117a6ba4734SToby Isaac   Output Parameters:
8118a6ba4734SToby Isaac . numBd - The number of BC
8119a6ba4734SToby Isaac 
8120a6ba4734SToby Isaac   Level: intermediate
8121a6ba4734SToby Isaac 
8122a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
8123a6ba4734SToby Isaac @*/
8124a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
8125a6ba4734SToby Isaac {
8126e5e52638SMatthew G. Knepley   PetscDS        ds;
812758ebd649SToby Isaac   PetscErrorCode ierr;
8128a6ba4734SToby Isaac 
8129a6ba4734SToby Isaac   PetscFunctionBegin;
8130a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8131e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8132e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
8133a6ba4734SToby Isaac   PetscFunctionReturn(0);
8134a6ba4734SToby Isaac }
8135a6ba4734SToby Isaac 
8136a6ba4734SToby Isaac /*@C
81371c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
8138a6ba4734SToby Isaac 
8139a6ba4734SToby Isaac   Input Parameters:
8140a6ba4734SToby Isaac + dm          - The mesh object
8141a6ba4734SToby Isaac - bd          - The BC number
8142a6ba4734SToby Isaac 
8143a6ba4734SToby Isaac   Output Parameters:
8144f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8145a6ba4734SToby Isaac . name        - The BC name
8146a6ba4734SToby Isaac . labelname   - The label defining constrained points
8147a6ba4734SToby Isaac . field       - The field to constrain
8148a6ba4734SToby Isaac . numcomps    - The number of constrained field components
8149a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8150a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
815156cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time derviative of the boundary values
8152a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8153a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8154a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8155a6ba4734SToby Isaac 
8156a6ba4734SToby Isaac   Options Database Keys:
8157a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8158a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8159a6ba4734SToby Isaac 
8160a6ba4734SToby Isaac   Level: developer
8161a6ba4734SToby Isaac 
8162a6ba4734SToby Isaac .seealso: DMAddBoundary()
8163a6ba4734SToby Isaac @*/
816456cf3b9cSMatthew 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)
8165a6ba4734SToby Isaac {
8166e5e52638SMatthew G. Knepley   PetscDS        ds;
816758ebd649SToby Isaac   PetscErrorCode ierr;
8168a6ba4734SToby Isaac 
8169a6ba4734SToby Isaac   PetscFunctionBegin;
8170a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8171e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
817256cf3b9cSMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, func_t, numids, ids, ctx);CHKERRQ(ierr);
8173a6ba4734SToby Isaac   PetscFunctionReturn(0);
8174a6ba4734SToby Isaac }
8175a6ba4734SToby Isaac 
8176e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8177e6f8dbb6SToby Isaac {
8178e5e52638SMatthew G. Knepley   PetscDS        ds;
8179dff059c6SToby Isaac   DMBoundary    *lastnext;
8180e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8181e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8182e6f8dbb6SToby Isaac 
8183e6f8dbb6SToby Isaac   PetscFunctionBegin;
8184e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8185e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
818647a1f5adSToby Isaac   if (dm->boundary) {
818747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
818847a1f5adSToby Isaac 
818947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
819047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
819147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
819247a1f5adSToby Isaac     while (next) {
819347a1f5adSToby Isaac       DMBoundary b = next;
819447a1f5adSToby Isaac 
819547a1f5adSToby Isaac       next = b->next;
819647a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8197a6ba4734SToby Isaac     }
819847a1f5adSToby Isaac     dm->boundary = NULL;
8199a6ba4734SToby Isaac   }
820047a1f5adSToby Isaac 
8201dff059c6SToby Isaac   lastnext = &(dm->boundary);
8202e6f8dbb6SToby Isaac   while (dsbound) {
8203e6f8dbb6SToby Isaac     DMBoundary dmbound;
8204e6f8dbb6SToby Isaac 
8205e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8206e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
8207e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
8208994fe344SLisandro 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);}
820947a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8210dff059c6SToby Isaac     *lastnext = dmbound;
8211dff059c6SToby Isaac     lastnext = &(dmbound->next);
8212dff059c6SToby Isaac     dsbound = dsbound->next;
8213a6ba4734SToby Isaac   }
8214a6ba4734SToby Isaac   PetscFunctionReturn(0);
8215a6ba4734SToby Isaac }
8216a6ba4734SToby Isaac 
8217a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8218a6ba4734SToby Isaac {
8219b95f2879SToby Isaac   DMBoundary     b;
8220a6ba4734SToby Isaac   PetscErrorCode ierr;
8221a6ba4734SToby Isaac 
8222a6ba4734SToby Isaac   PetscFunctionBegin;
8223a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8224534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8225a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8226e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8227b95f2879SToby Isaac   b = dm->boundary;
8228a6ba4734SToby Isaac   while (b && !(*isBd)) {
8229e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8230e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
82313424c85cSToby Isaac 
82323424c85cSToby Isaac     if (label) {
8233a6ba4734SToby Isaac       PetscInt i;
8234a6ba4734SToby Isaac 
8235e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
8236e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
8237a6ba4734SToby Isaac       }
8238a6ba4734SToby Isaac     }
8239a6ba4734SToby Isaac     b = b->next;
8240a6ba4734SToby Isaac   }
8241a6ba4734SToby Isaac   PetscFunctionReturn(0);
8242a6ba4734SToby Isaac }
82434d6f44ffSToby Isaac 
82444d6f44ffSToby Isaac /*@C
8245a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8246a6e0b375SMatthew G. Knepley 
8247a6e0b375SMatthew G. Knepley   Collective on DM
82484d6f44ffSToby Isaac 
82494d6f44ffSToby Isaac   Input Parameters:
82504d6f44ffSToby Isaac + dm      - The DM
82510709b2feSToby Isaac . time    - The time
82524d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
82534d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
82544d6f44ffSToby Isaac - mode    - The insertion mode for values
82554d6f44ffSToby Isaac 
82564d6f44ffSToby Isaac   Output Parameter:
82574d6f44ffSToby Isaac . X - vector
82584d6f44ffSToby Isaac 
82594d6f44ffSToby Isaac    Calling sequence of func:
82600709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
82614d6f44ffSToby Isaac 
82624d6f44ffSToby Isaac +  dim - The spatial dimension
82638ec8862eSJed Brown .  time - The time at which to sample
82644d6f44ffSToby Isaac .  x   - The coordinates
82654d6f44ffSToby Isaac .  Nf  - The number of fields
82664d6f44ffSToby Isaac .  u   - The output field values
82674d6f44ffSToby Isaac -  ctx - optional user-defined function context
82684d6f44ffSToby Isaac 
82694d6f44ffSToby Isaac   Level: developer
82704d6f44ffSToby Isaac 
8271a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
82724d6f44ffSToby Isaac @*/
82730709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
82744d6f44ffSToby Isaac {
82754d6f44ffSToby Isaac   Vec            localX;
82764d6f44ffSToby Isaac   PetscErrorCode ierr;
82774d6f44ffSToby Isaac 
82784d6f44ffSToby Isaac   PetscFunctionBegin;
82794d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82804d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
82810709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
82824d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
82834d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
82844d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
82854d6f44ffSToby Isaac   PetscFunctionReturn(0);
82864d6f44ffSToby Isaac }
82874d6f44ffSToby Isaac 
8288a6e0b375SMatthew G. Knepley /*@C
8289a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8290a6e0b375SMatthew G. Knepley 
8291a6e0b375SMatthew G. Knepley   Not collective
8292a6e0b375SMatthew G. Knepley 
8293a6e0b375SMatthew G. Knepley   Input Parameters:
8294a6e0b375SMatthew G. Knepley + dm      - The DM
8295a6e0b375SMatthew G. Knepley . time    - The time
8296a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8297a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8298a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8299a6e0b375SMatthew G. Knepley 
8300a6e0b375SMatthew G. Knepley   Output Parameter:
8301a6e0b375SMatthew G. Knepley . localX - vector
8302a6e0b375SMatthew G. Knepley 
8303a6e0b375SMatthew G. Knepley    Calling sequence of func:
8304a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8305a6e0b375SMatthew G. Knepley 
8306a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8307a6e0b375SMatthew G. Knepley .  x   - The coordinates
8308a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8309a6e0b375SMatthew G. Knepley .  u   - The output field values
8310a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8311a6e0b375SMatthew G. Knepley 
8312a6e0b375SMatthew G. Knepley   Level: developer
8313a6e0b375SMatthew G. Knepley 
8314a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8315a6e0b375SMatthew G. Knepley @*/
83160709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
83174d6f44ffSToby Isaac {
83184d6f44ffSToby Isaac   PetscErrorCode ierr;
83194d6f44ffSToby Isaac 
83204d6f44ffSToby Isaac   PetscFunctionBegin;
83214d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83224d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
83230918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
83240709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83254d6f44ffSToby Isaac   PetscFunctionReturn(0);
83264d6f44ffSToby Isaac }
83274d6f44ffSToby Isaac 
8328a6e0b375SMatthew G. Knepley /*@C
8329a6e0b375SMatthew 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.
8330a6e0b375SMatthew G. Knepley 
8331a6e0b375SMatthew G. Knepley   Collective on DM
8332a6e0b375SMatthew G. Knepley 
8333a6e0b375SMatthew G. Knepley   Input Parameters:
8334a6e0b375SMatthew G. Knepley + dm      - The DM
8335a6e0b375SMatthew G. Knepley . time    - The time
8336a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8337a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8338a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8339a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8340a6e0b375SMatthew G. Knepley 
8341a6e0b375SMatthew G. Knepley   Output Parameter:
8342a6e0b375SMatthew G. Knepley . X - vector
8343a6e0b375SMatthew G. Knepley 
8344a6e0b375SMatthew G. Knepley    Calling sequence of func:
8345a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8346a6e0b375SMatthew G. Knepley 
8347a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8348a6e0b375SMatthew G. Knepley .  x   - The coordinates
8349a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8350a6e0b375SMatthew G. Knepley .  u   - The output field values
8351a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8352a6e0b375SMatthew G. Knepley 
8353a6e0b375SMatthew G. Knepley   Level: developer
8354a6e0b375SMatthew G. Knepley 
8355a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8356a6e0b375SMatthew G. Knepley @*/
83572c53366bSMatthew 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)
83582c53366bSMatthew G. Knepley {
83592c53366bSMatthew G. Knepley   Vec            localX;
83602c53366bSMatthew G. Knepley   PetscErrorCode ierr;
83612c53366bSMatthew G. Knepley 
83622c53366bSMatthew G. Knepley   PetscFunctionBegin;
83632c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83642c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
83652c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83662c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
83672c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
83682c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
83692c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
83702c53366bSMatthew G. Knepley }
83712c53366bSMatthew G. Knepley 
8372a6e0b375SMatthew G. Knepley /*@C
8373a6e0b375SMatthew 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.
8374a6e0b375SMatthew G. Knepley 
8375a6e0b375SMatthew G. Knepley   Not collective
8376a6e0b375SMatthew G. Knepley 
8377a6e0b375SMatthew G. Knepley   Input Parameters:
8378a6e0b375SMatthew G. Knepley + dm      - The DM
8379a6e0b375SMatthew G. Knepley . time    - The time
8380a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8381a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8382a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8383a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8384a6e0b375SMatthew G. Knepley 
8385a6e0b375SMatthew G. Knepley   Output Parameter:
8386a6e0b375SMatthew G. Knepley . localX - vector
8387a6e0b375SMatthew G. Knepley 
8388a6e0b375SMatthew G. Knepley    Calling sequence of func:
8389a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8390a6e0b375SMatthew G. Knepley 
8391a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8392a6e0b375SMatthew G. Knepley .  x   - The coordinates
8393a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8394a6e0b375SMatthew G. Knepley .  u   - The output field values
8395a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8396a6e0b375SMatthew G. Knepley 
8397a6e0b375SMatthew G. Knepley   Level: developer
8398a6e0b375SMatthew G. Knepley 
8399a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8400a6e0b375SMatthew G. Knepley @*/
84011c531cf8SMatthew 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)
84024d6f44ffSToby Isaac {
84034d6f44ffSToby Isaac   PetscErrorCode ierr;
84044d6f44ffSToby Isaac 
84054d6f44ffSToby Isaac   PetscFunctionBegin;
84064d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84074d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
84080918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
84091c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
84104d6f44ffSToby Isaac   PetscFunctionReturn(0);
84114d6f44ffSToby Isaac }
84122716604bSToby Isaac 
8413a6e0b375SMatthew G. Knepley /*@C
8414a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8415a6e0b375SMatthew G. Knepley 
8416a6e0b375SMatthew G. Knepley   Not collective
8417a6e0b375SMatthew G. Knepley 
8418a6e0b375SMatthew G. Knepley   Input Parameters:
8419a6e0b375SMatthew G. Knepley + dm      - The DM
8420a6e0b375SMatthew G. Knepley . time    - The time
8421a6e0b375SMatthew G. Knepley . localU  - The input field vector
8422a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8423a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8424a6e0b375SMatthew G. Knepley 
8425a6e0b375SMatthew G. Knepley   Output Parameter:
8426a6e0b375SMatthew G. Knepley . localX  - The output vector
8427a6e0b375SMatthew G. Knepley 
8428a6e0b375SMatthew G. Knepley    Calling sequence of func:
8429a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8430a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8431a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8432a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8433a6e0b375SMatthew G. Knepley 
8434a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8435a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8436a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8437a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8438a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8439a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8440a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8441a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8442a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8443a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8444a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8445a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8446a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8447a6e0b375SMatthew G. Knepley .  t            - The current time
8448a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8449a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8450a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8451a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8452a6e0b375SMatthew G. Knepley 
8453a6e0b375SMatthew 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.
8454a6e0b375SMatthew 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
8455a6e0b375SMatthew 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
8456a6e0b375SMatthew 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.
8457a6e0b375SMatthew G. Knepley 
8458a6e0b375SMatthew G. Knepley   Level: intermediate
8459a6e0b375SMatthew G. Knepley 
8460a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8461a6e0b375SMatthew G. Knepley @*/
84628c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
84638c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
84648c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
84658c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8466191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
84678c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
84688c6c5593SMatthew G. Knepley {
84698c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
84708c6c5593SMatthew G. Knepley 
84718c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84728c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84738c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
84748c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
84750918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
84768c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
84778c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
84788c6c5593SMatthew G. Knepley }
84798c6c5593SMatthew G. Knepley 
8480a6e0b375SMatthew G. Knepley /*@C
8481a6e0b375SMatthew 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.
8482a6e0b375SMatthew G. Knepley 
8483a6e0b375SMatthew G. Knepley   Not collective
8484a6e0b375SMatthew G. Knepley 
8485a6e0b375SMatthew G. Knepley   Input Parameters:
8486a6e0b375SMatthew G. Knepley + dm      - The DM
8487a6e0b375SMatthew G. Knepley . time    - The time
8488a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8489a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8490a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8491a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8492a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8493a6e0b375SMatthew G. Knepley . localU  - The input field vector
8494a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8495a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8496a6e0b375SMatthew G. Knepley 
8497a6e0b375SMatthew G. Knepley   Output Parameter:
8498a6e0b375SMatthew G. Knepley . localX  - The output vector
8499a6e0b375SMatthew G. Knepley 
8500a6e0b375SMatthew G. Knepley    Calling sequence of func:
8501a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8502a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8503a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8504a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8505a6e0b375SMatthew G. Knepley 
8506a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8507a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8508a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8509a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8510a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8511a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8512a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8513a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8514a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8515a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8516a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8517a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8518a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8519a6e0b375SMatthew G. Knepley .  t            - The current time
8520a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8521a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8522a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8523a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8524a6e0b375SMatthew G. Knepley 
8525a6e0b375SMatthew 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.
8526a6e0b375SMatthew 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
8527a6e0b375SMatthew 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
8528a6e0b375SMatthew 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.
8529a6e0b375SMatthew G. Knepley 
8530a6e0b375SMatthew G. Knepley   Level: intermediate
8531a6e0b375SMatthew G. Knepley 
8532a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8533a6e0b375SMatthew G. Knepley @*/
85341c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
85358c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
85368c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
85378c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8538191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
85398c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
85408c6c5593SMatthew G. Knepley {
85418c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
85428c6c5593SMatthew G. Knepley 
85438c6c5593SMatthew G. Knepley   PetscFunctionBegin;
85448c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85458c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
85468c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8547ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
85481c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
85498c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
85508c6c5593SMatthew G. Knepley }
85518c6c5593SMatthew G. Knepley 
85522716604bSToby Isaac /*@C
8553ece3a9fcSMatthew 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.
8554ece3a9fcSMatthew G. Knepley 
8555ece3a9fcSMatthew G. Knepley   Not collective
8556ece3a9fcSMatthew G. Knepley 
8557ece3a9fcSMatthew G. Knepley   Input Parameters:
8558ece3a9fcSMatthew G. Knepley + dm      - The DM
8559ece3a9fcSMatthew G. Knepley . time    - The time
8560ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8561ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8562ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8563ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8564ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8565ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8566ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8567ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8568ece3a9fcSMatthew G. Knepley 
8569ece3a9fcSMatthew G. Knepley   Output Parameter:
8570ece3a9fcSMatthew G. Knepley . localX  - The output vector
8571ece3a9fcSMatthew G. Knepley 
8572ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8573ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8574ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8575ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8576ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8577ece3a9fcSMatthew G. Knepley 
8578ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8579ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8580ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8581ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8582ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8583ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8584ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8585ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8586ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8587ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8588ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8589ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8590ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8591ece3a9fcSMatthew G. Knepley .  t            - The current time
8592ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8593ece3a9fcSMatthew G. Knepley .  n            - The face normal
8594ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8595ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8596ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8597ece3a9fcSMatthew G. Knepley 
8598ece3a9fcSMatthew G. Knepley   Note:
8599ece3a9fcSMatthew 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.
8600ece3a9fcSMatthew 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
8601ece3a9fcSMatthew 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
8602ece3a9fcSMatthew 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.
8603ece3a9fcSMatthew G. Knepley 
8604ece3a9fcSMatthew G. Knepley   Level: intermediate
8605ece3a9fcSMatthew G. Knepley 
8606ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8607ece3a9fcSMatthew G. Knepley @*/
8608ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8609ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8610ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8611ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8612ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8613ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8614ece3a9fcSMatthew G. Knepley {
8615ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8616ece3a9fcSMatthew G. Knepley 
8617ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8618ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8619ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8620ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8621ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8622ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8623ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8624ece3a9fcSMatthew G. Knepley }
8625ece3a9fcSMatthew G. Knepley 
8626ece3a9fcSMatthew G. Knepley /*@C
86272716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
86282716604bSToby Isaac 
86292716604bSToby Isaac   Input Parameters:
86302716604bSToby Isaac + dm    - The DM
86310709b2feSToby Isaac . time  - The time
86322716604bSToby Isaac . funcs - The functions to evaluate for each field component
86332716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8634574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86352716604bSToby Isaac 
86362716604bSToby Isaac   Output Parameter:
86372716604bSToby Isaac . diff - The diff ||u - u_h||_2
86382716604bSToby Isaac 
86392716604bSToby Isaac   Level: developer
86402716604bSToby Isaac 
86411189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
86422716604bSToby Isaac @*/
86430709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
86442716604bSToby Isaac {
86452716604bSToby Isaac   PetscErrorCode ierr;
86462716604bSToby Isaac 
86472716604bSToby Isaac   PetscFunctionBegin;
86482716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8649b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
86500918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
86510709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
86522716604bSToby Isaac   PetscFunctionReturn(0);
86532716604bSToby Isaac }
8654b698f381SToby Isaac 
8655b698f381SToby Isaac /*@C
8656b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8657b698f381SToby Isaac 
8658d083f849SBarry Smith   Collective on dm
8659d083f849SBarry Smith 
8660b698f381SToby Isaac   Input Parameters:
8661b698f381SToby Isaac + dm    - The DM
8662b698f381SToby Isaac , time  - The time
8663b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8664b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8665574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8666b698f381SToby Isaac - n     - The vector to project along
8667b698f381SToby Isaac 
8668b698f381SToby Isaac   Output Parameter:
8669b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8670b698f381SToby Isaac 
8671b698f381SToby Isaac   Level: developer
8672b698f381SToby Isaac 
8673b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8674b698f381SToby Isaac @*/
8675b698f381SToby 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)
8676b698f381SToby Isaac {
8677b698f381SToby Isaac   PetscErrorCode ierr;
8678b698f381SToby Isaac 
8679b698f381SToby Isaac   PetscFunctionBegin;
8680b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8681b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8682b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8683b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8684b698f381SToby Isaac   PetscFunctionReturn(0);
8685b698f381SToby Isaac }
8686b698f381SToby Isaac 
86872a16baeaSToby Isaac /*@C
86882a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86892a16baeaSToby Isaac 
8690d083f849SBarry Smith   Collective on dm
8691d083f849SBarry Smith 
86922a16baeaSToby Isaac   Input Parameters:
86932a16baeaSToby Isaac + dm    - The DM
86942a16baeaSToby Isaac . time  - The time
86952a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86962a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8697574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86982a16baeaSToby Isaac 
86992a16baeaSToby Isaac   Output Parameter:
87002a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
87012a16baeaSToby Isaac 
87022a16baeaSToby Isaac   Level: developer
87032a16baeaSToby Isaac 
87041189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
87052a16baeaSToby Isaac @*/
87061189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
87072a16baeaSToby Isaac {
87082a16baeaSToby Isaac   PetscErrorCode ierr;
87092a16baeaSToby Isaac 
87102a16baeaSToby Isaac   PetscFunctionBegin;
87112a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87122a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
87130918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
87142a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
87152a16baeaSToby Isaac   PetscFunctionReturn(0);
87162a16baeaSToby Isaac }
87172a16baeaSToby Isaac 
8718df0b854cSToby Isaac /*@C
8719df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8720cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8721df0b854cSToby Isaac 
8722df0b854cSToby Isaac   Collective on dm
8723df0b854cSToby Isaac 
8724df0b854cSToby Isaac   Input parameters:
8725df0b854cSToby Isaac + dm - the pre-adaptation DM object
8726a1b0c543SToby Isaac - label - label with the flags
8727df0b854cSToby Isaac 
8728df0b854cSToby Isaac   Output parameters:
87290d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8730df0b854cSToby Isaac 
8731df0b854cSToby Isaac   Level: intermediate
87320d1cd5e0SMatthew G. Knepley 
87330d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8734df0b854cSToby Isaac @*/
87350d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8736df0b854cSToby Isaac {
8737df0b854cSToby Isaac   PetscErrorCode ierr;
8738df0b854cSToby Isaac 
8739df0b854cSToby Isaac   PetscFunctionBegin;
8740df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8741a1b0c543SToby Isaac   PetscValidPointer(label,2);
87420d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
87430d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
87446f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
87450d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
8746c8a6034eSMark   if (*dmAdapt) (*dmAdapt)->prealloc_only = dm->prealloc_only;  /* maybe this should go .... */
87470d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
87480d1cd5e0SMatthew G. Knepley }
87490d1cd5e0SMatthew G. Knepley 
87500d1cd5e0SMatthew G. Knepley /*@C
87510d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
87520d1cd5e0SMatthew G. Knepley 
87530d1cd5e0SMatthew G. Knepley   Input Parameters:
87540d1cd5e0SMatthew G. Knepley + dm - The DM object
87550d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
87566f25b0d8SLisandro 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_".
87570d1cd5e0SMatthew G. Knepley 
87580d1cd5e0SMatthew G. Knepley   Output Parameter:
87590d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
87600d1cd5e0SMatthew G. Knepley 
87610d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
87620d1cd5e0SMatthew G. Knepley 
87630d1cd5e0SMatthew G. Knepley   Level: advanced
87640d1cd5e0SMatthew G. Knepley 
87650d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
87660d1cd5e0SMatthew G. Knepley @*/
87670d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
87680d1cd5e0SMatthew G. Knepley {
87690d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
87700d1cd5e0SMatthew G. Knepley 
87710d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
87720d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87730d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
87740d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
87750d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
87766f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
87776f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
87780d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8779df0b854cSToby Isaac   PetscFunctionReturn(0);
8780df0b854cSToby Isaac }
8781c4088d22SMatthew G. Knepley 
8782502a2867SDave May /*@C
8783502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8784502a2867SDave May 
8785502a2867SDave May  Not Collective
8786502a2867SDave May 
8787502a2867SDave May  Input Parameter:
8788502a2867SDave May .  dm    - The DM
8789502a2867SDave May 
87900a19bb7dSprj-  Output Parameters:
87910a19bb7dSprj- +  nranks - the number of neighbours
87920a19bb7dSprj- -  ranks - the neighbors ranks
8793502a2867SDave May 
8794502a2867SDave May  Notes:
8795502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8796502a2867SDave May 
8797502a2867SDave May  Level: beginner
8798502a2867SDave May 
8799dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8800502a2867SDave May @*/
8801502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8802502a2867SDave May {
8803502a2867SDave May   PetscErrorCode ierr;
8804502a2867SDave May 
8805502a2867SDave May   PetscFunctionBegin;
8806502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88070918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8808502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8809502a2867SDave May   PetscFunctionReturn(0);
8810502a2867SDave May }
8811502a2867SDave May 
8812531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8813531c7667SBarry Smith 
8814531c7667SBarry Smith /*
8815531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8816531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8817531c7667SBarry Smith */
8818531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8819531c7667SBarry Smith {
8820531c7667SBarry Smith   PetscErrorCode ierr;
8821531c7667SBarry Smith 
8822531c7667SBarry Smith   PetscFunctionBegin;
8823531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8824531c7667SBarry Smith     Vec x1local;
8825531c7667SBarry Smith     DM  dm;
8826531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8827531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8828531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8829531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8830531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8831531c7667SBarry Smith     x1   = x1local;
8832531c7667SBarry Smith   }
8833531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8834531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8835531c7667SBarry Smith     DM  dm;
8836531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8837531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8838531c7667SBarry Smith   }
8839531c7667SBarry Smith   PetscFunctionReturn(0);
8840531c7667SBarry Smith }
8841531c7667SBarry Smith 
8842531c7667SBarry Smith /*@
8843531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8844531c7667SBarry Smith 
8845531c7667SBarry Smith     Input Parameter:
8846531c7667SBarry Smith .    coloring - the MatFDColoring object
8847531c7667SBarry Smith 
884895452b02SPatrick Sanan     Developer Notes:
884995452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8850531c7667SBarry Smith 
88511b266c99SBarry Smith     Level: advanced
88521b266c99SBarry Smith 
8853531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8854531c7667SBarry Smith @*/
8855531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8856531c7667SBarry Smith {
8857531c7667SBarry Smith   PetscFunctionBegin;
8858531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8859531c7667SBarry Smith   PetscFunctionReturn(0);
8860531c7667SBarry Smith }
88618320bc6fSPatrick Sanan 
88628320bc6fSPatrick Sanan /*@
88638320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
88648320bc6fSPatrick Sanan 
88658320bc6fSPatrick Sanan     Collective
88668320bc6fSPatrick Sanan 
88678320bc6fSPatrick Sanan     Input Parameters:
8868a5bc1bf3SBarry Smith +    dm1 - the first DM
88698320bc6fSPatrick Sanan -    dm2 - the second DM
88708320bc6fSPatrick Sanan 
88718320bc6fSPatrick Sanan     Output Parameters:
88728320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
88738320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
88748320bc6fSPatrick Sanan 
88758320bc6fSPatrick Sanan     Notes:
88768320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
88773d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
88788320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
88793d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
88803d862458SPatrick Sanan     decomposition, but hold different data.
88818320bc6fSPatrick Sanan 
88828320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
88838320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
88848320bc6fSPatrick Sanan 
88858320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
88868320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
88878320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
88888320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
88898320bc6fSPatrick Sanan 
88908320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
88918320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
88928320bc6fSPatrick Sanan .vb
88938320bc6fSPatrick Sanan   ...
88948320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
88958320bc6fSPatrick Sanan   if (set && compatible)  {
88968320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
88978320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
88983d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
88998320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
89008320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
89018320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
89028320bc6fSPatrick Sanan       }
89038320bc6fSPatrick Sanan     }
89048320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
89058320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
89068320bc6fSPatrick Sanan   } else {
89078320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
89088320bc6fSPatrick Sanan   }
89098320bc6fSPatrick Sanan   ...
89108320bc6fSPatrick Sanan .ve
89118320bc6fSPatrick Sanan 
89128320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
89138320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
89148320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
89158320bc6fSPatrick Sanan     always check the "set" output parameter.
89168320bc6fSPatrick Sanan 
89178320bc6fSPatrick Sanan     A DM is always compatible with itself.
89188320bc6fSPatrick Sanan 
89198320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
89208320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
89218320bc6fSPatrick Sanan     incompatible.
89228320bc6fSPatrick Sanan 
89238320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
89248320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
89258320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
89268320bc6fSPatrick Sanan 
89278320bc6fSPatrick Sanan     Developer Notes:
89283d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
89293d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8930a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
89318320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
89328320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
89333d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
89343d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
89358320bc6fSPatrick Sanan 
89368320bc6fSPatrick Sanan     Level: advanced
89378320bc6fSPatrick Sanan 
89383d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
89398320bc6fSPatrick Sanan @*/
89408320bc6fSPatrick Sanan 
8941a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
89428320bc6fSPatrick Sanan {
89438320bc6fSPatrick Sanan   PetscErrorCode ierr;
89448320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
89458320bc6fSPatrick Sanan   DMType         type,type2;
89468320bc6fSPatrick Sanan   PetscBool      sameType;
89478320bc6fSPatrick Sanan 
89488320bc6fSPatrick Sanan   PetscFunctionBegin;
8949a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
89508320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
89518320bc6fSPatrick Sanan 
89528320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8953a5bc1bf3SBarry Smith   if (dm1 == dm2) {
89548320bc6fSPatrick Sanan     *set = PETSC_TRUE;
89558320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
89568320bc6fSPatrick Sanan     PetscFunctionReturn(0);
89578320bc6fSPatrick Sanan   }
89588320bc6fSPatrick Sanan 
89598320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
89608320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
89618320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
89628320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8963a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
89648320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
89658320bc6fSPatrick Sanan     *set = PETSC_TRUE;
89668320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
89678320bc6fSPatrick Sanan     PetscFunctionReturn(0);
89688320bc6fSPatrick Sanan   }
89698320bc6fSPatrick Sanan 
89708320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8971a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8972a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8973b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
89748320bc6fSPatrick Sanan   }
89758320bc6fSPatrick Sanan 
8976a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
89778320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8978a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
89798320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
89808320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
89818320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8982a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
89838320bc6fSPatrick Sanan   } else {
89848320bc6fSPatrick Sanan     *set = PETSC_FALSE;
89858320bc6fSPatrick Sanan   }
89868320bc6fSPatrick Sanan   PetscFunctionReturn(0);
89878320bc6fSPatrick Sanan }
8988c0f0dcc3SMatthew G. Knepley 
8989c0f0dcc3SMatthew G. Knepley /*@C
8990c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8991c0f0dcc3SMatthew G. Knepley 
8992c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8993c0f0dcc3SMatthew G. Knepley 
8994c0f0dcc3SMatthew G. Knepley   Input Parameters:
8995c0f0dcc3SMatthew G. Knepley + DM - the DM
8996c0f0dcc3SMatthew G. Knepley . f - the monitor function
8997c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8998c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8999c0f0dcc3SMatthew G. Knepley 
9000c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9001c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9002c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9003c0f0dcc3SMatthew G. Knepley 
9004c0f0dcc3SMatthew G. Knepley   Notes:
9005c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9006c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9007c0f0dcc3SMatthew G. Knepley   order in which they were set.
9008c0f0dcc3SMatthew G. Knepley 
9009c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9010c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9011c0f0dcc3SMatthew G. Knepley 
9012c0f0dcc3SMatthew G. Knepley   Level: intermediate
9013c0f0dcc3SMatthew G. Knepley 
9014c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9015c0f0dcc3SMatthew G. Knepley @*/
9016c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9017c0f0dcc3SMatthew G. Knepley {
9018c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9019c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9020c0f0dcc3SMatthew G. Knepley 
9021c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9022c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9023c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9024c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9025c0f0dcc3SMatthew G. Knepley 
9026c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9027c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9028c0f0dcc3SMatthew G. Knepley   }
9029c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9030c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9031c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9032c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9033c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9034c0f0dcc3SMatthew G. Knepley }
9035c0f0dcc3SMatthew G. Knepley 
9036c0f0dcc3SMatthew G. Knepley /*@
9037c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9038c0f0dcc3SMatthew G. Knepley 
9039c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9040c0f0dcc3SMatthew G. Knepley 
9041c0f0dcc3SMatthew G. Knepley   Input Parameter:
9042c0f0dcc3SMatthew G. Knepley . dm - the DM
9043c0f0dcc3SMatthew G. Knepley 
9044c0f0dcc3SMatthew G. Knepley   Options Database Key:
9045c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9046c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9047c0f0dcc3SMatthew G. Knepley   set via the options database
9048c0f0dcc3SMatthew G. Knepley 
9049c0f0dcc3SMatthew G. Knepley   Notes:
9050c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9051c0f0dcc3SMatthew G. Knepley 
9052c0f0dcc3SMatthew G. Knepley   Level: intermediate
9053c0f0dcc3SMatthew G. Knepley 
9054c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9055c0f0dcc3SMatthew G. Knepley @*/
9056c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9057c0f0dcc3SMatthew G. Knepley {
9058c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9059c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9060c0f0dcc3SMatthew G. Knepley 
9061c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9062c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9063c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9064c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9065c0f0dcc3SMatthew G. Knepley   }
9066c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9067c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9068c0f0dcc3SMatthew G. Knepley }
9069c0f0dcc3SMatthew G. Knepley 
9070c0f0dcc3SMatthew G. Knepley /*@C
9071c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9072c0f0dcc3SMatthew G. Knepley 
9073c0f0dcc3SMatthew G. Knepley   Collective on DM
9074c0f0dcc3SMatthew G. Knepley 
9075c0f0dcc3SMatthew G. Knepley   Input Parameters:
9076c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9077c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9078c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9079c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9080c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9081c0f0dcc3SMatthew 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
9082c0f0dcc3SMatthew G. Knepley 
9083c0f0dcc3SMatthew G. Knepley   Output Parameter:
9084c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9085c0f0dcc3SMatthew G. Knepley 
9086c0f0dcc3SMatthew G. Knepley   Level: developer
9087c0f0dcc3SMatthew G. Knepley 
9088c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9089c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9090c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9091c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9092c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9093c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9094c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9095c0f0dcc3SMatthew G. Knepley @*/
9096c0f0dcc3SMatthew 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)
9097c0f0dcc3SMatthew G. Knepley {
9098c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9099c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9100c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9101c0f0dcc3SMatthew G. Knepley 
9102c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9103c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9104c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9105c0f0dcc3SMatthew G. Knepley   if (*flg) {
9106c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9107c0f0dcc3SMatthew G. Knepley 
9108c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9109c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9110c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9111c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9112c0f0dcc3SMatthew G. Knepley   }
9113c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9114c0f0dcc3SMatthew G. Knepley }
9115c0f0dcc3SMatthew G. Knepley 
9116c0f0dcc3SMatthew G. Knepley /*@
9117c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9118c0f0dcc3SMatthew G. Knepley 
9119c0f0dcc3SMatthew G. Knepley    Collective on DM
9120c0f0dcc3SMatthew G. Knepley 
9121c0f0dcc3SMatthew G. Knepley    Input Parameters:
9122c0f0dcc3SMatthew G. Knepley .  dm - The DM
9123c0f0dcc3SMatthew G. Knepley 
9124c0f0dcc3SMatthew G. Knepley    Level: developer
9125c0f0dcc3SMatthew G. Knepley 
9126c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9127c0f0dcc3SMatthew G. Knepley @*/
9128c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9129c0f0dcc3SMatthew G. Knepley {
9130c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9131c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9132c0f0dcc3SMatthew G. Knepley 
9133c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9134c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9135c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9136c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9137c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9138c0f0dcc3SMatthew G. Knepley   }
9139c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9140c0f0dcc3SMatthew G. Knepley }
9141