xref: /petsc/src/dm/interface/dm.c (revision e0b68406e04bbc41e6819f8c0ff59eea3b2c814e)
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);
4500b0c7db22SLisandro 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];
4579*e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
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 /*@
4778*e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4779*e0b68406SMatthew Knepley 
4780*e0b68406SMatthew Knepley   Logically collective on dm
4781*e0b68406SMatthew Knepley 
4782*e0b68406SMatthew Knepley   Input Parameters:
4783*e0b68406SMatthew Knepley + dm          - The DM
4784*e0b68406SMatthew Knepley . f           - The field index
4785*e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4786*e0b68406SMatthew Knepley 
4787*e0b68406SMatthew Knepley   Level: intermediate
4788*e0b68406SMatthew Knepley 
4789*e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
4790*e0b68406SMatthew Knepley @*/
4791*e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4792*e0b68406SMatthew Knepley {
4793*e0b68406SMatthew Knepley   PetscFunctionBegin;
4794*e0b68406SMatthew Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
4795*e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4796*e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4797*e0b68406SMatthew Knepley }
4798*e0b68406SMatthew Knepley 
4799*e0b68406SMatthew Knepley /*@
4800*e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4801*e0b68406SMatthew Knepley 
4802*e0b68406SMatthew Knepley   Logically collective on dm
4803*e0b68406SMatthew Knepley 
4804*e0b68406SMatthew Knepley   Input Parameters:
4805*e0b68406SMatthew Knepley + dm          - The DM
4806*e0b68406SMatthew Knepley - f           - The field index
4807*e0b68406SMatthew Knepley 
4808*e0b68406SMatthew Knepley   Output Parameter:
4809*e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4810*e0b68406SMatthew Knepley 
4811*e0b68406SMatthew Knepley   Level: intermediate
4812*e0b68406SMatthew Knepley 
4813*e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
4814*e0b68406SMatthew Knepley @*/
4815*e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4816*e0b68406SMatthew Knepley {
4817*e0b68406SMatthew Knepley   PetscFunctionBegin;
4818*e0b68406SMatthew Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
4819*e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4820*e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4821*e0b68406SMatthew Knepley }
4822*e0b68406SMatthew Knepley 
4823*e0b68406SMatthew Knepley /*@
4824e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4825e5e52638SMatthew G. Knepley 
4826d083f849SBarry Smith   Collective on dm
4827e5e52638SMatthew G. Knepley 
4828e5e52638SMatthew G. Knepley   Input Parameter:
4829e5e52638SMatthew G. Knepley . dm - The DM
4830e5e52638SMatthew G. Knepley 
4831e5e52638SMatthew G. Knepley   Output Parameter:
4832e5e52638SMatthew G. Knepley . newdm - The DM
4833e5e52638SMatthew G. Knepley 
4834e5e52638SMatthew G. Knepley   Level: advanced
4835e5e52638SMatthew G. Knepley 
4836e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4837e5e52638SMatthew G. Knepley @*/
4838e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4839e5e52638SMatthew G. Knepley {
4840e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4841e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4842e5e52638SMatthew G. Knepley 
4843e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4844e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4845e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4846e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4847e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4848e5e52638SMatthew G. Knepley     DMLabel     label;
4849e5e52638SMatthew G. Knepley     PetscObject field;
485034aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4851e5e52638SMatthew G. Knepley 
4852e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4853e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
485434aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
485534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
485634aa8a36SMatthew G. Knepley   }
485734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
485834aa8a36SMatthew G. Knepley }
485934aa8a36SMatthew G. Knepley 
486034aa8a36SMatthew G. Knepley /*@
486134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
486234aa8a36SMatthew G. Knepley 
486334aa8a36SMatthew G. Knepley   Not collective
486434aa8a36SMatthew G. Knepley 
486534aa8a36SMatthew G. Knepley   Input Parameters:
486634aa8a36SMatthew G. Knepley + dm - The DM object
486734aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
486834aa8a36SMatthew G. Knepley 
486934aa8a36SMatthew G. Knepley   Output Parameter:
487034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
487134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
487234aa8a36SMatthew G. Knepley 
487334aa8a36SMatthew G. Knepley   Notes:
487434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
487534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
487634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4877979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
487834aa8a36SMatthew G. Knepley 
487934aa8a36SMatthew G. Knepley   Level: developer
488034aa8a36SMatthew G. Knepley 
488134aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
488234aa8a36SMatthew G. Knepley @*/
488334aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
488434aa8a36SMatthew G. Knepley {
488534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
488634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4887534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4888534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
488934aa8a36SMatthew G. Knepley   if (f < 0) {
489034aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
489134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
489234aa8a36SMatthew G. Knepley   } else {
489334aa8a36SMatthew G. Knepley     PetscInt       Nf;
489434aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
489534aa8a36SMatthew G. Knepley 
489634aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
489734aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
489834aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
489934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
490034aa8a36SMatthew G. Knepley   }
490134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
490234aa8a36SMatthew G. Knepley }
490334aa8a36SMatthew G. Knepley 
490434aa8a36SMatthew G. Knepley /*@
490534aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
490634aa8a36SMatthew G. Knepley 
490734aa8a36SMatthew G. Knepley   Not collective
490834aa8a36SMatthew G. Knepley 
490934aa8a36SMatthew G. Knepley   Input Parameters:
491034aa8a36SMatthew G. Knepley + dm         - The DM object
491134aa8a36SMatthew G. Knepley . f          - The field number
491234aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
491334aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
491434aa8a36SMatthew G. Knepley 
491534aa8a36SMatthew G. Knepley   Notes:
491634aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
491734aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
491834aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4919979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
492034aa8a36SMatthew G. Knepley 
492134aa8a36SMatthew G. Knepley   Level: developer
492234aa8a36SMatthew G. Knepley 
492334aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
492434aa8a36SMatthew G. Knepley @*/
492534aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
492634aa8a36SMatthew G. Knepley {
492734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
492834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
492934aa8a36SMatthew G. Knepley   if (f < 0) {
493034aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
493134aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
493234aa8a36SMatthew G. Knepley   } else {
493334aa8a36SMatthew G. Knepley     PetscInt       Nf;
493434aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
493534aa8a36SMatthew G. Knepley 
493634aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
493734aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
493834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
493934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4940e5e52638SMatthew G. Knepley   }
4941e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4942e5e52638SMatthew G. Knepley }
4943e5e52638SMatthew G. Knepley 
4944b0441da4SMatthew G. Knepley /*@
4945b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4946b0441da4SMatthew G. Knepley 
4947b0441da4SMatthew G. Knepley   Not collective
4948b0441da4SMatthew G. Knepley 
4949b0441da4SMatthew G. Knepley   Input Parameters:
4950b0441da4SMatthew G. Knepley . dm - The DM object
4951b0441da4SMatthew G. Knepley 
4952b0441da4SMatthew G. Knepley   Output Parameter:
4953b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4954b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4955b0441da4SMatthew G. Knepley 
4956b0441da4SMatthew G. Knepley   Notes:
4957b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4958b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4959b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4960b0441da4SMatthew G. Knepley 
4961b0441da4SMatthew G. Knepley   Level: developer
4962b0441da4SMatthew G. Knepley 
4963b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4964b0441da4SMatthew G. Knepley @*/
4965b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4966b0441da4SMatthew G. Knepley {
4967b0441da4SMatthew G. Knepley   PetscInt       Nf;
4968b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4969b0441da4SMatthew G. Knepley 
4970b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4971b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4972534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4973534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4974b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4975b0441da4SMatthew G. Knepley   if (!Nf) {
4976b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4977b0441da4SMatthew G. Knepley   } else {
4978b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4979b0441da4SMatthew G. Knepley   }
4980b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4981b0441da4SMatthew G. Knepley }
4982b0441da4SMatthew G. Knepley 
4983b0441da4SMatthew G. Knepley /*@
4984b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4985b0441da4SMatthew G. Knepley 
4986b0441da4SMatthew G. Knepley   Not collective
4987b0441da4SMatthew G. Knepley 
4988b0441da4SMatthew G. Knepley   Input Parameters:
4989b0441da4SMatthew G. Knepley + dm         - The DM object
4990b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4991b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4992b0441da4SMatthew G. Knepley 
4993b0441da4SMatthew G. Knepley   Notes:
4994b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4995b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4996b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4997b0441da4SMatthew G. Knepley 
4998b0441da4SMatthew G. Knepley   Level: developer
4999b0441da4SMatthew G. Knepley 
5000b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5001b0441da4SMatthew G. Knepley @*/
5002b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5003b0441da4SMatthew G. Knepley {
5004b0441da4SMatthew G. Knepley   PetscInt       Nf;
5005b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5006b0441da4SMatthew G. Knepley 
5007b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5008b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5009b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5010b0441da4SMatthew G. Knepley   if (!Nf) {
5011b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5012b0441da4SMatthew G. Knepley   } else {
5013b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5014e5e52638SMatthew G. Knepley   }
5015e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5016e5e52638SMatthew G. Knepley }
5017e5e52638SMatthew G. Knepley 
5018783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
5019783e2ec8SMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, const char labelname[])
5020783e2ec8SMatthew G. Knepley {
5021783e2ec8SMatthew G. Knepley   DMLabel        label;
5022783e2ec8SMatthew G. Knepley   PetscObject    obj;
5023783e2ec8SMatthew G. Knepley   PetscClassId   id;
5024783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5025783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5026783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5027783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5028783e2ec8SMatthew G. Knepley 
5029783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5030783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5031783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5032783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5033783e2ec8SMatthew G. Knepley   ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
5034783e2ec8SMatthew G. Knepley   if (isFE && label) {
5035783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5036783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5037783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
5038783e2ec8SMatthew G. Knepley       const char *lname;
5039783e2ec8SMatthew G. Knepley 
504056cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, &lname, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5041783e2ec8SMatthew G. Knepley       ierr = PetscStrcmp(lname, labelname, &duplicate);CHKERRQ(ierr);
5042783e2ec8SMatthew G. Knepley       if (duplicate) break;
5043783e2ec8SMatthew G. Knepley     }
5044783e2ec8SMatthew G. Knepley     if (!duplicate) {
5045783e2ec8SMatthew G. Knepley       DM plex;
5046783e2ec8SMatthew G. Knepley 
5047783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5048783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5049783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5050783e2ec8SMatthew G. Knepley     }
5051783e2ec8SMatthew G. Knepley   }
5052783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5053783e2ec8SMatthew G. Knepley }
5054783e2ec8SMatthew G. Knepley 
5055e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5056e5e52638SMatthew G. Knepley {
5057e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5058e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5059e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5060e5e52638SMatthew G. Knepley 
5061e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5062e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5063e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5064e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5065b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5066e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5067e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5068e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5069e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5070e5e52638SMatthew G. Knepley }
5071e5e52638SMatthew G. Knepley 
5072e5e52638SMatthew G. Knepley /*@
5073e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5074e5e52638SMatthew G. Knepley 
5075e5e52638SMatthew G. Knepley   Not collective
5076e5e52638SMatthew G. Knepley 
5077e5e52638SMatthew G. Knepley   Input Parameter:
5078e5e52638SMatthew G. Knepley . dm - The DM
5079e5e52638SMatthew G. Knepley 
5080e5e52638SMatthew G. Knepley   Output Parameter:
5081e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5082e5e52638SMatthew G. Knepley 
5083e5e52638SMatthew G. Knepley   Level: intermediate
5084e5e52638SMatthew G. Knepley 
5085e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5086e5e52638SMatthew G. Knepley @*/
5087e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5088e5e52638SMatthew G. Knepley {
5089e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5090e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5091534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5092e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5093e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5094e5e52638SMatthew G. Knepley }
5095e5e52638SMatthew G. Knepley 
5096e5e52638SMatthew G. Knepley /*@
5097e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5098e5e52638SMatthew G. Knepley 
5099d083f849SBarry Smith   Logically collective on dm
5100e5e52638SMatthew G. Knepley 
5101e5e52638SMatthew G. Knepley   Input Parameter:
5102e5e52638SMatthew G. Knepley . dm - The DM
5103e5e52638SMatthew G. Knepley 
5104e5e52638SMatthew G. Knepley   Level: intermediate
5105e5e52638SMatthew G. Knepley 
5106e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5107e5e52638SMatthew G. Knepley @*/
5108e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5109e5e52638SMatthew G. Knepley {
5110e5e52638SMatthew G. Knepley   PetscInt       s;
5111e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5112e5e52638SMatthew G. Knepley 
5113e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5114e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5115e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5116e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5117e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5118b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5119e5e52638SMatthew G. Knepley   }
5120e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5121e5e52638SMatthew G. Knepley   dm->probs = NULL;
5122e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5123e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5124e5e52638SMatthew G. Knepley }
5125e5e52638SMatthew G. Knepley 
5126e5e52638SMatthew G. Knepley /*@
5127e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5128e5e52638SMatthew G. Knepley 
5129e5e52638SMatthew G. Knepley   Not collective
5130e5e52638SMatthew G. Knepley 
5131e5e52638SMatthew G. Knepley   Input Parameter:
5132e5e52638SMatthew G. Knepley . dm    - The DM
5133e5e52638SMatthew G. Knepley 
5134e5e52638SMatthew G. Knepley   Output Parameter:
5135e5e52638SMatthew G. Knepley . prob - The default PetscDS
5136e5e52638SMatthew G. Knepley 
5137e5e52638SMatthew G. Knepley   Level: intermediate
5138e5e52638SMatthew G. Knepley 
5139e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5140e5e52638SMatthew G. Knepley @*/
5141e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5142e5e52638SMatthew G. Knepley {
5143b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5144b0143b4dSMatthew G. Knepley 
5145e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5146e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5147e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5148b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5149b0143b4dSMatthew G. Knepley     PetscDS ds;
5150b0143b4dSMatthew G. Knepley 
5151b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5152b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5153b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5154b0143b4dSMatthew G. Knepley   }
5155b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5156e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5157e5e52638SMatthew G. Knepley }
5158e5e52638SMatthew G. Knepley 
5159e5e52638SMatthew G. Knepley /*@
5160e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5161e5e52638SMatthew G. Knepley 
5162e5e52638SMatthew G. Knepley   Not collective
5163e5e52638SMatthew G. Knepley 
5164e5e52638SMatthew G. Knepley   Input Parameters:
5165e5e52638SMatthew G. Knepley + dm    - The DM
5166e5e52638SMatthew G. Knepley - point - Cell for the DS
5167e5e52638SMatthew G. Knepley 
5168e5e52638SMatthew G. Knepley   Output Parameter:
5169e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5170e5e52638SMatthew G. Knepley 
5171e5e52638SMatthew G. Knepley   Level: developer
5172e5e52638SMatthew G. Knepley 
5173b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5174e5e52638SMatthew G. Knepley @*/
5175e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5176e5e52638SMatthew G. Knepley {
5177e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5178e5e52638SMatthew G. Knepley   PetscInt       s;
5179e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5180e5e52638SMatthew G. Knepley 
5181e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5182e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5183e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5184360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5185e5e52638SMatthew G. Knepley   *prob = NULL;
5186e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5187e5e52638SMatthew G. Knepley     PetscInt val;
5188e5e52638SMatthew G. Knepley 
5189e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5190e5e52638SMatthew G. Knepley     else {
5191e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5192e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5193e5e52638SMatthew G. Knepley     }
5194e5e52638SMatthew G. Knepley   }
5195e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5196e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5197e5e52638SMatthew G. Knepley }
5198e5e52638SMatthew G. Knepley 
5199e5e52638SMatthew G. Knepley /*@
5200e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5201e5e52638SMatthew G. Knepley 
5202e5e52638SMatthew G. Knepley   Not collective
5203e5e52638SMatthew G. Knepley 
5204e5e52638SMatthew G. Knepley   Input Parameters:
5205e5e52638SMatthew G. Knepley + dm    - The DM
5206e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5207e5e52638SMatthew G. Knepley 
5208b3cf3223SMatthew G. Knepley   Output Parameters:
5209b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5210b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5211e5e52638SMatthew G. Knepley 
5212e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5213e5e52638SMatthew G. Knepley 
5214e5e52638SMatthew G. Knepley   Level: advanced
5215e5e52638SMatthew G. Knepley 
5216e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5217e5e52638SMatthew G. Knepley @*/
5218b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5219e5e52638SMatthew G. Knepley {
5220e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5221e5e52638SMatthew G. Knepley 
5222e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5223e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5224e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5225b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5226b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5227e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5228b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5229b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5230b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5231b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5232b3cf3223SMatthew G. Knepley     }
5233e5e52638SMatthew G. Knepley   }
52342df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5235e5e52638SMatthew G. Knepley }
5236e5e52638SMatthew G. Knepley 
5237e5e52638SMatthew G. Knepley /*@
5238083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5239083401c6SMatthew G. Knepley 
5240083401c6SMatthew G. Knepley   Collective on dm
5241083401c6SMatthew G. Knepley 
5242083401c6SMatthew G. Knepley   Input Parameters:
5243083401c6SMatthew G. Knepley + dm     - The DM
5244083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5245083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5246083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5247083401c6SMatthew G. Knepley 
5248083401c6SMatthew 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,
5249083401c6SMatthew G. Knepley   the fields argument is ignored.
5250083401c6SMatthew G. Knepley 
5251083401c6SMatthew G. Knepley   Level: advanced
5252083401c6SMatthew G. Knepley 
5253083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5254083401c6SMatthew G. Knepley @*/
5255083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5256083401c6SMatthew G. Knepley {
5257083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5258083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5259083401c6SMatthew G. Knepley 
5260083401c6SMatthew G. Knepley   PetscFunctionBegin;
5261083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5262083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5263083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5264083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5265083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5266083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5267083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5268083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5269083401c6SMatthew G. Knepley     }
5270083401c6SMatthew G. Knepley   }
5271083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5272083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5273083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5274083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5275083401c6SMatthew G. Knepley   if (!label) {
5276083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5277083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5278083401c6SMatthew G. Knepley     Nds = 0;
5279083401c6SMatthew G. Knepley   }
5280083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5281083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5282083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5283083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5284083401c6SMatthew G. Knepley }
5285083401c6SMatthew G. Knepley 
5286083401c6SMatthew G. Knepley /*@
5287e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5288e5e52638SMatthew G. Knepley 
5289e5e52638SMatthew G. Knepley   Not collective
5290e5e52638SMatthew G. Knepley 
5291e5e52638SMatthew G. Knepley   Input Parameters:
5292e5e52638SMatthew G. Knepley + dm  - The DM
5293e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5294e5e52638SMatthew G. Knepley 
5295e5e52638SMatthew G. Knepley   Output Parameters:
5296b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5297b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5298083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5299e5e52638SMatthew G. Knepley 
5300e5e52638SMatthew G. Knepley   Level: advanced
5301e5e52638SMatthew G. Knepley 
5302e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5303e5e52638SMatthew G. Knepley @*/
5304b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5305e5e52638SMatthew G. Knepley {
5306e5e52638SMatthew G. Knepley   PetscInt       Nds;
5307e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5308e5e52638SMatthew G. Knepley 
5309e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5310e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5311e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5312e5e52638SMatthew 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);
5313e5e52638SMatthew G. Knepley   if (label) {
5314e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5315e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5316e5e52638SMatthew G. Knepley   }
5317b3cf3223SMatthew G. Knepley   if (fields) {
5318b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5319b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5320b3cf3223SMatthew G. Knepley   }
5321e5e52638SMatthew G. Knepley   if (ds) {
5322b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5323e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5324e5e52638SMatthew G. Knepley   }
5325e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5326e5e52638SMatthew G. Knepley }
5327e5e52638SMatthew G. Knepley 
5328e5e52638SMatthew G. Knepley /*@
5329083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5330e5e52638SMatthew G. Knepley 
5331083401c6SMatthew G. Knepley   Not collective
5332e5e52638SMatthew G. Knepley 
5333e5e52638SMatthew G. Knepley   Input Parameters:
5334e5e52638SMatthew G. Knepley + dm     - The DM
5335083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5336083401c6SMatthew G. Knepley . label  - The region label, or NULL
5337083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5338083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5339e5e52638SMatthew G. Knepley 
5340e5e52638SMatthew G. Knepley   Level: advanced
5341e5e52638SMatthew G. Knepley 
5342083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5343e5e52638SMatthew G. Knepley @*/
5344083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5345e5e52638SMatthew G. Knepley {
5346083401c6SMatthew G. Knepley   PetscInt       Nds;
5347e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5348e5e52638SMatthew G. Knepley 
5349e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5350e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5351083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5352083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5353083401c6SMatthew 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);
5354e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5355083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5356083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5357083401c6SMatthew G. Knepley   if (fields) {
5358083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5359b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5360083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5361083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5362e5e52638SMatthew G. Knepley   }
5363083401c6SMatthew G. Knepley   if (ds) {
5364083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5365083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5366083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5367083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5368083401c6SMatthew G. Knepley   }
5369e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5370e5e52638SMatthew G. Knepley }
5371e5e52638SMatthew G. Knepley 
5372e5e52638SMatthew G. Knepley /*@
53731d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
53741d3af9e0SMatthew G. Knepley 
53751d3af9e0SMatthew G. Knepley   Not collective
53761d3af9e0SMatthew G. Knepley 
53771d3af9e0SMatthew G. Knepley   Input Parameters:
53781d3af9e0SMatthew G. Knepley + dm  - The DM
53791d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
53801d3af9e0SMatthew G. Knepley 
53811d3af9e0SMatthew G. Knepley   Output Parameter:
53821d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
53831d3af9e0SMatthew G. Knepley 
53841d3af9e0SMatthew G. Knepley   Level: advanced
53851d3af9e0SMatthew G. Knepley 
53861d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
53871d3af9e0SMatthew G. Knepley @*/
53881d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
53891d3af9e0SMatthew G. Knepley {
53901d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
53911d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
53921d3af9e0SMatthew G. Knepley 
53931d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
53941d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53951d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
53961d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
53971d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
53981d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
53991d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
54001d3af9e0SMatthew G. Knepley   else          *num = n;
54011d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
54021d3af9e0SMatthew G. Knepley }
54031d3af9e0SMatthew G. Knepley 
54041d3af9e0SMatthew G. Knepley /*@
5405e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5406e5e52638SMatthew G. Knepley 
5407d083f849SBarry Smith   Collective on dm
5408e5e52638SMatthew G. Knepley 
5409e5e52638SMatthew G. Knepley   Input Parameter:
5410e5e52638SMatthew G. Knepley . dm - The DM
5411e5e52638SMatthew G. Knepley 
5412e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5413e5e52638SMatthew G. Knepley 
5414e5e52638SMatthew G. Knepley   Level: intermediate
5415e5e52638SMatthew G. Knepley 
5416e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5417e5e52638SMatthew G. Knepley @*/
5418e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5419e5e52638SMatthew G. Knepley {
5420e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5421083401c6SMatthew G. Knepley   PetscDS        dsDef;
5422083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5423083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5424e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5425e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5426e5e52638SMatthew G. Knepley 
5427e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5428e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5429e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5430e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5431083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5432083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5433083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5434083401c6SMatthew G. Knepley   Nl   = 0;
5435083401c6SMatthew G. Knepley   Ndef = 0;
5436083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5437083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5438083401c6SMatthew G. Knepley     PetscInt l;
5439083401c6SMatthew G. Knepley 
5440083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5441083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5442083401c6SMatthew G. Knepley     if (l < Nl) continue;
5443083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5444083401c6SMatthew G. Knepley   }
5445083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5446083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5447083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5448b3cf3223SMatthew G. Knepley     IS        fields;
5449b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5450b3cf3223SMatthew G. Knepley 
5451b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5452083401c6SMatthew G. Knepley     if (nf) {
5453b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5454b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
545588f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
545688f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
545788f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
545888f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
545988f0c812SMatthew G. Knepley 
5460083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5461083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5462083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5463b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
54642df9ee95SMatthew G. Knepley     }
5465083401c6SMatthew G. Knepley   }
5466083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5467083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5468083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5469083401c6SMatthew G. Knepley   if (Ndef && Nl) {
54700122748bSMatthew G. Knepley     DM              plex;
5471083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5472083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5473083401c6SMatthew G. Knepley     PetscInt       *fields;
5474083401c6SMatthew G. Knepley     const PetscInt *cells;
5475083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
54760122748bSMatthew G. Knepley 
54770122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
54780122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5479083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5480083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5481083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5482083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5483083401c6SMatthew G. Knepley       IS      pointIS;
5484083401c6SMatthew G. Knepley 
5485083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5486083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5487083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5488083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5489083401c6SMatthew G. Knepley     }
5490083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5491083401c6SMatthew G. Knepley 
5492083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5493083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5494083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5495083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5496083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5497083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5498083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5499083401c6SMatthew G. Knepley 
5500083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5501083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5502083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5503083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5504083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5505083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5506083401c6SMatthew G. Knepley 
5507083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5508083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5509083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5510083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5511083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5512083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
55130122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5514083401c6SMatthew G. Knepley   }
5515083401c6SMatthew G. Knepley   /* Create label DSes
5516083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5517083401c6SMatthew G. Knepley   */
5518083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5519083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5520083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5521083401c6SMatthew G. Knepley     PetscDS   ds;
5522083401c6SMatthew G. Knepley     IS        fields;
5523083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5524083401c6SMatthew G. Knepley 
5525083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5526083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5527083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5528083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5529083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5530083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5531083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5532083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5533083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5534083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5535083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5536083401c6SMatthew G. Knepley     {
5537083401c6SMatthew G. Knepley       DMPolytopeType ct;
5538083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5539083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
55400122748bSMatthew G. Knepley 
5541e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5542665f567fSMatthew G. Knepley       if (lStart >= 0) {
5543412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5544412e9a14SMatthew G. Knepley         switch (ct) {
5545412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5546412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5547412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5548412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5549083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5550083401c6SMatthew G. Knepley           default: break;
5551412e9a14SMatthew G. Knepley         }
5552665f567fSMatthew G. Knepley       }
5553083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5554083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5555e5e52638SMatthew G. Knepley     }
5556083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5557e5e52638SMatthew G. Knepley   }
5558083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5559e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5560083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5561083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5562083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5563083401c6SMatthew G. Knepley     const PetscInt *fld;
5564083401c6SMatthew G. Knepley     PetscInt        nf;
5565e5e52638SMatthew G. Knepley 
5566083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5567083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5568083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5569083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5570083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5571e5e52638SMatthew G. Knepley       PetscClassId id;
5572e5e52638SMatthew G. Knepley 
5573083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5574083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5575083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5576083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5577083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5578e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5579e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5580e5e52638SMatthew G. Knepley     }
5581083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5582e5e52638SMatthew G. Knepley   }
5583e5e52638SMatthew G. Knepley   /* Setup DSes */
5584e5e52638SMatthew G. Knepley   if (doSetup) {
5585e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5586e5e52638SMatthew G. Knepley   }
5587e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5588e5e52638SMatthew G. Knepley }
5589e5e52638SMatthew G. Knepley 
5590e5e52638SMatthew G. Knepley /*@
55917f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
55927f96f943SMatthew G. Knepley 
5593f2cacb80SMatthew G. Knepley   Collective on DM
5594f2cacb80SMatthew G. Knepley 
55957f96f943SMatthew G. Knepley   Input Parameters:
55967f96f943SMatthew G. Knepley + dm   - The DM
55977f96f943SMatthew G. Knepley - time - The time
55987f96f943SMatthew G. Knepley 
55997f96f943SMatthew G. Knepley   Output Parameters:
5600f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5601f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
56027f96f943SMatthew G. Knepley 
56037f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
56047f96f943SMatthew G. Knepley 
56057f96f943SMatthew G. Knepley   Level: developer
56067f96f943SMatthew G. Knepley 
56077f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
56087f96f943SMatthew G. Knepley @*/
5609f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
56107f96f943SMatthew G. Knepley {
56117f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
56127f96f943SMatthew G. Knepley   void            **ectxs;
56137f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
56147f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
56157f96f943SMatthew G. Knepley 
56167f96f943SMatthew G. Knepley   PetscFunctionBegin;
5617f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5618f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5619f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
56207f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
56217f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
56227f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
56237f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
56247f96f943SMatthew G. Knepley     PetscDS         ds;
56257f96f943SMatthew G. Knepley     DMLabel         label;
56267f96f943SMatthew G. Knepley     IS              fieldIS;
56277f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
56287f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
56297f96f943SMatthew G. Knepley 
56307f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
56317f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
56327f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
56337f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
56347f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5635f2cacb80SMatthew G. Knepley     if (u) {
56367f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
56377f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
56387f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
56397f96f943SMatthew G. Knepley       }
56407f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
56417f96f943SMatthew G. Knepley       if (label) {
56427f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56437f96f943SMatthew G. Knepley       } else {
56447f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56457f96f943SMatthew G. Knepley       }
56467f96f943SMatthew G. Knepley     }
5647f2cacb80SMatthew G. Knepley     if (u_t) {
5648f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5649f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5650f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5651f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5652f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5653f2cacb80SMatthew G. Knepley       }
5654f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5655f2cacb80SMatthew G. Knepley       if (label) {
5656f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5657f2cacb80SMatthew G. Knepley       } else {
5658f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5659f2cacb80SMatthew G. Knepley       }
5660f2cacb80SMatthew G. Knepley     }
5661f2cacb80SMatthew G. Knepley   }
5662f2cacb80SMatthew G. Knepley   if (u) {
56637f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
56647f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5665f2cacb80SMatthew G. Knepley   }
5666f2cacb80SMatthew G. Knepley   if (u_t) {
5667f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5668f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5669f2cacb80SMatthew G. Knepley   }
56707f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
56717f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
56727f96f943SMatthew G. Knepley }
56737f96f943SMatthew G. Knepley 
56747f96f943SMatthew G. Knepley /*@
5675e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5676e5e52638SMatthew G. Knepley 
5677d083f849SBarry Smith   Collective on dm
5678e5e52638SMatthew G. Knepley 
5679e5e52638SMatthew G. Knepley   Input Parameter:
5680e5e52638SMatthew G. Knepley . dm - The DM
5681e5e52638SMatthew G. Knepley 
5682e5e52638SMatthew G. Knepley   Output Parameter:
5683e5e52638SMatthew G. Knepley . newdm - The DM
5684e5e52638SMatthew G. Knepley 
5685e5e52638SMatthew G. Knepley   Level: advanced
5686e5e52638SMatthew G. Knepley 
5687e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5688e5e52638SMatthew G. Knepley @*/
5689e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5690e5e52638SMatthew G. Knepley {
5691e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5692e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5693e5e52638SMatthew G. Knepley 
5694e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5695e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5696e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5697e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5698e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5699e5e52638SMatthew G. Knepley     DMLabel  label;
5700b3cf3223SMatthew G. Knepley     IS       fields;
5701e5e52638SMatthew G. Knepley     PetscDS  ds;
5702783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5703e5e52638SMatthew G. Knepley 
5704b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5705b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5706783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5707783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5708783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5709783e2ec8SMatthew G. Knepley       PetscInt    field;
5710783e2ec8SMatthew G. Knepley 
5711783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
571256cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5713783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5714783e2ec8SMatthew G. Knepley     }
5715e5e52638SMatthew G. Knepley   }
5716e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5717e5e52638SMatthew G. Knepley }
5718e5e52638SMatthew G. Knepley 
5719e5e52638SMatthew G. Knepley /*@
5720e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5721e5e52638SMatthew G. Knepley 
5722d083f849SBarry Smith   Collective on dm
5723e5e52638SMatthew G. Knepley 
5724e5e52638SMatthew G. Knepley   Input Parameter:
5725e5e52638SMatthew G. Knepley . dm - The DM
5726e5e52638SMatthew G. Knepley 
5727e5e52638SMatthew G. Knepley   Output Parameter:
5728e5e52638SMatthew G. Knepley . newdm - The DM
5729e5e52638SMatthew G. Knepley 
5730e5e52638SMatthew G. Knepley   Level: advanced
5731e5e52638SMatthew G. Knepley 
5732e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5733e5e52638SMatthew G. Knepley @*/
5734e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5735e5e52638SMatthew G. Knepley {
5736e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5737e5e52638SMatthew G. Knepley 
5738e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5739e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5740e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5741e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5742e5e52638SMatthew G. Knepley }
5743e5e52638SMatthew G. Knepley 
5744b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5745b64e0483SPeter Brune {
5746b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5747b64e0483SPeter Brune   PetscErrorCode ierr;
5748b64e0483SPeter Brune   Vec coords,ccoords;
57496dbf9973SLawrence Mitchell   Mat inject;
5750b64e0483SPeter Brune   PetscFunctionBegin;
5751b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5752b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5753b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5754b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5755b64e0483SPeter Brune   if (coords && !ccoords) {
5756b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
57576668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
57586dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
57592adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
57606dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5761b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5762b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5763b64e0483SPeter Brune   }
5764b64e0483SPeter Brune   PetscFunctionReturn(0);
5765b64e0483SPeter Brune }
5766b64e0483SPeter Brune 
576703dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
576803dadc2fSPeter Brune {
576903dadc2fSPeter Brune   DM dm_coord,subdm_coord;
577003dadc2fSPeter Brune   PetscErrorCode ierr;
577103dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
577203dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
577303dadc2fSPeter Brune   PetscFunctionBegin;
577403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
577503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
577603dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
577703dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
577803dadc2fSPeter Brune   if (coords && !ccoords) {
577903dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
57806668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
578103dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
578224640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
578303dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
578403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
578503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
57861ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
578703dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
578803dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
578903dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
579003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
579103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
579203dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
579303dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
579403dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
579503dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
579603dadc2fSPeter Brune   }
579703dadc2fSPeter Brune   PetscFunctionReturn(0);
579803dadc2fSPeter Brune }
579903dadc2fSPeter Brune 
5800c73cfb54SMatthew G. Knepley /*@
5801c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5802c73cfb54SMatthew G. Knepley 
5803c73cfb54SMatthew G. Knepley   Not collective
5804c73cfb54SMatthew G. Knepley 
5805c73cfb54SMatthew G. Knepley   Input Parameter:
5806c73cfb54SMatthew G. Knepley . dm - The DM
5807c73cfb54SMatthew G. Knepley 
5808c73cfb54SMatthew G. Knepley   Output Parameter:
5809c73cfb54SMatthew G. Knepley . dim - The topological dimension
5810c73cfb54SMatthew G. Knepley 
5811c73cfb54SMatthew G. Knepley   Level: beginner
5812c73cfb54SMatthew G. Knepley 
5813c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5814c73cfb54SMatthew G. Knepley @*/
5815c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5816c73cfb54SMatthew G. Knepley {
5817c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5818c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5819534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5820c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5821c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5822c73cfb54SMatthew G. Knepley }
5823c73cfb54SMatthew G. Knepley 
5824c73cfb54SMatthew G. Knepley /*@
5825c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5826c73cfb54SMatthew G. Knepley 
5827c73cfb54SMatthew G. Knepley   Collective on dm
5828c73cfb54SMatthew G. Knepley 
5829c73cfb54SMatthew G. Knepley   Input Parameters:
5830c73cfb54SMatthew G. Knepley + dm - The DM
5831c73cfb54SMatthew G. Knepley - dim - The topological dimension
5832c73cfb54SMatthew G. Knepley 
5833c73cfb54SMatthew G. Knepley   Level: beginner
5834c73cfb54SMatthew G. Knepley 
5835c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5836c73cfb54SMatthew G. Knepley @*/
5837c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5838c73cfb54SMatthew G. Knepley {
5839e5e52638SMatthew G. Knepley   PetscDS        ds;
5840f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5841f17e8794SMatthew G. Knepley 
5842c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5843c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5844c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5845c73cfb54SMatthew G. Knepley   dm->dim = dim;
5846e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5847e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5848c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5849c73cfb54SMatthew G. Knepley }
5850c73cfb54SMatthew G. Knepley 
5851793f3fe5SMatthew G. Knepley /*@
5852793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5853793f3fe5SMatthew G. Knepley 
5854d083f849SBarry Smith   Collective on dm
5855793f3fe5SMatthew G. Knepley 
5856793f3fe5SMatthew G. Knepley   Input Parameters:
5857793f3fe5SMatthew G. Knepley + dm - the DM
5858793f3fe5SMatthew G. Knepley - dim - the dimension
5859793f3fe5SMatthew G. Knepley 
5860793f3fe5SMatthew G. Knepley   Output Parameters:
5861793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5862aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5863793f3fe5SMatthew G. Knepley 
5864793f3fe5SMatthew G. Knepley   Note:
5865793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5866a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5867793f3fe5SMatthew G. Knepley   then the interval is empty.
5868793f3fe5SMatthew G. Knepley 
5869793f3fe5SMatthew G. Knepley   Level: intermediate
5870793f3fe5SMatthew G. Knepley 
5871793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5872793f3fe5SMatthew G. Knepley @*/
5873793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5874793f3fe5SMatthew G. Knepley {
5875793f3fe5SMatthew G. Knepley   PetscInt       d;
5876793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5877793f3fe5SMatthew G. Knepley 
5878793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5879793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5880793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5881793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5882b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5883793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5884793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5885793f3fe5SMatthew G. Knepley }
5886793f3fe5SMatthew G. Knepley 
58876636e97aSMatthew G Knepley /*@
58886636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
58896636e97aSMatthew G Knepley 
5890d083f849SBarry Smith   Collective on dm
58916636e97aSMatthew G Knepley 
58926636e97aSMatthew G Knepley   Input Parameters:
58936636e97aSMatthew G Knepley + dm - the DM
58946636e97aSMatthew G Knepley - c - coordinate vector
58956636e97aSMatthew G Knepley 
58962dd40e9bSPatrick Sanan   Notes:
58972dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
58982dd40e9bSPatrick Sanan 
58992dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59006636e97aSMatthew G Knepley 
59016636e97aSMatthew G Knepley   Level: intermediate
59026636e97aSMatthew G Knepley 
59032dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59046636e97aSMatthew G Knepley @*/
59056636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
59066636e97aSMatthew G Knepley {
59076636e97aSMatthew G Knepley   PetscErrorCode ierr;
59086636e97aSMatthew G Knepley 
59096636e97aSMatthew G Knepley   PetscFunctionBegin;
59106636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59116636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59126636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59136636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59146636e97aSMatthew G Knepley   dm->coordinates = c;
59156636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5916b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
591703dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
59186636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59196636e97aSMatthew G Knepley }
59206636e97aSMatthew G Knepley 
59216636e97aSMatthew G Knepley /*@
59226636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
59236636e97aSMatthew G Knepley 
59247058e716SVaclav Hapla   Not collective
59256636e97aSMatthew G Knepley 
59266636e97aSMatthew G Knepley    Input Parameters:
59276636e97aSMatthew G Knepley +  dm - the DM
59286636e97aSMatthew G Knepley -  c - coordinate vector
59296636e97aSMatthew G Knepley 
59302dd40e9bSPatrick Sanan   Notes:
59316636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
59326636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
59336636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
59346636e97aSMatthew G Knepley 
59352dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59362dd40e9bSPatrick Sanan 
59376636e97aSMatthew G Knepley   Level: intermediate
59386636e97aSMatthew G Knepley 
59396636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
59406636e97aSMatthew G Knepley @*/
59416636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
59426636e97aSMatthew G Knepley {
59436636e97aSMatthew G Knepley   PetscErrorCode ierr;
59446636e97aSMatthew G Knepley 
59456636e97aSMatthew G Knepley   PetscFunctionBegin;
59466636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59476636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59486636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59496636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
59508865f1eaSKarl Rupp 
59516636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
59528865f1eaSKarl Rupp 
59536636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59546636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59556636e97aSMatthew G Knepley }
59566636e97aSMatthew G Knepley 
59576636e97aSMatthew G Knepley /*@
59586636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
59596636e97aSMatthew G Knepley 
5960d083f849SBarry Smith   Collective on dm
59616636e97aSMatthew G Knepley 
59626636e97aSMatthew G Knepley   Input Parameter:
59636636e97aSMatthew G Knepley . dm - the DM
59646636e97aSMatthew G Knepley 
59656636e97aSMatthew G Knepley   Output Parameter:
59666636e97aSMatthew G Knepley . c - global coordinate vector
59676636e97aSMatthew G Knepley 
59686636e97aSMatthew G Knepley   Note:
59696636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
59706636e97aSMatthew G Knepley 
59716636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
59726636e97aSMatthew G Knepley 
59736636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59746636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59756636e97aSMatthew G Knepley 
59766636e97aSMatthew G Knepley   Level: intermediate
59776636e97aSMatthew G Knepley 
59786636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59796636e97aSMatthew G Knepley @*/
59806636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
59816636e97aSMatthew G Knepley {
59826636e97aSMatthew G Knepley   PetscErrorCode ierr;
59836636e97aSMatthew G Knepley 
59846636e97aSMatthew G Knepley   PetscFunctionBegin;
59856636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59866636e97aSMatthew G Knepley   PetscValidPointer(c,2);
59871f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
59880298fd71SBarry Smith     DM        cdm = NULL;
59891970a576SMatthew G. Knepley     PetscBool localized;
59906636e97aSMatthew G Knepley 
59916636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
59926636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
59931970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
59941970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
59951970a576SMatthew G. Knepley     if (localized) {
59961970a576SMatthew G. Knepley       PetscInt cdim;
59971970a576SMatthew G. Knepley 
59981970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
59991970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60001970a576SMatthew G. Knepley     }
60016636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
60026636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60036636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60046636e97aSMatthew G Knepley   }
60056636e97aSMatthew G Knepley   *c = dm->coordinates;
60066636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60076636e97aSMatthew G Knepley }
60086636e97aSMatthew G Knepley 
60096636e97aSMatthew G Knepley /*@
601081e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
601181e9a530SVaclav Hapla 
6012d083f849SBarry Smith   Collective on dm
601381e9a530SVaclav Hapla 
601481e9a530SVaclav Hapla   Input Parameter:
601581e9a530SVaclav Hapla . dm - the DM
601681e9a530SVaclav Hapla 
601781e9a530SVaclav Hapla   Level: advanced
601881e9a530SVaclav Hapla 
601981e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
602081e9a530SVaclav Hapla @*/
602181e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
602281e9a530SVaclav Hapla {
602381e9a530SVaclav Hapla   PetscErrorCode ierr;
602481e9a530SVaclav Hapla 
602581e9a530SVaclav Hapla   PetscFunctionBegin;
602681e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
602781e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
60281970a576SMatthew G. Knepley     DM        cdm = NULL;
60291970a576SMatthew G. Knepley     PetscBool localized;
60301970a576SMatthew G. Knepley 
603181e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
603281e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
60331970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
60341970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
60351970a576SMatthew G. Knepley     if (localized) {
60361970a576SMatthew G. Knepley       PetscInt cdim;
60371970a576SMatthew G. Knepley 
60381970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
60391970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60401970a576SMatthew G. Knepley     }
604181e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
604281e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
604381e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
604481e9a530SVaclav Hapla   }
604581e9a530SVaclav Hapla   PetscFunctionReturn(0);
604681e9a530SVaclav Hapla }
604781e9a530SVaclav Hapla 
604881e9a530SVaclav Hapla /*@
60496636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
60506636e97aSMatthew G Knepley 
6051d083f849SBarry Smith   Collective on dm
60526636e97aSMatthew G Knepley 
60536636e97aSMatthew G Knepley   Input Parameter:
60546636e97aSMatthew G Knepley . dm - the DM
60556636e97aSMatthew G Knepley 
60566636e97aSMatthew G Knepley   Output Parameter:
60576636e97aSMatthew G Knepley . c - coordinate vector
60586636e97aSMatthew G Knepley 
60596636e97aSMatthew G Knepley   Note:
60606636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
60616636e97aSMatthew G Knepley 
60626636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
60636636e97aSMatthew G Knepley 
60646636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
60656636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
60666636e97aSMatthew G Knepley 
60676636e97aSMatthew G Knepley   Level: intermediate
60686636e97aSMatthew G Knepley 
606981e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
60706636e97aSMatthew G Knepley @*/
60716636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
60726636e97aSMatthew G Knepley {
60736636e97aSMatthew G Knepley   PetscErrorCode ierr;
60746636e97aSMatthew G Knepley 
60756636e97aSMatthew G Knepley   PetscFunctionBegin;
60766636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60776636e97aSMatthew G Knepley   PetscValidPointer(c,2);
607881e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
60796636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
60806636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60816636e97aSMatthew G Knepley }
60826636e97aSMatthew G Knepley 
608381e9a530SVaclav Hapla /*@
608481e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
608581e9a530SVaclav Hapla 
608681e9a530SVaclav Hapla   Not collective
608781e9a530SVaclav Hapla 
608881e9a530SVaclav Hapla   Input Parameter:
608981e9a530SVaclav Hapla . dm - the DM
609081e9a530SVaclav Hapla 
609181e9a530SVaclav Hapla   Output Parameter:
609281e9a530SVaclav Hapla . c - coordinate vector
609381e9a530SVaclav Hapla 
609481e9a530SVaclav Hapla   Level: advanced
609581e9a530SVaclav Hapla 
609681e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
609781e9a530SVaclav Hapla @*/
609881e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
609981e9a530SVaclav Hapla {
610081e9a530SVaclav Hapla   PetscFunctionBegin;
610181e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
610281e9a530SVaclav Hapla   PetscValidPointer(c,2);
610381e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
61046636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
61056636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61066636e97aSMatthew G Knepley }
61076636e97aSMatthew G Knepley 
61082db98f8dSVaclav Hapla /*@
61092db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
61102db98f8dSVaclav Hapla 
61112db98f8dSVaclav Hapla   Not collective
61122db98f8dSVaclav Hapla 
61132db98f8dSVaclav Hapla   Input Parameter:
61142db98f8dSVaclav Hapla + dm - the DM
61152db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
61162db98f8dSVaclav Hapla 
61172db98f8dSVaclav Hapla   Output Parameter:
61182db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
61192db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
61202db98f8dSVaclav Hapla 
61212db98f8dSVaclav Hapla   Note:
61222db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
61232db98f8dSVaclav Hapla 
61242db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
61252db98f8dSVaclav Hapla 
61262db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
61272db98f8dSVaclav Hapla 
61282db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
61292db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
61302db98f8dSVaclav Hapla 
61312db98f8dSVaclav Hapla   Level: advanced
61322db98f8dSVaclav Hapla 
61332db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
61342db98f8dSVaclav Hapla @*/
61352db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
61362db98f8dSVaclav Hapla {
61372db98f8dSVaclav Hapla   PetscSection        cs, newcs;
61382db98f8dSVaclav Hapla   Vec                 coords;
61392db98f8dSVaclav Hapla   const PetscScalar   *arr;
61402db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
61412db98f8dSVaclav Hapla   PetscInt            n;
61422db98f8dSVaclav Hapla   PetscErrorCode      ierr;
61432db98f8dSVaclav Hapla 
61442db98f8dSVaclav Hapla   PetscFunctionBegin;
61452db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61462db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
61472db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
61482db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
61492db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
61501bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
61511bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
61522db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
61532db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
61542db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
61552db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
61562db98f8dSVaclav Hapla   if (pCoord) {
61572db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
61582db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
61592db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
61602db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6161ad9ac99dSVaclav Hapla   } else {
6162ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
61632db98f8dSVaclav Hapla   }
6164ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6165ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
61662db98f8dSVaclav Hapla   PetscFunctionReturn(0);
61672db98f8dSVaclav Hapla }
61682db98f8dSVaclav Hapla 
6169f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6170f19dbd58SToby Isaac {
6171f19dbd58SToby Isaac   PetscErrorCode ierr;
6172f19dbd58SToby Isaac 
6173f19dbd58SToby Isaac   PetscFunctionBegin;
6174f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6175f19dbd58SToby Isaac   PetscValidPointer(field,2);
6176f19dbd58SToby Isaac   if (!dm->coordinateField) {
6177f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6178f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6179f19dbd58SToby Isaac     }
6180f19dbd58SToby Isaac   }
6181f19dbd58SToby Isaac   *field = dm->coordinateField;
6182f19dbd58SToby Isaac   PetscFunctionReturn(0);
6183f19dbd58SToby Isaac }
6184f19dbd58SToby Isaac 
6185f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6186f19dbd58SToby Isaac {
6187f19dbd58SToby Isaac   PetscErrorCode ierr;
6188f19dbd58SToby Isaac 
6189f19dbd58SToby Isaac   PetscFunctionBegin;
6190f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6191f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6192c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6193f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6194f19dbd58SToby Isaac   dm->coordinateField = field;
6195f19dbd58SToby Isaac   PetscFunctionReturn(0);
6196f19dbd58SToby Isaac }
6197f19dbd58SToby Isaac 
61986636e97aSMatthew G Knepley /*@
61991cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
62006636e97aSMatthew G Knepley 
6201d083f849SBarry Smith   Collective on dm
62026636e97aSMatthew G Knepley 
62036636e97aSMatthew G Knepley   Input Parameter:
62046636e97aSMatthew G Knepley . dm - the DM
62056636e97aSMatthew G Knepley 
62066636e97aSMatthew G Knepley   Output Parameter:
62076636e97aSMatthew G Knepley . cdm - coordinate DM
62086636e97aSMatthew G Knepley 
62096636e97aSMatthew G Knepley   Level: intermediate
62106636e97aSMatthew G Knepley 
62111cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62126636e97aSMatthew G Knepley @*/
62136636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
62146636e97aSMatthew G Knepley {
62156636e97aSMatthew G Knepley   PetscErrorCode ierr;
62166636e97aSMatthew G Knepley 
62176636e97aSMatthew G Knepley   PetscFunctionBegin;
62186636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62196636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
62206636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6221308f8a94SToby Isaac     DM cdm;
6222308f8a94SToby Isaac 
622382f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6224308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6225308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6226308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6227308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6228308f8a94SToby Isaac     dm->coordinateDM = cdm;
62296636e97aSMatthew G Knepley   }
62306636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
62316636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62326636e97aSMatthew G Knepley }
6233e87bb0d3SMatthew G Knepley 
62341cfe2091SMatthew G. Knepley /*@
62351cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
62361cfe2091SMatthew G. Knepley 
6237d083f849SBarry Smith   Logically Collective on dm
62381cfe2091SMatthew G. Knepley 
62391cfe2091SMatthew G. Knepley   Input Parameters:
62401cfe2091SMatthew G. Knepley + dm - the DM
62411cfe2091SMatthew G. Knepley - cdm - coordinate DM
62421cfe2091SMatthew G. Knepley 
62431cfe2091SMatthew G. Knepley   Level: intermediate
62441cfe2091SMatthew G. Knepley 
62451cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62461cfe2091SMatthew G. Knepley @*/
62471cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
62481cfe2091SMatthew G. Knepley {
62491cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
62501cfe2091SMatthew G. Knepley 
62511cfe2091SMatthew G. Knepley   PetscFunctionBegin;
62521cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62531cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6254f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
62551cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
62561cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
62571cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
62581cfe2091SMatthew G. Knepley }
62591cfe2091SMatthew G. Knepley 
626046e270d4SMatthew G. Knepley /*@
626146e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
626246e270d4SMatthew G. Knepley 
626346e270d4SMatthew G. Knepley   Not Collective
626446e270d4SMatthew G. Knepley 
626546e270d4SMatthew G. Knepley   Input Parameter:
626646e270d4SMatthew G. Knepley . dm - The DM object
626746e270d4SMatthew G. Knepley 
626846e270d4SMatthew G. Knepley   Output Parameter:
626946e270d4SMatthew G. Knepley . dim - The embedding dimension
627046e270d4SMatthew G. Knepley 
627146e270d4SMatthew G. Knepley   Level: intermediate
627246e270d4SMatthew G. Knepley 
627392fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
627446e270d4SMatthew G. Knepley @*/
627546e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
627646e270d4SMatthew G. Knepley {
627746e270d4SMatthew G. Knepley   PetscFunctionBegin;
627846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6279534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
62809a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
62819a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
62829a9a41abSToby Isaac   }
628346e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
628446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
628546e270d4SMatthew G. Knepley }
628646e270d4SMatthew G. Knepley 
628746e270d4SMatthew G. Knepley /*@
628846e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
628946e270d4SMatthew G. Knepley 
629046e270d4SMatthew G. Knepley   Not Collective
629146e270d4SMatthew G. Knepley 
629246e270d4SMatthew G. Knepley   Input Parameters:
629346e270d4SMatthew G. Knepley + dm  - The DM object
629446e270d4SMatthew G. Knepley - dim - The embedding dimension
629546e270d4SMatthew G. Knepley 
629646e270d4SMatthew G. Knepley   Level: intermediate
629746e270d4SMatthew G. Knepley 
629892fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
629946e270d4SMatthew G. Knepley @*/
630046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
630146e270d4SMatthew G. Knepley {
6302e5e52638SMatthew G. Knepley   PetscDS        ds;
6303f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6304f17e8794SMatthew G. Knepley 
630546e270d4SMatthew G. Knepley   PetscFunctionBegin;
630646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
630746e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6308e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6309e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
631046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
631146e270d4SMatthew G. Knepley }
631246e270d4SMatthew G. Knepley 
6313e8abe2deSMatthew G. Knepley /*@
6314e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6315e8abe2deSMatthew G. Knepley 
6316d083f849SBarry Smith   Collective on dm
6317e8abe2deSMatthew G. Knepley 
6318e8abe2deSMatthew G. Knepley   Input Parameter:
6319e8abe2deSMatthew G. Knepley . dm - The DM object
6320e8abe2deSMatthew G. Knepley 
6321e8abe2deSMatthew G. Knepley   Output Parameter:
6322e8abe2deSMatthew G. Knepley . section - The PetscSection object
6323e8abe2deSMatthew G. Knepley 
6324e8abe2deSMatthew G. Knepley   Level: intermediate
6325e8abe2deSMatthew G. Knepley 
632692fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6327e8abe2deSMatthew G. Knepley @*/
6328e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6329e8abe2deSMatthew G. Knepley {
6330e8abe2deSMatthew G. Knepley   DM             cdm;
6331e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6332e8abe2deSMatthew G. Knepley 
6333e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6334e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6335e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6336e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
633792fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6338e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6339e8abe2deSMatthew G. Knepley }
6340e8abe2deSMatthew G. Knepley 
6341e8abe2deSMatthew G. Knepley /*@
6342e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6343e8abe2deSMatthew G. Knepley 
6344e8abe2deSMatthew G. Knepley   Not Collective
6345e8abe2deSMatthew G. Knepley 
6346e8abe2deSMatthew G. Knepley   Input Parameters:
6347e8abe2deSMatthew G. Knepley + dm      - The DM object
634846e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6349e8abe2deSMatthew G. Knepley - section - The PetscSection object
6350e8abe2deSMatthew G. Knepley 
6351e8abe2deSMatthew G. Knepley   Level: intermediate
6352e8abe2deSMatthew G. Knepley 
635392fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6354e8abe2deSMatthew G. Knepley @*/
635546e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6356e8abe2deSMatthew G. Knepley {
6357e8abe2deSMatthew G. Knepley   DM             cdm;
6358e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6359e8abe2deSMatthew G. Knepley 
6360e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6361e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
636246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6363e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
636492fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
636546e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
63664c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
636746e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
636846e270d4SMatthew G. Knepley 
636946e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
637046e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
637146e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
637246e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
637346e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
637446e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
637546e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
637646e270d4SMatthew G. Knepley     }
6377ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
637846e270d4SMatthew G. Knepley   }
6379e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6380e8abe2deSMatthew G. Knepley }
6381e8abe2deSMatthew G. Knepley 
6382d864a3eaSLisandro Dalcin /*@
6383d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6384d864a3eaSLisandro Dalcin 
6385d864a3eaSLisandro Dalcin   Input Parameters:
6386d864a3eaSLisandro Dalcin + dm      - The DM object
6387d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6388d864a3eaSLisandro Dalcin 
6389d864a3eaSLisandro Dalcin   Level: intermediate
6390d864a3eaSLisandro Dalcin 
6391d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6392d864a3eaSLisandro Dalcin @*/
6393d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6394d864a3eaSLisandro Dalcin {
6395d864a3eaSLisandro Dalcin   PetscObject    discOld;
6396d864a3eaSLisandro Dalcin   PetscClassId   classid;
6397d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6398d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6399d864a3eaSLisandro Dalcin   Mat            matInterp;
6400d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6401d864a3eaSLisandro Dalcin 
6402d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6403d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6404d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6405d864a3eaSLisandro Dalcin 
6406d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6407d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6408d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6409d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
641029ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
641129ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
641229ad44c5SMatthew G. Knepley       PetscFE        feLinear;
641329ad44c5SMatthew G. Knepley       DMPolytopeType ct;
641429ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
641529ad44c5SMatthew G. Knepley       PetscBool      simplex;
641629ad44c5SMatthew G. Knepley 
641729ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
641829ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
641929ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
642029ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
642129ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
642229ad44c5SMatthew G. Knepley       switch (ct) {
642329ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
642429ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
642529ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
642629ad44c5SMatthew G. Knepley         default: break;
642729ad44c5SMatthew G. Knepley       }
642829ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
642929ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
643029ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
643129ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
643229ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
643329ad44c5SMatthew G. Knepley     } else {
643429ad44c5SMatthew G. Knepley       const char *discname;
643529ad44c5SMatthew G. Knepley 
643629ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
643729ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
643829ad44c5SMatthew G. Knepley     }
643929ad44c5SMatthew G. Knepley   }
6440d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6441d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6442d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6443d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6444d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6445d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6446d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6447d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6448d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6449d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6450d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6451d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6452d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6453d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6454d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6455d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6456d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6457d864a3eaSLisandro Dalcin }
6458d864a3eaSLisandro Dalcin 
64595dc8c3f7SMatthew G. Knepley /*@C
646090b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
64615dc8c3f7SMatthew G. Knepley 
64625dc8c3f7SMatthew G. Knepley   Input Parameters:
646390b157c4SStefano Zampini . dm      - The DM object
646490b157c4SStefano Zampini 
646590b157c4SStefano Zampini   Output Parameters:
646690b157c4SStefano Zampini + per     - Whether the DM is periodic or not
64675dc8c3f7SMatthew 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
64685dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
64695dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
64705dc8c3f7SMatthew G. Knepley 
64715dc8c3f7SMatthew G. Knepley   Level: developer
64725dc8c3f7SMatthew G. Knepley 
64735dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
64745dc8c3f7SMatthew G. Knepley @*/
647590b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6476c6b900c6SMatthew G. Knepley {
6477c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6478c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
647990b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6480c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6481c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
64825dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6483c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6484c6b900c6SMatthew G. Knepley }
6485c6b900c6SMatthew G. Knepley 
64865dc8c3f7SMatthew G. Knepley /*@C
64875dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
64885dc8c3f7SMatthew G. Knepley 
64895dc8c3f7SMatthew G. Knepley   Input Parameters:
64905dc8c3f7SMatthew G. Knepley + dm      - The DM object
6491db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6492db2bf62eSStefano 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.
64935dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
64945dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
64955dc8c3f7SMatthew G. Knepley 
6496db2bf62eSStefano 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.
6497db2bf62eSStefano Zampini 
64985dc8c3f7SMatthew G. Knepley   Level: developer
64995dc8c3f7SMatthew G. Knepley 
65005dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
65015dc8c3f7SMatthew G. Knepley @*/
650290b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6503c6b900c6SMatthew G. Knepley {
6504c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6505c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6506c6b900c6SMatthew G. Knepley 
6507c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6508c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
650990b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6510412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6511412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6512412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
65135dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
651490b157c4SStefano Zampini   if (maxCell) {
6515412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6516412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6517db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6518db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6519412e9a14SMatthew G. Knepley   }
6520db2bf62eSStefano Zampini 
6521412e9a14SMatthew G. Knepley   if (L) {
6522412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6523412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6524412e9a14SMatthew G. Knepley   }
6525412e9a14SMatthew G. Knepley   if (bd) {
6526412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6527412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
652890b157c4SStefano Zampini   }
6529072d7d67SStefano Zampini   dm->periodic = per;
6530c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6531c6b900c6SMatthew G. Knepley }
6532c6b900c6SMatthew G. Knepley 
65332e17dfb7SMatthew G. Knepley /*@
65342e17dfb7SMatthew 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.
65352e17dfb7SMatthew G. Knepley 
65362e17dfb7SMatthew G. Knepley   Input Parameters:
65372e17dfb7SMatthew G. Knepley + dm     - The DM
653865da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
653965da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
65402e17dfb7SMatthew G. Knepley 
65412e17dfb7SMatthew G. Knepley   Output Parameter:
65422e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65432e17dfb7SMatthew G. Knepley 
65442e17dfb7SMatthew G. Knepley   Level: developer
65452e17dfb7SMatthew G. Knepley 
65462e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
65472e17dfb7SMatthew G. Knepley @*/
654865da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
65492e17dfb7SMatthew G. Knepley {
65502e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
65512e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
65522e17dfb7SMatthew G. Knepley 
65532e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65542e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
65552e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65562e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
65572e17dfb7SMatthew G. Knepley   } else {
655865da65dcSMatthew G. Knepley     if (endpoint) {
655965da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6560da3333bfSMatthew 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)) {
6561da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
656265da65dcSMatthew G. Knepley         } else {
6563da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
656465da65dcSMatthew G. Knepley         }
656565da65dcSMatthew G. Knepley       }
656665da65dcSMatthew G. Knepley     } else {
65672e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
65681118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
65692e17dfb7SMatthew G. Knepley       }
65702e17dfb7SMatthew G. Knepley     }
657165da65dcSMatthew G. Knepley   }
65722e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65732e17dfb7SMatthew G. Knepley }
65742e17dfb7SMatthew G. Knepley 
65752e17dfb7SMatthew G. Knepley /*
65762e17dfb7SMatthew 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.
65772e17dfb7SMatthew G. Knepley 
65782e17dfb7SMatthew G. Knepley   Input Parameters:
65792e17dfb7SMatthew G. Knepley + dm     - The DM
65802e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
65812e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
65822e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
65832e17dfb7SMatthew G. Knepley 
65842e17dfb7SMatthew G. Knepley   Output Parameter:
65852e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65862e17dfb7SMatthew G. Knepley 
65872e17dfb7SMatthew G. Knepley   Level: developer
65882e17dfb7SMatthew G. Knepley 
65892e17dfb7SMatthew 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
65902e17dfb7SMatthew G. Knepley 
65912e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
65922e17dfb7SMatthew G. Knepley */
65932e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
65942e17dfb7SMatthew G. Knepley {
65952e17dfb7SMatthew G. Knepley   PetscInt d;
65962e17dfb7SMatthew G. Knepley 
65972e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65982e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65992e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
66002e17dfb7SMatthew G. Knepley   } else {
66012e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6602908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
66032e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
66042e17dfb7SMatthew G. Knepley       } else {
66052e17dfb7SMatthew G. Knepley         out[d] = in[d];
66062e17dfb7SMatthew G. Knepley       }
66072e17dfb7SMatthew G. Knepley     }
66082e17dfb7SMatthew G. Knepley   }
66092e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66102e17dfb7SMatthew G. Knepley }
6611a5801f52SStefano Zampini 
66122e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
66132e17dfb7SMatthew G. Knepley {
66142e17dfb7SMatthew G. Knepley   PetscInt d;
66152e17dfb7SMatthew G. Knepley 
66162e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66172e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66182e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
66192e17dfb7SMatthew G. Knepley   } else {
66202e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6621908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
66222e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
66232e17dfb7SMatthew G. Knepley       } else {
66242e17dfb7SMatthew G. Knepley         out[d] = in[d];
66252e17dfb7SMatthew G. Knepley       }
66262e17dfb7SMatthew G. Knepley     }
66272e17dfb7SMatthew G. Knepley   }
66282e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66292e17dfb7SMatthew G. Knepley }
66302e17dfb7SMatthew G. Knepley 
66312e17dfb7SMatthew G. Knepley /*
66322e17dfb7SMatthew 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.
66332e17dfb7SMatthew G. Knepley 
66342e17dfb7SMatthew G. Knepley   Input Parameters:
66352e17dfb7SMatthew G. Knepley + dm     - The DM
66362e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
66372e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
66382e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
66392e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
66402e17dfb7SMatthew G. Knepley 
66412e17dfb7SMatthew G. Knepley   Output Parameter:
66422e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
66432e17dfb7SMatthew G. Knepley 
66442e17dfb7SMatthew G. Knepley   Level: developer
66452e17dfb7SMatthew G. Knepley 
66462e17dfb7SMatthew 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
66472e17dfb7SMatthew G. Knepley 
66482e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
66492e17dfb7SMatthew G. Knepley */
66502e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
66512e17dfb7SMatthew G. Knepley {
66522e17dfb7SMatthew G. Knepley   PetscInt d;
66532e17dfb7SMatthew G. Knepley 
66542e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66552e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66562e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
66572e17dfb7SMatthew G. Knepley   } else {
66582e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6659412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6660412e9a14SMatthew G. Knepley 
6661412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6662412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6663412e9a14SMatthew G. Knepley 
6664412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6665412e9a14SMatthew 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]));
6666412e9a14SMatthew G. Knepley         out[d] += newCoord;
66672e17dfb7SMatthew G. Knepley       } else {
66682e17dfb7SMatthew G. Knepley         out[d] += in[d];
66692e17dfb7SMatthew G. Knepley       }
66702e17dfb7SMatthew G. Knepley     }
66712e17dfb7SMatthew G. Knepley   }
66722e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66732e17dfb7SMatthew G. Knepley }
66742e17dfb7SMatthew G. Knepley 
667536447a5eSToby Isaac /*@
66768f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
66778f700142SStefano Zampini 
66788f700142SStefano Zampini   Not collective
667936447a5eSToby Isaac 
668036447a5eSToby Isaac   Input Parameter:
668136447a5eSToby Isaac . dm - The DM
668236447a5eSToby Isaac 
668336447a5eSToby Isaac   Output Parameter:
668436447a5eSToby Isaac   areLocalized - True if localized
668536447a5eSToby Isaac 
668636447a5eSToby Isaac   Level: developer
668736447a5eSToby Isaac 
66888f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
668936447a5eSToby Isaac @*/
66908f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
669136447a5eSToby Isaac {
669236447a5eSToby Isaac   DM             cdm;
669336447a5eSToby Isaac   PetscSection   coordSection;
669446a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
669546a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
669636447a5eSToby Isaac   PetscErrorCode ierr;
669736447a5eSToby Isaac 
669836447a5eSToby Isaac   PetscFunctionBegin;
669936447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6700534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
67018b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
670246a3a80fSLisandro Dalcin 
670336447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
670436447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
670546a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
67069f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
670746a3a80fSLisandro Dalcin 
67089f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
670946a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
671036447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
671136447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
671246a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
671346a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
671436447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
671546a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
671636447a5eSToby Isaac   }
67178f700142SStefano Zampini   *areLocalized = alreadyLocalized;
671836447a5eSToby Isaac   PetscFunctionReturn(0);
671936447a5eSToby Isaac }
672036447a5eSToby Isaac 
67218f700142SStefano Zampini /*@
67228f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
67238f700142SStefano Zampini 
67248f700142SStefano Zampini   Collective on dm
67258f700142SStefano Zampini 
67268f700142SStefano Zampini   Input Parameter:
67278f700142SStefano Zampini . dm - The DM
67288f700142SStefano Zampini 
67298f700142SStefano Zampini   Output Parameter:
67308f700142SStefano Zampini   areLocalized - True if localized
67318f700142SStefano Zampini 
67328f700142SStefano Zampini   Level: developer
67338f700142SStefano Zampini 
67348f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
67358f700142SStefano Zampini @*/
67368f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
67378f700142SStefano Zampini {
67388f700142SStefano Zampini   PetscBool      localized;
67398f700142SStefano Zampini   PetscErrorCode ierr;
67408f700142SStefano Zampini 
67418f700142SStefano Zampini   PetscFunctionBegin;
67428f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6743534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
67448f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
67458f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
67468f700142SStefano Zampini   PetscFunctionReturn(0);
67478f700142SStefano Zampini }
674836447a5eSToby Isaac 
67492e17dfb7SMatthew G. Knepley /*@
6750492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
67512e17dfb7SMatthew G. Knepley 
67528f700142SStefano Zampini   Collective on dm
67538f700142SStefano Zampini 
67542e17dfb7SMatthew G. Knepley   Input Parameter:
67552e17dfb7SMatthew G. Knepley . dm - The DM
67562e17dfb7SMatthew G. Knepley 
67572e17dfb7SMatthew G. Knepley   Level: developer
67582e17dfb7SMatthew G. Knepley 
67598f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
67602e17dfb7SMatthew G. Knepley @*/
67612e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
67622e17dfb7SMatthew G. Knepley {
67632e17dfb7SMatthew G. Knepley   DM             cdm;
67642e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
67652e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
67663e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
67673e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6768e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
67693e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
67703e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
67712e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
67722e17dfb7SMatthew G. Knepley 
67732e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67742e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
677592c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6776f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6777f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6778f7cbd40bSStefano Zampini 
67792e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
67802e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
67812e17dfb7SMatthew G. Knepley   {
67822e17dfb7SMatthew G. Knepley     PetscBool isplex;
67832e17dfb7SMatthew G. Knepley 
67842e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
67852e17dfb7SMatthew G. Knepley     if (isplex) {
67862e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
67873e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
678869291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
67893e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
67903e922f36SToby Isaac       newStart = vStart;
67913e922f36SToby Isaac       newEnd   = vEnd;
67923e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
67933e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
67943e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
67953e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
67963e922f36SToby Isaac       }
67972e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
67982e17dfb7SMatthew G. Knepley   }
67992e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
680043eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
68012e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
68023e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6803e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
68043e922f36SToby Isaac 
68052e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
68062e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
68072e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
68082e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
68093e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
68103e922f36SToby Isaac 
681169291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68123e922f36SToby Isaac   localized = &anchor[bs];
68133e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
68143e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68153e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68163e922f36SToby Isaac 
68173e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
68183e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
68193e922f36SToby Isaac       PetscInt     b;
68203e922f36SToby Isaac 
68213e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
68223e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68233e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68243e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
68253e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
68263e922f36SToby Isaac         for (b = 0; b < bs; b++) {
68273e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
68283e922f36SToby Isaac         }
68293e922f36SToby Isaac         if (b < bs) break;
68303e922f36SToby Isaac       }
68313e922f36SToby Isaac       if (d < dof/bs) {
68323e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
68333e922f36SToby Isaac           PetscInt cdof;
68343e922f36SToby Isaac 
68353e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
68363e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
68373e922f36SToby Isaac         }
68383e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
68393e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
68403e922f36SToby Isaac       }
68413e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68423e922f36SToby Isaac     }
68433e922f36SToby Isaac   }
68443e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
68453e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
684669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68473e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
684869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
68493e922f36SToby Isaac     PetscFunctionReturn(0);
68503e922f36SToby Isaac   }
68512e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68522e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68532e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
68542e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
68552e17dfb7SMatthew G. Knepley   }
68562e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
68572e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6858c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
68592e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
68602e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
68612e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
68622e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6863c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68642e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
68652e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68662e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68672e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
68682e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
68692e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
68702e17dfb7SMatthew G. Knepley   }
68713e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68723e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68733e922f36SToby Isaac 
68742e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
68752e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
68763e922f36SToby Isaac       PetscInt     b, cdof;
68772e17dfb7SMatthew G. Knepley 
68783e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
68793e922f36SToby Isaac       if (!cdof) continue;
68802e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68812e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
68822e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68832e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
68842e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68852e17dfb7SMatthew G. Knepley     }
68863e922f36SToby Isaac   }
688769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
688869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6889c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68902e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
68912e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
68922e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
68932e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
68942e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
68952e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68962e17dfb7SMatthew G. Knepley }
68972e17dfb7SMatthew G. Knepley 
6898e87bb0d3SMatthew G Knepley /*@
68993a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6900e87bb0d3SMatthew G Knepley 
6901d083f849SBarry Smith   Collective on v (see explanation below)
6902e87bb0d3SMatthew G Knepley 
6903e87bb0d3SMatthew G Knepley   Input Parameters:
6904e87bb0d3SMatthew G Knepley + dm - The DM
69053a93e3b7SToby Isaac . v - The Vec of points
690662a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
69073a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6908e87bb0d3SMatthew G Knepley 
690961e3bb9bSMatthew G Knepley   Output Parameter:
691062a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
691162a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
69123a93e3b7SToby Isaac 
6913e87bb0d3SMatthew G Knepley 
6914e87bb0d3SMatthew G Knepley   Level: developer
691561e3bb9bSMatthew G Knepley 
691662a38674SMatthew G. Knepley   Notes:
69173a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
691862a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
69193a93e3b7SToby Isaac 
69203a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
692162a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
69223a93e3b7SToby Isaac 
69233a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
69243a93e3b7SToby Isaac 
692562a38674SMatthew G. Knepley $    const PetscSFNode *cells;
692662a38674SMatthew G. Knepley $    PetscInt           nFound;
6927a6216909SToby Isaac $    const PetscInt    *found;
692862a38674SMatthew G. Knepley $
6929a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
69303a93e3b7SToby Isaac 
69313a93e3b7SToby 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
69323a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
69333a93e3b7SToby Isaac 
693462a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
693561e3bb9bSMatthew G Knepley @*/
693662a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6937e87bb0d3SMatthew G Knepley {
6938735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6939735aa83eSMatthew G Knepley 
6940e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6941e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6942e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6943e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
69443a93e3b7SToby Isaac   if (*cellSF) {
69453a93e3b7SToby Isaac     PetscMPIInt result;
69463a93e3b7SToby Isaac 
6947e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6948a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
69493a93e3b7SToby 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");
6950e0fc9d1bSMatthew G. Knepley   } else {
69513a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
69523a93e3b7SToby Isaac   }
6953b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
695447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
695562a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
695647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6957e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6958e87bb0d3SMatthew G Knepley }
695914f150ffSMatthew G. Knepley 
6960f4d763aaSMatthew G. Knepley /*@
6961f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6962f4d763aaSMatthew G. Knepley 
69638f700142SStefano Zampini   Collective on dm
69648f700142SStefano Zampini 
6965f4d763aaSMatthew G. Knepley   Input Parameter:
6966f4d763aaSMatthew G. Knepley . dm - The original DM
6967f4d763aaSMatthew G. Knepley 
6968f4d763aaSMatthew G. Knepley   Output Parameter:
6969f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6970f4d763aaSMatthew G. Knepley 
6971f4d763aaSMatthew G. Knepley   Level: intermediate
6972f4d763aaSMatthew G. Knepley 
6973e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6974f4d763aaSMatthew G. Knepley @*/
697514f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
697614f150ffSMatthew G. Knepley {
6977c26acbdeSMatthew G. Knepley   PetscSection   section;
69782d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
697914f150ffSMatthew G. Knepley   PetscErrorCode ierr;
698014f150ffSMatthew G. Knepley 
698114f150ffSMatthew G. Knepley   PetscFunctionBegin;
698214f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
698314f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
698492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6985c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6986127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
69872d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6988c26acbdeSMatthew G. Knepley     *odm = dm;
6989c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6990c26acbdeSMatthew G. Knepley   }
699114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6992c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
699314f150ffSMatthew G. Knepley     PetscSF      sf;
699414f150ffSMatthew G. Knepley 
699514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6996e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
699714f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
699892fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
699914f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
700014f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
700115b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7002e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
700314f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
700414f150ffSMatthew G. Knepley   }
700514f150ffSMatthew G. Knepley   *odm = dm->dmBC;
700614f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
700714f150ffSMatthew G. Knepley }
7008f4d763aaSMatthew G. Knepley 
7009f4d763aaSMatthew G. Knepley /*@
7010cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7011f4d763aaSMatthew G. Knepley 
7012f4d763aaSMatthew G. Knepley   Input Parameter:
7013f4d763aaSMatthew G. Knepley . dm - The original DM
7014f4d763aaSMatthew G. Knepley 
7015cdb7a50dSMatthew G. Knepley   Output Parameters:
7016cdb7a50dSMatthew G. Knepley + num - The output sequence number
7017cdb7a50dSMatthew G. Knepley - val - The output sequence value
7018f4d763aaSMatthew G. Knepley 
7019f4d763aaSMatthew G. Knepley   Level: intermediate
7020f4d763aaSMatthew G. Knepley 
7021f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7022f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7023f4d763aaSMatthew G. Knepley 
7024f4d763aaSMatthew G. Knepley .seealso: VecView()
7025f4d763aaSMatthew G. Knepley @*/
7026cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7027f4d763aaSMatthew G. Knepley {
7028f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7029f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7030534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7031534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7032f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7033f4d763aaSMatthew G. Knepley }
7034f4d763aaSMatthew G. Knepley 
7035f4d763aaSMatthew G. Knepley /*@
7036cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7037f4d763aaSMatthew G. Knepley 
7038f4d763aaSMatthew G. Knepley   Input Parameters:
7039f4d763aaSMatthew G. Knepley + dm - The original DM
7040cdb7a50dSMatthew G. Knepley . num - The output sequence number
7041cdb7a50dSMatthew G. Knepley - val - The output sequence value
7042f4d763aaSMatthew G. Knepley 
7043f4d763aaSMatthew G. Knepley   Level: intermediate
7044f4d763aaSMatthew G. Knepley 
7045f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7046f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7047f4d763aaSMatthew G. Knepley 
7048f4d763aaSMatthew G. Knepley .seealso: VecView()
7049f4d763aaSMatthew G. Knepley @*/
7050cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7051f4d763aaSMatthew G. Knepley {
7052f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7053f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7054f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7055cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7056cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7057cdb7a50dSMatthew G. Knepley }
7058cdb7a50dSMatthew G. Knepley 
7059cdb7a50dSMatthew G. Knepley /*@C
7060cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7061cdb7a50dSMatthew G. Knepley 
7062cdb7a50dSMatthew G. Knepley   Input Parameters:
7063cdb7a50dSMatthew G. Knepley + dm   - The original DM
7064cdb7a50dSMatthew G. Knepley . name - The sequence name
7065cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7066cdb7a50dSMatthew G. Knepley 
7067cdb7a50dSMatthew G. Knepley   Output Parameter:
7068cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7069cdb7a50dSMatthew G. Knepley 
7070cdb7a50dSMatthew G. Knepley   Level: intermediate
7071cdb7a50dSMatthew G. Knepley 
7072cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7073cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7074cdb7a50dSMatthew G. Knepley 
7075cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7076cdb7a50dSMatthew G. Knepley @*/
7077cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7078cdb7a50dSMatthew G. Knepley {
7079cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7080cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7081cdb7a50dSMatthew G. Knepley 
7082cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7083cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7084cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7085534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
7086cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7087cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7088cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7089cdb7a50dSMatthew G. Knepley     PetscScalar value;
7090cdb7a50dSMatthew G. Knepley 
709139d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
70924aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7093cdb7a50dSMatthew G. Knepley #endif
7094cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7095f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7096f4d763aaSMatthew G. Knepley }
70978e4ac7eaSMatthew G. Knepley 
70988e4ac7eaSMatthew G. Knepley /*@
70998e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
71008e4ac7eaSMatthew G. Knepley 
71018e4ac7eaSMatthew G. Knepley   Not collective
71028e4ac7eaSMatthew G. Knepley 
71038e4ac7eaSMatthew G. Knepley   Input Parameter:
71048e4ac7eaSMatthew G. Knepley . dm - The DM
71058e4ac7eaSMatthew G. Knepley 
71068e4ac7eaSMatthew G. Knepley   Output Parameter:
71078e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
71088e4ac7eaSMatthew G. Knepley 
71098e4ac7eaSMatthew G. Knepley   Level: beginner
71108e4ac7eaSMatthew G. Knepley 
71118e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
71128e4ac7eaSMatthew G. Knepley @*/
71138e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
71148e4ac7eaSMatthew G. Knepley {
71158e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71168e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7117534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
71188e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
71198e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71208e4ac7eaSMatthew G. Knepley }
71218e4ac7eaSMatthew G. Knepley 
71228e4ac7eaSMatthew G. Knepley /*@
71235d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
71248e4ac7eaSMatthew G. Knepley 
71258e4ac7eaSMatthew G. Knepley   Collective on dm
71268e4ac7eaSMatthew G. Knepley 
71278e4ac7eaSMatthew G. Knepley   Input Parameters:
71288e4ac7eaSMatthew G. Knepley + dm - The DM
71298e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
71308e4ac7eaSMatthew G. Knepley 
71315d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
71325d3b26e6SMatthew G. Knepley 
71338e4ac7eaSMatthew G. Knepley   Level: beginner
71348e4ac7eaSMatthew G. Knepley 
71355d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
71368e4ac7eaSMatthew G. Knepley @*/
71378e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
71388e4ac7eaSMatthew G. Knepley {
71398e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71408e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71418833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
71428e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
71438e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71448e4ac7eaSMatthew G. Knepley }
7145c58f1c22SToby Isaac 
7146c58f1c22SToby Isaac 
7147c58f1c22SToby Isaac /*@C
7148c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7149c58f1c22SToby Isaac 
7150c58f1c22SToby Isaac   Not Collective
7151c58f1c22SToby Isaac 
7152c58f1c22SToby Isaac   Input Parameters:
7153c58f1c22SToby Isaac + dm   - The DM object
7154c58f1c22SToby Isaac - name - The label name
7155c58f1c22SToby Isaac 
7156c58f1c22SToby Isaac   Level: intermediate
7157c58f1c22SToby Isaac 
7158c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7159c58f1c22SToby Isaac @*/
7160c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7161c58f1c22SToby Isaac {
71625d80c0bfSVaclav Hapla   PetscBool      flg;
71635d80c0bfSVaclav Hapla   DMLabel        label;
7164c58f1c22SToby Isaac   PetscErrorCode ierr;
7165c58f1c22SToby Isaac 
7166c58f1c22SToby Isaac   PetscFunctionBegin;
7167c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7168c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
71695d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7170c58f1c22SToby Isaac   if (!flg) {
71715d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
71725d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
71735d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7174c58f1c22SToby Isaac   }
7175c58f1c22SToby Isaac   PetscFunctionReturn(0);
7176c58f1c22SToby Isaac }
7177c58f1c22SToby Isaac 
7178c58f1c22SToby Isaac /*@C
7179c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7180c58f1c22SToby Isaac 
7181c58f1c22SToby Isaac   Not Collective
7182c58f1c22SToby Isaac 
7183c58f1c22SToby Isaac   Input Parameters:
7184c58f1c22SToby Isaac + dm   - The DM object
7185c58f1c22SToby Isaac . name - The label name
7186c58f1c22SToby Isaac - point - The mesh point
7187c58f1c22SToby Isaac 
7188c58f1c22SToby Isaac   Output Parameter:
7189c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7190c58f1c22SToby Isaac 
7191c58f1c22SToby Isaac   Level: beginner
7192c58f1c22SToby Isaac 
7193c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7194c58f1c22SToby Isaac @*/
7195c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7196c58f1c22SToby Isaac {
7197c58f1c22SToby Isaac   DMLabel        label;
7198c58f1c22SToby Isaac   PetscErrorCode ierr;
7199c58f1c22SToby Isaac 
7200c58f1c22SToby Isaac   PetscFunctionBegin;
7201c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7202c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7203c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
720413903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7205c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7206c58f1c22SToby Isaac   PetscFunctionReturn(0);
7207c58f1c22SToby Isaac }
7208c58f1c22SToby Isaac 
7209c58f1c22SToby Isaac /*@C
7210c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7211c58f1c22SToby Isaac 
7212c58f1c22SToby Isaac   Not Collective
7213c58f1c22SToby Isaac 
7214c58f1c22SToby Isaac   Input Parameters:
7215c58f1c22SToby Isaac + dm   - The DM object
7216c58f1c22SToby Isaac . name - The label name
7217c58f1c22SToby Isaac . point - The mesh point
7218c58f1c22SToby Isaac - value - The label value for this point
7219c58f1c22SToby Isaac 
7220c58f1c22SToby Isaac   Output Parameter:
7221c58f1c22SToby Isaac 
7222c58f1c22SToby Isaac   Level: beginner
7223c58f1c22SToby Isaac 
7224c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7225c58f1c22SToby Isaac @*/
7226c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7227c58f1c22SToby Isaac {
7228c58f1c22SToby Isaac   DMLabel        label;
7229c58f1c22SToby Isaac   PetscErrorCode ierr;
7230c58f1c22SToby Isaac 
7231c58f1c22SToby Isaac   PetscFunctionBegin;
7232c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7233c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7234c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7235c58f1c22SToby Isaac   if (!label) {
7236c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7237c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7238c58f1c22SToby Isaac   }
7239c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7240c58f1c22SToby Isaac   PetscFunctionReturn(0);
7241c58f1c22SToby Isaac }
7242c58f1c22SToby Isaac 
7243c58f1c22SToby Isaac /*@C
7244c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7245c58f1c22SToby Isaac 
7246c58f1c22SToby Isaac   Not Collective
7247c58f1c22SToby Isaac 
7248c58f1c22SToby Isaac   Input Parameters:
7249c58f1c22SToby Isaac + dm   - The DM object
7250c58f1c22SToby Isaac . name - The label name
7251c58f1c22SToby Isaac . point - The mesh point
7252c58f1c22SToby Isaac - value - The label value for this point
7253c58f1c22SToby Isaac 
7254c58f1c22SToby Isaac   Output Parameter:
7255c58f1c22SToby Isaac 
7256c58f1c22SToby Isaac   Level: beginner
7257c58f1c22SToby Isaac 
7258c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7259c58f1c22SToby Isaac @*/
7260c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7261c58f1c22SToby Isaac {
7262c58f1c22SToby Isaac   DMLabel        label;
7263c58f1c22SToby Isaac   PetscErrorCode ierr;
7264c58f1c22SToby Isaac 
7265c58f1c22SToby Isaac   PetscFunctionBegin;
7266c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7267c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7268c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7269c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7270c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7271c58f1c22SToby Isaac   PetscFunctionReturn(0);
7272c58f1c22SToby Isaac }
7273c58f1c22SToby Isaac 
7274c58f1c22SToby Isaac /*@C
7275c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7276c58f1c22SToby Isaac 
7277c58f1c22SToby Isaac   Not Collective
7278c58f1c22SToby Isaac 
7279c58f1c22SToby Isaac   Input Parameters:
7280c58f1c22SToby Isaac + dm   - The DM object
7281c58f1c22SToby Isaac - name - The label name
7282c58f1c22SToby Isaac 
7283c58f1c22SToby Isaac   Output Parameter:
7284c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7285c58f1c22SToby Isaac 
7286c58f1c22SToby Isaac   Level: beginner
7287c58f1c22SToby Isaac 
7288df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7289c58f1c22SToby Isaac @*/
7290c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7291c58f1c22SToby Isaac {
7292c58f1c22SToby Isaac   DMLabel        label;
7293c58f1c22SToby Isaac   PetscErrorCode ierr;
7294c58f1c22SToby Isaac 
7295c58f1c22SToby Isaac   PetscFunctionBegin;
7296c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7297c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7298534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7299c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7300c58f1c22SToby Isaac   *size = 0;
7301c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7302c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7303c58f1c22SToby Isaac   PetscFunctionReturn(0);
7304c58f1c22SToby Isaac }
7305c58f1c22SToby Isaac 
7306c58f1c22SToby Isaac /*@C
7307c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7308c58f1c22SToby Isaac 
7309c58f1c22SToby Isaac   Not Collective
7310c58f1c22SToby Isaac 
7311c58f1c22SToby Isaac   Input Parameters:
7312c58f1c22SToby Isaac + mesh - The DM object
7313c58f1c22SToby Isaac - name - The label name
7314c58f1c22SToby Isaac 
7315c58f1c22SToby Isaac   Output Parameter:
7316c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7317c58f1c22SToby Isaac 
7318c58f1c22SToby Isaac   Level: beginner
7319c58f1c22SToby Isaac 
7320c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7321c58f1c22SToby Isaac @*/
7322c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7323c58f1c22SToby Isaac {
7324c58f1c22SToby Isaac   DMLabel        label;
7325c58f1c22SToby Isaac   PetscErrorCode ierr;
7326c58f1c22SToby Isaac 
7327c58f1c22SToby Isaac   PetscFunctionBegin;
7328c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7329c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7330c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7331c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7332c58f1c22SToby Isaac   *ids = NULL;
7333dab2e251SBlaise Bourdin  if (label) {
7334c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7335dab2e251SBlaise Bourdin   } else {
7336dab2e251SBlaise Bourdin     /* returning an empty IS */
7337dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7338dab2e251SBlaise Bourdin   }
7339c58f1c22SToby Isaac   PetscFunctionReturn(0);
7340c58f1c22SToby Isaac }
7341c58f1c22SToby Isaac 
7342c58f1c22SToby Isaac /*@C
7343c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7344c58f1c22SToby Isaac 
7345c58f1c22SToby Isaac   Not Collective
7346c58f1c22SToby Isaac 
7347c58f1c22SToby Isaac   Input Parameters:
7348c58f1c22SToby Isaac + dm - The DM object
7349c58f1c22SToby Isaac . name - The label name
7350c58f1c22SToby Isaac - value - The stratum value
7351c58f1c22SToby Isaac 
7352c58f1c22SToby Isaac   Output Parameter:
7353c58f1c22SToby Isaac . size - The stratum size
7354c58f1c22SToby Isaac 
7355c58f1c22SToby Isaac   Level: beginner
7356c58f1c22SToby Isaac 
7357c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7358c58f1c22SToby Isaac @*/
7359c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7360c58f1c22SToby Isaac {
7361c58f1c22SToby Isaac   DMLabel        label;
7362c58f1c22SToby Isaac   PetscErrorCode ierr;
7363c58f1c22SToby Isaac 
7364c58f1c22SToby Isaac   PetscFunctionBegin;
7365c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7366c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7367534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7368c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7369c58f1c22SToby Isaac   *size = 0;
7370c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7371c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7372c58f1c22SToby Isaac   PetscFunctionReturn(0);
7373c58f1c22SToby Isaac }
7374c58f1c22SToby Isaac 
7375c58f1c22SToby Isaac /*@C
7376c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7377c58f1c22SToby Isaac 
7378c58f1c22SToby Isaac   Not Collective
7379c58f1c22SToby Isaac 
7380c58f1c22SToby Isaac   Input Parameters:
7381c58f1c22SToby Isaac + dm - The DM object
7382c58f1c22SToby Isaac . name - The label name
7383c58f1c22SToby Isaac - value - The stratum value
7384c58f1c22SToby Isaac 
7385c58f1c22SToby Isaac   Output Parameter:
7386c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7387c58f1c22SToby Isaac 
7388c58f1c22SToby Isaac   Level: beginner
7389c58f1c22SToby Isaac 
7390c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7391c58f1c22SToby Isaac @*/
7392c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7393c58f1c22SToby Isaac {
7394c58f1c22SToby Isaac   DMLabel        label;
7395c58f1c22SToby Isaac   PetscErrorCode ierr;
7396c58f1c22SToby Isaac 
7397c58f1c22SToby Isaac   PetscFunctionBegin;
7398c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7399c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7400c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7401c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7402c58f1c22SToby Isaac   *points = NULL;
7403c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7404c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7405c58f1c22SToby Isaac   PetscFunctionReturn(0);
7406c58f1c22SToby Isaac }
7407c58f1c22SToby Isaac 
74084de306b1SToby Isaac /*@C
74099044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
74104de306b1SToby Isaac 
74114de306b1SToby Isaac   Not Collective
74124de306b1SToby Isaac 
74134de306b1SToby Isaac   Input Parameters:
74144de306b1SToby Isaac + dm - The DM object
74154de306b1SToby Isaac . name - The label name
74164de306b1SToby Isaac . value - The stratum value
74174de306b1SToby Isaac - points - The stratum points
74184de306b1SToby Isaac 
74194de306b1SToby Isaac   Level: beginner
74204de306b1SToby Isaac 
74214de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
74224de306b1SToby Isaac @*/
74234de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
74244de306b1SToby Isaac {
74254de306b1SToby Isaac   DMLabel        label;
74264de306b1SToby Isaac   PetscErrorCode ierr;
74274de306b1SToby Isaac 
74284de306b1SToby Isaac   PetscFunctionBegin;
74294de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74304de306b1SToby Isaac   PetscValidCharPointer(name, 2);
74314de306b1SToby Isaac   PetscValidPointer(points, 4);
74324de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
74334de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
74344de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
74354de306b1SToby Isaac   PetscFunctionReturn(0);
74364de306b1SToby Isaac }
74374de306b1SToby Isaac 
7438c58f1c22SToby Isaac /*@C
7439c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7440c58f1c22SToby Isaac 
7441c58f1c22SToby Isaac   Not Collective
7442c58f1c22SToby Isaac 
7443c58f1c22SToby Isaac   Input Parameters:
7444c58f1c22SToby Isaac + dm   - The DM object
7445c58f1c22SToby Isaac . name - The label name
7446c58f1c22SToby Isaac - value - The label value for this point
7447c58f1c22SToby Isaac 
7448c58f1c22SToby Isaac   Output Parameter:
7449c58f1c22SToby Isaac 
7450c58f1c22SToby Isaac   Level: beginner
7451c58f1c22SToby Isaac 
7452c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7453c58f1c22SToby Isaac @*/
7454c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7455c58f1c22SToby Isaac {
7456c58f1c22SToby Isaac   DMLabel        label;
7457c58f1c22SToby Isaac   PetscErrorCode ierr;
7458c58f1c22SToby Isaac 
7459c58f1c22SToby Isaac   PetscFunctionBegin;
7460c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7461c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7462c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7463c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7464c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7465c58f1c22SToby Isaac   PetscFunctionReturn(0);
7466c58f1c22SToby Isaac }
7467c58f1c22SToby Isaac 
7468c58f1c22SToby Isaac /*@
7469c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7470c58f1c22SToby Isaac 
7471c58f1c22SToby Isaac   Not Collective
7472c58f1c22SToby Isaac 
7473c58f1c22SToby Isaac   Input Parameter:
7474c58f1c22SToby Isaac . dm   - The DM object
7475c58f1c22SToby Isaac 
7476c58f1c22SToby Isaac   Output Parameter:
7477c58f1c22SToby Isaac . numLabels - the number of Labels
7478c58f1c22SToby Isaac 
7479c58f1c22SToby Isaac   Level: intermediate
7480c58f1c22SToby Isaac 
7481c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7482c58f1c22SToby Isaac @*/
7483c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7484c58f1c22SToby Isaac {
74855d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7486c58f1c22SToby Isaac   PetscInt  n    = 0;
7487c58f1c22SToby Isaac 
7488c58f1c22SToby Isaac   PetscFunctionBegin;
7489c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7490534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7491c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7492c58f1c22SToby Isaac   *numLabels = n;
7493c58f1c22SToby Isaac   PetscFunctionReturn(0);
7494c58f1c22SToby Isaac }
7495c58f1c22SToby Isaac 
7496c58f1c22SToby Isaac /*@C
7497c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7498c58f1c22SToby Isaac 
7499c58f1c22SToby Isaac   Not Collective
7500c58f1c22SToby Isaac 
7501c58f1c22SToby Isaac   Input Parameters:
7502c58f1c22SToby Isaac + dm - The DM object
7503c58f1c22SToby Isaac - n  - the label number
7504c58f1c22SToby Isaac 
7505c58f1c22SToby Isaac   Output Parameter:
7506c58f1c22SToby Isaac . name - the label name
7507c58f1c22SToby Isaac 
7508c58f1c22SToby Isaac   Level: intermediate
7509c58f1c22SToby Isaac 
7510c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7511c58f1c22SToby Isaac @*/
7512c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7513c58f1c22SToby Isaac {
75145d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7515c58f1c22SToby Isaac   PetscInt       l    = 0;
7516d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7517c58f1c22SToby Isaac 
7518c58f1c22SToby Isaac   PetscFunctionBegin;
7519c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7520c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7521c58f1c22SToby Isaac   while (next) {
7522c58f1c22SToby Isaac     if (l == n) {
7523d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7524c58f1c22SToby Isaac       PetscFunctionReturn(0);
7525c58f1c22SToby Isaac     }
7526c58f1c22SToby Isaac     ++l;
7527c58f1c22SToby Isaac     next = next->next;
7528c58f1c22SToby Isaac   }
7529c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7530c58f1c22SToby Isaac }
7531c58f1c22SToby Isaac 
7532c58f1c22SToby Isaac /*@C
7533c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7534c58f1c22SToby Isaac 
7535c58f1c22SToby Isaac   Not Collective
7536c58f1c22SToby Isaac 
7537c58f1c22SToby Isaac   Input Parameters:
7538c58f1c22SToby Isaac + dm   - The DM object
7539c58f1c22SToby Isaac - name - The label name
7540c58f1c22SToby Isaac 
7541c58f1c22SToby Isaac   Output Parameter:
7542c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7543c58f1c22SToby Isaac 
7544c58f1c22SToby Isaac   Level: intermediate
7545c58f1c22SToby Isaac 
7546c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7547c58f1c22SToby Isaac @*/
7548c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7549c58f1c22SToby Isaac {
75505d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7551d67d17b1SMatthew G. Knepley   const char    *lname;
7552c58f1c22SToby Isaac   PetscErrorCode ierr;
7553c58f1c22SToby Isaac 
7554c58f1c22SToby Isaac   PetscFunctionBegin;
7555c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7556c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7557534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7558c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7559c58f1c22SToby Isaac   while (next) {
7560d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7561d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7562c58f1c22SToby Isaac     if (*hasLabel) break;
7563c58f1c22SToby Isaac     next = next->next;
7564c58f1c22SToby Isaac   }
7565c58f1c22SToby Isaac   PetscFunctionReturn(0);
7566c58f1c22SToby Isaac }
7567c58f1c22SToby Isaac 
7568c58f1c22SToby Isaac /*@C
7569c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7570c58f1c22SToby Isaac 
7571c58f1c22SToby Isaac   Not Collective
7572c58f1c22SToby Isaac 
7573c58f1c22SToby Isaac   Input Parameters:
7574c58f1c22SToby Isaac + dm   - The DM object
7575c58f1c22SToby Isaac - name - The label name
7576c58f1c22SToby Isaac 
7577c58f1c22SToby Isaac   Output Parameter:
7578c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7579c58f1c22SToby Isaac 
7580c58f1c22SToby Isaac   Level: intermediate
7581c58f1c22SToby Isaac 
7582c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7583c58f1c22SToby Isaac @*/
7584c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7585c58f1c22SToby Isaac {
75865d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7587c58f1c22SToby Isaac   PetscBool      hasLabel;
7588d67d17b1SMatthew G. Knepley   const char    *lname;
7589c58f1c22SToby Isaac   PetscErrorCode ierr;
7590c58f1c22SToby Isaac 
7591c58f1c22SToby Isaac   PetscFunctionBegin;
7592c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7593c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7594c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7595c58f1c22SToby Isaac   *label = NULL;
7596c58f1c22SToby Isaac   while (next) {
7597d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7598d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7599c58f1c22SToby Isaac     if (hasLabel) {
7600c58f1c22SToby Isaac       *label = next->label;
7601c58f1c22SToby Isaac       break;
7602c58f1c22SToby Isaac     }
7603c58f1c22SToby Isaac     next = next->next;
7604c58f1c22SToby Isaac   }
7605c58f1c22SToby Isaac   PetscFunctionReturn(0);
7606c58f1c22SToby Isaac }
7607c58f1c22SToby Isaac 
7608c58f1c22SToby Isaac /*@C
7609c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7610c58f1c22SToby Isaac 
7611c58f1c22SToby Isaac   Not Collective
7612c58f1c22SToby Isaac 
7613c58f1c22SToby Isaac   Input Parameters:
7614c58f1c22SToby Isaac + dm - The DM object
7615c58f1c22SToby Isaac - n  - the label number
7616c58f1c22SToby Isaac 
7617c58f1c22SToby Isaac   Output Parameter:
7618c58f1c22SToby Isaac . label - the label
7619c58f1c22SToby Isaac 
7620c58f1c22SToby Isaac   Level: intermediate
7621c58f1c22SToby Isaac 
7622c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7623c58f1c22SToby Isaac @*/
7624c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7625c58f1c22SToby Isaac {
76265d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7627c58f1c22SToby Isaac   PetscInt    l    = 0;
7628c58f1c22SToby Isaac 
7629c58f1c22SToby Isaac   PetscFunctionBegin;
7630c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7631c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7632c58f1c22SToby Isaac   while (next) {
7633c58f1c22SToby Isaac     if (l == n) {
7634c58f1c22SToby Isaac       *label = next->label;
7635c58f1c22SToby Isaac       PetscFunctionReturn(0);
7636c58f1c22SToby Isaac     }
7637c58f1c22SToby Isaac     ++l;
7638c58f1c22SToby Isaac     next = next->next;
7639c58f1c22SToby Isaac   }
7640c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7641c58f1c22SToby Isaac }
7642c58f1c22SToby Isaac 
7643c58f1c22SToby Isaac /*@C
7644c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7645c58f1c22SToby Isaac 
7646c58f1c22SToby Isaac   Not Collective
7647c58f1c22SToby Isaac 
7648c58f1c22SToby Isaac   Input Parameters:
7649c58f1c22SToby Isaac + dm   - The DM object
7650c58f1c22SToby Isaac - label - The DMLabel
7651c58f1c22SToby Isaac 
7652c58f1c22SToby Isaac   Level: developer
7653c58f1c22SToby Isaac 
7654c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7655c58f1c22SToby Isaac @*/
7656c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7657c58f1c22SToby Isaac {
76585d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7659c58f1c22SToby Isaac   PetscBool      hasLabel;
7660d67d17b1SMatthew G. Knepley   const char    *lname;
76615d80c0bfSVaclav Hapla   PetscBool      flg;
7662c58f1c22SToby Isaac   PetscErrorCode ierr;
7663c58f1c22SToby Isaac 
7664c58f1c22SToby Isaac   PetscFunctionBegin;
7665c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7666d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7667d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7668d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7669c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7670c58f1c22SToby Isaac   tmpLabel->label  = label;
7671c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
76725d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
76735d80c0bfSVaclav Hapla   *p = tmpLabel;
767408f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
76755d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
76765d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7677ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7678ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7679c58f1c22SToby Isaac   PetscFunctionReturn(0);
7680c58f1c22SToby Isaac }
7681c58f1c22SToby Isaac 
7682c58f1c22SToby Isaac /*@C
7683e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7684c58f1c22SToby Isaac 
7685c58f1c22SToby Isaac   Not Collective
7686c58f1c22SToby Isaac 
7687c58f1c22SToby Isaac   Input Parameters:
7688c58f1c22SToby Isaac + dm   - The DM object
7689c58f1c22SToby Isaac - name - The label name
7690c58f1c22SToby Isaac 
7691c58f1c22SToby Isaac   Output Parameter:
7692c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7693c58f1c22SToby Isaac 
7694c58f1c22SToby Isaac   Level: developer
7695c58f1c22SToby Isaac 
7696e5472504SVaclav Hapla   Notes:
7697e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7698e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7699e5472504SVaclav Hapla 
7700e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7701e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7702e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7703e5472504SVaclav Hapla 
7704e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7705c58f1c22SToby Isaac @*/
7706c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7707c58f1c22SToby Isaac {
770895d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7709c58f1c22SToby Isaac   PetscBool      hasLabel;
7710d67d17b1SMatthew G. Knepley   const char    *lname;
7711c58f1c22SToby Isaac   PetscErrorCode ierr;
7712c58f1c22SToby Isaac 
7713c58f1c22SToby Isaac   PetscFunctionBegin;
7714c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7715e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7716e5472504SVaclav Hapla   if (label) {
7717e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7718c58f1c22SToby Isaac     *label = NULL;
7719e5472504SVaclav Hapla   }
77205d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
772195d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7722d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7723c58f1c22SToby Isaac     if (hasLabel) {
772495d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7725c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
772695d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7727ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7728ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
772995d578d6SVaclav Hapla       if (label) *label = link->label;
773095d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
773195d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7732c58f1c22SToby Isaac       break;
7733c58f1c22SToby Isaac     }
7734c58f1c22SToby Isaac   }
7735c58f1c22SToby Isaac   PetscFunctionReturn(0);
7736c58f1c22SToby Isaac }
7737c58f1c22SToby Isaac 
7738306894acSVaclav Hapla /*@
7739306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7740306894acSVaclav Hapla 
7741306894acSVaclav Hapla   Not Collective
7742306894acSVaclav Hapla 
7743306894acSVaclav Hapla   Input Parameters:
7744306894acSVaclav Hapla + dm   - The DM object
7745306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7746306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7747306894acSVaclav Hapla 
7748306894acSVaclav Hapla   Level: developer
7749306894acSVaclav Hapla 
7750306894acSVaclav Hapla   Notes:
7751306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7752306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7753306894acSVaclav Hapla   *label nullified.
7754306894acSVaclav Hapla 
7755306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7756306894acSVaclav Hapla @*/
7757306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7758306894acSVaclav Hapla {
775943e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7760306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7761306894acSVaclav Hapla   PetscErrorCode ierr;
7762306894acSVaclav Hapla 
7763306894acSVaclav Hapla   PetscFunctionBegin;
7764306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7765306894acSVaclav Hapla   PetscValidPointer(label, 2);
7766f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7767306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7768306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
77695d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
777043e45a93SVaclav Hapla     if (*label == link->label) {
7771306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
777243e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7773306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7774ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
777543e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
777643e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
777743e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7778306894acSVaclav Hapla       break;
7779306894acSVaclav Hapla     }
7780306894acSVaclav Hapla   }
7781306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7782306894acSVaclav Hapla   PetscFunctionReturn(0);
7783306894acSVaclav Hapla }
7784306894acSVaclav Hapla 
7785c58f1c22SToby Isaac /*@C
7786c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7787c58f1c22SToby Isaac 
7788c58f1c22SToby Isaac   Not Collective
7789c58f1c22SToby Isaac 
7790c58f1c22SToby Isaac   Input Parameters:
7791c58f1c22SToby Isaac + dm   - The DM object
7792c58f1c22SToby Isaac - name - The label name
7793c58f1c22SToby Isaac 
7794c58f1c22SToby Isaac   Output Parameter:
7795c58f1c22SToby Isaac . output - The flag for output
7796c58f1c22SToby Isaac 
7797c58f1c22SToby Isaac   Level: developer
7798c58f1c22SToby Isaac 
7799c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7800c58f1c22SToby Isaac @*/
7801c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7802c58f1c22SToby Isaac {
78035d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7804d67d17b1SMatthew G. Knepley   const char    *lname;
7805c58f1c22SToby Isaac   PetscErrorCode ierr;
7806c58f1c22SToby Isaac 
7807c58f1c22SToby Isaac   PetscFunctionBegin;
7808c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7809c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7810c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7811c58f1c22SToby Isaac   while (next) {
7812c58f1c22SToby Isaac     PetscBool flg;
7813c58f1c22SToby Isaac 
7814d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7815d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7816c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7817c58f1c22SToby Isaac     next = next->next;
7818c58f1c22SToby Isaac   }
7819c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7820c58f1c22SToby Isaac }
7821c58f1c22SToby Isaac 
7822c58f1c22SToby Isaac /*@C
7823c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7824c58f1c22SToby Isaac 
7825c58f1c22SToby Isaac   Not Collective
7826c58f1c22SToby Isaac 
7827c58f1c22SToby Isaac   Input Parameters:
7828c58f1c22SToby Isaac + dm     - The DM object
7829c58f1c22SToby Isaac . name   - The label name
7830c58f1c22SToby Isaac - output - The flag for output
7831c58f1c22SToby Isaac 
7832c58f1c22SToby Isaac   Level: developer
7833c58f1c22SToby Isaac 
7834c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7835c58f1c22SToby Isaac @*/
7836c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7837c58f1c22SToby Isaac {
78385d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7839d67d17b1SMatthew G. Knepley   const char    *lname;
7840c58f1c22SToby Isaac   PetscErrorCode ierr;
7841c58f1c22SToby Isaac 
7842c58f1c22SToby Isaac   PetscFunctionBegin;
7843c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7844534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7845c58f1c22SToby Isaac   while (next) {
7846c58f1c22SToby Isaac     PetscBool flg;
7847c58f1c22SToby Isaac 
7848d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7849d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7850c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7851c58f1c22SToby Isaac     next = next->next;
7852c58f1c22SToby Isaac   }
7853c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7854c58f1c22SToby Isaac }
7855c58f1c22SToby Isaac 
7856c58f1c22SToby Isaac /*@
7857c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7858c58f1c22SToby Isaac 
7859d083f849SBarry Smith   Collective on dmA
7860c58f1c22SToby Isaac 
7861c58f1c22SToby Isaac   Input Parameter:
78625d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
78632e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
78645d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7865ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7866c58f1c22SToby Isaac 
7867c58f1c22SToby Isaac   Level: intermediate
7868c58f1c22SToby Isaac 
7869c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7870c58f1c22SToby Isaac 
78715d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7872c58f1c22SToby Isaac @*/
78735d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7874c58f1c22SToby Isaac {
7875c58f1c22SToby Isaac   DMLabel        label, labelNew;
7876c58f1c22SToby Isaac   const char    *name;
7877c58f1c22SToby Isaac   PetscBool      flg;
78785d80c0bfSVaclav Hapla   DMLabelLink    link;
78795d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7880c58f1c22SToby Isaac 
78815d80c0bfSVaclav Hapla   PetscFunctionBegin;
78825d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
78835d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
78845d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
78855d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
78865d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
78875d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
78885d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
78895d80c0bfSVaclav Hapla     label=link->label;
78905d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
78915d80c0bfSVaclav Hapla     if (!all) {
7892c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7893c58f1c22SToby Isaac       if (flg) continue;
78947d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
78957d5acc75SStefano Zampini       if (flg) continue;
7896ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7897ba2698f1SMatthew G. Knepley       if (flg) continue;
78985d80c0bfSVaclav Hapla     }
78995d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7900c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
79015d80c0bfSVaclav Hapla     } else {
79025d80c0bfSVaclav Hapla       labelNew = label;
79035d80c0bfSVaclav Hapla     }
7904c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
79055d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7906c58f1c22SToby Isaac   }
7907c58f1c22SToby Isaac   PetscFunctionReturn(0);
7908c58f1c22SToby Isaac }
7909a8fb8f29SToby Isaac 
7910a8fb8f29SToby Isaac /*@
7911a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7912a8fb8f29SToby Isaac 
7913a8fb8f29SToby Isaac   Input Parameter:
7914a8fb8f29SToby Isaac . dm - The DM object
7915a8fb8f29SToby Isaac 
7916a8fb8f29SToby Isaac   Output Parameter:
7917a8fb8f29SToby Isaac . cdm - The coarse DM
7918a8fb8f29SToby Isaac 
7919a8fb8f29SToby Isaac   Level: intermediate
7920a8fb8f29SToby Isaac 
7921a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7922a8fb8f29SToby Isaac @*/
7923a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7924a8fb8f29SToby Isaac {
7925a8fb8f29SToby Isaac   PetscFunctionBegin;
7926a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7927a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7928a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7929a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7930a8fb8f29SToby Isaac }
7931a8fb8f29SToby Isaac 
7932a8fb8f29SToby Isaac /*@
7933a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7934a8fb8f29SToby Isaac 
7935a8fb8f29SToby Isaac   Input Parameters:
7936a8fb8f29SToby Isaac + dm - The DM object
7937a8fb8f29SToby Isaac - cdm - The coarse DM
7938a8fb8f29SToby Isaac 
7939a8fb8f29SToby Isaac   Level: intermediate
7940a8fb8f29SToby Isaac 
7941a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7942a8fb8f29SToby Isaac @*/
7943a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7944a8fb8f29SToby Isaac {
7945a8fb8f29SToby Isaac   PetscErrorCode ierr;
7946a8fb8f29SToby Isaac 
7947a8fb8f29SToby Isaac   PetscFunctionBegin;
7948a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7949a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7950a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7951a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7952a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7953a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7954a8fb8f29SToby Isaac }
7955a8fb8f29SToby Isaac 
795688bdff64SToby Isaac /*@
795788bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
795888bdff64SToby Isaac 
795988bdff64SToby Isaac   Input Parameter:
796088bdff64SToby Isaac . dm - The DM object
796188bdff64SToby Isaac 
796288bdff64SToby Isaac   Output Parameter:
796388bdff64SToby Isaac . fdm - The fine DM
796488bdff64SToby Isaac 
796588bdff64SToby Isaac   Level: intermediate
796688bdff64SToby Isaac 
796788bdff64SToby Isaac .seealso: DMSetFineDM()
796888bdff64SToby Isaac @*/
796988bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
797088bdff64SToby Isaac {
797188bdff64SToby Isaac   PetscFunctionBegin;
797288bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
797388bdff64SToby Isaac   PetscValidPointer(fdm, 2);
797488bdff64SToby Isaac   *fdm = dm->fineMesh;
797588bdff64SToby Isaac   PetscFunctionReturn(0);
797688bdff64SToby Isaac }
797788bdff64SToby Isaac 
797888bdff64SToby Isaac /*@
797988bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
798088bdff64SToby Isaac 
798188bdff64SToby Isaac   Input Parameters:
798288bdff64SToby Isaac + dm - The DM object
798388bdff64SToby Isaac - fdm - The fine DM
798488bdff64SToby Isaac 
798588bdff64SToby Isaac   Level: intermediate
798688bdff64SToby Isaac 
798788bdff64SToby Isaac .seealso: DMGetFineDM()
798888bdff64SToby Isaac @*/
798988bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
799088bdff64SToby Isaac {
799188bdff64SToby Isaac   PetscErrorCode ierr;
799288bdff64SToby Isaac 
799388bdff64SToby Isaac   PetscFunctionBegin;
799488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
799588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
799688bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
799788bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
799888bdff64SToby Isaac   dm->fineMesh = fdm;
799988bdff64SToby Isaac   PetscFunctionReturn(0);
800088bdff64SToby Isaac }
800188bdff64SToby Isaac 
8002a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8003a6ba4734SToby Isaac 
8004a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
8005a6ba4734SToby Isaac {
8006e5e52638SMatthew G. Knepley   PetscInt       d;
8007a6ba4734SToby Isaac   PetscErrorCode ierr;
8008a6ba4734SToby Isaac 
8009a6ba4734SToby Isaac   PetscFunctionBegin;
8010e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
8011e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
8012e5e52638SMatthew G. Knepley   }
8013a6ba4734SToby Isaac   PetscFunctionReturn(0);
8014a6ba4734SToby Isaac }
8015a6ba4734SToby Isaac 
8016a6ba4734SToby Isaac /*@C
8017a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8018a6ba4734SToby Isaac 
8019783e2ec8SMatthew G. Knepley   Collective on dm
8020783e2ec8SMatthew G. Knepley 
8021a6ba4734SToby Isaac   Input Parameters:
80224c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
8023f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8024a6ba4734SToby Isaac . name        - The BC name
8025a6ba4734SToby Isaac . labelname   - The label defining constrained points
8026a6ba4734SToby Isaac . field       - The field to constrain
8027e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
8028a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8029a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
803056cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time deriative of the boundary values, or NULL
8031a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8032a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8033a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8034a6ba4734SToby Isaac 
8035a6ba4734SToby Isaac   Options Database Keys:
8036a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8037a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8038a6ba4734SToby Isaac 
803956cf3b9cSMatthew G. Knepley   Note:
804056cf3b9cSMatthew 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:
804156cf3b9cSMatthew G. Knepley 
804256cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
804356cf3b9cSMatthew G. Knepley 
804456cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
804556cf3b9cSMatthew G. Knepley 
804656cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
804756cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
804856cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
804956cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
805056cf3b9cSMatthew G. Knepley 
805156cf3b9cSMatthew G. Knepley + dim - the spatial dimension
805256cf3b9cSMatthew G. Knepley . Nf - the number of fields
805356cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
805456cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
805556cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
805656cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
805756cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
805856cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
805956cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
806056cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
806156cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
806256cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
806356cf3b9cSMatthew G. Knepley . t - current time
806456cf3b9cSMatthew G. Knepley . x - coordinates of the current point
806556cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
806656cf3b9cSMatthew G. Knepley . constants - constant parameters
806756cf3b9cSMatthew G. Knepley - bcval - output values at the current point
806856cf3b9cSMatthew G. Knepley 
8069a6ba4734SToby Isaac   Level: developer
8070a6ba4734SToby Isaac 
807156cf3b9cSMatthew G. Knepley .seealso: DMGetBoundary(), PetscDSAddBoundary()
8072a6ba4734SToby Isaac @*/
807356cf3b9cSMatthew 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)
8074a6ba4734SToby Isaac {
8075e5e52638SMatthew G. Knepley   PetscDS        ds;
8076a6ba4734SToby Isaac   PetscErrorCode ierr;
8077a6ba4734SToby Isaac 
8078a6ba4734SToby Isaac   PetscFunctionBegin;
8079a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8080783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
8081783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
8082783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
8083783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
8084e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8085783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
808656cf3b9cSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, bcFunc_t, numids, ids, ctx);CHKERRQ(ierr);
8087a6ba4734SToby Isaac   PetscFunctionReturn(0);
8088a6ba4734SToby Isaac }
8089a6ba4734SToby Isaac 
8090a6ba4734SToby Isaac /*@
8091a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
8092a6ba4734SToby Isaac 
8093a6ba4734SToby Isaac   Input Parameters:
8094a6ba4734SToby Isaac . dm - The mesh object
8095a6ba4734SToby Isaac 
8096a6ba4734SToby Isaac   Output Parameters:
8097a6ba4734SToby Isaac . numBd - The number of BC
8098a6ba4734SToby Isaac 
8099a6ba4734SToby Isaac   Level: intermediate
8100a6ba4734SToby Isaac 
8101a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
8102a6ba4734SToby Isaac @*/
8103a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
8104a6ba4734SToby Isaac {
8105e5e52638SMatthew G. Knepley   PetscDS        ds;
810658ebd649SToby Isaac   PetscErrorCode ierr;
8107a6ba4734SToby Isaac 
8108a6ba4734SToby Isaac   PetscFunctionBegin;
8109a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8110e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8111e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
8112a6ba4734SToby Isaac   PetscFunctionReturn(0);
8113a6ba4734SToby Isaac }
8114a6ba4734SToby Isaac 
8115a6ba4734SToby Isaac /*@C
81161c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
8117a6ba4734SToby Isaac 
8118a6ba4734SToby Isaac   Input Parameters:
8119a6ba4734SToby Isaac + dm          - The mesh object
8120a6ba4734SToby Isaac - bd          - The BC number
8121a6ba4734SToby Isaac 
8122a6ba4734SToby Isaac   Output Parameters:
8123f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8124a6ba4734SToby Isaac . name        - The BC name
8125a6ba4734SToby Isaac . labelname   - The label defining constrained points
8126a6ba4734SToby Isaac . field       - The field to constrain
8127a6ba4734SToby Isaac . numcomps    - The number of constrained field components
8128a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8129a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
813056cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time derviative of the boundary values
8131a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8132a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8133a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8134a6ba4734SToby Isaac 
8135a6ba4734SToby Isaac   Options Database Keys:
8136a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8137a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8138a6ba4734SToby Isaac 
8139a6ba4734SToby Isaac   Level: developer
8140a6ba4734SToby Isaac 
8141a6ba4734SToby Isaac .seealso: DMAddBoundary()
8142a6ba4734SToby Isaac @*/
814356cf3b9cSMatthew 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)
8144a6ba4734SToby Isaac {
8145e5e52638SMatthew G. Knepley   PetscDS        ds;
814658ebd649SToby Isaac   PetscErrorCode ierr;
8147a6ba4734SToby Isaac 
8148a6ba4734SToby Isaac   PetscFunctionBegin;
8149a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8150e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
815156cf3b9cSMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, func_t, numids, ids, ctx);CHKERRQ(ierr);
8152a6ba4734SToby Isaac   PetscFunctionReturn(0);
8153a6ba4734SToby Isaac }
8154a6ba4734SToby Isaac 
8155e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8156e6f8dbb6SToby Isaac {
8157e5e52638SMatthew G. Knepley   PetscDS        ds;
8158dff059c6SToby Isaac   DMBoundary    *lastnext;
8159e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8160e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8161e6f8dbb6SToby Isaac 
8162e6f8dbb6SToby Isaac   PetscFunctionBegin;
8163e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8164e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
816547a1f5adSToby Isaac   if (dm->boundary) {
816647a1f5adSToby Isaac     DMBoundary next = dm->boundary;
816747a1f5adSToby Isaac 
816847a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
816947a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
817047a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
817147a1f5adSToby Isaac     while (next) {
817247a1f5adSToby Isaac       DMBoundary b = next;
817347a1f5adSToby Isaac 
817447a1f5adSToby Isaac       next = b->next;
817547a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8176a6ba4734SToby Isaac     }
817747a1f5adSToby Isaac     dm->boundary = NULL;
8178a6ba4734SToby Isaac   }
817947a1f5adSToby Isaac 
8180dff059c6SToby Isaac   lastnext = &(dm->boundary);
8181e6f8dbb6SToby Isaac   while (dsbound) {
8182e6f8dbb6SToby Isaac     DMBoundary dmbound;
8183e6f8dbb6SToby Isaac 
8184e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8185e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
8186e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
8187994fe344SLisandro 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);}
818847a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8189dff059c6SToby Isaac     *lastnext = dmbound;
8190dff059c6SToby Isaac     lastnext = &(dmbound->next);
8191dff059c6SToby Isaac     dsbound = dsbound->next;
8192a6ba4734SToby Isaac   }
8193a6ba4734SToby Isaac   PetscFunctionReturn(0);
8194a6ba4734SToby Isaac }
8195a6ba4734SToby Isaac 
8196a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8197a6ba4734SToby Isaac {
8198b95f2879SToby Isaac   DMBoundary     b;
8199a6ba4734SToby Isaac   PetscErrorCode ierr;
8200a6ba4734SToby Isaac 
8201a6ba4734SToby Isaac   PetscFunctionBegin;
8202a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8203534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8204a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8205e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8206b95f2879SToby Isaac   b = dm->boundary;
8207a6ba4734SToby Isaac   while (b && !(*isBd)) {
8208e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8209e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
82103424c85cSToby Isaac 
82113424c85cSToby Isaac     if (label) {
8212a6ba4734SToby Isaac       PetscInt i;
8213a6ba4734SToby Isaac 
8214e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
8215e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
8216a6ba4734SToby Isaac       }
8217a6ba4734SToby Isaac     }
8218a6ba4734SToby Isaac     b = b->next;
8219a6ba4734SToby Isaac   }
8220a6ba4734SToby Isaac   PetscFunctionReturn(0);
8221a6ba4734SToby Isaac }
82224d6f44ffSToby Isaac 
82234d6f44ffSToby Isaac /*@C
8224a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8225a6e0b375SMatthew G. Knepley 
8226a6e0b375SMatthew G. Knepley   Collective on DM
82274d6f44ffSToby Isaac 
82284d6f44ffSToby Isaac   Input Parameters:
82294d6f44ffSToby Isaac + dm      - The DM
82300709b2feSToby Isaac . time    - The time
82314d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
82324d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
82334d6f44ffSToby Isaac - mode    - The insertion mode for values
82344d6f44ffSToby Isaac 
82354d6f44ffSToby Isaac   Output Parameter:
82364d6f44ffSToby Isaac . X - vector
82374d6f44ffSToby Isaac 
82384d6f44ffSToby Isaac    Calling sequence of func:
82390709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
82404d6f44ffSToby Isaac 
82414d6f44ffSToby Isaac +  dim - The spatial dimension
82428ec8862eSJed Brown .  time - The time at which to sample
82434d6f44ffSToby Isaac .  x   - The coordinates
82444d6f44ffSToby Isaac .  Nf  - The number of fields
82454d6f44ffSToby Isaac .  u   - The output field values
82464d6f44ffSToby Isaac -  ctx - optional user-defined function context
82474d6f44ffSToby Isaac 
82484d6f44ffSToby Isaac   Level: developer
82494d6f44ffSToby Isaac 
8250a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
82514d6f44ffSToby Isaac @*/
82520709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
82534d6f44ffSToby Isaac {
82544d6f44ffSToby Isaac   Vec            localX;
82554d6f44ffSToby Isaac   PetscErrorCode ierr;
82564d6f44ffSToby Isaac 
82574d6f44ffSToby Isaac   PetscFunctionBegin;
82584d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82594d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
82600709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
82614d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
82624d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
82634d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
82644d6f44ffSToby Isaac   PetscFunctionReturn(0);
82654d6f44ffSToby Isaac }
82664d6f44ffSToby Isaac 
8267a6e0b375SMatthew G. Knepley /*@C
8268a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8269a6e0b375SMatthew G. Knepley 
8270a6e0b375SMatthew G. Knepley   Not collective
8271a6e0b375SMatthew G. Knepley 
8272a6e0b375SMatthew G. Knepley   Input Parameters:
8273a6e0b375SMatthew G. Knepley + dm      - The DM
8274a6e0b375SMatthew G. Knepley . time    - The time
8275a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8276a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8277a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8278a6e0b375SMatthew G. Knepley 
8279a6e0b375SMatthew G. Knepley   Output Parameter:
8280a6e0b375SMatthew G. Knepley . localX - vector
8281a6e0b375SMatthew G. Knepley 
8282a6e0b375SMatthew G. Knepley    Calling sequence of func:
8283a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8284a6e0b375SMatthew G. Knepley 
8285a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8286a6e0b375SMatthew G. Knepley .  x   - The coordinates
8287a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8288a6e0b375SMatthew G. Knepley .  u   - The output field values
8289a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8290a6e0b375SMatthew G. Knepley 
8291a6e0b375SMatthew G. Knepley   Level: developer
8292a6e0b375SMatthew G. Knepley 
8293a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8294a6e0b375SMatthew G. Knepley @*/
82950709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
82964d6f44ffSToby Isaac {
82974d6f44ffSToby Isaac   PetscErrorCode ierr;
82984d6f44ffSToby Isaac 
82994d6f44ffSToby Isaac   PetscFunctionBegin;
83004d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83014d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
83020918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
83030709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83044d6f44ffSToby Isaac   PetscFunctionReturn(0);
83054d6f44ffSToby Isaac }
83064d6f44ffSToby Isaac 
8307a6e0b375SMatthew G. Knepley /*@C
8308a6e0b375SMatthew 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.
8309a6e0b375SMatthew G. Knepley 
8310a6e0b375SMatthew G. Knepley   Collective on DM
8311a6e0b375SMatthew G. Knepley 
8312a6e0b375SMatthew G. Knepley   Input Parameters:
8313a6e0b375SMatthew G. Knepley + dm      - The DM
8314a6e0b375SMatthew G. Knepley . time    - The time
8315a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8316a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8317a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8318a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8319a6e0b375SMatthew G. Knepley 
8320a6e0b375SMatthew G. Knepley   Output Parameter:
8321a6e0b375SMatthew G. Knepley . X - vector
8322a6e0b375SMatthew G. Knepley 
8323a6e0b375SMatthew G. Knepley    Calling sequence of func:
8324a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8325a6e0b375SMatthew G. Knepley 
8326a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8327a6e0b375SMatthew G. Knepley .  x   - The coordinates
8328a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8329a6e0b375SMatthew G. Knepley .  u   - The output field values
8330a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8331a6e0b375SMatthew G. Knepley 
8332a6e0b375SMatthew G. Knepley   Level: developer
8333a6e0b375SMatthew G. Knepley 
8334a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8335a6e0b375SMatthew G. Knepley @*/
83362c53366bSMatthew 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)
83372c53366bSMatthew G. Knepley {
83382c53366bSMatthew G. Knepley   Vec            localX;
83392c53366bSMatthew G. Knepley   PetscErrorCode ierr;
83402c53366bSMatthew G. Knepley 
83412c53366bSMatthew G. Knepley   PetscFunctionBegin;
83422c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83432c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
83442c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83452c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
83462c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
83472c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
83482c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
83492c53366bSMatthew G. Knepley }
83502c53366bSMatthew G. Knepley 
8351a6e0b375SMatthew G. Knepley /*@C
8352a6e0b375SMatthew 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.
8353a6e0b375SMatthew G. Knepley 
8354a6e0b375SMatthew G. Knepley   Not collective
8355a6e0b375SMatthew G. Knepley 
8356a6e0b375SMatthew G. Knepley   Input Parameters:
8357a6e0b375SMatthew G. Knepley + dm      - The DM
8358a6e0b375SMatthew G. Knepley . time    - The time
8359a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8360a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8361a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8362a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8363a6e0b375SMatthew G. Knepley 
8364a6e0b375SMatthew G. Knepley   Output Parameter:
8365a6e0b375SMatthew G. Knepley . localX - vector
8366a6e0b375SMatthew G. Knepley 
8367a6e0b375SMatthew G. Knepley    Calling sequence of func:
8368a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8369a6e0b375SMatthew G. Knepley 
8370a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8371a6e0b375SMatthew G. Knepley .  x   - The coordinates
8372a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8373a6e0b375SMatthew G. Knepley .  u   - The output field values
8374a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8375a6e0b375SMatthew G. Knepley 
8376a6e0b375SMatthew G. Knepley   Level: developer
8377a6e0b375SMatthew G. Knepley 
8378a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8379a6e0b375SMatthew G. Knepley @*/
83801c531cf8SMatthew 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)
83814d6f44ffSToby Isaac {
83824d6f44ffSToby Isaac   PetscErrorCode ierr;
83834d6f44ffSToby Isaac 
83844d6f44ffSToby Isaac   PetscFunctionBegin;
83854d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83864d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
83870918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
83881c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83894d6f44ffSToby Isaac   PetscFunctionReturn(0);
83904d6f44ffSToby Isaac }
83912716604bSToby Isaac 
8392a6e0b375SMatthew G. Knepley /*@C
8393a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8394a6e0b375SMatthew G. Knepley 
8395a6e0b375SMatthew G. Knepley   Not collective
8396a6e0b375SMatthew G. Knepley 
8397a6e0b375SMatthew G. Knepley   Input Parameters:
8398a6e0b375SMatthew G. Knepley + dm      - The DM
8399a6e0b375SMatthew G. Knepley . time    - The time
8400a6e0b375SMatthew G. Knepley . localU  - The input field vector
8401a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8402a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8403a6e0b375SMatthew G. Knepley 
8404a6e0b375SMatthew G. Knepley   Output Parameter:
8405a6e0b375SMatthew G. Knepley . localX  - The output vector
8406a6e0b375SMatthew G. Knepley 
8407a6e0b375SMatthew G. Knepley    Calling sequence of func:
8408a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8409a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8410a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8411a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8412a6e0b375SMatthew G. Knepley 
8413a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8414a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8415a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8416a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8417a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8418a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8419a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8420a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8421a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8422a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8423a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8424a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8425a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8426a6e0b375SMatthew G. Knepley .  t            - The current time
8427a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8428a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8429a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8430a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8431a6e0b375SMatthew G. Knepley 
8432a6e0b375SMatthew 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.
8433a6e0b375SMatthew 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
8434a6e0b375SMatthew 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
8435a6e0b375SMatthew 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.
8436a6e0b375SMatthew G. Knepley 
8437a6e0b375SMatthew G. Knepley   Level: intermediate
8438a6e0b375SMatthew G. Knepley 
8439a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8440a6e0b375SMatthew G. Knepley @*/
84418c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
84428c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
84438c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
84448c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8445191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
84468c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
84478c6c5593SMatthew G. Knepley {
84488c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
84498c6c5593SMatthew G. Knepley 
84508c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84518c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84528c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
84538c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
84540918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
84558c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
84568c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
84578c6c5593SMatthew G. Knepley }
84588c6c5593SMatthew G. Knepley 
8459a6e0b375SMatthew G. Knepley /*@C
8460a6e0b375SMatthew 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.
8461a6e0b375SMatthew G. Knepley 
8462a6e0b375SMatthew G. Knepley   Not collective
8463a6e0b375SMatthew G. Knepley 
8464a6e0b375SMatthew G. Knepley   Input Parameters:
8465a6e0b375SMatthew G. Knepley + dm      - The DM
8466a6e0b375SMatthew G. Knepley . time    - The time
8467a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8468a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8469a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8470a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8471a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8472a6e0b375SMatthew G. Knepley . localU  - The input field vector
8473a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8474a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8475a6e0b375SMatthew G. Knepley 
8476a6e0b375SMatthew G. Knepley   Output Parameter:
8477a6e0b375SMatthew G. Knepley . localX  - The output vector
8478a6e0b375SMatthew G. Knepley 
8479a6e0b375SMatthew G. Knepley    Calling sequence of func:
8480a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8481a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8482a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8483a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8484a6e0b375SMatthew G. Knepley 
8485a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8486a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8487a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8488a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8489a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8490a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8491a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8492a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8493a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8494a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8495a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8496a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8497a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8498a6e0b375SMatthew G. Knepley .  t            - The current time
8499a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8500a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8501a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8502a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8503a6e0b375SMatthew G. Knepley 
8504a6e0b375SMatthew 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.
8505a6e0b375SMatthew 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
8506a6e0b375SMatthew 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
8507a6e0b375SMatthew 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.
8508a6e0b375SMatthew G. Knepley 
8509a6e0b375SMatthew G. Knepley   Level: intermediate
8510a6e0b375SMatthew G. Knepley 
8511a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8512a6e0b375SMatthew G. Knepley @*/
85131c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
85148c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
85158c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
85168c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8517191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
85188c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
85198c6c5593SMatthew G. Knepley {
85208c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
85218c6c5593SMatthew G. Knepley 
85228c6c5593SMatthew G. Knepley   PetscFunctionBegin;
85238c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85248c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
85258c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8526ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
85271c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
85288c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
85298c6c5593SMatthew G. Knepley }
85308c6c5593SMatthew G. Knepley 
85312716604bSToby Isaac /*@C
8532ece3a9fcSMatthew 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.
8533ece3a9fcSMatthew G. Knepley 
8534ece3a9fcSMatthew G. Knepley   Not collective
8535ece3a9fcSMatthew G. Knepley 
8536ece3a9fcSMatthew G. Knepley   Input Parameters:
8537ece3a9fcSMatthew G. Knepley + dm      - The DM
8538ece3a9fcSMatthew G. Knepley . time    - The time
8539ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8540ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8541ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8542ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8543ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8544ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8545ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8546ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8547ece3a9fcSMatthew G. Knepley 
8548ece3a9fcSMatthew G. Knepley   Output Parameter:
8549ece3a9fcSMatthew G. Knepley . localX  - The output vector
8550ece3a9fcSMatthew G. Knepley 
8551ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8552ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8553ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8554ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8555ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8556ece3a9fcSMatthew G. Knepley 
8557ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8558ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8559ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8560ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8561ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8562ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8563ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8564ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8565ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8566ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8567ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8568ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8569ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8570ece3a9fcSMatthew G. Knepley .  t            - The current time
8571ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8572ece3a9fcSMatthew G. Knepley .  n            - The face normal
8573ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8574ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8575ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8576ece3a9fcSMatthew G. Knepley 
8577ece3a9fcSMatthew G. Knepley   Note:
8578ece3a9fcSMatthew 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.
8579ece3a9fcSMatthew 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
8580ece3a9fcSMatthew 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
8581ece3a9fcSMatthew 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.
8582ece3a9fcSMatthew G. Knepley 
8583ece3a9fcSMatthew G. Knepley   Level: intermediate
8584ece3a9fcSMatthew G. Knepley 
8585ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8586ece3a9fcSMatthew G. Knepley @*/
8587ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8588ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8589ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8590ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8591ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8592ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8593ece3a9fcSMatthew G. Knepley {
8594ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8595ece3a9fcSMatthew G. Knepley 
8596ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8597ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8598ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8599ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8600ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8601ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8602ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8603ece3a9fcSMatthew G. Knepley }
8604ece3a9fcSMatthew G. Knepley 
8605ece3a9fcSMatthew G. Knepley /*@C
86062716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
86072716604bSToby Isaac 
86082716604bSToby Isaac   Input Parameters:
86092716604bSToby Isaac + dm    - The DM
86100709b2feSToby Isaac . time  - The time
86112716604bSToby Isaac . funcs - The functions to evaluate for each field component
86122716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8613574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86142716604bSToby Isaac 
86152716604bSToby Isaac   Output Parameter:
86162716604bSToby Isaac . diff - The diff ||u - u_h||_2
86172716604bSToby Isaac 
86182716604bSToby Isaac   Level: developer
86192716604bSToby Isaac 
86201189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
86212716604bSToby Isaac @*/
86220709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
86232716604bSToby Isaac {
86242716604bSToby Isaac   PetscErrorCode ierr;
86252716604bSToby Isaac 
86262716604bSToby Isaac   PetscFunctionBegin;
86272716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8628b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
86290918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
86300709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
86312716604bSToby Isaac   PetscFunctionReturn(0);
86322716604bSToby Isaac }
8633b698f381SToby Isaac 
8634b698f381SToby Isaac /*@C
8635b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8636b698f381SToby Isaac 
8637d083f849SBarry Smith   Collective on dm
8638d083f849SBarry Smith 
8639b698f381SToby Isaac   Input Parameters:
8640b698f381SToby Isaac + dm    - The DM
8641b698f381SToby Isaac , time  - The time
8642b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8643b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8644574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8645b698f381SToby Isaac - n     - The vector to project along
8646b698f381SToby Isaac 
8647b698f381SToby Isaac   Output Parameter:
8648b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8649b698f381SToby Isaac 
8650b698f381SToby Isaac   Level: developer
8651b698f381SToby Isaac 
8652b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8653b698f381SToby Isaac @*/
8654b698f381SToby 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)
8655b698f381SToby Isaac {
8656b698f381SToby Isaac   PetscErrorCode ierr;
8657b698f381SToby Isaac 
8658b698f381SToby Isaac   PetscFunctionBegin;
8659b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8660b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8661b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8662b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8663b698f381SToby Isaac   PetscFunctionReturn(0);
8664b698f381SToby Isaac }
8665b698f381SToby Isaac 
86662a16baeaSToby Isaac /*@C
86672a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86682a16baeaSToby Isaac 
8669d083f849SBarry Smith   Collective on dm
8670d083f849SBarry Smith 
86712a16baeaSToby Isaac   Input Parameters:
86722a16baeaSToby Isaac + dm    - The DM
86732a16baeaSToby Isaac . time  - The time
86742a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86752a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8676574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86772a16baeaSToby Isaac 
86782a16baeaSToby Isaac   Output Parameter:
86792a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86802a16baeaSToby Isaac 
86812a16baeaSToby Isaac   Level: developer
86822a16baeaSToby Isaac 
86831189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
86842a16baeaSToby Isaac @*/
86851189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
86862a16baeaSToby Isaac {
86872a16baeaSToby Isaac   PetscErrorCode ierr;
86882a16baeaSToby Isaac 
86892a16baeaSToby Isaac   PetscFunctionBegin;
86902a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86912a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
86920918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
86932a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
86942a16baeaSToby Isaac   PetscFunctionReturn(0);
86952a16baeaSToby Isaac }
86962a16baeaSToby Isaac 
8697df0b854cSToby Isaac /*@C
8698df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8699cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8700df0b854cSToby Isaac 
8701df0b854cSToby Isaac   Collective on dm
8702df0b854cSToby Isaac 
8703df0b854cSToby Isaac   Input parameters:
8704df0b854cSToby Isaac + dm - the pre-adaptation DM object
8705a1b0c543SToby Isaac - label - label with the flags
8706df0b854cSToby Isaac 
8707df0b854cSToby Isaac   Output parameters:
87080d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8709df0b854cSToby Isaac 
8710df0b854cSToby Isaac   Level: intermediate
87110d1cd5e0SMatthew G. Knepley 
87120d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8713df0b854cSToby Isaac @*/
87140d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8715df0b854cSToby Isaac {
8716df0b854cSToby Isaac   PetscErrorCode ierr;
8717df0b854cSToby Isaac 
8718df0b854cSToby Isaac   PetscFunctionBegin;
8719df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8720a1b0c543SToby Isaac   PetscValidPointer(label,2);
87210d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
87220d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
87236f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
87240d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
8725c8a6034eSMark   if (*dmAdapt) (*dmAdapt)->prealloc_only = dm->prealloc_only;  /* maybe this should go .... */
87260d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
87270d1cd5e0SMatthew G. Knepley }
87280d1cd5e0SMatthew G. Knepley 
87290d1cd5e0SMatthew G. Knepley /*@C
87300d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
87310d1cd5e0SMatthew G. Knepley 
87320d1cd5e0SMatthew G. Knepley   Input Parameters:
87330d1cd5e0SMatthew G. Knepley + dm - The DM object
87340d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
87356f25b0d8SLisandro 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_".
87360d1cd5e0SMatthew G. Knepley 
87370d1cd5e0SMatthew G. Knepley   Output Parameter:
87380d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
87390d1cd5e0SMatthew G. Knepley 
87400d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
87410d1cd5e0SMatthew G. Knepley 
87420d1cd5e0SMatthew G. Knepley   Level: advanced
87430d1cd5e0SMatthew G. Knepley 
87440d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
87450d1cd5e0SMatthew G. Knepley @*/
87460d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
87470d1cd5e0SMatthew G. Knepley {
87480d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
87490d1cd5e0SMatthew G. Knepley 
87500d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
87510d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87520d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
87530d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
87540d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
87556f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
87566f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
87570d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8758df0b854cSToby Isaac   PetscFunctionReturn(0);
8759df0b854cSToby Isaac }
8760c4088d22SMatthew G. Knepley 
8761502a2867SDave May /*@C
8762502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8763502a2867SDave May 
8764502a2867SDave May  Not Collective
8765502a2867SDave May 
8766502a2867SDave May  Input Parameter:
8767502a2867SDave May .  dm    - The DM
8768502a2867SDave May 
87690a19bb7dSprj-  Output Parameters:
87700a19bb7dSprj- +  nranks - the number of neighbours
87710a19bb7dSprj- -  ranks - the neighbors ranks
8772502a2867SDave May 
8773502a2867SDave May  Notes:
8774502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8775502a2867SDave May 
8776502a2867SDave May  Level: beginner
8777502a2867SDave May 
8778dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8779502a2867SDave May @*/
8780502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8781502a2867SDave May {
8782502a2867SDave May   PetscErrorCode ierr;
8783502a2867SDave May 
8784502a2867SDave May   PetscFunctionBegin;
8785502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87860918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8787502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8788502a2867SDave May   PetscFunctionReturn(0);
8789502a2867SDave May }
8790502a2867SDave May 
8791531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8792531c7667SBarry Smith 
8793531c7667SBarry Smith /*
8794531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8795531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8796531c7667SBarry Smith */
8797531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8798531c7667SBarry Smith {
8799531c7667SBarry Smith   PetscErrorCode ierr;
8800531c7667SBarry Smith 
8801531c7667SBarry Smith   PetscFunctionBegin;
8802531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8803531c7667SBarry Smith     Vec x1local;
8804531c7667SBarry Smith     DM  dm;
8805531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8806531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8807531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8808531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8809531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8810531c7667SBarry Smith     x1   = x1local;
8811531c7667SBarry Smith   }
8812531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8813531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8814531c7667SBarry Smith     DM  dm;
8815531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8816531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8817531c7667SBarry Smith   }
8818531c7667SBarry Smith   PetscFunctionReturn(0);
8819531c7667SBarry Smith }
8820531c7667SBarry Smith 
8821531c7667SBarry Smith /*@
8822531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8823531c7667SBarry Smith 
8824531c7667SBarry Smith     Input Parameter:
8825531c7667SBarry Smith .    coloring - the MatFDColoring object
8826531c7667SBarry Smith 
882795452b02SPatrick Sanan     Developer Notes:
882895452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8829531c7667SBarry Smith 
88301b266c99SBarry Smith     Level: advanced
88311b266c99SBarry Smith 
8832531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8833531c7667SBarry Smith @*/
8834531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8835531c7667SBarry Smith {
8836531c7667SBarry Smith   PetscFunctionBegin;
8837531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8838531c7667SBarry Smith   PetscFunctionReturn(0);
8839531c7667SBarry Smith }
88408320bc6fSPatrick Sanan 
88418320bc6fSPatrick Sanan /*@
88428320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
88438320bc6fSPatrick Sanan 
88448320bc6fSPatrick Sanan     Collective
88458320bc6fSPatrick Sanan 
88468320bc6fSPatrick Sanan     Input Parameters:
8847a5bc1bf3SBarry Smith +    dm1 - the first DM
88488320bc6fSPatrick Sanan -    dm2 - the second DM
88498320bc6fSPatrick Sanan 
88508320bc6fSPatrick Sanan     Output Parameters:
88518320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
88528320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
88538320bc6fSPatrick Sanan 
88548320bc6fSPatrick Sanan     Notes:
88558320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
88563d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
88578320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
88583d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
88593d862458SPatrick Sanan     decomposition, but hold different data.
88608320bc6fSPatrick Sanan 
88618320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
88628320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
88638320bc6fSPatrick Sanan 
88648320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
88658320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
88668320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
88678320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
88688320bc6fSPatrick Sanan 
88698320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
88708320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
88718320bc6fSPatrick Sanan .vb
88728320bc6fSPatrick Sanan   ...
88738320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
88748320bc6fSPatrick Sanan   if (set && compatible)  {
88758320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
88768320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
88773d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
88788320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
88798320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
88808320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
88818320bc6fSPatrick Sanan       }
88828320bc6fSPatrick Sanan     }
88838320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
88848320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
88858320bc6fSPatrick Sanan   } else {
88868320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
88878320bc6fSPatrick Sanan   }
88888320bc6fSPatrick Sanan   ...
88898320bc6fSPatrick Sanan .ve
88908320bc6fSPatrick Sanan 
88918320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
88928320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
88938320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
88948320bc6fSPatrick Sanan     always check the "set" output parameter.
88958320bc6fSPatrick Sanan 
88968320bc6fSPatrick Sanan     A DM is always compatible with itself.
88978320bc6fSPatrick Sanan 
88988320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
88998320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
89008320bc6fSPatrick Sanan     incompatible.
89018320bc6fSPatrick Sanan 
89028320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
89038320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
89048320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
89058320bc6fSPatrick Sanan 
89068320bc6fSPatrick Sanan     Developer Notes:
89073d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
89083d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8909a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
89108320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
89118320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
89123d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
89133d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
89148320bc6fSPatrick Sanan 
89158320bc6fSPatrick Sanan     Level: advanced
89168320bc6fSPatrick Sanan 
89173d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
89188320bc6fSPatrick Sanan @*/
89198320bc6fSPatrick Sanan 
8920a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
89218320bc6fSPatrick Sanan {
89228320bc6fSPatrick Sanan   PetscErrorCode ierr;
89238320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
89248320bc6fSPatrick Sanan   DMType         type,type2;
89258320bc6fSPatrick Sanan   PetscBool      sameType;
89268320bc6fSPatrick Sanan 
89278320bc6fSPatrick Sanan   PetscFunctionBegin;
8928a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
89298320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
89308320bc6fSPatrick Sanan 
89318320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8932a5bc1bf3SBarry Smith   if (dm1 == dm2) {
89338320bc6fSPatrick Sanan     *set = PETSC_TRUE;
89348320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
89358320bc6fSPatrick Sanan     PetscFunctionReturn(0);
89368320bc6fSPatrick Sanan   }
89378320bc6fSPatrick Sanan 
89388320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
89398320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
89408320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
89418320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8942a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
89438320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
89448320bc6fSPatrick Sanan     *set = PETSC_TRUE;
89458320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
89468320bc6fSPatrick Sanan     PetscFunctionReturn(0);
89478320bc6fSPatrick Sanan   }
89488320bc6fSPatrick Sanan 
89498320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8950a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8951a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8952b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
89538320bc6fSPatrick Sanan   }
89548320bc6fSPatrick Sanan 
8955a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
89568320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8957a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
89588320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
89598320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
89608320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8961a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
89628320bc6fSPatrick Sanan   } else {
89638320bc6fSPatrick Sanan     *set = PETSC_FALSE;
89648320bc6fSPatrick Sanan   }
89658320bc6fSPatrick Sanan   PetscFunctionReturn(0);
89668320bc6fSPatrick Sanan }
8967c0f0dcc3SMatthew G. Knepley 
8968c0f0dcc3SMatthew G. Knepley /*@C
8969c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8970c0f0dcc3SMatthew G. Knepley 
8971c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8972c0f0dcc3SMatthew G. Knepley 
8973c0f0dcc3SMatthew G. Knepley   Input Parameters:
8974c0f0dcc3SMatthew G. Knepley + DM - the DM
8975c0f0dcc3SMatthew G. Knepley . f - the monitor function
8976c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8977c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8978c0f0dcc3SMatthew G. Knepley 
8979c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8980c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8981c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8982c0f0dcc3SMatthew G. Knepley 
8983c0f0dcc3SMatthew G. Knepley   Notes:
8984c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8985c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8986c0f0dcc3SMatthew G. Knepley   order in which they were set.
8987c0f0dcc3SMatthew G. Knepley 
8988c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8989c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8990c0f0dcc3SMatthew G. Knepley 
8991c0f0dcc3SMatthew G. Knepley   Level: intermediate
8992c0f0dcc3SMatthew G. Knepley 
8993c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8994c0f0dcc3SMatthew G. Knepley @*/
8995c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8996c0f0dcc3SMatthew G. Knepley {
8997c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8998c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8999c0f0dcc3SMatthew G. Knepley 
9000c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9001c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9002c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9003c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9004c0f0dcc3SMatthew G. Knepley 
9005c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9006c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9007c0f0dcc3SMatthew G. Knepley   }
9008c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9009c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9010c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9011c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9012c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9013c0f0dcc3SMatthew G. Knepley }
9014c0f0dcc3SMatthew G. Knepley 
9015c0f0dcc3SMatthew G. Knepley /*@
9016c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9017c0f0dcc3SMatthew G. Knepley 
9018c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9019c0f0dcc3SMatthew G. Knepley 
9020c0f0dcc3SMatthew G. Knepley   Input Parameter:
9021c0f0dcc3SMatthew G. Knepley . dm - the DM
9022c0f0dcc3SMatthew G. Knepley 
9023c0f0dcc3SMatthew G. Knepley   Options Database Key:
9024c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9025c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9026c0f0dcc3SMatthew G. Knepley   set via the options database
9027c0f0dcc3SMatthew G. Knepley 
9028c0f0dcc3SMatthew G. Knepley   Notes:
9029c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9030c0f0dcc3SMatthew G. Knepley 
9031c0f0dcc3SMatthew G. Knepley   Level: intermediate
9032c0f0dcc3SMatthew G. Knepley 
9033c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9034c0f0dcc3SMatthew G. Knepley @*/
9035c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9036c0f0dcc3SMatthew G. Knepley {
9037c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9038c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9039c0f0dcc3SMatthew G. Knepley 
9040c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9041c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9042c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9043c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9044c0f0dcc3SMatthew G. Knepley   }
9045c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9046c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9047c0f0dcc3SMatthew G. Knepley }
9048c0f0dcc3SMatthew G. Knepley 
9049c0f0dcc3SMatthew G. Knepley /*@C
9050c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9051c0f0dcc3SMatthew G. Knepley 
9052c0f0dcc3SMatthew G. Knepley   Collective on DM
9053c0f0dcc3SMatthew G. Knepley 
9054c0f0dcc3SMatthew G. Knepley   Input Parameters:
9055c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9056c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9057c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9058c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9059c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9060c0f0dcc3SMatthew 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
9061c0f0dcc3SMatthew G. Knepley 
9062c0f0dcc3SMatthew G. Knepley   Output Parameter:
9063c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9064c0f0dcc3SMatthew G. Knepley 
9065c0f0dcc3SMatthew G. Knepley   Level: developer
9066c0f0dcc3SMatthew G. Knepley 
9067c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9068c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9069c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9070c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9071c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9072c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9073c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9074c0f0dcc3SMatthew G. Knepley @*/
9075c0f0dcc3SMatthew 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)
9076c0f0dcc3SMatthew G. Knepley {
9077c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9078c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9079c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9080c0f0dcc3SMatthew G. Knepley 
9081c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9082c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9083c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9084c0f0dcc3SMatthew G. Knepley   if (*flg) {
9085c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9086c0f0dcc3SMatthew G. Knepley 
9087c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9088c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9089c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9090c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9091c0f0dcc3SMatthew G. Knepley   }
9092c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9093c0f0dcc3SMatthew G. Knepley }
9094c0f0dcc3SMatthew G. Knepley 
9095c0f0dcc3SMatthew G. Knepley /*@
9096c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9097c0f0dcc3SMatthew G. Knepley 
9098c0f0dcc3SMatthew G. Knepley    Collective on DM
9099c0f0dcc3SMatthew G. Knepley 
9100c0f0dcc3SMatthew G. Knepley    Input Parameters:
9101c0f0dcc3SMatthew G. Knepley .  dm - The DM
9102c0f0dcc3SMatthew G. Knepley 
9103c0f0dcc3SMatthew G. Knepley    Level: developer
9104c0f0dcc3SMatthew G. Knepley 
9105c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9106c0f0dcc3SMatthew G. Knepley @*/
9107c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9108c0f0dcc3SMatthew G. Knepley {
9109c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9110c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9111c0f0dcc3SMatthew G. Knepley 
9112c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9113c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9114c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9115c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9116c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9117c0f0dcc3SMatthew G. Knepley   }
9118c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9119c0f0dcc3SMatthew G. Knepley }
9120