xref: /petsc/src/dm/interface/dm.c (revision b0c7db2251b14c2bf1fcf12ecb441159a8187d90)
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);
2512a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2513a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2514d0295fc0SJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray);CHKERRQ(ierr);
2515a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2516a256111fSJunchao 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 
2557a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2558a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
25597128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2560a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2561a256111fSJunchao 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 {
2760a256111fSJunchao 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 {
2766a256111fSJunchao 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) {
2806a256111fSJunchao 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) {
2814a256111fSJunchao 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 {
2873a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
2874ca3d3a14SMatthew G. Knepley     }
2875a256111fSJunchao 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 {
2881a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2882ca3d3a14SMatthew G. Knepley     }
2883a256111fSJunchao 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 {
449688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
449788ed4aceSMatthew G Knepley 
449888ed4aceSMatthew G Knepley   PetscFunctionBegin;
449988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4500*b0c7db22SLisandro Dalcin   ierr = PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection);CHKERRQ(ierr);
450188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
450288ed4aceSMatthew G Knepley }
4503af122d2aSMatthew G Knepley 
4504b21d0597SMatthew G Knepley /*@
4505b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4506b21d0597SMatthew G Knepley 
4507b21d0597SMatthew G Knepley   Input Parameter:
4508b21d0597SMatthew G Knepley . dm - The DM
4509b21d0597SMatthew G Knepley 
4510b21d0597SMatthew G Knepley   Output Parameter:
4511b21d0597SMatthew G Knepley . sf - The PetscSF
4512b21d0597SMatthew G Knepley 
4513b21d0597SMatthew G Knepley   Level: intermediate
4514b21d0597SMatthew G Knepley 
4515b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4516b21d0597SMatthew G Knepley 
45171bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4518b21d0597SMatthew G Knepley @*/
45190adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
45200adebc6cSBarry Smith {
4521b21d0597SMatthew G Knepley   PetscFunctionBegin;
4522b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4523b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4524b21d0597SMatthew G Knepley   *sf = dm->sf;
4525b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4526b21d0597SMatthew G Knepley }
4527b21d0597SMatthew G Knepley 
4528057b4bcdSMatthew G Knepley /*@
4529057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4530057b4bcdSMatthew G Knepley 
4531057b4bcdSMatthew G Knepley   Input Parameters:
4532057b4bcdSMatthew G Knepley + dm - The DM
4533057b4bcdSMatthew G Knepley - sf - The PetscSF
4534057b4bcdSMatthew G Knepley 
4535057b4bcdSMatthew G Knepley   Level: intermediate
4536057b4bcdSMatthew G Knepley 
45371bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4538057b4bcdSMatthew G Knepley @*/
45390adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
45400adebc6cSBarry Smith {
4541057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4542057b4bcdSMatthew G Knepley 
4543057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4544057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4545b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4546057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
454733907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4548057b4bcdSMatthew G Knepley   dm->sf = sf;
4549057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4550057b4bcdSMatthew G Knepley }
4551057b4bcdSMatthew G Knepley 
455234aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
455334aa8a36SMatthew G. Knepley {
455434aa8a36SMatthew G. Knepley   PetscClassId   id;
455534aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
455634aa8a36SMatthew G. Knepley 
455734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
455834aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
455934aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
456034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
456134aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
456234aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
456317c1d62eSMatthew G. Knepley   } else {
456417c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
456534aa8a36SMatthew G. Knepley   }
456634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
456734aa8a36SMatthew G. Knepley }
456834aa8a36SMatthew G. Knepley 
456944a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
457044a7f3ddSMatthew G. Knepley {
457144a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
457244a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
457344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
457444a7f3ddSMatthew G. Knepley 
457544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
457644a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
457744a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
457844a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
457944a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
458044a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
458144a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
458244a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
458344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
458444a7f3ddSMatthew G. Knepley }
458544a7f3ddSMatthew G. Knepley 
458644a7f3ddSMatthew G. Knepley /*@
458744a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
458844a7f3ddSMatthew G. Knepley 
4589d083f849SBarry Smith   Logically collective on dm
459044a7f3ddSMatthew G. Knepley 
459144a7f3ddSMatthew G. Knepley   Input Parameter:
459244a7f3ddSMatthew G. Knepley . dm - The DM
459344a7f3ddSMatthew G. Knepley 
459444a7f3ddSMatthew G. Knepley   Level: intermediate
459544a7f3ddSMatthew G. Knepley 
459644a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
459744a7f3ddSMatthew G. Knepley @*/
459844a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
459944a7f3ddSMatthew G. Knepley {
460044a7f3ddSMatthew G. Knepley   PetscInt       f;
460144a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
460244a7f3ddSMatthew G. Knepley 
460344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
460444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
460544a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
460644a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
460744a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
460844a7f3ddSMatthew G. Knepley   }
460944a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
461044a7f3ddSMatthew G. Knepley   dm->fields = NULL;
461144a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
461244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
461344a7f3ddSMatthew G. Knepley }
461444a7f3ddSMatthew G. Knepley 
4615689b5837SMatthew G. Knepley /*@
4616689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4617689b5837SMatthew G. Knepley 
4618689b5837SMatthew G. Knepley   Not collective
4619689b5837SMatthew G. Knepley 
4620689b5837SMatthew G. Knepley   Input Parameter:
4621689b5837SMatthew G. Knepley . dm - The DM
4622689b5837SMatthew G. Knepley 
4623689b5837SMatthew G. Knepley   Output Parameter:
4624689b5837SMatthew G. Knepley . Nf - The number of fields
4625689b5837SMatthew G. Knepley 
4626689b5837SMatthew G. Knepley   Level: intermediate
4627689b5837SMatthew G. Knepley 
4628689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4629689b5837SMatthew G. Knepley @*/
46300f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
46310f21e855SMatthew G. Knepley {
46320f21e855SMatthew G. Knepley   PetscFunctionBegin;
46330f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4634534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
463544a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4636af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4637af122d2aSMatthew G Knepley }
4638af122d2aSMatthew G Knepley 
4639689b5837SMatthew G. Knepley /*@
4640689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4641689b5837SMatthew G. Knepley 
4642d083f849SBarry Smith   Logically collective on dm
4643689b5837SMatthew G. Knepley 
4644689b5837SMatthew G. Knepley   Input Parameters:
4645689b5837SMatthew G. Knepley + dm - The DM
4646689b5837SMatthew G. Knepley - Nf - The number of fields
4647689b5837SMatthew G. Knepley 
4648689b5837SMatthew G. Knepley   Level: intermediate
4649689b5837SMatthew G. Knepley 
4650689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4651689b5837SMatthew G. Knepley @*/
4652af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4653af122d2aSMatthew G Knepley {
46540f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4655af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4656af122d2aSMatthew G Knepley 
4657af122d2aSMatthew G Knepley   PetscFunctionBegin;
4658af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
465944a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
46600f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
46610f21e855SMatthew G. Knepley     PetscContainer obj;
46620f21e855SMatthew G. Knepley 
46630f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
466444a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
46650f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4666af122d2aSMatthew G Knepley   }
4667af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4668af122d2aSMatthew G Knepley }
4669af122d2aSMatthew G Knepley 
4670c1929be8SMatthew G. Knepley /*@
4671c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4672c1929be8SMatthew G. Knepley 
4673c1929be8SMatthew G. Knepley   Not collective
4674c1929be8SMatthew G. Knepley 
4675c1929be8SMatthew G. Knepley   Input Parameters:
4676c1929be8SMatthew G. Knepley + dm - The DM
4677c1929be8SMatthew G. Knepley - f  - The field number
4678c1929be8SMatthew G. Knepley 
467944a7f3ddSMatthew G. Knepley   Output Parameters:
468044a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
468144a7f3ddSMatthew G. Knepley - field - The discretization object
4682c1929be8SMatthew G. Knepley 
468344a7f3ddSMatthew G. Knepley   Level: intermediate
4684c1929be8SMatthew G. Knepley 
468544a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4686c1929be8SMatthew G. Knepley @*/
468744a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4688af122d2aSMatthew G Knepley {
4689af122d2aSMatthew G Knepley   PetscFunctionBegin;
4690af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
469144a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
469244a7f3ddSMatthew 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);
469344a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
469444a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4695decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4696decb47aaSMatthew G. Knepley }
4697decb47aaSMatthew G. Knepley 
4698083401c6SMatthew G. Knepley /* Does not clear the DS */
4699083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4700083401c6SMatthew G. Knepley {
4701083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4702083401c6SMatthew G. Knepley 
4703083401c6SMatthew G. Knepley   PetscFunctionBegin;
4704083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4705083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4706083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4707083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4708083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4709083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4710083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4711083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4712083401c6SMatthew G. Knepley }
4713083401c6SMatthew G. Knepley 
4714c1929be8SMatthew G. Knepley /*@
4715c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4716c1929be8SMatthew G. Knepley 
4717d083f849SBarry Smith   Logically collective on dm
4718c1929be8SMatthew G. Knepley 
4719c1929be8SMatthew G. Knepley   Input Parameters:
4720c1929be8SMatthew G. Knepley + dm    - The DM
4721c1929be8SMatthew G. Knepley . f     - The field number
472244a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4723c1929be8SMatthew G. Knepley - field - The discretization object
4724c1929be8SMatthew G. Knepley 
472544a7f3ddSMatthew G. Knepley   Level: intermediate
4726c1929be8SMatthew G. Knepley 
472744a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4728c1929be8SMatthew G. Knepley @*/
472944a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4730decb47aaSMatthew G. Knepley {
4731decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4732decb47aaSMatthew G. Knepley 
4733decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4734decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4735e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
473644a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4737e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4738083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
473934aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
47402df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
474144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
474244a7f3ddSMatthew G. Knepley }
474344a7f3ddSMatthew G. Knepley 
474444a7f3ddSMatthew G. Knepley /*@
474544a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
474644a7f3ddSMatthew G. Knepley 
4747d083f849SBarry Smith   Logically collective on dm
474844a7f3ddSMatthew G. Knepley 
474944a7f3ddSMatthew G. Knepley   Input Parameters:
475044a7f3ddSMatthew G. Knepley + dm    - The DM
475144a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
475244a7f3ddSMatthew G. Knepley - field - The discretization object
475344a7f3ddSMatthew G. Knepley 
475444a7f3ddSMatthew G. Knepley   Level: intermediate
475544a7f3ddSMatthew G. Knepley 
475644a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
475744a7f3ddSMatthew G. Knepley @*/
475844a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
475944a7f3ddSMatthew G. Knepley {
476044a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
476144a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
476244a7f3ddSMatthew G. Knepley 
476344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
476444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
476544a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
476644a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
476744a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
476844a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
476944a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
477044a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
477144a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
477234aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
47732df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4774af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4775af122d2aSMatthew G Knepley }
47766636e97aSMatthew G Knepley 
4777e5e52638SMatthew G. Knepley /*@
4778e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4779e5e52638SMatthew G. Knepley 
4780d083f849SBarry Smith   Collective on dm
4781e5e52638SMatthew G. Knepley 
4782e5e52638SMatthew G. Knepley   Input Parameter:
4783e5e52638SMatthew G. Knepley . dm - The DM
4784e5e52638SMatthew G. Knepley 
4785e5e52638SMatthew G. Knepley   Output Parameter:
4786e5e52638SMatthew G. Knepley . newdm - The DM
4787e5e52638SMatthew G. Knepley 
4788e5e52638SMatthew G. Knepley   Level: advanced
4789e5e52638SMatthew G. Knepley 
4790e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4791e5e52638SMatthew G. Knepley @*/
4792e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4793e5e52638SMatthew G. Knepley {
4794e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4795e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4796e5e52638SMatthew G. Knepley 
4797e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4798e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4799e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4800e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4801e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4802e5e52638SMatthew G. Knepley     DMLabel     label;
4803e5e52638SMatthew G. Knepley     PetscObject field;
480434aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4805e5e52638SMatthew G. Knepley 
4806e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4807e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
480834aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
480934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
481034aa8a36SMatthew G. Knepley   }
481134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
481234aa8a36SMatthew G. Knepley }
481334aa8a36SMatthew G. Knepley 
481434aa8a36SMatthew G. Knepley /*@
481534aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
481634aa8a36SMatthew G. Knepley 
481734aa8a36SMatthew G. Knepley   Not collective
481834aa8a36SMatthew G. Knepley 
481934aa8a36SMatthew G. Knepley   Input Parameters:
482034aa8a36SMatthew G. Knepley + dm - The DM object
482134aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
482234aa8a36SMatthew G. Knepley 
482334aa8a36SMatthew G. Knepley   Output Parameter:
482434aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
482534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
482634aa8a36SMatthew G. Knepley 
482734aa8a36SMatthew G. Knepley   Notes:
482834aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
482934aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
483034aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4831979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
483234aa8a36SMatthew G. Knepley 
483334aa8a36SMatthew G. Knepley   Level: developer
483434aa8a36SMatthew G. Knepley 
483534aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
483634aa8a36SMatthew G. Knepley @*/
483734aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
483834aa8a36SMatthew G. Knepley {
483934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
484034aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4841534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4842534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
484334aa8a36SMatthew G. Knepley   if (f < 0) {
484434aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
484534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
484634aa8a36SMatthew G. Knepley   } else {
484734aa8a36SMatthew G. Knepley     PetscInt       Nf;
484834aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
484934aa8a36SMatthew G. Knepley 
485034aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
485134aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
485234aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
485334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
485434aa8a36SMatthew G. Knepley   }
485534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
485634aa8a36SMatthew G. Knepley }
485734aa8a36SMatthew G. Knepley 
485834aa8a36SMatthew G. Knepley /*@
485934aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
486034aa8a36SMatthew G. Knepley 
486134aa8a36SMatthew G. Knepley   Not collective
486234aa8a36SMatthew G. Knepley 
486334aa8a36SMatthew G. Knepley   Input Parameters:
486434aa8a36SMatthew G. Knepley + dm         - The DM object
486534aa8a36SMatthew G. Knepley . f          - The field number
486634aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
486734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
486834aa8a36SMatthew G. Knepley 
486934aa8a36SMatthew G. Knepley   Notes:
487034aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
487134aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
487234aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4873979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
487434aa8a36SMatthew G. Knepley 
487534aa8a36SMatthew G. Knepley   Level: developer
487634aa8a36SMatthew G. Knepley 
487734aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
487834aa8a36SMatthew G. Knepley @*/
487934aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
488034aa8a36SMatthew G. Knepley {
488134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
488234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
488334aa8a36SMatthew G. Knepley   if (f < 0) {
488434aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
488534aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
488634aa8a36SMatthew G. Knepley   } else {
488734aa8a36SMatthew G. Knepley     PetscInt       Nf;
488834aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
488934aa8a36SMatthew G. Knepley 
489034aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
489134aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
489234aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
489334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4894e5e52638SMatthew G. Knepley   }
4895e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4896e5e52638SMatthew G. Knepley }
4897e5e52638SMatthew G. Knepley 
4898b0441da4SMatthew G. Knepley /*@
4899b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4900b0441da4SMatthew G. Knepley 
4901b0441da4SMatthew G. Knepley   Not collective
4902b0441da4SMatthew G. Knepley 
4903b0441da4SMatthew G. Knepley   Input Parameters:
4904b0441da4SMatthew G. Knepley . dm - The DM object
4905b0441da4SMatthew G. Knepley 
4906b0441da4SMatthew G. Knepley   Output Parameter:
4907b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4908b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4909b0441da4SMatthew G. Knepley 
4910b0441da4SMatthew G. Knepley   Notes:
4911b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4912b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4913b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4914b0441da4SMatthew G. Knepley 
4915b0441da4SMatthew G. Knepley   Level: developer
4916b0441da4SMatthew G. Knepley 
4917b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4918b0441da4SMatthew G. Knepley @*/
4919b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4920b0441da4SMatthew G. Knepley {
4921b0441da4SMatthew G. Knepley   PetscInt       Nf;
4922b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4923b0441da4SMatthew G. Knepley 
4924b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4925b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4926534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4927534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4928b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4929b0441da4SMatthew G. Knepley   if (!Nf) {
4930b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4931b0441da4SMatthew G. Knepley   } else {
4932b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4933b0441da4SMatthew G. Knepley   }
4934b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4935b0441da4SMatthew G. Knepley }
4936b0441da4SMatthew G. Knepley 
4937b0441da4SMatthew G. Knepley /*@
4938b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4939b0441da4SMatthew G. Knepley 
4940b0441da4SMatthew G. Knepley   Not collective
4941b0441da4SMatthew G. Knepley 
4942b0441da4SMatthew G. Knepley   Input Parameters:
4943b0441da4SMatthew G. Knepley + dm         - The DM object
4944b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4945b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4946b0441da4SMatthew G. Knepley 
4947b0441da4SMatthew G. Knepley   Notes:
4948b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4949b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4950b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4951b0441da4SMatthew G. Knepley 
4952b0441da4SMatthew G. Knepley   Level: developer
4953b0441da4SMatthew G. Knepley 
4954b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4955b0441da4SMatthew G. Knepley @*/
4956b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4957b0441da4SMatthew G. Knepley {
4958b0441da4SMatthew G. Knepley   PetscInt       Nf;
4959b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4960b0441da4SMatthew G. Knepley 
4961b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4962b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4963b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4964b0441da4SMatthew G. Knepley   if (!Nf) {
4965b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4966b0441da4SMatthew G. Knepley   } else {
4967b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4968e5e52638SMatthew G. Knepley   }
4969e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4970e5e52638SMatthew G. Knepley }
4971e5e52638SMatthew G. Knepley 
4972783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
4973783e2ec8SMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, const char labelname[])
4974783e2ec8SMatthew G. Knepley {
4975783e2ec8SMatthew G. Knepley   DMLabel        label;
4976783e2ec8SMatthew G. Knepley   PetscObject    obj;
4977783e2ec8SMatthew G. Knepley   PetscClassId   id;
4978783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
4979783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
4980783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
4981783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
4982783e2ec8SMatthew G. Knepley 
4983783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
4984783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
4985783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
4986783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
4987783e2ec8SMatthew G. Knepley   ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
4988783e2ec8SMatthew G. Knepley   if (isFE && label) {
4989783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
4990783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
4991783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
4992783e2ec8SMatthew G. Knepley       const char *lname;
4993783e2ec8SMatthew G. Knepley 
499456cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, &lname, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
4995783e2ec8SMatthew G. Knepley       ierr = PetscStrcmp(lname, labelname, &duplicate);CHKERRQ(ierr);
4996783e2ec8SMatthew G. Knepley       if (duplicate) break;
4997783e2ec8SMatthew G. Knepley     }
4998783e2ec8SMatthew G. Knepley     if (!duplicate) {
4999783e2ec8SMatthew G. Knepley       DM plex;
5000783e2ec8SMatthew G. Knepley 
5001783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5002783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5003783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5004783e2ec8SMatthew G. Knepley     }
5005783e2ec8SMatthew G. Knepley   }
5006783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5007783e2ec8SMatthew G. Knepley }
5008783e2ec8SMatthew G. Knepley 
5009e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5010e5e52638SMatthew G. Knepley {
5011e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5012e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5013e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5014e5e52638SMatthew G. Knepley 
5015e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5016e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5017e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5018e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5019b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5020e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5021e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5022e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5023e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5024e5e52638SMatthew G. Knepley }
5025e5e52638SMatthew G. Knepley 
5026e5e52638SMatthew G. Knepley /*@
5027e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5028e5e52638SMatthew G. Knepley 
5029e5e52638SMatthew G. Knepley   Not collective
5030e5e52638SMatthew G. Knepley 
5031e5e52638SMatthew G. Knepley   Input Parameter:
5032e5e52638SMatthew G. Knepley . dm - The DM
5033e5e52638SMatthew G. Knepley 
5034e5e52638SMatthew G. Knepley   Output Parameter:
5035e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5036e5e52638SMatthew G. Knepley 
5037e5e52638SMatthew G. Knepley   Level: intermediate
5038e5e52638SMatthew G. Knepley 
5039e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5040e5e52638SMatthew G. Knepley @*/
5041e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5042e5e52638SMatthew G. Knepley {
5043e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5044e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5045534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5046e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5047e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5048e5e52638SMatthew G. Knepley }
5049e5e52638SMatthew G. Knepley 
5050e5e52638SMatthew G. Knepley /*@
5051e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5052e5e52638SMatthew G. Knepley 
5053d083f849SBarry Smith   Logically collective on dm
5054e5e52638SMatthew G. Knepley 
5055e5e52638SMatthew G. Knepley   Input Parameter:
5056e5e52638SMatthew G. Knepley . dm - The DM
5057e5e52638SMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley   Level: intermediate
5059e5e52638SMatthew G. Knepley 
5060e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5061e5e52638SMatthew G. Knepley @*/
5062e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5063e5e52638SMatthew G. Knepley {
5064e5e52638SMatthew G. Knepley   PetscInt       s;
5065e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5066e5e52638SMatthew G. Knepley 
5067e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5068e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5069e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5070e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5071e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5072b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5073e5e52638SMatthew G. Knepley   }
5074e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5075e5e52638SMatthew G. Knepley   dm->probs = NULL;
5076e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5077e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5078e5e52638SMatthew G. Knepley }
5079e5e52638SMatthew G. Knepley 
5080e5e52638SMatthew G. Knepley /*@
5081e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5082e5e52638SMatthew G. Knepley 
5083e5e52638SMatthew G. Knepley   Not collective
5084e5e52638SMatthew G. Knepley 
5085e5e52638SMatthew G. Knepley   Input Parameter:
5086e5e52638SMatthew G. Knepley . dm    - The DM
5087e5e52638SMatthew G. Knepley 
5088e5e52638SMatthew G. Knepley   Output Parameter:
5089e5e52638SMatthew G. Knepley . prob - The default PetscDS
5090e5e52638SMatthew G. Knepley 
5091e5e52638SMatthew G. Knepley   Level: intermediate
5092e5e52638SMatthew G. Knepley 
5093e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5094e5e52638SMatthew G. Knepley @*/
5095e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5096e5e52638SMatthew G. Knepley {
5097b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5098b0143b4dSMatthew G. Knepley 
5099e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5100e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5101e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5102b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5103b0143b4dSMatthew G. Knepley     PetscDS ds;
5104b0143b4dSMatthew G. Knepley 
5105b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5106b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5107b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5108b0143b4dSMatthew G. Knepley   }
5109b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5110e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5111e5e52638SMatthew G. Knepley }
5112e5e52638SMatthew G. Knepley 
5113e5e52638SMatthew G. Knepley /*@
5114e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5115e5e52638SMatthew G. Knepley 
5116e5e52638SMatthew G. Knepley   Not collective
5117e5e52638SMatthew G. Knepley 
5118e5e52638SMatthew G. Knepley   Input Parameters:
5119e5e52638SMatthew G. Knepley + dm    - The DM
5120e5e52638SMatthew G. Knepley - point - Cell for the DS
5121e5e52638SMatthew G. Knepley 
5122e5e52638SMatthew G. Knepley   Output Parameter:
5123e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5124e5e52638SMatthew G. Knepley 
5125e5e52638SMatthew G. Knepley   Level: developer
5126e5e52638SMatthew G. Knepley 
5127b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5128e5e52638SMatthew G. Knepley @*/
5129e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5130e5e52638SMatthew G. Knepley {
5131e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5132e5e52638SMatthew G. Knepley   PetscInt       s;
5133e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5134e5e52638SMatthew G. Knepley 
5135e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5136e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5137e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5138360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5139e5e52638SMatthew G. Knepley   *prob = NULL;
5140e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5141e5e52638SMatthew G. Knepley     PetscInt val;
5142e5e52638SMatthew G. Knepley 
5143e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5144e5e52638SMatthew G. Knepley     else {
5145e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5146e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5147e5e52638SMatthew G. Knepley     }
5148e5e52638SMatthew G. Knepley   }
5149e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5150e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5151e5e52638SMatthew G. Knepley }
5152e5e52638SMatthew G. Knepley 
5153e5e52638SMatthew G. Knepley /*@
5154e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5155e5e52638SMatthew G. Knepley 
5156e5e52638SMatthew G. Knepley   Not collective
5157e5e52638SMatthew G. Knepley 
5158e5e52638SMatthew G. Knepley   Input Parameters:
5159e5e52638SMatthew G. Knepley + dm    - The DM
5160e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5161e5e52638SMatthew G. Knepley 
5162b3cf3223SMatthew G. Knepley   Output Parameters:
5163b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5164b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5165e5e52638SMatthew G. Knepley 
5166e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5167e5e52638SMatthew G. Knepley 
5168e5e52638SMatthew G. Knepley   Level: advanced
5169e5e52638SMatthew G. Knepley 
5170e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5171e5e52638SMatthew G. Knepley @*/
5172b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5173e5e52638SMatthew G. Knepley {
5174e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5175e5e52638SMatthew G. Knepley 
5176e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5177e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5178e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5179b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5180b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5181e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5182b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5183b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5184b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5185b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5186b3cf3223SMatthew G. Knepley     }
5187e5e52638SMatthew G. Knepley   }
51882df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5189e5e52638SMatthew G. Knepley }
5190e5e52638SMatthew G. Knepley 
5191e5e52638SMatthew G. Knepley /*@
5192083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5193083401c6SMatthew G. Knepley 
5194083401c6SMatthew G. Knepley   Collective on dm
5195083401c6SMatthew G. Knepley 
5196083401c6SMatthew G. Knepley   Input Parameters:
5197083401c6SMatthew G. Knepley + dm     - The DM
5198083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5199083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5200083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5201083401c6SMatthew G. Knepley 
5202083401c6SMatthew 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,
5203083401c6SMatthew G. Knepley   the fields argument is ignored.
5204083401c6SMatthew G. Knepley 
5205083401c6SMatthew G. Knepley   Level: advanced
5206083401c6SMatthew G. Knepley 
5207083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5208083401c6SMatthew G. Knepley @*/
5209083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5210083401c6SMatthew G. Knepley {
5211083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5212083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5213083401c6SMatthew G. Knepley 
5214083401c6SMatthew G. Knepley   PetscFunctionBegin;
5215083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5216083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5217083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5218083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5219083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5220083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5221083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5222083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5223083401c6SMatthew G. Knepley     }
5224083401c6SMatthew G. Knepley   }
5225083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5226083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5227083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5228083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5229083401c6SMatthew G. Knepley   if (!label) {
5230083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5231083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5232083401c6SMatthew G. Knepley     Nds = 0;
5233083401c6SMatthew G. Knepley   }
5234083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5235083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5236083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5237083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5238083401c6SMatthew G. Knepley }
5239083401c6SMatthew G. Knepley 
5240083401c6SMatthew G. Knepley /*@
5241e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5242e5e52638SMatthew G. Knepley 
5243e5e52638SMatthew G. Knepley   Not collective
5244e5e52638SMatthew G. Knepley 
5245e5e52638SMatthew G. Knepley   Input Parameters:
5246e5e52638SMatthew G. Knepley + dm  - The DM
5247e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5248e5e52638SMatthew G. Knepley 
5249e5e52638SMatthew G. Knepley   Output Parameters:
5250b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5251b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5252083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5253e5e52638SMatthew G. Knepley 
5254e5e52638SMatthew G. Knepley   Level: advanced
5255e5e52638SMatthew G. Knepley 
5256e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5257e5e52638SMatthew G. Knepley @*/
5258b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5259e5e52638SMatthew G. Knepley {
5260e5e52638SMatthew G. Knepley   PetscInt       Nds;
5261e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5262e5e52638SMatthew G. Knepley 
5263e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5264e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5265e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5266e5e52638SMatthew 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);
5267e5e52638SMatthew G. Knepley   if (label) {
5268e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5269e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5270e5e52638SMatthew G. Knepley   }
5271b3cf3223SMatthew G. Knepley   if (fields) {
5272b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5273b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5274b3cf3223SMatthew G. Knepley   }
5275e5e52638SMatthew G. Knepley   if (ds) {
5276b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5277e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5278e5e52638SMatthew G. Knepley   }
5279e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5280e5e52638SMatthew G. Knepley }
5281e5e52638SMatthew G. Knepley 
5282e5e52638SMatthew G. Knepley /*@
5283083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5284e5e52638SMatthew G. Knepley 
5285083401c6SMatthew G. Knepley   Not collective
5286e5e52638SMatthew G. Knepley 
5287e5e52638SMatthew G. Knepley   Input Parameters:
5288e5e52638SMatthew G. Knepley + dm     - The DM
5289083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5290083401c6SMatthew G. Knepley . label  - The region label, or NULL
5291083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5292083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5293e5e52638SMatthew G. Knepley 
5294e5e52638SMatthew G. Knepley   Level: advanced
5295e5e52638SMatthew G. Knepley 
5296083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5297e5e52638SMatthew G. Knepley @*/
5298083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5299e5e52638SMatthew G. Knepley {
5300083401c6SMatthew G. Knepley   PetscInt       Nds;
5301e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5302e5e52638SMatthew G. Knepley 
5303e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5304e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5305083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5306083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5307083401c6SMatthew 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);
5308e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5309083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5310083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5311083401c6SMatthew G. Knepley   if (fields) {
5312083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5313b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5314083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5315083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5316e5e52638SMatthew G. Knepley   }
5317083401c6SMatthew G. Knepley   if (ds) {
5318083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5319083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5320083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5321083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5322083401c6SMatthew G. Knepley   }
5323e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5324e5e52638SMatthew G. Knepley }
5325e5e52638SMatthew G. Knepley 
5326e5e52638SMatthew G. Knepley /*@
53271d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
53281d3af9e0SMatthew G. Knepley 
53291d3af9e0SMatthew G. Knepley   Not collective
53301d3af9e0SMatthew G. Knepley 
53311d3af9e0SMatthew G. Knepley   Input Parameters:
53321d3af9e0SMatthew G. Knepley + dm  - The DM
53331d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
53341d3af9e0SMatthew G. Knepley 
53351d3af9e0SMatthew G. Knepley   Output Parameter:
53361d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
53371d3af9e0SMatthew G. Knepley 
53381d3af9e0SMatthew G. Knepley   Level: advanced
53391d3af9e0SMatthew G. Knepley 
53401d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
53411d3af9e0SMatthew G. Knepley @*/
53421d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
53431d3af9e0SMatthew G. Knepley {
53441d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
53451d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
53461d3af9e0SMatthew G. Knepley 
53471d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
53481d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53491d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
53501d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
53511d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
53521d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
53531d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
53541d3af9e0SMatthew G. Knepley   else          *num = n;
53551d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
53561d3af9e0SMatthew G. Knepley }
53571d3af9e0SMatthew G. Knepley 
53581d3af9e0SMatthew G. Knepley /*@
5359e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5360e5e52638SMatthew G. Knepley 
5361d083f849SBarry Smith   Collective on dm
5362e5e52638SMatthew G. Knepley 
5363e5e52638SMatthew G. Knepley   Input Parameter:
5364e5e52638SMatthew G. Knepley . dm - The DM
5365e5e52638SMatthew G. Knepley 
5366e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5367e5e52638SMatthew G. Knepley 
5368e5e52638SMatthew G. Knepley   Level: intermediate
5369e5e52638SMatthew G. Knepley 
5370e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5371e5e52638SMatthew G. Knepley @*/
5372e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5373e5e52638SMatthew G. Knepley {
5374e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5375083401c6SMatthew G. Knepley   PetscDS        dsDef;
5376083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5377083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5378e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5379e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5380e5e52638SMatthew G. Knepley 
5381e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5382e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5383e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5384e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5385083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5386083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5387083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5388083401c6SMatthew G. Knepley   Nl   = 0;
5389083401c6SMatthew G. Knepley   Ndef = 0;
5390083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5391083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5392083401c6SMatthew G. Knepley     PetscInt l;
5393083401c6SMatthew G. Knepley 
5394083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5395083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5396083401c6SMatthew G. Knepley     if (l < Nl) continue;
5397083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5398083401c6SMatthew G. Knepley   }
5399083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5400083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5401083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5402b3cf3223SMatthew G. Knepley     IS        fields;
5403b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5404b3cf3223SMatthew G. Knepley 
5405b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5406083401c6SMatthew G. Knepley     if (nf) {
5407b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5408b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
540988f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
541088f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
541188f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
541288f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
541388f0c812SMatthew G. Knepley 
5414083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5415083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5416083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5417b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
54182df9ee95SMatthew G. Knepley     }
5419083401c6SMatthew G. Knepley   }
5420083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5421083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5422083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5423083401c6SMatthew G. Knepley   if (Ndef && Nl) {
54240122748bSMatthew G. Knepley     DM              plex;
5425083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5426083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5427083401c6SMatthew G. Knepley     PetscInt       *fields;
5428083401c6SMatthew G. Knepley     const PetscInt *cells;
5429083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
54300122748bSMatthew G. Knepley 
54310122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
54320122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5433083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5434083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5435083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5436083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5437083401c6SMatthew G. Knepley       IS      pointIS;
5438083401c6SMatthew G. Knepley 
5439083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5440083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5441083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5442083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5443083401c6SMatthew G. Knepley     }
5444083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5445083401c6SMatthew G. Knepley 
5446083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5447083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5448083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5449083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5450083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5451083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5452083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5453083401c6SMatthew G. Knepley 
5454083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5455083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5456083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5457083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5458083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5459083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5460083401c6SMatthew G. Knepley 
5461083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5462083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5463083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5464083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5465083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5466083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
54670122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5468083401c6SMatthew G. Knepley   }
5469083401c6SMatthew G. Knepley   /* Create label DSes
5470083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5471083401c6SMatthew G. Knepley   */
5472083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5473083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5474083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5475083401c6SMatthew G. Knepley     PetscDS   ds;
5476083401c6SMatthew G. Knepley     IS        fields;
5477083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5478083401c6SMatthew G. Knepley 
5479083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5480083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5481083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5482083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5483083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5484083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5485083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5486083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5487083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5488083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5489083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5490083401c6SMatthew G. Knepley     {
5491083401c6SMatthew G. Knepley       DMPolytopeType ct;
5492083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5493083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
54940122748bSMatthew G. Knepley 
5495e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5496665f567fSMatthew G. Knepley       if (lStart >= 0) {
5497412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5498412e9a14SMatthew G. Knepley         switch (ct) {
5499412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5500412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5501412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5502412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5503083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5504083401c6SMatthew G. Knepley           default: break;
5505412e9a14SMatthew G. Knepley         }
5506665f567fSMatthew G. Knepley       }
5507083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5508083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5509e5e52638SMatthew G. Knepley     }
5510083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5511e5e52638SMatthew G. Knepley   }
5512083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5513e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5514083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5515083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5516083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5517083401c6SMatthew G. Knepley     const PetscInt *fld;
5518083401c6SMatthew G. Knepley     PetscInt        nf;
5519e5e52638SMatthew G. Knepley 
5520083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5521083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5522083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5523083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5524083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5525e5e52638SMatthew G. Knepley       PetscClassId id;
5526e5e52638SMatthew G. Knepley 
5527083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5528083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5529083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5530083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5531083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5532e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5533e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5534e5e52638SMatthew G. Knepley     }
5535083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5536e5e52638SMatthew G. Knepley   }
5537e5e52638SMatthew G. Knepley   /* Setup DSes */
5538e5e52638SMatthew G. Knepley   if (doSetup) {
5539e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5540e5e52638SMatthew G. Knepley   }
5541e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5542e5e52638SMatthew G. Knepley }
5543e5e52638SMatthew G. Knepley 
5544e5e52638SMatthew G. Knepley /*@
55457f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
55467f96f943SMatthew G. Knepley 
5547f2cacb80SMatthew G. Knepley   Collective on DM
5548f2cacb80SMatthew G. Knepley 
55497f96f943SMatthew G. Knepley   Input Parameters:
55507f96f943SMatthew G. Knepley + dm   - The DM
55517f96f943SMatthew G. Knepley - time - The time
55527f96f943SMatthew G. Knepley 
55537f96f943SMatthew G. Knepley   Output Parameters:
5554f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5555f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
55567f96f943SMatthew G. Knepley 
55577f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
55587f96f943SMatthew G. Knepley 
55597f96f943SMatthew G. Knepley   Level: developer
55607f96f943SMatthew G. Knepley 
55617f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
55627f96f943SMatthew G. Knepley @*/
5563f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
55647f96f943SMatthew G. Knepley {
55657f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
55667f96f943SMatthew G. Knepley   void            **ectxs;
55677f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
55687f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
55697f96f943SMatthew G. Knepley 
55707f96f943SMatthew G. Knepley   PetscFunctionBegin;
5571f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5572f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5573f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
55747f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
55757f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
55767f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
55777f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
55787f96f943SMatthew G. Knepley     PetscDS         ds;
55797f96f943SMatthew G. Knepley     DMLabel         label;
55807f96f943SMatthew G. Knepley     IS              fieldIS;
55817f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
55827f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
55837f96f943SMatthew G. Knepley 
55847f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
55857f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
55867f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
55877f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
55887f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5589f2cacb80SMatthew G. Knepley     if (u) {
55907f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
55917f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
55927f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
55937f96f943SMatthew G. Knepley       }
55947f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
55957f96f943SMatthew G. Knepley       if (label) {
55967f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
55977f96f943SMatthew G. Knepley       } else {
55987f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
55997f96f943SMatthew G. Knepley       }
56007f96f943SMatthew G. Knepley     }
5601f2cacb80SMatthew G. Knepley     if (u_t) {
5602f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5603f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5604f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5605f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5606f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5607f2cacb80SMatthew G. Knepley       }
5608f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5609f2cacb80SMatthew G. Knepley       if (label) {
5610f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5611f2cacb80SMatthew G. Knepley       } else {
5612f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5613f2cacb80SMatthew G. Knepley       }
5614f2cacb80SMatthew G. Knepley     }
5615f2cacb80SMatthew G. Knepley   }
5616f2cacb80SMatthew G. Knepley   if (u) {
56177f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
56187f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5619f2cacb80SMatthew G. Knepley   }
5620f2cacb80SMatthew G. Knepley   if (u_t) {
5621f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5622f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5623f2cacb80SMatthew G. Knepley   }
56247f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
56257f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
56267f96f943SMatthew G. Knepley }
56277f96f943SMatthew G. Knepley 
56287f96f943SMatthew G. Knepley /*@
5629e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5630e5e52638SMatthew G. Knepley 
5631d083f849SBarry Smith   Collective on dm
5632e5e52638SMatthew G. Knepley 
5633e5e52638SMatthew G. Knepley   Input Parameter:
5634e5e52638SMatthew G. Knepley . dm - The DM
5635e5e52638SMatthew G. Knepley 
5636e5e52638SMatthew G. Knepley   Output Parameter:
5637e5e52638SMatthew G. Knepley . newdm - The DM
5638e5e52638SMatthew G. Knepley 
5639e5e52638SMatthew G. Knepley   Level: advanced
5640e5e52638SMatthew G. Knepley 
5641e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5642e5e52638SMatthew G. Knepley @*/
5643e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5644e5e52638SMatthew G. Knepley {
5645e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5646e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5647e5e52638SMatthew G. Knepley 
5648e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5649e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5650e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5651e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5652e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5653e5e52638SMatthew G. Knepley     DMLabel  label;
5654b3cf3223SMatthew G. Knepley     IS       fields;
5655e5e52638SMatthew G. Knepley     PetscDS  ds;
5656783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5657e5e52638SMatthew G. Knepley 
5658b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5659b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5660783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5661783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5662783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5663783e2ec8SMatthew G. Knepley       PetscInt    field;
5664783e2ec8SMatthew G. Knepley 
5665783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
566656cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5667783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5668783e2ec8SMatthew G. Knepley     }
5669e5e52638SMatthew G. Knepley   }
5670e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5671e5e52638SMatthew G. Knepley }
5672e5e52638SMatthew G. Knepley 
5673e5e52638SMatthew G. Knepley /*@
5674e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5675e5e52638SMatthew G. Knepley 
5676d083f849SBarry Smith   Collective on dm
5677e5e52638SMatthew G. Knepley 
5678e5e52638SMatthew G. Knepley   Input Parameter:
5679e5e52638SMatthew G. Knepley . dm - The DM
5680e5e52638SMatthew G. Knepley 
5681e5e52638SMatthew G. Knepley   Output Parameter:
5682e5e52638SMatthew G. Knepley . newdm - The DM
5683e5e52638SMatthew G. Knepley 
5684e5e52638SMatthew G. Knepley   Level: advanced
5685e5e52638SMatthew G. Knepley 
5686e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5687e5e52638SMatthew G. Knepley @*/
5688e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5689e5e52638SMatthew G. Knepley {
5690e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5691e5e52638SMatthew G. Knepley 
5692e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5693e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5694e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5695e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5696e5e52638SMatthew G. Knepley }
5697e5e52638SMatthew G. Knepley 
5698b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5699b64e0483SPeter Brune {
5700b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5701b64e0483SPeter Brune   PetscErrorCode ierr;
5702b64e0483SPeter Brune   Vec coords,ccoords;
57036dbf9973SLawrence Mitchell   Mat inject;
5704b64e0483SPeter Brune   PetscFunctionBegin;
5705b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5706b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5707b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5708b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5709b64e0483SPeter Brune   if (coords && !ccoords) {
5710b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
57116668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
57126dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
57132adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
57146dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5715b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5716b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5717b64e0483SPeter Brune   }
5718b64e0483SPeter Brune   PetscFunctionReturn(0);
5719b64e0483SPeter Brune }
5720b64e0483SPeter Brune 
572103dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
572203dadc2fSPeter Brune {
572303dadc2fSPeter Brune   DM dm_coord,subdm_coord;
572403dadc2fSPeter Brune   PetscErrorCode ierr;
572503dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
572603dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
572703dadc2fSPeter Brune   PetscFunctionBegin;
572803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
572903dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
573003dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
573103dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
573203dadc2fSPeter Brune   if (coords && !ccoords) {
573303dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
57346668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
573503dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
573624640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
573703dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
573803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
573903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
57401ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
574103dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
574203dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
574303dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
574403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
574503dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
574603dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
574703dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
574803dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
574903dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
575003dadc2fSPeter Brune   }
575103dadc2fSPeter Brune   PetscFunctionReturn(0);
575203dadc2fSPeter Brune }
575303dadc2fSPeter Brune 
5754c73cfb54SMatthew G. Knepley /*@
5755c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5756c73cfb54SMatthew G. Knepley 
5757c73cfb54SMatthew G. Knepley   Not collective
5758c73cfb54SMatthew G. Knepley 
5759c73cfb54SMatthew G. Knepley   Input Parameter:
5760c73cfb54SMatthew G. Knepley . dm - The DM
5761c73cfb54SMatthew G. Knepley 
5762c73cfb54SMatthew G. Knepley   Output Parameter:
5763c73cfb54SMatthew G. Knepley . dim - The topological dimension
5764c73cfb54SMatthew G. Knepley 
5765c73cfb54SMatthew G. Knepley   Level: beginner
5766c73cfb54SMatthew G. Knepley 
5767c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5768c73cfb54SMatthew G. Knepley @*/
5769c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5770c73cfb54SMatthew G. Knepley {
5771c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5772c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5773534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5774c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5775c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5776c73cfb54SMatthew G. Knepley }
5777c73cfb54SMatthew G. Knepley 
5778c73cfb54SMatthew G. Knepley /*@
5779c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5780c73cfb54SMatthew G. Knepley 
5781c73cfb54SMatthew G. Knepley   Collective on dm
5782c73cfb54SMatthew G. Knepley 
5783c73cfb54SMatthew G. Knepley   Input Parameters:
5784c73cfb54SMatthew G. Knepley + dm - The DM
5785c73cfb54SMatthew G. Knepley - dim - The topological dimension
5786c73cfb54SMatthew G. Knepley 
5787c73cfb54SMatthew G. Knepley   Level: beginner
5788c73cfb54SMatthew G. Knepley 
5789c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5790c73cfb54SMatthew G. Knepley @*/
5791c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5792c73cfb54SMatthew G. Knepley {
5793e5e52638SMatthew G. Knepley   PetscDS        ds;
5794f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5795f17e8794SMatthew G. Knepley 
5796c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5797c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5798c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5799c73cfb54SMatthew G. Knepley   dm->dim = dim;
5800e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5801e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5802c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5803c73cfb54SMatthew G. Knepley }
5804c73cfb54SMatthew G. Knepley 
5805793f3fe5SMatthew G. Knepley /*@
5806793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5807793f3fe5SMatthew G. Knepley 
5808d083f849SBarry Smith   Collective on dm
5809793f3fe5SMatthew G. Knepley 
5810793f3fe5SMatthew G. Knepley   Input Parameters:
5811793f3fe5SMatthew G. Knepley + dm - the DM
5812793f3fe5SMatthew G. Knepley - dim - the dimension
5813793f3fe5SMatthew G. Knepley 
5814793f3fe5SMatthew G. Knepley   Output Parameters:
5815793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5816aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5817793f3fe5SMatthew G. Knepley 
5818793f3fe5SMatthew G. Knepley   Note:
5819793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5820a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5821793f3fe5SMatthew G. Knepley   then the interval is empty.
5822793f3fe5SMatthew G. Knepley 
5823793f3fe5SMatthew G. Knepley   Level: intermediate
5824793f3fe5SMatthew G. Knepley 
5825793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5826793f3fe5SMatthew G. Knepley @*/
5827793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5828793f3fe5SMatthew G. Knepley {
5829793f3fe5SMatthew G. Knepley   PetscInt       d;
5830793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5831793f3fe5SMatthew G. Knepley 
5832793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5833793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5834793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5835793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5836b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5837793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5838793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5839793f3fe5SMatthew G. Knepley }
5840793f3fe5SMatthew G. Knepley 
58416636e97aSMatthew G Knepley /*@
58426636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
58436636e97aSMatthew G Knepley 
5844d083f849SBarry Smith   Collective on dm
58456636e97aSMatthew G Knepley 
58466636e97aSMatthew G Knepley   Input Parameters:
58476636e97aSMatthew G Knepley + dm - the DM
58486636e97aSMatthew G Knepley - c - coordinate vector
58496636e97aSMatthew G Knepley 
58502dd40e9bSPatrick Sanan   Notes:
58512dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
58522dd40e9bSPatrick Sanan 
58532dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
58546636e97aSMatthew G Knepley 
58556636e97aSMatthew G Knepley   Level: intermediate
58566636e97aSMatthew G Knepley 
58572dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
58586636e97aSMatthew G Knepley @*/
58596636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
58606636e97aSMatthew G Knepley {
58616636e97aSMatthew G Knepley   PetscErrorCode ierr;
58626636e97aSMatthew G Knepley 
58636636e97aSMatthew G Knepley   PetscFunctionBegin;
58646636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
58666636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
58676636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
58686636e97aSMatthew G Knepley   dm->coordinates = c;
58696636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5870b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
587103dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
58726636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58736636e97aSMatthew G Knepley }
58746636e97aSMatthew G Knepley 
58756636e97aSMatthew G Knepley /*@
58766636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
58776636e97aSMatthew G Knepley 
58787058e716SVaclav Hapla   Not collective
58796636e97aSMatthew G Knepley 
58806636e97aSMatthew G Knepley    Input Parameters:
58816636e97aSMatthew G Knepley +  dm - the DM
58826636e97aSMatthew G Knepley -  c - coordinate vector
58836636e97aSMatthew G Knepley 
58842dd40e9bSPatrick Sanan   Notes:
58856636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
58866636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
58876636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
58886636e97aSMatthew G Knepley 
58892dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
58902dd40e9bSPatrick Sanan 
58916636e97aSMatthew G Knepley   Level: intermediate
58926636e97aSMatthew G Knepley 
58936636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
58946636e97aSMatthew G Knepley @*/
58956636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
58966636e97aSMatthew G Knepley {
58976636e97aSMatthew G Knepley   PetscErrorCode ierr;
58986636e97aSMatthew G Knepley 
58996636e97aSMatthew G Knepley   PetscFunctionBegin;
59006636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59016636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59026636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59036636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
59048865f1eaSKarl Rupp 
59056636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
59068865f1eaSKarl Rupp 
59076636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59086636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59096636e97aSMatthew G Knepley }
59106636e97aSMatthew G Knepley 
59116636e97aSMatthew G Knepley /*@
59126636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
59136636e97aSMatthew G Knepley 
5914d083f849SBarry Smith   Collective on dm
59156636e97aSMatthew G Knepley 
59166636e97aSMatthew G Knepley   Input Parameter:
59176636e97aSMatthew G Knepley . dm - the DM
59186636e97aSMatthew G Knepley 
59196636e97aSMatthew G Knepley   Output Parameter:
59206636e97aSMatthew G Knepley . c - global coordinate vector
59216636e97aSMatthew G Knepley 
59226636e97aSMatthew G Knepley   Note:
59236636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
59246636e97aSMatthew G Knepley 
59256636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
59266636e97aSMatthew G Knepley 
59276636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59286636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59296636e97aSMatthew G Knepley 
59306636e97aSMatthew G Knepley   Level: intermediate
59316636e97aSMatthew G Knepley 
59326636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59336636e97aSMatthew G Knepley @*/
59346636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
59356636e97aSMatthew G Knepley {
59366636e97aSMatthew G Knepley   PetscErrorCode ierr;
59376636e97aSMatthew G Knepley 
59386636e97aSMatthew G Knepley   PetscFunctionBegin;
59396636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59406636e97aSMatthew G Knepley   PetscValidPointer(c,2);
59411f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
59420298fd71SBarry Smith     DM        cdm = NULL;
59431970a576SMatthew G. Knepley     PetscBool localized;
59446636e97aSMatthew G Knepley 
59456636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
59466636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
59471970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
59481970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
59491970a576SMatthew G. Knepley     if (localized) {
59501970a576SMatthew G. Knepley       PetscInt cdim;
59511970a576SMatthew G. Knepley 
59521970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
59531970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
59541970a576SMatthew G. Knepley     }
59556636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
59566636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
59576636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
59586636e97aSMatthew G Knepley   }
59596636e97aSMatthew G Knepley   *c = dm->coordinates;
59606636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59616636e97aSMatthew G Knepley }
59626636e97aSMatthew G Knepley 
59636636e97aSMatthew G Knepley /*@
596481e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
596581e9a530SVaclav Hapla 
5966d083f849SBarry Smith   Collective on dm
596781e9a530SVaclav Hapla 
596881e9a530SVaclav Hapla   Input Parameter:
596981e9a530SVaclav Hapla . dm - the DM
597081e9a530SVaclav Hapla 
597181e9a530SVaclav Hapla   Level: advanced
597281e9a530SVaclav Hapla 
597381e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
597481e9a530SVaclav Hapla @*/
597581e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
597681e9a530SVaclav Hapla {
597781e9a530SVaclav Hapla   PetscErrorCode ierr;
597881e9a530SVaclav Hapla 
597981e9a530SVaclav Hapla   PetscFunctionBegin;
598081e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
598181e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
59821970a576SMatthew G. Knepley     DM        cdm = NULL;
59831970a576SMatthew G. Knepley     PetscBool localized;
59841970a576SMatthew G. Knepley 
598581e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
598681e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
59871970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
59881970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
59891970a576SMatthew G. Knepley     if (localized) {
59901970a576SMatthew G. Knepley       PetscInt cdim;
59911970a576SMatthew G. Knepley 
59921970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
59931970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
59941970a576SMatthew G. Knepley     }
599581e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
599681e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
599781e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
599881e9a530SVaclav Hapla   }
599981e9a530SVaclav Hapla   PetscFunctionReturn(0);
600081e9a530SVaclav Hapla }
600181e9a530SVaclav Hapla 
600281e9a530SVaclav Hapla /*@
60036636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
60046636e97aSMatthew G Knepley 
6005d083f849SBarry Smith   Collective on dm
60066636e97aSMatthew G Knepley 
60076636e97aSMatthew G Knepley   Input Parameter:
60086636e97aSMatthew G Knepley . dm - the DM
60096636e97aSMatthew G Knepley 
60106636e97aSMatthew G Knepley   Output Parameter:
60116636e97aSMatthew G Knepley . c - coordinate vector
60126636e97aSMatthew G Knepley 
60136636e97aSMatthew G Knepley   Note:
60146636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
60156636e97aSMatthew G Knepley 
60166636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
60176636e97aSMatthew G Knepley 
60186636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
60196636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
60206636e97aSMatthew G Knepley 
60216636e97aSMatthew G Knepley   Level: intermediate
60226636e97aSMatthew G Knepley 
602381e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
60246636e97aSMatthew G Knepley @*/
60256636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
60266636e97aSMatthew G Knepley {
60276636e97aSMatthew G Knepley   PetscErrorCode ierr;
60286636e97aSMatthew G Knepley 
60296636e97aSMatthew G Knepley   PetscFunctionBegin;
60306636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60316636e97aSMatthew G Knepley   PetscValidPointer(c,2);
603281e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
60336636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
60346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60356636e97aSMatthew G Knepley }
60366636e97aSMatthew G Knepley 
603781e9a530SVaclav Hapla /*@
603881e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
603981e9a530SVaclav Hapla 
604081e9a530SVaclav Hapla   Not collective
604181e9a530SVaclav Hapla 
604281e9a530SVaclav Hapla   Input Parameter:
604381e9a530SVaclav Hapla . dm - the DM
604481e9a530SVaclav Hapla 
604581e9a530SVaclav Hapla   Output Parameter:
604681e9a530SVaclav Hapla . c - coordinate vector
604781e9a530SVaclav Hapla 
604881e9a530SVaclav Hapla   Level: advanced
604981e9a530SVaclav Hapla 
605081e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
605181e9a530SVaclav Hapla @*/
605281e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
605381e9a530SVaclav Hapla {
605481e9a530SVaclav Hapla   PetscFunctionBegin;
605581e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
605681e9a530SVaclav Hapla   PetscValidPointer(c,2);
605781e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
60586636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
60596636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60606636e97aSMatthew G Knepley }
60616636e97aSMatthew G Knepley 
60622db98f8dSVaclav Hapla /*@
60632db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
60642db98f8dSVaclav Hapla 
60652db98f8dSVaclav Hapla   Not collective
60662db98f8dSVaclav Hapla 
60672db98f8dSVaclav Hapla   Input Parameter:
60682db98f8dSVaclav Hapla + dm - the DM
60692db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
60702db98f8dSVaclav Hapla 
60712db98f8dSVaclav Hapla   Output Parameter:
60722db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
60732db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
60742db98f8dSVaclav Hapla 
60752db98f8dSVaclav Hapla   Note:
60762db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
60772db98f8dSVaclav Hapla 
60782db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
60792db98f8dSVaclav Hapla 
60802db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
60812db98f8dSVaclav Hapla 
60822db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
60832db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
60842db98f8dSVaclav Hapla 
60852db98f8dSVaclav Hapla   Level: advanced
60862db98f8dSVaclav Hapla 
60872db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
60882db98f8dSVaclav Hapla @*/
60892db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
60902db98f8dSVaclav Hapla {
60912db98f8dSVaclav Hapla   PetscSection        cs, newcs;
60922db98f8dSVaclav Hapla   Vec                 coords;
60932db98f8dSVaclav Hapla   const PetscScalar   *arr;
60942db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
60952db98f8dSVaclav Hapla   PetscInt            n;
60962db98f8dSVaclav Hapla   PetscErrorCode      ierr;
60972db98f8dSVaclav Hapla 
60982db98f8dSVaclav Hapla   PetscFunctionBegin;
60992db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61002db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
61012db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
61022db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
61032db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
61041bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
61051bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
61062db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
61072db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
61082db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
61092db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
61102db98f8dSVaclav Hapla   if (pCoord) {
61112db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
61122db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
61132db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
61142db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6115ad9ac99dSVaclav Hapla   } else {
6116ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
61172db98f8dSVaclav Hapla   }
6118ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6119ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
61202db98f8dSVaclav Hapla   PetscFunctionReturn(0);
61212db98f8dSVaclav Hapla }
61222db98f8dSVaclav Hapla 
6123f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6124f19dbd58SToby Isaac {
6125f19dbd58SToby Isaac   PetscErrorCode ierr;
6126f19dbd58SToby Isaac 
6127f19dbd58SToby Isaac   PetscFunctionBegin;
6128f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6129f19dbd58SToby Isaac   PetscValidPointer(field,2);
6130f19dbd58SToby Isaac   if (!dm->coordinateField) {
6131f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6132f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6133f19dbd58SToby Isaac     }
6134f19dbd58SToby Isaac   }
6135f19dbd58SToby Isaac   *field = dm->coordinateField;
6136f19dbd58SToby Isaac   PetscFunctionReturn(0);
6137f19dbd58SToby Isaac }
6138f19dbd58SToby Isaac 
6139f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6140f19dbd58SToby Isaac {
6141f19dbd58SToby Isaac   PetscErrorCode ierr;
6142f19dbd58SToby Isaac 
6143f19dbd58SToby Isaac   PetscFunctionBegin;
6144f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6145f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6146c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6147f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6148f19dbd58SToby Isaac   dm->coordinateField = field;
6149f19dbd58SToby Isaac   PetscFunctionReturn(0);
6150f19dbd58SToby Isaac }
6151f19dbd58SToby Isaac 
61526636e97aSMatthew G Knepley /*@
61531cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
61546636e97aSMatthew G Knepley 
6155d083f849SBarry Smith   Collective on dm
61566636e97aSMatthew G Knepley 
61576636e97aSMatthew G Knepley   Input Parameter:
61586636e97aSMatthew G Knepley . dm - the DM
61596636e97aSMatthew G Knepley 
61606636e97aSMatthew G Knepley   Output Parameter:
61616636e97aSMatthew G Knepley . cdm - coordinate DM
61626636e97aSMatthew G Knepley 
61636636e97aSMatthew G Knepley   Level: intermediate
61646636e97aSMatthew G Knepley 
61651cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
61666636e97aSMatthew G Knepley @*/
61676636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
61686636e97aSMatthew G Knepley {
61696636e97aSMatthew G Knepley   PetscErrorCode ierr;
61706636e97aSMatthew G Knepley 
61716636e97aSMatthew G Knepley   PetscFunctionBegin;
61726636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61736636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
61746636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6175308f8a94SToby Isaac     DM cdm;
6176308f8a94SToby Isaac 
617782f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6178308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6179308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6180308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6181308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6182308f8a94SToby Isaac     dm->coordinateDM = cdm;
61836636e97aSMatthew G Knepley   }
61846636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
61856636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61866636e97aSMatthew G Knepley }
6187e87bb0d3SMatthew G Knepley 
61881cfe2091SMatthew G. Knepley /*@
61891cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
61901cfe2091SMatthew G. Knepley 
6191d083f849SBarry Smith   Logically Collective on dm
61921cfe2091SMatthew G. Knepley 
61931cfe2091SMatthew G. Knepley   Input Parameters:
61941cfe2091SMatthew G. Knepley + dm - the DM
61951cfe2091SMatthew G. Knepley - cdm - coordinate DM
61961cfe2091SMatthew G. Knepley 
61971cfe2091SMatthew G. Knepley   Level: intermediate
61981cfe2091SMatthew G. Knepley 
61991cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62001cfe2091SMatthew G. Knepley @*/
62011cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
62021cfe2091SMatthew G. Knepley {
62031cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
62041cfe2091SMatthew G. Knepley 
62051cfe2091SMatthew G. Knepley   PetscFunctionBegin;
62061cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62071cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6208f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
62091cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
62101cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
62111cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
62121cfe2091SMatthew G. Knepley }
62131cfe2091SMatthew G. Knepley 
621446e270d4SMatthew G. Knepley /*@
621546e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
621646e270d4SMatthew G. Knepley 
621746e270d4SMatthew G. Knepley   Not Collective
621846e270d4SMatthew G. Knepley 
621946e270d4SMatthew G. Knepley   Input Parameter:
622046e270d4SMatthew G. Knepley . dm - The DM object
622146e270d4SMatthew G. Knepley 
622246e270d4SMatthew G. Knepley   Output Parameter:
622346e270d4SMatthew G. Knepley . dim - The embedding dimension
622446e270d4SMatthew G. Knepley 
622546e270d4SMatthew G. Knepley   Level: intermediate
622646e270d4SMatthew G. Knepley 
622792fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
622846e270d4SMatthew G. Knepley @*/
622946e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
623046e270d4SMatthew G. Knepley {
623146e270d4SMatthew G. Knepley   PetscFunctionBegin;
623246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6233534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
62349a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
62359a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
62369a9a41abSToby Isaac   }
623746e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
623846e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
623946e270d4SMatthew G. Knepley }
624046e270d4SMatthew G. Knepley 
624146e270d4SMatthew G. Knepley /*@
624246e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
624346e270d4SMatthew G. Knepley 
624446e270d4SMatthew G. Knepley   Not Collective
624546e270d4SMatthew G. Knepley 
624646e270d4SMatthew G. Knepley   Input Parameters:
624746e270d4SMatthew G. Knepley + dm  - The DM object
624846e270d4SMatthew G. Knepley - dim - The embedding dimension
624946e270d4SMatthew G. Knepley 
625046e270d4SMatthew G. Knepley   Level: intermediate
625146e270d4SMatthew G. Knepley 
625292fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
625346e270d4SMatthew G. Knepley @*/
625446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
625546e270d4SMatthew G. Knepley {
6256e5e52638SMatthew G. Knepley   PetscDS        ds;
6257f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6258f17e8794SMatthew G. Knepley 
625946e270d4SMatthew G. Knepley   PetscFunctionBegin;
626046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
626146e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6262e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6263e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
626446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
626546e270d4SMatthew G. Knepley }
626646e270d4SMatthew G. Knepley 
6267e8abe2deSMatthew G. Knepley /*@
6268e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6269e8abe2deSMatthew G. Knepley 
6270d083f849SBarry Smith   Collective on dm
6271e8abe2deSMatthew G. Knepley 
6272e8abe2deSMatthew G. Knepley   Input Parameter:
6273e8abe2deSMatthew G. Knepley . dm - The DM object
6274e8abe2deSMatthew G. Knepley 
6275e8abe2deSMatthew G. Knepley   Output Parameter:
6276e8abe2deSMatthew G. Knepley . section - The PetscSection object
6277e8abe2deSMatthew G. Knepley 
6278e8abe2deSMatthew G. Knepley   Level: intermediate
6279e8abe2deSMatthew G. Knepley 
628092fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6281e8abe2deSMatthew G. Knepley @*/
6282e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6283e8abe2deSMatthew G. Knepley {
6284e8abe2deSMatthew G. Knepley   DM             cdm;
6285e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6286e8abe2deSMatthew G. Knepley 
6287e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6288e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6289e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6290e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
629192fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6292e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6293e8abe2deSMatthew G. Knepley }
6294e8abe2deSMatthew G. Knepley 
6295e8abe2deSMatthew G. Knepley /*@
6296e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6297e8abe2deSMatthew G. Knepley 
6298e8abe2deSMatthew G. Knepley   Not Collective
6299e8abe2deSMatthew G. Knepley 
6300e8abe2deSMatthew G. Knepley   Input Parameters:
6301e8abe2deSMatthew G. Knepley + dm      - The DM object
630246e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6303e8abe2deSMatthew G. Knepley - section - The PetscSection object
6304e8abe2deSMatthew G. Knepley 
6305e8abe2deSMatthew G. Knepley   Level: intermediate
6306e8abe2deSMatthew G. Knepley 
630792fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6308e8abe2deSMatthew G. Knepley @*/
630946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6310e8abe2deSMatthew G. Knepley {
6311e8abe2deSMatthew G. Knepley   DM             cdm;
6312e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6313e8abe2deSMatthew G. Knepley 
6314e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6315e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
631646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6317e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
631892fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
631946e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
63204c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
632146e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
632246e270d4SMatthew G. Knepley 
632346e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
632446e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
632546e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
632646e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
632746e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
632846e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
632946e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
633046e270d4SMatthew G. Knepley     }
6331ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
633246e270d4SMatthew G. Knepley   }
6333e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6334e8abe2deSMatthew G. Knepley }
6335e8abe2deSMatthew G. Knepley 
6336d864a3eaSLisandro Dalcin /*@
6337d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6338d864a3eaSLisandro Dalcin 
6339d864a3eaSLisandro Dalcin   Input Parameters:
6340d864a3eaSLisandro Dalcin + dm      - The DM object
6341d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6342d864a3eaSLisandro Dalcin 
6343d864a3eaSLisandro Dalcin   Level: intermediate
6344d864a3eaSLisandro Dalcin 
6345d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6346d864a3eaSLisandro Dalcin @*/
6347d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6348d864a3eaSLisandro Dalcin {
6349d864a3eaSLisandro Dalcin   PetscObject    discOld;
6350d864a3eaSLisandro Dalcin   PetscClassId   classid;
6351d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6352d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6353d864a3eaSLisandro Dalcin   Mat            matInterp;
6354d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6355d864a3eaSLisandro Dalcin 
6356d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6357d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6358d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6359d864a3eaSLisandro Dalcin 
6360d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6361d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6362d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6363d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
636429ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
636529ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
636629ad44c5SMatthew G. Knepley       PetscFE        feLinear;
636729ad44c5SMatthew G. Knepley       DMPolytopeType ct;
636829ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
636929ad44c5SMatthew G. Knepley       PetscBool      simplex;
637029ad44c5SMatthew G. Knepley 
637129ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
637229ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
637329ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
637429ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
637529ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
637629ad44c5SMatthew G. Knepley       switch (ct) {
637729ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
637829ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
637929ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
638029ad44c5SMatthew G. Knepley         default: break;
638129ad44c5SMatthew G. Knepley       }
638229ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
638329ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
638429ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
638529ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
638629ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
638729ad44c5SMatthew G. Knepley     } else {
638829ad44c5SMatthew G. Knepley       const char *discname;
638929ad44c5SMatthew G. Knepley 
639029ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
639129ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
639229ad44c5SMatthew G. Knepley     }
639329ad44c5SMatthew G. Knepley   }
6394d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6395d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6396d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6397d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6398d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6399d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6400d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6401d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6402d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6403d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6404d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6405d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6406d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6407d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6408d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6409d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6410d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6411d864a3eaSLisandro Dalcin }
6412d864a3eaSLisandro Dalcin 
64135dc8c3f7SMatthew G. Knepley /*@C
641490b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
64155dc8c3f7SMatthew G. Knepley 
64165dc8c3f7SMatthew G. Knepley   Input Parameters:
641790b157c4SStefano Zampini . dm      - The DM object
641890b157c4SStefano Zampini 
641990b157c4SStefano Zampini   Output Parameters:
642090b157c4SStefano Zampini + per     - Whether the DM is periodic or not
64215dc8c3f7SMatthew 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
64225dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
64235dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
64245dc8c3f7SMatthew G. Knepley 
64255dc8c3f7SMatthew G. Knepley   Level: developer
64265dc8c3f7SMatthew G. Knepley 
64275dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
64285dc8c3f7SMatthew G. Knepley @*/
642990b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6430c6b900c6SMatthew G. Knepley {
6431c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6432c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
643390b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6434c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6435c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
64365dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6437c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6438c6b900c6SMatthew G. Knepley }
6439c6b900c6SMatthew G. Knepley 
64405dc8c3f7SMatthew G. Knepley /*@C
64415dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
64425dc8c3f7SMatthew G. Knepley 
64435dc8c3f7SMatthew G. Knepley   Input Parameters:
64445dc8c3f7SMatthew G. Knepley + dm      - The DM object
6445db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6446db2bf62eSStefano 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.
64475dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
64485dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
64495dc8c3f7SMatthew G. Knepley 
6450db2bf62eSStefano 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.
6451db2bf62eSStefano Zampini 
64525dc8c3f7SMatthew G. Knepley   Level: developer
64535dc8c3f7SMatthew G. Knepley 
64545dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
64555dc8c3f7SMatthew G. Knepley @*/
645690b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6457c6b900c6SMatthew G. Knepley {
6458c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6459c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6460c6b900c6SMatthew G. Knepley 
6461c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6462c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
646390b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6464412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6465412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6466412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
64675dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
646890b157c4SStefano Zampini   if (maxCell) {
6469412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6470412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6471db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6472db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6473412e9a14SMatthew G. Knepley   }
6474db2bf62eSStefano Zampini 
6475412e9a14SMatthew G. Knepley   if (L) {
6476412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6477412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6478412e9a14SMatthew G. Knepley   }
6479412e9a14SMatthew G. Knepley   if (bd) {
6480412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6481412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
648290b157c4SStefano Zampini   }
6483072d7d67SStefano Zampini   dm->periodic = per;
6484c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6485c6b900c6SMatthew G. Knepley }
6486c6b900c6SMatthew G. Knepley 
64872e17dfb7SMatthew G. Knepley /*@
64882e17dfb7SMatthew 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.
64892e17dfb7SMatthew G. Knepley 
64902e17dfb7SMatthew G. Knepley   Input Parameters:
64912e17dfb7SMatthew G. Knepley + dm     - The DM
649265da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
649365da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
64942e17dfb7SMatthew G. Knepley 
64952e17dfb7SMatthew G. Knepley   Output Parameter:
64962e17dfb7SMatthew G. Knepley . out - The localized coordinate point
64972e17dfb7SMatthew G. Knepley 
64982e17dfb7SMatthew G. Knepley   Level: developer
64992e17dfb7SMatthew G. Knepley 
65002e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
65012e17dfb7SMatthew G. Knepley @*/
650265da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
65032e17dfb7SMatthew G. Knepley {
65042e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
65052e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
65062e17dfb7SMatthew G. Knepley 
65072e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65082e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
65092e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65102e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
65112e17dfb7SMatthew G. Knepley   } else {
651265da65dcSMatthew G. Knepley     if (endpoint) {
651365da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6514da3333bfSMatthew 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)) {
6515da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
651665da65dcSMatthew G. Knepley         } else {
6517da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
651865da65dcSMatthew G. Knepley         }
651965da65dcSMatthew G. Knepley       }
652065da65dcSMatthew G. Knepley     } else {
65212e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
65221118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
65232e17dfb7SMatthew G. Knepley       }
65242e17dfb7SMatthew G. Knepley     }
652565da65dcSMatthew G. Knepley   }
65262e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65272e17dfb7SMatthew G. Knepley }
65282e17dfb7SMatthew G. Knepley 
65292e17dfb7SMatthew G. Knepley /*
65302e17dfb7SMatthew 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.
65312e17dfb7SMatthew G. Knepley 
65322e17dfb7SMatthew G. Knepley   Input Parameters:
65332e17dfb7SMatthew G. Knepley + dm     - The DM
65342e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
65352e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
65362e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
65372e17dfb7SMatthew G. Knepley 
65382e17dfb7SMatthew G. Knepley   Output Parameter:
65392e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65402e17dfb7SMatthew G. Knepley 
65412e17dfb7SMatthew G. Knepley   Level: developer
65422e17dfb7SMatthew G. Knepley 
65432e17dfb7SMatthew 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
65442e17dfb7SMatthew G. Knepley 
65452e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
65462e17dfb7SMatthew G. Knepley */
65472e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
65482e17dfb7SMatthew G. Knepley {
65492e17dfb7SMatthew G. Knepley   PetscInt d;
65502e17dfb7SMatthew G. Knepley 
65512e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65522e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65532e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
65542e17dfb7SMatthew G. Knepley   } else {
65552e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6556908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
65572e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
65582e17dfb7SMatthew G. Knepley       } else {
65592e17dfb7SMatthew G. Knepley         out[d] = in[d];
65602e17dfb7SMatthew G. Knepley       }
65612e17dfb7SMatthew G. Knepley     }
65622e17dfb7SMatthew G. Knepley   }
65632e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65642e17dfb7SMatthew G. Knepley }
6565a5801f52SStefano Zampini 
65662e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
65672e17dfb7SMatthew G. Knepley {
65682e17dfb7SMatthew G. Knepley   PetscInt d;
65692e17dfb7SMatthew G. Knepley 
65702e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65712e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65722e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
65732e17dfb7SMatthew G. Knepley   } else {
65742e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6575908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
65762e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
65772e17dfb7SMatthew G. Knepley       } else {
65782e17dfb7SMatthew G. Knepley         out[d] = in[d];
65792e17dfb7SMatthew G. Knepley       }
65802e17dfb7SMatthew G. Knepley     }
65812e17dfb7SMatthew G. Knepley   }
65822e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65832e17dfb7SMatthew G. Knepley }
65842e17dfb7SMatthew G. Knepley 
65852e17dfb7SMatthew G. Knepley /*
65862e17dfb7SMatthew 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.
65872e17dfb7SMatthew G. Knepley 
65882e17dfb7SMatthew G. Knepley   Input Parameters:
65892e17dfb7SMatthew G. Knepley + dm     - The DM
65902e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
65912e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
65922e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
65932e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
65942e17dfb7SMatthew G. Knepley 
65952e17dfb7SMatthew G. Knepley   Output Parameter:
65962e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
65972e17dfb7SMatthew G. Knepley 
65982e17dfb7SMatthew G. Knepley   Level: developer
65992e17dfb7SMatthew G. Knepley 
66002e17dfb7SMatthew 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
66012e17dfb7SMatthew G. Knepley 
66022e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
66032e17dfb7SMatthew G. Knepley */
66042e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
66052e17dfb7SMatthew G. Knepley {
66062e17dfb7SMatthew G. Knepley   PetscInt d;
66072e17dfb7SMatthew G. Knepley 
66082e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66092e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66102e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
66112e17dfb7SMatthew G. Knepley   } else {
66122e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6613412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6614412e9a14SMatthew G. Knepley 
6615412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6616412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6617412e9a14SMatthew G. Knepley 
6618412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6619412e9a14SMatthew 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]));
6620412e9a14SMatthew G. Knepley         out[d] += newCoord;
66212e17dfb7SMatthew G. Knepley       } else {
66222e17dfb7SMatthew G. Knepley         out[d] += in[d];
66232e17dfb7SMatthew G. Knepley       }
66242e17dfb7SMatthew G. Knepley     }
66252e17dfb7SMatthew G. Knepley   }
66262e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66272e17dfb7SMatthew G. Knepley }
66282e17dfb7SMatthew G. Knepley 
662936447a5eSToby Isaac /*@
66308f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
66318f700142SStefano Zampini 
66328f700142SStefano Zampini   Not collective
663336447a5eSToby Isaac 
663436447a5eSToby Isaac   Input Parameter:
663536447a5eSToby Isaac . dm - The DM
663636447a5eSToby Isaac 
663736447a5eSToby Isaac   Output Parameter:
663836447a5eSToby Isaac   areLocalized - True if localized
663936447a5eSToby Isaac 
664036447a5eSToby Isaac   Level: developer
664136447a5eSToby Isaac 
66428f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
664336447a5eSToby Isaac @*/
66448f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
664536447a5eSToby Isaac {
664636447a5eSToby Isaac   DM             cdm;
664736447a5eSToby Isaac   PetscSection   coordSection;
664846a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
664946a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
665036447a5eSToby Isaac   PetscErrorCode ierr;
665136447a5eSToby Isaac 
665236447a5eSToby Isaac   PetscFunctionBegin;
665336447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6654534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
66558b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
665646a3a80fSLisandro Dalcin 
665736447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
665836447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
665946a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
66609f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
666146a3a80fSLisandro Dalcin 
66629f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
666346a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
666436447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
666536447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
666646a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
666746a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
666836447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
666946a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
667036447a5eSToby Isaac   }
66718f700142SStefano Zampini   *areLocalized = alreadyLocalized;
667236447a5eSToby Isaac   PetscFunctionReturn(0);
667336447a5eSToby Isaac }
667436447a5eSToby Isaac 
66758f700142SStefano Zampini /*@
66768f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
66778f700142SStefano Zampini 
66788f700142SStefano Zampini   Collective on dm
66798f700142SStefano Zampini 
66808f700142SStefano Zampini   Input Parameter:
66818f700142SStefano Zampini . dm - The DM
66828f700142SStefano Zampini 
66838f700142SStefano Zampini   Output Parameter:
66848f700142SStefano Zampini   areLocalized - True if localized
66858f700142SStefano Zampini 
66868f700142SStefano Zampini   Level: developer
66878f700142SStefano Zampini 
66888f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
66898f700142SStefano Zampini @*/
66908f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
66918f700142SStefano Zampini {
66928f700142SStefano Zampini   PetscBool      localized;
66938f700142SStefano Zampini   PetscErrorCode ierr;
66948f700142SStefano Zampini 
66958f700142SStefano Zampini   PetscFunctionBegin;
66968f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6697534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
66988f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
66998f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
67008f700142SStefano Zampini   PetscFunctionReturn(0);
67018f700142SStefano Zampini }
670236447a5eSToby Isaac 
67032e17dfb7SMatthew G. Knepley /*@
6704492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
67052e17dfb7SMatthew G. Knepley 
67068f700142SStefano Zampini   Collective on dm
67078f700142SStefano Zampini 
67082e17dfb7SMatthew G. Knepley   Input Parameter:
67092e17dfb7SMatthew G. Knepley . dm - The DM
67102e17dfb7SMatthew G. Knepley 
67112e17dfb7SMatthew G. Knepley   Level: developer
67122e17dfb7SMatthew G. Knepley 
67138f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
67142e17dfb7SMatthew G. Knepley @*/
67152e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
67162e17dfb7SMatthew G. Knepley {
67172e17dfb7SMatthew G. Knepley   DM             cdm;
67182e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
67192e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
67203e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
67213e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6722e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
67233e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
67243e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
67252e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
67262e17dfb7SMatthew G. Knepley 
67272e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67282e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
672992c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6730f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6731f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6732f7cbd40bSStefano Zampini 
67332e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
67342e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
67352e17dfb7SMatthew G. Knepley   {
67362e17dfb7SMatthew G. Knepley     PetscBool isplex;
67372e17dfb7SMatthew G. Knepley 
67382e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
67392e17dfb7SMatthew G. Knepley     if (isplex) {
67402e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
67413e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
674269291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
67433e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
67443e922f36SToby Isaac       newStart = vStart;
67453e922f36SToby Isaac       newEnd   = vEnd;
67463e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
67473e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
67483e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
67493e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
67503e922f36SToby Isaac       }
67512e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
67522e17dfb7SMatthew G. Knepley   }
67532e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
675443eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
67552e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
67563e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6757e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
67583e922f36SToby Isaac 
67592e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
67602e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
67612e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
67622e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
67633e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
67643e922f36SToby Isaac 
676569291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
67663e922f36SToby Isaac   localized = &anchor[bs];
67673e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
67683e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
67693e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
67703e922f36SToby Isaac 
67713e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
67723e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
67733e922f36SToby Isaac       PetscInt     b;
67743e922f36SToby Isaac 
67753e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
67763e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
67773e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
67783e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
67793e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
67803e922f36SToby Isaac         for (b = 0; b < bs; b++) {
67813e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
67823e922f36SToby Isaac         }
67833e922f36SToby Isaac         if (b < bs) break;
67843e922f36SToby Isaac       }
67853e922f36SToby Isaac       if (d < dof/bs) {
67863e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
67873e922f36SToby Isaac           PetscInt cdof;
67883e922f36SToby Isaac 
67893e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
67903e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
67913e922f36SToby Isaac         }
67923e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
67933e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
67943e922f36SToby Isaac       }
67953e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
67963e922f36SToby Isaac     }
67973e922f36SToby Isaac   }
67983e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
67993e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
680069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68013e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
680269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
68033e922f36SToby Isaac     PetscFunctionReturn(0);
68043e922f36SToby Isaac   }
68052e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68062e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68072e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
68082e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
68092e17dfb7SMatthew G. Knepley   }
68102e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
68112e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6812c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
68132e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
68142e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
68152e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
68162e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6817c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68182e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
68192e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68202e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68212e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
68222e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
68232e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
68242e17dfb7SMatthew G. Knepley   }
68253e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68263e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68273e922f36SToby Isaac 
68282e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
68292e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
68303e922f36SToby Isaac       PetscInt     b, cdof;
68312e17dfb7SMatthew G. Knepley 
68323e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
68333e922f36SToby Isaac       if (!cdof) continue;
68342e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68352e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
68362e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68372e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
68382e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68392e17dfb7SMatthew G. Knepley     }
68403e922f36SToby Isaac   }
684169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
684269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6843c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68442e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
68452e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
68462e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
68472e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
68482e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
68492e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68502e17dfb7SMatthew G. Knepley }
68512e17dfb7SMatthew G. Knepley 
6852e87bb0d3SMatthew G Knepley /*@
68533a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6854e87bb0d3SMatthew G Knepley 
6855d083f849SBarry Smith   Collective on v (see explanation below)
6856e87bb0d3SMatthew G Knepley 
6857e87bb0d3SMatthew G Knepley   Input Parameters:
6858e87bb0d3SMatthew G Knepley + dm - The DM
68593a93e3b7SToby Isaac . v - The Vec of points
686062a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
68613a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6862e87bb0d3SMatthew G Knepley 
686361e3bb9bSMatthew G Knepley   Output Parameter:
686462a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
686562a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
68663a93e3b7SToby Isaac 
6867e87bb0d3SMatthew G Knepley 
6868e87bb0d3SMatthew G Knepley   Level: developer
686961e3bb9bSMatthew G Knepley 
687062a38674SMatthew G. Knepley   Notes:
68713a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
687262a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
68733a93e3b7SToby Isaac 
68743a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
687562a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
68763a93e3b7SToby Isaac 
68773a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
68783a93e3b7SToby Isaac 
687962a38674SMatthew G. Knepley $    const PetscSFNode *cells;
688062a38674SMatthew G. Knepley $    PetscInt           nFound;
6881a6216909SToby Isaac $    const PetscInt    *found;
688262a38674SMatthew G. Knepley $
6883a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
68843a93e3b7SToby Isaac 
68853a93e3b7SToby 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
68863a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
68873a93e3b7SToby Isaac 
688862a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
688961e3bb9bSMatthew G Knepley @*/
689062a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6891e87bb0d3SMatthew G Knepley {
6892735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6893735aa83eSMatthew G Knepley 
6894e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6895e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6896e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6897e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
68983a93e3b7SToby Isaac   if (*cellSF) {
68993a93e3b7SToby Isaac     PetscMPIInt result;
69003a93e3b7SToby Isaac 
6901e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6902a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
69033a93e3b7SToby 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");
6904e0fc9d1bSMatthew G. Knepley   } else {
69053a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
69063a93e3b7SToby Isaac   }
6907b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
690847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
690962a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
691047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6911e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6912e87bb0d3SMatthew G Knepley }
691314f150ffSMatthew G. Knepley 
6914f4d763aaSMatthew G. Knepley /*@
6915f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6916f4d763aaSMatthew G. Knepley 
69178f700142SStefano Zampini   Collective on dm
69188f700142SStefano Zampini 
6919f4d763aaSMatthew G. Knepley   Input Parameter:
6920f4d763aaSMatthew G. Knepley . dm - The original DM
6921f4d763aaSMatthew G. Knepley 
6922f4d763aaSMatthew G. Knepley   Output Parameter:
6923f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6924f4d763aaSMatthew G. Knepley 
6925f4d763aaSMatthew G. Knepley   Level: intermediate
6926f4d763aaSMatthew G. Knepley 
6927e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6928f4d763aaSMatthew G. Knepley @*/
692914f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
693014f150ffSMatthew G. Knepley {
6931c26acbdeSMatthew G. Knepley   PetscSection   section;
69322d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
693314f150ffSMatthew G. Knepley   PetscErrorCode ierr;
693414f150ffSMatthew G. Knepley 
693514f150ffSMatthew G. Knepley   PetscFunctionBegin;
693614f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
693714f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
693892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6939c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6940127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
69412d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6942c26acbdeSMatthew G. Knepley     *odm = dm;
6943c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6944c26acbdeSMatthew G. Knepley   }
694514f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6946c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
694714f150ffSMatthew G. Knepley     PetscSF      sf;
694814f150ffSMatthew G. Knepley 
694914f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6950e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
695114f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
695292fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
695314f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
695414f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
695515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6956e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
695714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
695814f150ffSMatthew G. Knepley   }
695914f150ffSMatthew G. Knepley   *odm = dm->dmBC;
696014f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
696114f150ffSMatthew G. Knepley }
6962f4d763aaSMatthew G. Knepley 
6963f4d763aaSMatthew G. Knepley /*@
6964cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6965f4d763aaSMatthew G. Knepley 
6966f4d763aaSMatthew G. Knepley   Input Parameter:
6967f4d763aaSMatthew G. Knepley . dm - The original DM
6968f4d763aaSMatthew G. Knepley 
6969cdb7a50dSMatthew G. Knepley   Output Parameters:
6970cdb7a50dSMatthew G. Knepley + num - The output sequence number
6971cdb7a50dSMatthew G. Knepley - val - The output sequence value
6972f4d763aaSMatthew G. Knepley 
6973f4d763aaSMatthew G. Knepley   Level: intermediate
6974f4d763aaSMatthew G. Knepley 
6975f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6976f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6977f4d763aaSMatthew G. Knepley 
6978f4d763aaSMatthew G. Knepley .seealso: VecView()
6979f4d763aaSMatthew G. Knepley @*/
6980cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6981f4d763aaSMatthew G. Knepley {
6982f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6983f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6984534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6985534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6986f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6987f4d763aaSMatthew G. Knepley }
6988f4d763aaSMatthew G. Knepley 
6989f4d763aaSMatthew G. Knepley /*@
6990cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6991f4d763aaSMatthew G. Knepley 
6992f4d763aaSMatthew G. Knepley   Input Parameters:
6993f4d763aaSMatthew G. Knepley + dm - The original DM
6994cdb7a50dSMatthew G. Knepley . num - The output sequence number
6995cdb7a50dSMatthew G. Knepley - val - The output sequence value
6996f4d763aaSMatthew G. Knepley 
6997f4d763aaSMatthew G. Knepley   Level: intermediate
6998f4d763aaSMatthew G. Knepley 
6999f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7000f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7001f4d763aaSMatthew G. Knepley 
7002f4d763aaSMatthew G. Knepley .seealso: VecView()
7003f4d763aaSMatthew G. Knepley @*/
7004cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7005f4d763aaSMatthew G. Knepley {
7006f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7007f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7008f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7009cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7010cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7011cdb7a50dSMatthew G. Knepley }
7012cdb7a50dSMatthew G. Knepley 
7013cdb7a50dSMatthew G. Knepley /*@C
7014cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7015cdb7a50dSMatthew G. Knepley 
7016cdb7a50dSMatthew G. Knepley   Input Parameters:
7017cdb7a50dSMatthew G. Knepley + dm   - The original DM
7018cdb7a50dSMatthew G. Knepley . name - The sequence name
7019cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7020cdb7a50dSMatthew G. Knepley 
7021cdb7a50dSMatthew G. Knepley   Output Parameter:
7022cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7023cdb7a50dSMatthew G. Knepley 
7024cdb7a50dSMatthew G. Knepley   Level: intermediate
7025cdb7a50dSMatthew G. Knepley 
7026cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7027cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7028cdb7a50dSMatthew G. Knepley 
7029cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7030cdb7a50dSMatthew G. Knepley @*/
7031cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7032cdb7a50dSMatthew G. Knepley {
7033cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7034cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7035cdb7a50dSMatthew G. Knepley 
7036cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7037cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7038cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7039534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
7040cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7041cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7042cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7043cdb7a50dSMatthew G. Knepley     PetscScalar value;
7044cdb7a50dSMatthew G. Knepley 
704539d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
70464aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7047cdb7a50dSMatthew G. Knepley #endif
7048cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7049f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7050f4d763aaSMatthew G. Knepley }
70518e4ac7eaSMatthew G. Knepley 
70528e4ac7eaSMatthew G. Knepley /*@
70538e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
70548e4ac7eaSMatthew G. Knepley 
70558e4ac7eaSMatthew G. Knepley   Not collective
70568e4ac7eaSMatthew G. Knepley 
70578e4ac7eaSMatthew G. Knepley   Input Parameter:
70588e4ac7eaSMatthew G. Knepley . dm - The DM
70598e4ac7eaSMatthew G. Knepley 
70608e4ac7eaSMatthew G. Knepley   Output Parameter:
70618e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
70628e4ac7eaSMatthew G. Knepley 
70638e4ac7eaSMatthew G. Knepley   Level: beginner
70648e4ac7eaSMatthew G. Knepley 
70658e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
70668e4ac7eaSMatthew G. Knepley @*/
70678e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
70688e4ac7eaSMatthew G. Knepley {
70698e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
70708e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7071534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
70728e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
70738e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
70748e4ac7eaSMatthew G. Knepley }
70758e4ac7eaSMatthew G. Knepley 
70768e4ac7eaSMatthew G. Knepley /*@
70775d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
70788e4ac7eaSMatthew G. Knepley 
70798e4ac7eaSMatthew G. Knepley   Collective on dm
70808e4ac7eaSMatthew G. Knepley 
70818e4ac7eaSMatthew G. Knepley   Input Parameters:
70828e4ac7eaSMatthew G. Knepley + dm - The DM
70838e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
70848e4ac7eaSMatthew G. Knepley 
70855d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
70865d3b26e6SMatthew G. Knepley 
70878e4ac7eaSMatthew G. Knepley   Level: beginner
70888e4ac7eaSMatthew G. Knepley 
70895d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
70908e4ac7eaSMatthew G. Knepley @*/
70918e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
70928e4ac7eaSMatthew G. Knepley {
70938e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
70948e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70958833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
70968e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
70978e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
70988e4ac7eaSMatthew G. Knepley }
7099c58f1c22SToby Isaac 
7100c58f1c22SToby Isaac 
7101c58f1c22SToby Isaac /*@C
7102c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7103c58f1c22SToby Isaac 
7104c58f1c22SToby Isaac   Not Collective
7105c58f1c22SToby Isaac 
7106c58f1c22SToby Isaac   Input Parameters:
7107c58f1c22SToby Isaac + dm   - The DM object
7108c58f1c22SToby Isaac - name - The label name
7109c58f1c22SToby Isaac 
7110c58f1c22SToby Isaac   Level: intermediate
7111c58f1c22SToby Isaac 
7112c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7113c58f1c22SToby Isaac @*/
7114c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7115c58f1c22SToby Isaac {
71165d80c0bfSVaclav Hapla   PetscBool      flg;
71175d80c0bfSVaclav Hapla   DMLabel        label;
7118c58f1c22SToby Isaac   PetscErrorCode ierr;
7119c58f1c22SToby Isaac 
7120c58f1c22SToby Isaac   PetscFunctionBegin;
7121c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7122c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
71235d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7124c58f1c22SToby Isaac   if (!flg) {
71255d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
71265d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
71275d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7128c58f1c22SToby Isaac   }
7129c58f1c22SToby Isaac   PetscFunctionReturn(0);
7130c58f1c22SToby Isaac }
7131c58f1c22SToby Isaac 
7132c58f1c22SToby Isaac /*@C
7133c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7134c58f1c22SToby Isaac 
7135c58f1c22SToby Isaac   Not Collective
7136c58f1c22SToby Isaac 
7137c58f1c22SToby Isaac   Input Parameters:
7138c58f1c22SToby Isaac + dm   - The DM object
7139c58f1c22SToby Isaac . name - The label name
7140c58f1c22SToby Isaac - point - The mesh point
7141c58f1c22SToby Isaac 
7142c58f1c22SToby Isaac   Output Parameter:
7143c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7144c58f1c22SToby Isaac 
7145c58f1c22SToby Isaac   Level: beginner
7146c58f1c22SToby Isaac 
7147c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7148c58f1c22SToby Isaac @*/
7149c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7150c58f1c22SToby Isaac {
7151c58f1c22SToby Isaac   DMLabel        label;
7152c58f1c22SToby Isaac   PetscErrorCode ierr;
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac   PetscFunctionBegin;
7155c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7156c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7157c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
715813903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7159c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7160c58f1c22SToby Isaac   PetscFunctionReturn(0);
7161c58f1c22SToby Isaac }
7162c58f1c22SToby Isaac 
7163c58f1c22SToby Isaac /*@C
7164c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7165c58f1c22SToby Isaac 
7166c58f1c22SToby Isaac   Not Collective
7167c58f1c22SToby Isaac 
7168c58f1c22SToby Isaac   Input Parameters:
7169c58f1c22SToby Isaac + dm   - The DM object
7170c58f1c22SToby Isaac . name - The label name
7171c58f1c22SToby Isaac . point - The mesh point
7172c58f1c22SToby Isaac - value - The label value for this point
7173c58f1c22SToby Isaac 
7174c58f1c22SToby Isaac   Output Parameter:
7175c58f1c22SToby Isaac 
7176c58f1c22SToby Isaac   Level: beginner
7177c58f1c22SToby Isaac 
7178c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7179c58f1c22SToby Isaac @*/
7180c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7181c58f1c22SToby Isaac {
7182c58f1c22SToby Isaac   DMLabel        label;
7183c58f1c22SToby Isaac   PetscErrorCode ierr;
7184c58f1c22SToby Isaac 
7185c58f1c22SToby Isaac   PetscFunctionBegin;
7186c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7187c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7188c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7189c58f1c22SToby Isaac   if (!label) {
7190c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7191c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7192c58f1c22SToby Isaac   }
7193c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7194c58f1c22SToby Isaac   PetscFunctionReturn(0);
7195c58f1c22SToby Isaac }
7196c58f1c22SToby Isaac 
7197c58f1c22SToby Isaac /*@C
7198c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7199c58f1c22SToby Isaac 
7200c58f1c22SToby Isaac   Not Collective
7201c58f1c22SToby Isaac 
7202c58f1c22SToby Isaac   Input Parameters:
7203c58f1c22SToby Isaac + dm   - The DM object
7204c58f1c22SToby Isaac . name - The label name
7205c58f1c22SToby Isaac . point - The mesh point
7206c58f1c22SToby Isaac - value - The label value for this point
7207c58f1c22SToby Isaac 
7208c58f1c22SToby Isaac   Output Parameter:
7209c58f1c22SToby Isaac 
7210c58f1c22SToby Isaac   Level: beginner
7211c58f1c22SToby Isaac 
7212c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7213c58f1c22SToby Isaac @*/
7214c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7215c58f1c22SToby Isaac {
7216c58f1c22SToby Isaac   DMLabel        label;
7217c58f1c22SToby Isaac   PetscErrorCode ierr;
7218c58f1c22SToby Isaac 
7219c58f1c22SToby Isaac   PetscFunctionBegin;
7220c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7221c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7222c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7223c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7224c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7225c58f1c22SToby Isaac   PetscFunctionReturn(0);
7226c58f1c22SToby Isaac }
7227c58f1c22SToby Isaac 
7228c58f1c22SToby Isaac /*@C
7229c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7230c58f1c22SToby Isaac 
7231c58f1c22SToby Isaac   Not Collective
7232c58f1c22SToby Isaac 
7233c58f1c22SToby Isaac   Input Parameters:
7234c58f1c22SToby Isaac + dm   - The DM object
7235c58f1c22SToby Isaac - name - The label name
7236c58f1c22SToby Isaac 
7237c58f1c22SToby Isaac   Output Parameter:
7238c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7239c58f1c22SToby Isaac 
7240c58f1c22SToby Isaac   Level: beginner
7241c58f1c22SToby Isaac 
7242df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7243c58f1c22SToby Isaac @*/
7244c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7245c58f1c22SToby Isaac {
7246c58f1c22SToby Isaac   DMLabel        label;
7247c58f1c22SToby Isaac   PetscErrorCode ierr;
7248c58f1c22SToby Isaac 
7249c58f1c22SToby Isaac   PetscFunctionBegin;
7250c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7251c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7252534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7253c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7254c58f1c22SToby Isaac   *size = 0;
7255c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7256c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7257c58f1c22SToby Isaac   PetscFunctionReturn(0);
7258c58f1c22SToby Isaac }
7259c58f1c22SToby Isaac 
7260c58f1c22SToby Isaac /*@C
7261c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7262c58f1c22SToby Isaac 
7263c58f1c22SToby Isaac   Not Collective
7264c58f1c22SToby Isaac 
7265c58f1c22SToby Isaac   Input Parameters:
7266c58f1c22SToby Isaac + mesh - The DM object
7267c58f1c22SToby Isaac - name - The label name
7268c58f1c22SToby Isaac 
7269c58f1c22SToby Isaac   Output Parameter:
7270c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7271c58f1c22SToby Isaac 
7272c58f1c22SToby Isaac   Level: beginner
7273c58f1c22SToby Isaac 
7274c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7275c58f1c22SToby Isaac @*/
7276c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7277c58f1c22SToby Isaac {
7278c58f1c22SToby Isaac   DMLabel        label;
7279c58f1c22SToby Isaac   PetscErrorCode ierr;
7280c58f1c22SToby Isaac 
7281c58f1c22SToby Isaac   PetscFunctionBegin;
7282c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7283c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7284c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7285c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7286c58f1c22SToby Isaac   *ids = NULL;
7287dab2e251SBlaise Bourdin  if (label) {
7288c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7289dab2e251SBlaise Bourdin   } else {
7290dab2e251SBlaise Bourdin     /* returning an empty IS */
7291dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7292dab2e251SBlaise Bourdin   }
7293c58f1c22SToby Isaac   PetscFunctionReturn(0);
7294c58f1c22SToby Isaac }
7295c58f1c22SToby Isaac 
7296c58f1c22SToby Isaac /*@C
7297c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7298c58f1c22SToby Isaac 
7299c58f1c22SToby Isaac   Not Collective
7300c58f1c22SToby Isaac 
7301c58f1c22SToby Isaac   Input Parameters:
7302c58f1c22SToby Isaac + dm - The DM object
7303c58f1c22SToby Isaac . name - The label name
7304c58f1c22SToby Isaac - value - The stratum value
7305c58f1c22SToby Isaac 
7306c58f1c22SToby Isaac   Output Parameter:
7307c58f1c22SToby Isaac . size - The stratum size
7308c58f1c22SToby Isaac 
7309c58f1c22SToby Isaac   Level: beginner
7310c58f1c22SToby Isaac 
7311c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7312c58f1c22SToby Isaac @*/
7313c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7314c58f1c22SToby Isaac {
7315c58f1c22SToby Isaac   DMLabel        label;
7316c58f1c22SToby Isaac   PetscErrorCode ierr;
7317c58f1c22SToby Isaac 
7318c58f1c22SToby Isaac   PetscFunctionBegin;
7319c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7320c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7321534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7322c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7323c58f1c22SToby Isaac   *size = 0;
7324c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7325c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7326c58f1c22SToby Isaac   PetscFunctionReturn(0);
7327c58f1c22SToby Isaac }
7328c58f1c22SToby Isaac 
7329c58f1c22SToby Isaac /*@C
7330c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7331c58f1c22SToby Isaac 
7332c58f1c22SToby Isaac   Not Collective
7333c58f1c22SToby Isaac 
7334c58f1c22SToby Isaac   Input Parameters:
7335c58f1c22SToby Isaac + dm - The DM object
7336c58f1c22SToby Isaac . name - The label name
7337c58f1c22SToby Isaac - value - The stratum value
7338c58f1c22SToby Isaac 
7339c58f1c22SToby Isaac   Output Parameter:
7340c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7341c58f1c22SToby Isaac 
7342c58f1c22SToby Isaac   Level: beginner
7343c58f1c22SToby Isaac 
7344c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7345c58f1c22SToby Isaac @*/
7346c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7347c58f1c22SToby Isaac {
7348c58f1c22SToby Isaac   DMLabel        label;
7349c58f1c22SToby Isaac   PetscErrorCode ierr;
7350c58f1c22SToby Isaac 
7351c58f1c22SToby Isaac   PetscFunctionBegin;
7352c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7353c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7354c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7355c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7356c58f1c22SToby Isaac   *points = NULL;
7357c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7358c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7359c58f1c22SToby Isaac   PetscFunctionReturn(0);
7360c58f1c22SToby Isaac }
7361c58f1c22SToby Isaac 
73624de306b1SToby Isaac /*@C
73639044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
73644de306b1SToby Isaac 
73654de306b1SToby Isaac   Not Collective
73664de306b1SToby Isaac 
73674de306b1SToby Isaac   Input Parameters:
73684de306b1SToby Isaac + dm - The DM object
73694de306b1SToby Isaac . name - The label name
73704de306b1SToby Isaac . value - The stratum value
73714de306b1SToby Isaac - points - The stratum points
73724de306b1SToby Isaac 
73734de306b1SToby Isaac   Level: beginner
73744de306b1SToby Isaac 
73754de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
73764de306b1SToby Isaac @*/
73774de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
73784de306b1SToby Isaac {
73794de306b1SToby Isaac   DMLabel        label;
73804de306b1SToby Isaac   PetscErrorCode ierr;
73814de306b1SToby Isaac 
73824de306b1SToby Isaac   PetscFunctionBegin;
73834de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73844de306b1SToby Isaac   PetscValidCharPointer(name, 2);
73854de306b1SToby Isaac   PetscValidPointer(points, 4);
73864de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
73874de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
73884de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
73894de306b1SToby Isaac   PetscFunctionReturn(0);
73904de306b1SToby Isaac }
73914de306b1SToby Isaac 
7392c58f1c22SToby Isaac /*@C
7393c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7394c58f1c22SToby Isaac 
7395c58f1c22SToby Isaac   Not Collective
7396c58f1c22SToby Isaac 
7397c58f1c22SToby Isaac   Input Parameters:
7398c58f1c22SToby Isaac + dm   - The DM object
7399c58f1c22SToby Isaac . name - The label name
7400c58f1c22SToby Isaac - value - The label value for this point
7401c58f1c22SToby Isaac 
7402c58f1c22SToby Isaac   Output Parameter:
7403c58f1c22SToby Isaac 
7404c58f1c22SToby Isaac   Level: beginner
7405c58f1c22SToby Isaac 
7406c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7407c58f1c22SToby Isaac @*/
7408c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7409c58f1c22SToby Isaac {
7410c58f1c22SToby Isaac   DMLabel        label;
7411c58f1c22SToby Isaac   PetscErrorCode ierr;
7412c58f1c22SToby Isaac 
7413c58f1c22SToby Isaac   PetscFunctionBegin;
7414c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7415c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7416c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7417c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7418c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7419c58f1c22SToby Isaac   PetscFunctionReturn(0);
7420c58f1c22SToby Isaac }
7421c58f1c22SToby Isaac 
7422c58f1c22SToby Isaac /*@
7423c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7424c58f1c22SToby Isaac 
7425c58f1c22SToby Isaac   Not Collective
7426c58f1c22SToby Isaac 
7427c58f1c22SToby Isaac   Input Parameter:
7428c58f1c22SToby Isaac . dm   - The DM object
7429c58f1c22SToby Isaac 
7430c58f1c22SToby Isaac   Output Parameter:
7431c58f1c22SToby Isaac . numLabels - the number of Labels
7432c58f1c22SToby Isaac 
7433c58f1c22SToby Isaac   Level: intermediate
7434c58f1c22SToby Isaac 
7435c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7436c58f1c22SToby Isaac @*/
7437c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7438c58f1c22SToby Isaac {
74395d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7440c58f1c22SToby Isaac   PetscInt  n    = 0;
7441c58f1c22SToby Isaac 
7442c58f1c22SToby Isaac   PetscFunctionBegin;
7443c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7444534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7445c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7446c58f1c22SToby Isaac   *numLabels = n;
7447c58f1c22SToby Isaac   PetscFunctionReturn(0);
7448c58f1c22SToby Isaac }
7449c58f1c22SToby Isaac 
7450c58f1c22SToby Isaac /*@C
7451c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7452c58f1c22SToby Isaac 
7453c58f1c22SToby Isaac   Not Collective
7454c58f1c22SToby Isaac 
7455c58f1c22SToby Isaac   Input Parameters:
7456c58f1c22SToby Isaac + dm - The DM object
7457c58f1c22SToby Isaac - n  - the label number
7458c58f1c22SToby Isaac 
7459c58f1c22SToby Isaac   Output Parameter:
7460c58f1c22SToby Isaac . name - the label name
7461c58f1c22SToby Isaac 
7462c58f1c22SToby Isaac   Level: intermediate
7463c58f1c22SToby Isaac 
7464c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7465c58f1c22SToby Isaac @*/
7466c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7467c58f1c22SToby Isaac {
74685d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7469c58f1c22SToby Isaac   PetscInt       l    = 0;
7470d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7471c58f1c22SToby Isaac 
7472c58f1c22SToby Isaac   PetscFunctionBegin;
7473c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7474c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7475c58f1c22SToby Isaac   while (next) {
7476c58f1c22SToby Isaac     if (l == n) {
7477d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7478c58f1c22SToby Isaac       PetscFunctionReturn(0);
7479c58f1c22SToby Isaac     }
7480c58f1c22SToby Isaac     ++l;
7481c58f1c22SToby Isaac     next = next->next;
7482c58f1c22SToby Isaac   }
7483c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7484c58f1c22SToby Isaac }
7485c58f1c22SToby Isaac 
7486c58f1c22SToby Isaac /*@C
7487c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7488c58f1c22SToby Isaac 
7489c58f1c22SToby Isaac   Not Collective
7490c58f1c22SToby Isaac 
7491c58f1c22SToby Isaac   Input Parameters:
7492c58f1c22SToby Isaac + dm   - The DM object
7493c58f1c22SToby Isaac - name - The label name
7494c58f1c22SToby Isaac 
7495c58f1c22SToby Isaac   Output Parameter:
7496c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7497c58f1c22SToby Isaac 
7498c58f1c22SToby Isaac   Level: intermediate
7499c58f1c22SToby Isaac 
7500c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7501c58f1c22SToby Isaac @*/
7502c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7503c58f1c22SToby Isaac {
75045d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7505d67d17b1SMatthew G. Knepley   const char    *lname;
7506c58f1c22SToby Isaac   PetscErrorCode ierr;
7507c58f1c22SToby Isaac 
7508c58f1c22SToby Isaac   PetscFunctionBegin;
7509c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7510c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7511534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7512c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7513c58f1c22SToby Isaac   while (next) {
7514d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7515d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7516c58f1c22SToby Isaac     if (*hasLabel) break;
7517c58f1c22SToby Isaac     next = next->next;
7518c58f1c22SToby Isaac   }
7519c58f1c22SToby Isaac   PetscFunctionReturn(0);
7520c58f1c22SToby Isaac }
7521c58f1c22SToby Isaac 
7522c58f1c22SToby Isaac /*@C
7523c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7524c58f1c22SToby Isaac 
7525c58f1c22SToby Isaac   Not Collective
7526c58f1c22SToby Isaac 
7527c58f1c22SToby Isaac   Input Parameters:
7528c58f1c22SToby Isaac + dm   - The DM object
7529c58f1c22SToby Isaac - name - The label name
7530c58f1c22SToby Isaac 
7531c58f1c22SToby Isaac   Output Parameter:
7532c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7533c58f1c22SToby Isaac 
7534c58f1c22SToby Isaac   Level: intermediate
7535c58f1c22SToby Isaac 
7536c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7537c58f1c22SToby Isaac @*/
7538c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7539c58f1c22SToby Isaac {
75405d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7541c58f1c22SToby Isaac   PetscBool      hasLabel;
7542d67d17b1SMatthew G. Knepley   const char    *lname;
7543c58f1c22SToby Isaac   PetscErrorCode ierr;
7544c58f1c22SToby Isaac 
7545c58f1c22SToby Isaac   PetscFunctionBegin;
7546c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7547c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7548c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7549c58f1c22SToby Isaac   *label = NULL;
7550c58f1c22SToby Isaac   while (next) {
7551d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7552d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7553c58f1c22SToby Isaac     if (hasLabel) {
7554c58f1c22SToby Isaac       *label = next->label;
7555c58f1c22SToby Isaac       break;
7556c58f1c22SToby Isaac     }
7557c58f1c22SToby Isaac     next = next->next;
7558c58f1c22SToby Isaac   }
7559c58f1c22SToby Isaac   PetscFunctionReturn(0);
7560c58f1c22SToby Isaac }
7561c58f1c22SToby Isaac 
7562c58f1c22SToby Isaac /*@C
7563c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7564c58f1c22SToby Isaac 
7565c58f1c22SToby Isaac   Not Collective
7566c58f1c22SToby Isaac 
7567c58f1c22SToby Isaac   Input Parameters:
7568c58f1c22SToby Isaac + dm - The DM object
7569c58f1c22SToby Isaac - n  - the label number
7570c58f1c22SToby Isaac 
7571c58f1c22SToby Isaac   Output Parameter:
7572c58f1c22SToby Isaac . label - the label
7573c58f1c22SToby Isaac 
7574c58f1c22SToby Isaac   Level: intermediate
7575c58f1c22SToby Isaac 
7576c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7577c58f1c22SToby Isaac @*/
7578c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7579c58f1c22SToby Isaac {
75805d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7581c58f1c22SToby Isaac   PetscInt    l    = 0;
7582c58f1c22SToby Isaac 
7583c58f1c22SToby Isaac   PetscFunctionBegin;
7584c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7585c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7586c58f1c22SToby Isaac   while (next) {
7587c58f1c22SToby Isaac     if (l == n) {
7588c58f1c22SToby Isaac       *label = next->label;
7589c58f1c22SToby Isaac       PetscFunctionReturn(0);
7590c58f1c22SToby Isaac     }
7591c58f1c22SToby Isaac     ++l;
7592c58f1c22SToby Isaac     next = next->next;
7593c58f1c22SToby Isaac   }
7594c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7595c58f1c22SToby Isaac }
7596c58f1c22SToby Isaac 
7597c58f1c22SToby Isaac /*@C
7598c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7599c58f1c22SToby Isaac 
7600c58f1c22SToby Isaac   Not Collective
7601c58f1c22SToby Isaac 
7602c58f1c22SToby Isaac   Input Parameters:
7603c58f1c22SToby Isaac + dm   - The DM object
7604c58f1c22SToby Isaac - label - The DMLabel
7605c58f1c22SToby Isaac 
7606c58f1c22SToby Isaac   Level: developer
7607c58f1c22SToby Isaac 
7608c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7609c58f1c22SToby Isaac @*/
7610c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7611c58f1c22SToby Isaac {
76125d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7613c58f1c22SToby Isaac   PetscBool      hasLabel;
7614d67d17b1SMatthew G. Knepley   const char    *lname;
76155d80c0bfSVaclav Hapla   PetscBool      flg;
7616c58f1c22SToby Isaac   PetscErrorCode ierr;
7617c58f1c22SToby Isaac 
7618c58f1c22SToby Isaac   PetscFunctionBegin;
7619c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7620d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7621d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7622d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7623c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7624c58f1c22SToby Isaac   tmpLabel->label  = label;
7625c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
76265d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
76275d80c0bfSVaclav Hapla   *p = tmpLabel;
762808f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
76295d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
76305d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7631ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7632ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7633c58f1c22SToby Isaac   PetscFunctionReturn(0);
7634c58f1c22SToby Isaac }
7635c58f1c22SToby Isaac 
7636c58f1c22SToby Isaac /*@C
7637e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7638c58f1c22SToby Isaac 
7639c58f1c22SToby Isaac   Not Collective
7640c58f1c22SToby Isaac 
7641c58f1c22SToby Isaac   Input Parameters:
7642c58f1c22SToby Isaac + dm   - The DM object
7643c58f1c22SToby Isaac - name - The label name
7644c58f1c22SToby Isaac 
7645c58f1c22SToby Isaac   Output Parameter:
7646c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7647c58f1c22SToby Isaac 
7648c58f1c22SToby Isaac   Level: developer
7649c58f1c22SToby Isaac 
7650e5472504SVaclav Hapla   Notes:
7651e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7652e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7653e5472504SVaclav Hapla 
7654e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7655e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7656e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7657e5472504SVaclav Hapla 
7658e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7659c58f1c22SToby Isaac @*/
7660c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7661c58f1c22SToby Isaac {
766295d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7663c58f1c22SToby Isaac   PetscBool      hasLabel;
7664d67d17b1SMatthew G. Knepley   const char    *lname;
7665c58f1c22SToby Isaac   PetscErrorCode ierr;
7666c58f1c22SToby Isaac 
7667c58f1c22SToby Isaac   PetscFunctionBegin;
7668c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7669e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7670e5472504SVaclav Hapla   if (label) {
7671e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7672c58f1c22SToby Isaac     *label = NULL;
7673e5472504SVaclav Hapla   }
76745d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
767595d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7676d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7677c58f1c22SToby Isaac     if (hasLabel) {
767895d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7679c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
768095d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7681ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7682ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
768395d578d6SVaclav Hapla       if (label) *label = link->label;
768495d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
768595d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7686c58f1c22SToby Isaac       break;
7687c58f1c22SToby Isaac     }
7688c58f1c22SToby Isaac   }
7689c58f1c22SToby Isaac   PetscFunctionReturn(0);
7690c58f1c22SToby Isaac }
7691c58f1c22SToby Isaac 
7692306894acSVaclav Hapla /*@
7693306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7694306894acSVaclav Hapla 
7695306894acSVaclav Hapla   Not Collective
7696306894acSVaclav Hapla 
7697306894acSVaclav Hapla   Input Parameters:
7698306894acSVaclav Hapla + dm   - The DM object
7699306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7700306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7701306894acSVaclav Hapla 
7702306894acSVaclav Hapla   Level: developer
7703306894acSVaclav Hapla 
7704306894acSVaclav Hapla   Notes:
7705306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7706306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7707306894acSVaclav Hapla   *label nullified.
7708306894acSVaclav Hapla 
7709306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7710306894acSVaclav Hapla @*/
7711306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7712306894acSVaclav Hapla {
771343e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7714306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7715306894acSVaclav Hapla   PetscErrorCode ierr;
7716306894acSVaclav Hapla 
7717306894acSVaclav Hapla   PetscFunctionBegin;
7718306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7719306894acSVaclav Hapla   PetscValidPointer(label, 2);
7720f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7721306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7722306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
77235d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
772443e45a93SVaclav Hapla     if (*label == link->label) {
7725306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
772643e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7727306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7728ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
772943e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
773043e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
773143e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7732306894acSVaclav Hapla       break;
7733306894acSVaclav Hapla     }
7734306894acSVaclav Hapla   }
7735306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7736306894acSVaclav Hapla   PetscFunctionReturn(0);
7737306894acSVaclav Hapla }
7738306894acSVaclav Hapla 
7739c58f1c22SToby Isaac /*@C
7740c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7741c58f1c22SToby Isaac 
7742c58f1c22SToby Isaac   Not Collective
7743c58f1c22SToby Isaac 
7744c58f1c22SToby Isaac   Input Parameters:
7745c58f1c22SToby Isaac + dm   - The DM object
7746c58f1c22SToby Isaac - name - The label name
7747c58f1c22SToby Isaac 
7748c58f1c22SToby Isaac   Output Parameter:
7749c58f1c22SToby Isaac . output - The flag for output
7750c58f1c22SToby Isaac 
7751c58f1c22SToby Isaac   Level: developer
7752c58f1c22SToby Isaac 
7753c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7754c58f1c22SToby Isaac @*/
7755c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7756c58f1c22SToby Isaac {
77575d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7758d67d17b1SMatthew G. Knepley   const char    *lname;
7759c58f1c22SToby Isaac   PetscErrorCode ierr;
7760c58f1c22SToby Isaac 
7761c58f1c22SToby Isaac   PetscFunctionBegin;
7762c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7763c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7764c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7765c58f1c22SToby Isaac   while (next) {
7766c58f1c22SToby Isaac     PetscBool flg;
7767c58f1c22SToby Isaac 
7768d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7769d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7770c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7771c58f1c22SToby Isaac     next = next->next;
7772c58f1c22SToby Isaac   }
7773c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7774c58f1c22SToby Isaac }
7775c58f1c22SToby Isaac 
7776c58f1c22SToby Isaac /*@C
7777c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7778c58f1c22SToby Isaac 
7779c58f1c22SToby Isaac   Not Collective
7780c58f1c22SToby Isaac 
7781c58f1c22SToby Isaac   Input Parameters:
7782c58f1c22SToby Isaac + dm     - The DM object
7783c58f1c22SToby Isaac . name   - The label name
7784c58f1c22SToby Isaac - output - The flag for output
7785c58f1c22SToby Isaac 
7786c58f1c22SToby Isaac   Level: developer
7787c58f1c22SToby Isaac 
7788c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7789c58f1c22SToby Isaac @*/
7790c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7791c58f1c22SToby Isaac {
77925d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7793d67d17b1SMatthew G. Knepley   const char    *lname;
7794c58f1c22SToby Isaac   PetscErrorCode ierr;
7795c58f1c22SToby Isaac 
7796c58f1c22SToby Isaac   PetscFunctionBegin;
7797c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7798534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7799c58f1c22SToby Isaac   while (next) {
7800c58f1c22SToby Isaac     PetscBool flg;
7801c58f1c22SToby Isaac 
7802d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7803d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7804c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7805c58f1c22SToby Isaac     next = next->next;
7806c58f1c22SToby Isaac   }
7807c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7808c58f1c22SToby Isaac }
7809c58f1c22SToby Isaac 
7810c58f1c22SToby Isaac /*@
7811c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7812c58f1c22SToby Isaac 
7813d083f849SBarry Smith   Collective on dmA
7814c58f1c22SToby Isaac 
7815c58f1c22SToby Isaac   Input Parameter:
78165d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
78172e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
78185d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7819ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7820c58f1c22SToby Isaac 
7821c58f1c22SToby Isaac   Level: intermediate
7822c58f1c22SToby Isaac 
7823c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7824c58f1c22SToby Isaac 
78255d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7826c58f1c22SToby Isaac @*/
78275d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7828c58f1c22SToby Isaac {
7829c58f1c22SToby Isaac   DMLabel        label, labelNew;
7830c58f1c22SToby Isaac   const char    *name;
7831c58f1c22SToby Isaac   PetscBool      flg;
78325d80c0bfSVaclav Hapla   DMLabelLink    link;
78335d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7834c58f1c22SToby Isaac 
78355d80c0bfSVaclav Hapla   PetscFunctionBegin;
78365d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
78375d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
78385d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
78395d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
78405d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
78415d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
78425d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
78435d80c0bfSVaclav Hapla     label=link->label;
78445d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
78455d80c0bfSVaclav Hapla     if (!all) {
7846c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7847c58f1c22SToby Isaac       if (flg) continue;
78487d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
78497d5acc75SStefano Zampini       if (flg) continue;
7850ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7851ba2698f1SMatthew G. Knepley       if (flg) continue;
78525d80c0bfSVaclav Hapla     }
78535d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7854c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
78555d80c0bfSVaclav Hapla     } else {
78565d80c0bfSVaclav Hapla       labelNew = label;
78575d80c0bfSVaclav Hapla     }
7858c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
78595d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7860c58f1c22SToby Isaac   }
7861c58f1c22SToby Isaac   PetscFunctionReturn(0);
7862c58f1c22SToby Isaac }
7863a8fb8f29SToby Isaac 
7864a8fb8f29SToby Isaac /*@
7865a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7866a8fb8f29SToby Isaac 
7867a8fb8f29SToby Isaac   Input Parameter:
7868a8fb8f29SToby Isaac . dm - The DM object
7869a8fb8f29SToby Isaac 
7870a8fb8f29SToby Isaac   Output Parameter:
7871a8fb8f29SToby Isaac . cdm - The coarse DM
7872a8fb8f29SToby Isaac 
7873a8fb8f29SToby Isaac   Level: intermediate
7874a8fb8f29SToby Isaac 
7875a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7876a8fb8f29SToby Isaac @*/
7877a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7878a8fb8f29SToby Isaac {
7879a8fb8f29SToby Isaac   PetscFunctionBegin;
7880a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7881a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7882a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7883a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7884a8fb8f29SToby Isaac }
7885a8fb8f29SToby Isaac 
7886a8fb8f29SToby Isaac /*@
7887a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7888a8fb8f29SToby Isaac 
7889a8fb8f29SToby Isaac   Input Parameters:
7890a8fb8f29SToby Isaac + dm - The DM object
7891a8fb8f29SToby Isaac - cdm - The coarse DM
7892a8fb8f29SToby Isaac 
7893a8fb8f29SToby Isaac   Level: intermediate
7894a8fb8f29SToby Isaac 
7895a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7896a8fb8f29SToby Isaac @*/
7897a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7898a8fb8f29SToby Isaac {
7899a8fb8f29SToby Isaac   PetscErrorCode ierr;
7900a8fb8f29SToby Isaac 
7901a8fb8f29SToby Isaac   PetscFunctionBegin;
7902a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7903a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7904a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7905a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7906a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7907a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7908a8fb8f29SToby Isaac }
7909a8fb8f29SToby Isaac 
791088bdff64SToby Isaac /*@
791188bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
791288bdff64SToby Isaac 
791388bdff64SToby Isaac   Input Parameter:
791488bdff64SToby Isaac . dm - The DM object
791588bdff64SToby Isaac 
791688bdff64SToby Isaac   Output Parameter:
791788bdff64SToby Isaac . fdm - The fine DM
791888bdff64SToby Isaac 
791988bdff64SToby Isaac   Level: intermediate
792088bdff64SToby Isaac 
792188bdff64SToby Isaac .seealso: DMSetFineDM()
792288bdff64SToby Isaac @*/
792388bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
792488bdff64SToby Isaac {
792588bdff64SToby Isaac   PetscFunctionBegin;
792688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
792788bdff64SToby Isaac   PetscValidPointer(fdm, 2);
792888bdff64SToby Isaac   *fdm = dm->fineMesh;
792988bdff64SToby Isaac   PetscFunctionReturn(0);
793088bdff64SToby Isaac }
793188bdff64SToby Isaac 
793288bdff64SToby Isaac /*@
793388bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
793488bdff64SToby Isaac 
793588bdff64SToby Isaac   Input Parameters:
793688bdff64SToby Isaac + dm - The DM object
793788bdff64SToby Isaac - fdm - The fine DM
793888bdff64SToby Isaac 
793988bdff64SToby Isaac   Level: intermediate
794088bdff64SToby Isaac 
794188bdff64SToby Isaac .seealso: DMGetFineDM()
794288bdff64SToby Isaac @*/
794388bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
794488bdff64SToby Isaac {
794588bdff64SToby Isaac   PetscErrorCode ierr;
794688bdff64SToby Isaac 
794788bdff64SToby Isaac   PetscFunctionBegin;
794888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
794988bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
795088bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
795188bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
795288bdff64SToby Isaac   dm->fineMesh = fdm;
795388bdff64SToby Isaac   PetscFunctionReturn(0);
795488bdff64SToby Isaac }
795588bdff64SToby Isaac 
7956a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7957a6ba4734SToby Isaac 
7958a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7959a6ba4734SToby Isaac {
7960e5e52638SMatthew G. Knepley   PetscInt       d;
7961a6ba4734SToby Isaac   PetscErrorCode ierr;
7962a6ba4734SToby Isaac 
7963a6ba4734SToby Isaac   PetscFunctionBegin;
7964e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7965e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7966e5e52638SMatthew G. Knepley   }
7967a6ba4734SToby Isaac   PetscFunctionReturn(0);
7968a6ba4734SToby Isaac }
7969a6ba4734SToby Isaac 
7970a6ba4734SToby Isaac /*@C
7971a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7972a6ba4734SToby Isaac 
7973783e2ec8SMatthew G. Knepley   Collective on dm
7974783e2ec8SMatthew G. Knepley 
7975a6ba4734SToby Isaac   Input Parameters:
79764c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7977f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7978a6ba4734SToby Isaac . name        - The BC name
7979a6ba4734SToby Isaac . labelname   - The label defining constrained points
7980a6ba4734SToby Isaac . field       - The field to constrain
7981e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7982a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7983a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
798456cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time deriative of the boundary values, or NULL
7985a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7986a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7987a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7988a6ba4734SToby Isaac 
7989a6ba4734SToby Isaac   Options Database Keys:
7990a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7991a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7992a6ba4734SToby Isaac 
799356cf3b9cSMatthew G. Knepley   Note:
799456cf3b9cSMatthew 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:
799556cf3b9cSMatthew G. Knepley 
799656cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
799756cf3b9cSMatthew G. Knepley 
799856cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
799956cf3b9cSMatthew G. Knepley 
800056cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
800156cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
800256cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
800356cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
800456cf3b9cSMatthew G. Knepley 
800556cf3b9cSMatthew G. Knepley + dim - the spatial dimension
800656cf3b9cSMatthew G. Knepley . Nf - the number of fields
800756cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
800856cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
800956cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
801056cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
801156cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
801256cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
801356cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
801456cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
801556cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
801656cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
801756cf3b9cSMatthew G. Knepley . t - current time
801856cf3b9cSMatthew G. Knepley . x - coordinates of the current point
801956cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
802056cf3b9cSMatthew G. Knepley . constants - constant parameters
802156cf3b9cSMatthew G. Knepley - bcval - output values at the current point
802256cf3b9cSMatthew G. Knepley 
8023a6ba4734SToby Isaac   Level: developer
8024a6ba4734SToby Isaac 
802556cf3b9cSMatthew G. Knepley .seealso: DMGetBoundary(), PetscDSAddBoundary()
8026a6ba4734SToby Isaac @*/
802756cf3b9cSMatthew 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)
8028a6ba4734SToby Isaac {
8029e5e52638SMatthew G. Knepley   PetscDS        ds;
8030a6ba4734SToby Isaac   PetscErrorCode ierr;
8031a6ba4734SToby Isaac 
8032a6ba4734SToby Isaac   PetscFunctionBegin;
8033a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8034783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
8035783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
8036783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
8037783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
8038e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8039783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
804056cf3b9cSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, bcFunc_t, numids, ids, ctx);CHKERRQ(ierr);
8041a6ba4734SToby Isaac   PetscFunctionReturn(0);
8042a6ba4734SToby Isaac }
8043a6ba4734SToby Isaac 
8044a6ba4734SToby Isaac /*@
8045a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
8046a6ba4734SToby Isaac 
8047a6ba4734SToby Isaac   Input Parameters:
8048a6ba4734SToby Isaac . dm - The mesh object
8049a6ba4734SToby Isaac 
8050a6ba4734SToby Isaac   Output Parameters:
8051a6ba4734SToby Isaac . numBd - The number of BC
8052a6ba4734SToby Isaac 
8053a6ba4734SToby Isaac   Level: intermediate
8054a6ba4734SToby Isaac 
8055a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
8056a6ba4734SToby Isaac @*/
8057a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
8058a6ba4734SToby Isaac {
8059e5e52638SMatthew G. Knepley   PetscDS        ds;
806058ebd649SToby Isaac   PetscErrorCode ierr;
8061a6ba4734SToby Isaac 
8062a6ba4734SToby Isaac   PetscFunctionBegin;
8063a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8064e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8065e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
8066a6ba4734SToby Isaac   PetscFunctionReturn(0);
8067a6ba4734SToby Isaac }
8068a6ba4734SToby Isaac 
8069a6ba4734SToby Isaac /*@C
80701c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
8071a6ba4734SToby Isaac 
8072a6ba4734SToby Isaac   Input Parameters:
8073a6ba4734SToby Isaac + dm          - The mesh object
8074a6ba4734SToby Isaac - bd          - The BC number
8075a6ba4734SToby Isaac 
8076a6ba4734SToby Isaac   Output Parameters:
8077f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8078a6ba4734SToby Isaac . name        - The BC name
8079a6ba4734SToby Isaac . labelname   - The label defining constrained points
8080a6ba4734SToby Isaac . field       - The field to constrain
8081a6ba4734SToby Isaac . numcomps    - The number of constrained field components
8082a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8083a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
808456cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time derviative of the boundary values
8085a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8086a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8087a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8088a6ba4734SToby Isaac 
8089a6ba4734SToby Isaac   Options Database Keys:
8090a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8091a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8092a6ba4734SToby Isaac 
8093a6ba4734SToby Isaac   Level: developer
8094a6ba4734SToby Isaac 
8095a6ba4734SToby Isaac .seealso: DMAddBoundary()
8096a6ba4734SToby Isaac @*/
809756cf3b9cSMatthew 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)
8098a6ba4734SToby Isaac {
8099e5e52638SMatthew G. Knepley   PetscDS        ds;
810058ebd649SToby Isaac   PetscErrorCode ierr;
8101a6ba4734SToby Isaac 
8102a6ba4734SToby Isaac   PetscFunctionBegin;
8103a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8104e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
810556cf3b9cSMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, func_t, numids, ids, ctx);CHKERRQ(ierr);
8106a6ba4734SToby Isaac   PetscFunctionReturn(0);
8107a6ba4734SToby Isaac }
8108a6ba4734SToby Isaac 
8109e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8110e6f8dbb6SToby Isaac {
8111e5e52638SMatthew G. Knepley   PetscDS        ds;
8112dff059c6SToby Isaac   DMBoundary    *lastnext;
8113e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8114e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8115e6f8dbb6SToby Isaac 
8116e6f8dbb6SToby Isaac   PetscFunctionBegin;
8117e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8118e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
811947a1f5adSToby Isaac   if (dm->boundary) {
812047a1f5adSToby Isaac     DMBoundary next = dm->boundary;
812147a1f5adSToby Isaac 
812247a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
812347a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
812447a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
812547a1f5adSToby Isaac     while (next) {
812647a1f5adSToby Isaac       DMBoundary b = next;
812747a1f5adSToby Isaac 
812847a1f5adSToby Isaac       next = b->next;
812947a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8130a6ba4734SToby Isaac     }
813147a1f5adSToby Isaac     dm->boundary = NULL;
8132a6ba4734SToby Isaac   }
813347a1f5adSToby Isaac 
8134dff059c6SToby Isaac   lastnext = &(dm->boundary);
8135e6f8dbb6SToby Isaac   while (dsbound) {
8136e6f8dbb6SToby Isaac     DMBoundary dmbound;
8137e6f8dbb6SToby Isaac 
8138e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8139e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
8140e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
8141994fe344SLisandro 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);}
814247a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8143dff059c6SToby Isaac     *lastnext = dmbound;
8144dff059c6SToby Isaac     lastnext = &(dmbound->next);
8145dff059c6SToby Isaac     dsbound = dsbound->next;
8146a6ba4734SToby Isaac   }
8147a6ba4734SToby Isaac   PetscFunctionReturn(0);
8148a6ba4734SToby Isaac }
8149a6ba4734SToby Isaac 
8150a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8151a6ba4734SToby Isaac {
8152b95f2879SToby Isaac   DMBoundary     b;
8153a6ba4734SToby Isaac   PetscErrorCode ierr;
8154a6ba4734SToby Isaac 
8155a6ba4734SToby Isaac   PetscFunctionBegin;
8156a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8157534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8158a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8159e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8160b95f2879SToby Isaac   b = dm->boundary;
8161a6ba4734SToby Isaac   while (b && !(*isBd)) {
8162e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8163e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
81643424c85cSToby Isaac 
81653424c85cSToby Isaac     if (label) {
8166a6ba4734SToby Isaac       PetscInt i;
8167a6ba4734SToby Isaac 
8168e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
8169e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
8170a6ba4734SToby Isaac       }
8171a6ba4734SToby Isaac     }
8172a6ba4734SToby Isaac     b = b->next;
8173a6ba4734SToby Isaac   }
8174a6ba4734SToby Isaac   PetscFunctionReturn(0);
8175a6ba4734SToby Isaac }
81764d6f44ffSToby Isaac 
81774d6f44ffSToby Isaac /*@C
8178a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8179a6e0b375SMatthew G. Knepley 
8180a6e0b375SMatthew G. Knepley   Collective on DM
81814d6f44ffSToby Isaac 
81824d6f44ffSToby Isaac   Input Parameters:
81834d6f44ffSToby Isaac + dm      - The DM
81840709b2feSToby Isaac . time    - The time
81854d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
81864d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
81874d6f44ffSToby Isaac - mode    - The insertion mode for values
81884d6f44ffSToby Isaac 
81894d6f44ffSToby Isaac   Output Parameter:
81904d6f44ffSToby Isaac . X - vector
81914d6f44ffSToby Isaac 
81924d6f44ffSToby Isaac    Calling sequence of func:
81930709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
81944d6f44ffSToby Isaac 
81954d6f44ffSToby Isaac +  dim - The spatial dimension
81968ec8862eSJed Brown .  time - The time at which to sample
81974d6f44ffSToby Isaac .  x   - The coordinates
81984d6f44ffSToby Isaac .  Nf  - The number of fields
81994d6f44ffSToby Isaac .  u   - The output field values
82004d6f44ffSToby Isaac -  ctx - optional user-defined function context
82014d6f44ffSToby Isaac 
82024d6f44ffSToby Isaac   Level: developer
82034d6f44ffSToby Isaac 
8204a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
82054d6f44ffSToby Isaac @*/
82060709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
82074d6f44ffSToby Isaac {
82084d6f44ffSToby Isaac   Vec            localX;
82094d6f44ffSToby Isaac   PetscErrorCode ierr;
82104d6f44ffSToby Isaac 
82114d6f44ffSToby Isaac   PetscFunctionBegin;
82124d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82134d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
82140709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
82154d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
82164d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
82174d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
82184d6f44ffSToby Isaac   PetscFunctionReturn(0);
82194d6f44ffSToby Isaac }
82204d6f44ffSToby Isaac 
8221a6e0b375SMatthew G. Knepley /*@C
8222a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8223a6e0b375SMatthew G. Knepley 
8224a6e0b375SMatthew G. Knepley   Not collective
8225a6e0b375SMatthew G. Knepley 
8226a6e0b375SMatthew G. Knepley   Input Parameters:
8227a6e0b375SMatthew G. Knepley + dm      - The DM
8228a6e0b375SMatthew G. Knepley . time    - The time
8229a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8230a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8231a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8232a6e0b375SMatthew G. Knepley 
8233a6e0b375SMatthew G. Knepley   Output Parameter:
8234a6e0b375SMatthew G. Knepley . localX - vector
8235a6e0b375SMatthew G. Knepley 
8236a6e0b375SMatthew G. Knepley    Calling sequence of func:
8237a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8238a6e0b375SMatthew G. Knepley 
8239a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8240a6e0b375SMatthew G. Knepley .  x   - The coordinates
8241a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8242a6e0b375SMatthew G. Knepley .  u   - The output field values
8243a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8244a6e0b375SMatthew G. Knepley 
8245a6e0b375SMatthew G. Knepley   Level: developer
8246a6e0b375SMatthew G. Knepley 
8247a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8248a6e0b375SMatthew G. Knepley @*/
82490709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
82504d6f44ffSToby Isaac {
82514d6f44ffSToby Isaac   PetscErrorCode ierr;
82524d6f44ffSToby Isaac 
82534d6f44ffSToby Isaac   PetscFunctionBegin;
82544d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82554d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
82560918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
82570709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
82584d6f44ffSToby Isaac   PetscFunctionReturn(0);
82594d6f44ffSToby Isaac }
82604d6f44ffSToby Isaac 
8261a6e0b375SMatthew G. Knepley /*@C
8262a6e0b375SMatthew 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.
8263a6e0b375SMatthew G. Knepley 
8264a6e0b375SMatthew G. Knepley   Collective on DM
8265a6e0b375SMatthew G. Knepley 
8266a6e0b375SMatthew G. Knepley   Input Parameters:
8267a6e0b375SMatthew G. Knepley + dm      - The DM
8268a6e0b375SMatthew G. Knepley . time    - The time
8269a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8270a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8271a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8272a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8273a6e0b375SMatthew G. Knepley 
8274a6e0b375SMatthew G. Knepley   Output Parameter:
8275a6e0b375SMatthew G. Knepley . X - vector
8276a6e0b375SMatthew G. Knepley 
8277a6e0b375SMatthew G. Knepley    Calling sequence of func:
8278a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8279a6e0b375SMatthew G. Knepley 
8280a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8281a6e0b375SMatthew G. Knepley .  x   - The coordinates
8282a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8283a6e0b375SMatthew G. Knepley .  u   - The output field values
8284a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8285a6e0b375SMatthew G. Knepley 
8286a6e0b375SMatthew G. Knepley   Level: developer
8287a6e0b375SMatthew G. Knepley 
8288a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8289a6e0b375SMatthew G. Knepley @*/
82902c53366bSMatthew 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)
82912c53366bSMatthew G. Knepley {
82922c53366bSMatthew G. Knepley   Vec            localX;
82932c53366bSMatthew G. Knepley   PetscErrorCode ierr;
82942c53366bSMatthew G. Knepley 
82952c53366bSMatthew G. Knepley   PetscFunctionBegin;
82962c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82972c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
82982c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
82992c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
83002c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
83012c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
83022c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
83032c53366bSMatthew G. Knepley }
83042c53366bSMatthew G. Knepley 
8305a6e0b375SMatthew G. Knepley /*@C
8306a6e0b375SMatthew 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.
8307a6e0b375SMatthew G. Knepley 
8308a6e0b375SMatthew G. Knepley   Not collective
8309a6e0b375SMatthew G. Knepley 
8310a6e0b375SMatthew G. Knepley   Input Parameters:
8311a6e0b375SMatthew G. Knepley + dm      - The DM
8312a6e0b375SMatthew G. Knepley . time    - The time
8313a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8314a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8315a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8316a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8317a6e0b375SMatthew G. Knepley 
8318a6e0b375SMatthew G. Knepley   Output Parameter:
8319a6e0b375SMatthew G. Knepley . localX - vector
8320a6e0b375SMatthew G. Knepley 
8321a6e0b375SMatthew G. Knepley    Calling sequence of func:
8322a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8323a6e0b375SMatthew G. Knepley 
8324a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8325a6e0b375SMatthew G. Knepley .  x   - The coordinates
8326a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8327a6e0b375SMatthew G. Knepley .  u   - The output field values
8328a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8329a6e0b375SMatthew G. Knepley 
8330a6e0b375SMatthew G. Knepley   Level: developer
8331a6e0b375SMatthew G. Knepley 
8332a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8333a6e0b375SMatthew G. Knepley @*/
83341c531cf8SMatthew 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)
83354d6f44ffSToby Isaac {
83364d6f44ffSToby Isaac   PetscErrorCode ierr;
83374d6f44ffSToby Isaac 
83384d6f44ffSToby Isaac   PetscFunctionBegin;
83394d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83404d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
83410918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
83421c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83434d6f44ffSToby Isaac   PetscFunctionReturn(0);
83444d6f44ffSToby Isaac }
83452716604bSToby Isaac 
8346a6e0b375SMatthew G. Knepley /*@C
8347a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8348a6e0b375SMatthew G. Knepley 
8349a6e0b375SMatthew G. Knepley   Not collective
8350a6e0b375SMatthew G. Knepley 
8351a6e0b375SMatthew G. Knepley   Input Parameters:
8352a6e0b375SMatthew G. Knepley + dm      - The DM
8353a6e0b375SMatthew G. Knepley . time    - The time
8354a6e0b375SMatthew G. Knepley . localU  - The input field vector
8355a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8356a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8357a6e0b375SMatthew G. Knepley 
8358a6e0b375SMatthew G. Knepley   Output Parameter:
8359a6e0b375SMatthew G. Knepley . localX  - The output vector
8360a6e0b375SMatthew G. Knepley 
8361a6e0b375SMatthew G. Knepley    Calling sequence of func:
8362a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8363a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8364a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8365a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8366a6e0b375SMatthew G. Knepley 
8367a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8368a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8369a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8370a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8371a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8372a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8373a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8374a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8375a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8376a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8377a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8378a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8379a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8380a6e0b375SMatthew G. Knepley .  t            - The current time
8381a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8382a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8383a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8384a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8385a6e0b375SMatthew G. Knepley 
8386a6e0b375SMatthew 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.
8387a6e0b375SMatthew 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
8388a6e0b375SMatthew 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
8389a6e0b375SMatthew 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.
8390a6e0b375SMatthew G. Knepley 
8391a6e0b375SMatthew G. Knepley   Level: intermediate
8392a6e0b375SMatthew G. Knepley 
8393a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8394a6e0b375SMatthew G. Knepley @*/
83958c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
83968c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
83978c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
83988c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8399191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
84008c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
84018c6c5593SMatthew G. Knepley {
84028c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
84038c6c5593SMatthew G. Knepley 
84048c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84058c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84068c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
84078c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
84080918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
84098c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
84108c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
84118c6c5593SMatthew G. Knepley }
84128c6c5593SMatthew G. Knepley 
8413a6e0b375SMatthew G. Knepley /*@C
8414a6e0b375SMatthew 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.
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 . label   - The DMLabel marking the portion of the domain to output
8422a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8423a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8424a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8425a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8426a6e0b375SMatthew G. Knepley . localU  - The input field vector
8427a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8428a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8429a6e0b375SMatthew G. Knepley 
8430a6e0b375SMatthew G. Knepley   Output Parameter:
8431a6e0b375SMatthew G. Knepley . localX  - The output vector
8432a6e0b375SMatthew G. Knepley 
8433a6e0b375SMatthew G. Knepley    Calling sequence of func:
8434a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8435a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8436a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8437a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8438a6e0b375SMatthew G. Knepley 
8439a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8440a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8441a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8442a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8443a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8444a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8445a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8446a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8447a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8448a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8449a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8450a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8451a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8452a6e0b375SMatthew G. Knepley .  t            - The current time
8453a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8454a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8455a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8456a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8457a6e0b375SMatthew G. Knepley 
8458a6e0b375SMatthew 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.
8459a6e0b375SMatthew 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
8460a6e0b375SMatthew 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
8461a6e0b375SMatthew 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.
8462a6e0b375SMatthew G. Knepley 
8463a6e0b375SMatthew G. Knepley   Level: intermediate
8464a6e0b375SMatthew G. Knepley 
8465a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8466a6e0b375SMatthew G. Knepley @*/
84671c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
84688c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
84698c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
84708c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8471191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
84728c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
84738c6c5593SMatthew G. Knepley {
84748c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
84758c6c5593SMatthew G. Knepley 
84768c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84778c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84788c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
84798c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8480ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
84811c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
84828c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
84838c6c5593SMatthew G. Knepley }
84848c6c5593SMatthew G. Knepley 
84852716604bSToby Isaac /*@C
8486ece3a9fcSMatthew 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.
8487ece3a9fcSMatthew G. Knepley 
8488ece3a9fcSMatthew G. Knepley   Not collective
8489ece3a9fcSMatthew G. Knepley 
8490ece3a9fcSMatthew G. Knepley   Input Parameters:
8491ece3a9fcSMatthew G. Knepley + dm      - The DM
8492ece3a9fcSMatthew G. Knepley . time    - The time
8493ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8494ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8495ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8496ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8497ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8498ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8499ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8500ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8501ece3a9fcSMatthew G. Knepley 
8502ece3a9fcSMatthew G. Knepley   Output Parameter:
8503ece3a9fcSMatthew G. Knepley . localX  - The output vector
8504ece3a9fcSMatthew G. Knepley 
8505ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8506ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8507ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8508ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8509ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8510ece3a9fcSMatthew G. Knepley 
8511ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8512ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8513ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8514ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8515ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8516ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8517ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8518ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8519ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8520ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8521ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8522ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8523ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8524ece3a9fcSMatthew G. Knepley .  t            - The current time
8525ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8526ece3a9fcSMatthew G. Knepley .  n            - The face normal
8527ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8528ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8529ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8530ece3a9fcSMatthew G. Knepley 
8531ece3a9fcSMatthew G. Knepley   Note:
8532ece3a9fcSMatthew 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.
8533ece3a9fcSMatthew 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
8534ece3a9fcSMatthew 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
8535ece3a9fcSMatthew 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.
8536ece3a9fcSMatthew G. Knepley 
8537ece3a9fcSMatthew G. Knepley   Level: intermediate
8538ece3a9fcSMatthew G. Knepley 
8539ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8540ece3a9fcSMatthew G. Knepley @*/
8541ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8542ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8543ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8544ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8545ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8546ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8547ece3a9fcSMatthew G. Knepley {
8548ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8549ece3a9fcSMatthew G. Knepley 
8550ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8551ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8552ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8553ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8554ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8555ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8556ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8557ece3a9fcSMatthew G. Knepley }
8558ece3a9fcSMatthew G. Knepley 
8559ece3a9fcSMatthew G. Knepley /*@C
85602716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85612716604bSToby Isaac 
85622716604bSToby Isaac   Input Parameters:
85632716604bSToby Isaac + dm    - The DM
85640709b2feSToby Isaac . time  - The time
85652716604bSToby Isaac . funcs - The functions to evaluate for each field component
85662716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8567574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85682716604bSToby Isaac 
85692716604bSToby Isaac   Output Parameter:
85702716604bSToby Isaac . diff - The diff ||u - u_h||_2
85712716604bSToby Isaac 
85722716604bSToby Isaac   Level: developer
85732716604bSToby Isaac 
85741189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
85752716604bSToby Isaac @*/
85760709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
85772716604bSToby Isaac {
85782716604bSToby Isaac   PetscErrorCode ierr;
85792716604bSToby Isaac 
85802716604bSToby Isaac   PetscFunctionBegin;
85812716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8582b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
85830918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
85840709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
85852716604bSToby Isaac   PetscFunctionReturn(0);
85862716604bSToby Isaac }
8587b698f381SToby Isaac 
8588b698f381SToby Isaac /*@C
8589b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8590b698f381SToby Isaac 
8591d083f849SBarry Smith   Collective on dm
8592d083f849SBarry Smith 
8593b698f381SToby Isaac   Input Parameters:
8594b698f381SToby Isaac + dm    - The DM
8595b698f381SToby Isaac , time  - The time
8596b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8597b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8598574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8599b698f381SToby Isaac - n     - The vector to project along
8600b698f381SToby Isaac 
8601b698f381SToby Isaac   Output Parameter:
8602b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8603b698f381SToby Isaac 
8604b698f381SToby Isaac   Level: developer
8605b698f381SToby Isaac 
8606b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8607b698f381SToby Isaac @*/
8608b698f381SToby 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)
8609b698f381SToby Isaac {
8610b698f381SToby Isaac   PetscErrorCode ierr;
8611b698f381SToby Isaac 
8612b698f381SToby Isaac   PetscFunctionBegin;
8613b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8614b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8615b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8616b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8617b698f381SToby Isaac   PetscFunctionReturn(0);
8618b698f381SToby Isaac }
8619b698f381SToby Isaac 
86202a16baeaSToby Isaac /*@C
86212a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86222a16baeaSToby Isaac 
8623d083f849SBarry Smith   Collective on dm
8624d083f849SBarry Smith 
86252a16baeaSToby Isaac   Input Parameters:
86262a16baeaSToby Isaac + dm    - The DM
86272a16baeaSToby Isaac . time  - The time
86282a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86292a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8630574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86312a16baeaSToby Isaac 
86322a16baeaSToby Isaac   Output Parameter:
86332a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86342a16baeaSToby Isaac 
86352a16baeaSToby Isaac   Level: developer
86362a16baeaSToby Isaac 
86371189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
86382a16baeaSToby Isaac @*/
86391189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
86402a16baeaSToby Isaac {
86412a16baeaSToby Isaac   PetscErrorCode ierr;
86422a16baeaSToby Isaac 
86432a16baeaSToby Isaac   PetscFunctionBegin;
86442a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86452a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
86460918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
86472a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
86482a16baeaSToby Isaac   PetscFunctionReturn(0);
86492a16baeaSToby Isaac }
86502a16baeaSToby Isaac 
8651df0b854cSToby Isaac /*@C
8652df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8653cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8654df0b854cSToby Isaac 
8655df0b854cSToby Isaac   Collective on dm
8656df0b854cSToby Isaac 
8657df0b854cSToby Isaac   Input parameters:
8658df0b854cSToby Isaac + dm - the pre-adaptation DM object
8659a1b0c543SToby Isaac - label - label with the flags
8660df0b854cSToby Isaac 
8661df0b854cSToby Isaac   Output parameters:
86620d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8663df0b854cSToby Isaac 
8664df0b854cSToby Isaac   Level: intermediate
86650d1cd5e0SMatthew G. Knepley 
86660d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8667df0b854cSToby Isaac @*/
86680d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8669df0b854cSToby Isaac {
8670df0b854cSToby Isaac   PetscErrorCode ierr;
8671df0b854cSToby Isaac 
8672df0b854cSToby Isaac   PetscFunctionBegin;
8673df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8674a1b0c543SToby Isaac   PetscValidPointer(label,2);
86750d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
86760d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
86776f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
86780d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
8679c8a6034eSMark   if (*dmAdapt) (*dmAdapt)->prealloc_only = dm->prealloc_only;  /* maybe this should go .... */
86800d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
86810d1cd5e0SMatthew G. Knepley }
86820d1cd5e0SMatthew G. Knepley 
86830d1cd5e0SMatthew G. Knepley /*@C
86840d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
86850d1cd5e0SMatthew G. Knepley 
86860d1cd5e0SMatthew G. Knepley   Input Parameters:
86870d1cd5e0SMatthew G. Knepley + dm - The DM object
86880d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
86896f25b0d8SLisandro 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_".
86900d1cd5e0SMatthew G. Knepley 
86910d1cd5e0SMatthew G. Knepley   Output Parameter:
86920d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
86930d1cd5e0SMatthew G. Knepley 
86940d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
86950d1cd5e0SMatthew G. Knepley 
86960d1cd5e0SMatthew G. Knepley   Level: advanced
86970d1cd5e0SMatthew G. Knepley 
86980d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
86990d1cd5e0SMatthew G. Knepley @*/
87000d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
87010d1cd5e0SMatthew G. Knepley {
87020d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
87030d1cd5e0SMatthew G. Knepley 
87040d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
87050d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87060d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
87070d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
87080d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
87096f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
87106f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
87110d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8712df0b854cSToby Isaac   PetscFunctionReturn(0);
8713df0b854cSToby Isaac }
8714c4088d22SMatthew G. Knepley 
8715502a2867SDave May /*@C
8716502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8717502a2867SDave May 
8718502a2867SDave May  Not Collective
8719502a2867SDave May 
8720502a2867SDave May  Input Parameter:
8721502a2867SDave May .  dm    - The DM
8722502a2867SDave May 
87230a19bb7dSprj-  Output Parameters:
87240a19bb7dSprj- +  nranks - the number of neighbours
87250a19bb7dSprj- -  ranks - the neighbors ranks
8726502a2867SDave May 
8727502a2867SDave May  Notes:
8728502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8729502a2867SDave May 
8730502a2867SDave May  Level: beginner
8731502a2867SDave May 
8732dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8733502a2867SDave May @*/
8734502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8735502a2867SDave May {
8736502a2867SDave May   PetscErrorCode ierr;
8737502a2867SDave May 
8738502a2867SDave May   PetscFunctionBegin;
8739502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87400918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8741502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8742502a2867SDave May   PetscFunctionReturn(0);
8743502a2867SDave May }
8744502a2867SDave May 
8745531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8746531c7667SBarry Smith 
8747531c7667SBarry Smith /*
8748531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8749531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8750531c7667SBarry Smith */
8751531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8752531c7667SBarry Smith {
8753531c7667SBarry Smith   PetscErrorCode ierr;
8754531c7667SBarry Smith 
8755531c7667SBarry Smith   PetscFunctionBegin;
8756531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8757531c7667SBarry Smith     Vec x1local;
8758531c7667SBarry Smith     DM  dm;
8759531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8760531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8761531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8762531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8763531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8764531c7667SBarry Smith     x1   = x1local;
8765531c7667SBarry Smith   }
8766531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8767531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8768531c7667SBarry Smith     DM  dm;
8769531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8770531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8771531c7667SBarry Smith   }
8772531c7667SBarry Smith   PetscFunctionReturn(0);
8773531c7667SBarry Smith }
8774531c7667SBarry Smith 
8775531c7667SBarry Smith /*@
8776531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8777531c7667SBarry Smith 
8778531c7667SBarry Smith     Input Parameter:
8779531c7667SBarry Smith .    coloring - the MatFDColoring object
8780531c7667SBarry Smith 
878195452b02SPatrick Sanan     Developer Notes:
878295452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8783531c7667SBarry Smith 
87841b266c99SBarry Smith     Level: advanced
87851b266c99SBarry Smith 
8786531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8787531c7667SBarry Smith @*/
8788531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8789531c7667SBarry Smith {
8790531c7667SBarry Smith   PetscFunctionBegin;
8791531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8792531c7667SBarry Smith   PetscFunctionReturn(0);
8793531c7667SBarry Smith }
87948320bc6fSPatrick Sanan 
87958320bc6fSPatrick Sanan /*@
87968320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
87978320bc6fSPatrick Sanan 
87988320bc6fSPatrick Sanan     Collective
87998320bc6fSPatrick Sanan 
88008320bc6fSPatrick Sanan     Input Parameters:
8801a5bc1bf3SBarry Smith +    dm1 - the first DM
88028320bc6fSPatrick Sanan -    dm2 - the second DM
88038320bc6fSPatrick Sanan 
88048320bc6fSPatrick Sanan     Output Parameters:
88058320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
88068320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
88078320bc6fSPatrick Sanan 
88088320bc6fSPatrick Sanan     Notes:
88098320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
88103d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
88118320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
88123d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
88133d862458SPatrick Sanan     decomposition, but hold different data.
88148320bc6fSPatrick Sanan 
88158320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
88168320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
88178320bc6fSPatrick Sanan 
88188320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
88198320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
88208320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
88218320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
88228320bc6fSPatrick Sanan 
88238320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
88248320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
88258320bc6fSPatrick Sanan .vb
88268320bc6fSPatrick Sanan   ...
88278320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
88288320bc6fSPatrick Sanan   if (set && compatible)  {
88298320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
88308320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
88313d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
88328320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
88338320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
88348320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
88358320bc6fSPatrick Sanan       }
88368320bc6fSPatrick Sanan     }
88378320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
88388320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
88398320bc6fSPatrick Sanan   } else {
88408320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
88418320bc6fSPatrick Sanan   }
88428320bc6fSPatrick Sanan   ...
88438320bc6fSPatrick Sanan .ve
88448320bc6fSPatrick Sanan 
88458320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
88468320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
88478320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
88488320bc6fSPatrick Sanan     always check the "set" output parameter.
88498320bc6fSPatrick Sanan 
88508320bc6fSPatrick Sanan     A DM is always compatible with itself.
88518320bc6fSPatrick Sanan 
88528320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
88538320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
88548320bc6fSPatrick Sanan     incompatible.
88558320bc6fSPatrick Sanan 
88568320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
88578320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
88588320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
88598320bc6fSPatrick Sanan 
88608320bc6fSPatrick Sanan     Developer Notes:
88613d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
88623d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8863a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
88648320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
88658320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
88663d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
88673d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
88688320bc6fSPatrick Sanan 
88698320bc6fSPatrick Sanan     Level: advanced
88708320bc6fSPatrick Sanan 
88713d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
88728320bc6fSPatrick Sanan @*/
88738320bc6fSPatrick Sanan 
8874a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
88758320bc6fSPatrick Sanan {
88768320bc6fSPatrick Sanan   PetscErrorCode ierr;
88778320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
88788320bc6fSPatrick Sanan   DMType         type,type2;
88798320bc6fSPatrick Sanan   PetscBool      sameType;
88808320bc6fSPatrick Sanan 
88818320bc6fSPatrick Sanan   PetscFunctionBegin;
8882a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
88838320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
88848320bc6fSPatrick Sanan 
88858320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8886a5bc1bf3SBarry Smith   if (dm1 == dm2) {
88878320bc6fSPatrick Sanan     *set = PETSC_TRUE;
88888320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
88898320bc6fSPatrick Sanan     PetscFunctionReturn(0);
88908320bc6fSPatrick Sanan   }
88918320bc6fSPatrick Sanan 
88928320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
88938320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
88948320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
88958320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8896a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
88978320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
88988320bc6fSPatrick Sanan     *set = PETSC_TRUE;
88998320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
89008320bc6fSPatrick Sanan     PetscFunctionReturn(0);
89018320bc6fSPatrick Sanan   }
89028320bc6fSPatrick Sanan 
89038320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8904a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8905a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8906b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
89078320bc6fSPatrick Sanan   }
89088320bc6fSPatrick Sanan 
8909a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
89108320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8911a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
89128320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
89138320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
89148320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8915a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
89168320bc6fSPatrick Sanan   } else {
89178320bc6fSPatrick Sanan     *set = PETSC_FALSE;
89188320bc6fSPatrick Sanan   }
89198320bc6fSPatrick Sanan   PetscFunctionReturn(0);
89208320bc6fSPatrick Sanan }
8921c0f0dcc3SMatthew G. Knepley 
8922c0f0dcc3SMatthew G. Knepley /*@C
8923c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8924c0f0dcc3SMatthew G. Knepley 
8925c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8926c0f0dcc3SMatthew G. Knepley 
8927c0f0dcc3SMatthew G. Knepley   Input Parameters:
8928c0f0dcc3SMatthew G. Knepley + DM - the DM
8929c0f0dcc3SMatthew G. Knepley . f - the monitor function
8930c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8931c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8932c0f0dcc3SMatthew G. Knepley 
8933c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8934c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8935c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8936c0f0dcc3SMatthew G. Knepley 
8937c0f0dcc3SMatthew G. Knepley   Notes:
8938c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8939c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8940c0f0dcc3SMatthew G. Knepley   order in which they were set.
8941c0f0dcc3SMatthew G. Knepley 
8942c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8943c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8944c0f0dcc3SMatthew G. Knepley 
8945c0f0dcc3SMatthew G. Knepley   Level: intermediate
8946c0f0dcc3SMatthew G. Knepley 
8947c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8948c0f0dcc3SMatthew G. Knepley @*/
8949c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8950c0f0dcc3SMatthew G. Knepley {
8951c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8952c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8953c0f0dcc3SMatthew G. Knepley 
8954c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8955c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8956c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8957c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8958c0f0dcc3SMatthew G. Knepley 
8959c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8960c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8961c0f0dcc3SMatthew G. Knepley   }
8962c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8963c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8964c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8965c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8966c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8967c0f0dcc3SMatthew G. Knepley }
8968c0f0dcc3SMatthew G. Knepley 
8969c0f0dcc3SMatthew G. Knepley /*@
8970c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8971c0f0dcc3SMatthew G. Knepley 
8972c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8973c0f0dcc3SMatthew G. Knepley 
8974c0f0dcc3SMatthew G. Knepley   Input Parameter:
8975c0f0dcc3SMatthew G. Knepley . dm - the DM
8976c0f0dcc3SMatthew G. Knepley 
8977c0f0dcc3SMatthew G. Knepley   Options Database Key:
8978c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8979c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8980c0f0dcc3SMatthew G. Knepley   set via the options database
8981c0f0dcc3SMatthew G. Knepley 
8982c0f0dcc3SMatthew G. Knepley   Notes:
8983c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8984c0f0dcc3SMatthew G. Knepley 
8985c0f0dcc3SMatthew G. Knepley   Level: intermediate
8986c0f0dcc3SMatthew G. Knepley 
8987c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8988c0f0dcc3SMatthew G. Knepley @*/
8989c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8990c0f0dcc3SMatthew G. Knepley {
8991c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8992c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8993c0f0dcc3SMatthew G. Knepley 
8994c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8995c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8996c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8997c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
8998c0f0dcc3SMatthew G. Knepley   }
8999c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9000c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9001c0f0dcc3SMatthew G. Knepley }
9002c0f0dcc3SMatthew G. Knepley 
9003c0f0dcc3SMatthew G. Knepley /*@C
9004c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9005c0f0dcc3SMatthew G. Knepley 
9006c0f0dcc3SMatthew G. Knepley   Collective on DM
9007c0f0dcc3SMatthew G. Knepley 
9008c0f0dcc3SMatthew G. Knepley   Input Parameters:
9009c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9010c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9011c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9012c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9013c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9014c0f0dcc3SMatthew 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
9015c0f0dcc3SMatthew G. Knepley 
9016c0f0dcc3SMatthew G. Knepley   Output Parameter:
9017c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9018c0f0dcc3SMatthew G. Knepley 
9019c0f0dcc3SMatthew G. Knepley   Level: developer
9020c0f0dcc3SMatthew G. Knepley 
9021c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9022c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9023c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9024c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9025c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9026c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9027c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9028c0f0dcc3SMatthew G. Knepley @*/
9029c0f0dcc3SMatthew 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)
9030c0f0dcc3SMatthew G. Knepley {
9031c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9032c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9033c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9034c0f0dcc3SMatthew G. Knepley 
9035c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9036c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9037c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9038c0f0dcc3SMatthew G. Knepley   if (*flg) {
9039c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9040c0f0dcc3SMatthew G. Knepley 
9041c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9042c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9043c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9044c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9045c0f0dcc3SMatthew G. Knepley   }
9046c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9047c0f0dcc3SMatthew G. Knepley }
9048c0f0dcc3SMatthew G. Knepley 
9049c0f0dcc3SMatthew G. Knepley /*@
9050c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9051c0f0dcc3SMatthew G. Knepley 
9052c0f0dcc3SMatthew G. Knepley    Collective on DM
9053c0f0dcc3SMatthew G. Knepley 
9054c0f0dcc3SMatthew G. Knepley    Input Parameters:
9055c0f0dcc3SMatthew G. Knepley .  dm - The DM
9056c0f0dcc3SMatthew G. Knepley 
9057c0f0dcc3SMatthew G. Knepley    Level: developer
9058c0f0dcc3SMatthew G. Knepley 
9059c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9060c0f0dcc3SMatthew G. Knepley @*/
9061c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9062c0f0dcc3SMatthew G. Knepley {
9063c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9064c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9065c0f0dcc3SMatthew G. Knepley 
9066c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9067c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9068c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9069c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9070c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9071c0f0dcc3SMatthew G. Knepley   }
9072c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9073c0f0dcc3SMatthew G. Knepley }
9074