xref: /petsc/src/dm/interface/dm.c (revision ba2698f1808e61e430b749a0784ecbf435cb3659)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
10d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
1158cd63d5SVaclav Hapla PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_Load;
1267a56275SMatthew G Knepley 
13e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
1440967b3bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_",0};
15*ba2698f1SMatthew G. Knepley const char *const DMPolytopeTypes[] = {"point", "segment", "triangle", "quadrilateral", "segment tensor prism", "tetrahedron", "hexahedron", "triangular prism", "triangular tensor prism", "quadrilateral tensor prism", "unknown", "DMPolytopeType", "DM_POLYTOPE_", 0};
16a4121054SBarry Smith /*@
17de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
18a4121054SBarry Smith 
19a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
20a4121054SBarry Smith    error when you try to use the vector.
21a4121054SBarry Smith 
22d083f849SBarry Smith   Collective
23a4121054SBarry Smith 
24a4121054SBarry Smith   Input Parameter:
25a4121054SBarry Smith . comm - The communicator for the DM object
26a4121054SBarry Smith 
27a4121054SBarry Smith   Output Parameter:
28a4121054SBarry Smith . dm - The DM object
29a4121054SBarry Smith 
30a4121054SBarry Smith   Level: beginner
31a4121054SBarry Smith 
328472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
33a4121054SBarry Smith @*/
347087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
35a4121054SBarry Smith {
36a4121054SBarry Smith   DM             v;
37e5e52638SMatthew G. Knepley   PetscDS        ds;
38a4121054SBarry Smith   PetscErrorCode ierr;
39a4121054SBarry Smith 
40a4121054SBarry Smith   PetscFunctionBegin;
411411c6eeSJed Brown   PetscValidPointer(dm,2);
420298fd71SBarry Smith   *dm = NULL;
43607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
44a4121054SBarry Smith 
4573107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
46e7c4fc90SDmitry Karpeev 
4749be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
4849be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
490298fd71SBarry Smith   v->ltogmap                  = NULL;
501411c6eeSJed Brown   v->bs                       = 1;
51171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5288ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
531bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
54c58f1c22SToby Isaac   v->labels                   = NULL;
5534aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
5634aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
57c58f1c22SToby Isaac   v->depthLabel               = NULL;
58*ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
591bb6d2a8SBarry Smith   v->localSection             = NULL;
601bb6d2a8SBarry Smith   v->globalSection            = NULL;
61fba222abSToby Isaac   v->defaultConstraintSection = NULL;
62fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
63c6b900c6SMatthew G. Knepley   v->L                        = NULL;
64c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
655dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
669a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
6796173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
68435a35e8SMatthew G Knepley   {
69435a35e8SMatthew G Knepley     PetscInt i;
70435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
710298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
72f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
73435a35e8SMatthew G Knepley     }
74435a35e8SMatthew G Knepley   }
75e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
76b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
77e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
7814f150ffSMatthew G. Knepley   v->dmBC = NULL;
79a8fb8f29SToby Isaac   v->coarseMesh = NULL;
80f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
81cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
82c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
83b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
844a7a4c06SLawrence Mitchell 
851411c6eeSJed Brown   *dm = v;
86a4121054SBarry Smith   PetscFunctionReturn(0);
87a4121054SBarry Smith }
88a4121054SBarry Smith 
8938221697SMatthew G. Knepley /*@
9038221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9138221697SMatthew G. Knepley 
92d083f849SBarry Smith   Collective
9338221697SMatthew G. Knepley 
9438221697SMatthew G. Knepley   Input Parameter:
9538221697SMatthew G. Knepley . dm - The original DM object
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley   Output Parameter:
9838221697SMatthew G. Knepley . newdm  - The new DM object
9938221697SMatthew G. Knepley 
10038221697SMatthew G. Knepley   Level: beginner
10138221697SMatthew G. Knepley 
1021bb6d2a8SBarry Smith   Notes: For some DM this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1031bb6d2a8SBarry Smith          DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does
1041bb6d2a8SBarry Smith          share the PetscSection of the original DM
1051bb6d2a8SBarry Smith 
10692cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1071bb6d2a8SBarry Smith 
10838221697SMatthew G. Knepley @*/
10938221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11038221697SMatthew G. Knepley {
11138221697SMatthew G. Knepley   PetscSF        sf;
11238221697SMatthew G. Knepley   Vec            coords;
11338221697SMatthew G. Knepley   void          *ctx;
114a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11538221697SMatthew G. Knepley   PetscErrorCode ierr;
11638221697SMatthew G. Knepley 
11738221697SMatthew G. Knepley   PetscFunctionBegin;
11838221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11938221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12038221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1215d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
122ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
123ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1241de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1251de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12638221697SMatthew G. Knepley   if (dm->ops->clone) {
12738221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
12838221697SMatthew G. Knepley   }
1293f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13038221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13138221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13238221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13338221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
134be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
135be4c1c3eSMatthew G. Knepley     DM           ncdm;
136be4c1c3eSMatthew G. Knepley     PetscSection cs;
1375a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
138be4c1c3eSMatthew G. Knepley 
13992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
140be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1415a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1425a0206caSToby Isaac     if (pEndMax >= 0) {
143be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
14483bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
14592fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
146a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
147be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
148be4c1c3eSMatthew G. Knepley     }
149be4c1c3eSMatthew G. Knepley   }
150a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
151a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15238221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   if (coords) {
15438221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15538221697SMatthew G. Knepley   } else {
15638221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
15738221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
15838221697SMatthew G. Knepley   }
15990b157c4SStefano Zampini   {
16090b157c4SStefano Zampini     PetscBool             isper;
161c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1625dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16390b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16490b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
165c6b900c6SMatthew G. Knepley   }
16634aa8a36SMatthew G. Knepley   {
16734aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
16834aa8a36SMatthew G. Knepley 
16934aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17134aa8a36SMatthew G. Knepley   }
17238221697SMatthew G. Knepley   PetscFunctionReturn(0);
17338221697SMatthew G. Knepley }
17438221697SMatthew G. Knepley 
1759a42bb27SBarry Smith /*@C
176564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1779a42bb27SBarry Smith 
178d083f849SBarry Smith    Logically Collective on da
1799a42bb27SBarry Smith 
1809a42bb27SBarry Smith    Input Parameter:
1819a42bb27SBarry Smith +  da - initial distributed array
182e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1839a42bb27SBarry Smith 
1849a42bb27SBarry Smith    Options Database:
185dd85299cSBarry Smith .   -dm_vec_type ctype
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith    Level: intermediate
1889a42bb27SBarry Smith 
189a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1909a42bb27SBarry Smith @*/
19119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1929a42bb27SBarry Smith {
1939a42bb27SBarry Smith   PetscErrorCode ierr;
1949a42bb27SBarry Smith 
1959a42bb27SBarry Smith   PetscFunctionBegin;
1969a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1979a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
19819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1999a42bb27SBarry Smith   PetscFunctionReturn(0);
2009a42bb27SBarry Smith }
2019a42bb27SBarry Smith 
202c0dedaeaSBarry Smith /*@C
203c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
204c0dedaeaSBarry Smith 
205d083f849SBarry Smith    Logically Collective on da
206c0dedaeaSBarry Smith 
207c0dedaeaSBarry Smith    Input Parameter:
208c0dedaeaSBarry Smith .  da - initial distributed array
209c0dedaeaSBarry Smith 
210c0dedaeaSBarry Smith    Output Parameter:
211c0dedaeaSBarry Smith .  ctype - the vector type
212c0dedaeaSBarry Smith 
213c0dedaeaSBarry Smith    Level: intermediate
214c0dedaeaSBarry Smith 
215a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
216c0dedaeaSBarry Smith @*/
217c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
218c0dedaeaSBarry Smith {
219c0dedaeaSBarry Smith   PetscFunctionBegin;
220c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
221c0dedaeaSBarry Smith   *ctype = da->vectype;
222c0dedaeaSBarry Smith   PetscFunctionReturn(0);
223c0dedaeaSBarry Smith }
224c0dedaeaSBarry Smith 
2255f1ad066SMatthew G Knepley /*@
22634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2275f1ad066SMatthew G Knepley 
2285f1ad066SMatthew G Knepley   Not collective
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley   Input Parameter:
2315f1ad066SMatthew G Knepley . v - The Vec
2325f1ad066SMatthew G Knepley 
2335f1ad066SMatthew G Knepley   Output Parameter:
2345f1ad066SMatthew G Knepley . dm - The DM
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   Level: intermediate
2375f1ad066SMatthew G Knepley 
2385f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2395f1ad066SMatthew G Knepley @*/
2405f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2415f1ad066SMatthew G Knepley {
2425f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2435f1ad066SMatthew G Knepley 
2445f1ad066SMatthew G Knepley   PetscFunctionBegin;
2455f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2465f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2475f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2485f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2495f1ad066SMatthew G Knepley }
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley /*@
252d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley   Not collective
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley   Input Parameters:
2575f1ad066SMatthew G Knepley + v - The Vec
2585f1ad066SMatthew G Knepley - dm - The DM
2595f1ad066SMatthew G Knepley 
260d9805387SMatthew 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.
261d9805387SMatthew G. Knepley 
2625f1ad066SMatthew G Knepley   Level: intermediate
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2655f1ad066SMatthew G Knepley @*/
2665f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2675f1ad066SMatthew G Knepley {
2685f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   PetscFunctionBegin;
2715f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
272d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2735f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2745f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2755f1ad066SMatthew G Knepley }
2765f1ad066SMatthew G Knepley 
277521d9a4cSLisandro Dalcin /*@C
2788f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2798f1509bcSBarry Smith 
280d083f849SBarry Smith    Logically Collective on dm
2818f1509bcSBarry Smith 
2828f1509bcSBarry Smith    Input Parameters:
2838f1509bcSBarry Smith +  dm - the DM context
2848f1509bcSBarry Smith -  ctype - the matrix type
2858f1509bcSBarry Smith 
2868f1509bcSBarry Smith    Options Database:
2878f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2888f1509bcSBarry Smith 
2898f1509bcSBarry Smith    Level: intermediate
2908f1509bcSBarry Smith 
2918f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2928f1509bcSBarry Smith           DMGetISColoringType()
2938f1509bcSBarry Smith @*/
2948f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2958f1509bcSBarry Smith {
2968f1509bcSBarry Smith   PetscFunctionBegin;
2978f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2988f1509bcSBarry Smith   dm->coloringtype = ctype;
2998f1509bcSBarry Smith   PetscFunctionReturn(0);
3008f1509bcSBarry Smith }
3018f1509bcSBarry Smith 
3028f1509bcSBarry Smith /*@C
3038f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
304521d9a4cSLisandro Dalcin 
305d083f849SBarry Smith    Logically Collective on dm
306521d9a4cSLisandro Dalcin 
307521d9a4cSLisandro Dalcin    Input Parameter:
3088f1509bcSBarry Smith .  dm - the DM context
3098f1509bcSBarry Smith 
3108f1509bcSBarry Smith    Output Parameter:
3118f1509bcSBarry Smith .  ctype - the matrix type
3128f1509bcSBarry Smith 
3138f1509bcSBarry Smith    Options Database:
3148f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3158f1509bcSBarry Smith 
3168f1509bcSBarry Smith    Level: intermediate
3178f1509bcSBarry Smith 
3188f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3198f1509bcSBarry Smith           DMGetISColoringType()
3208f1509bcSBarry Smith @*/
3218f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3228f1509bcSBarry Smith {
3238f1509bcSBarry Smith   PetscFunctionBegin;
3248f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3258f1509bcSBarry Smith   *ctype = dm->coloringtype;
3268f1509bcSBarry Smith   PetscFunctionReturn(0);
3278f1509bcSBarry Smith }
3288f1509bcSBarry Smith 
3298f1509bcSBarry Smith /*@C
3308f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3318f1509bcSBarry Smith 
332d083f849SBarry Smith    Logically Collective on dm
3338f1509bcSBarry Smith 
3348f1509bcSBarry Smith    Input Parameters:
335521d9a4cSLisandro Dalcin +  dm - the DM context
336a2b5a043SBarry Smith -  ctype - the matrix type
337521d9a4cSLisandro Dalcin 
338521d9a4cSLisandro Dalcin    Options Database:
339521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
340521d9a4cSLisandro Dalcin 
341521d9a4cSLisandro Dalcin    Level: intermediate
342521d9a4cSLisandro Dalcin 
343a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
344521d9a4cSLisandro Dalcin @*/
34519fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
346521d9a4cSLisandro Dalcin {
347521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
34888f0584fSBarry Smith 
349521d9a4cSLisandro Dalcin   PetscFunctionBegin;
350521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
351521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
353521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
354521d9a4cSLisandro Dalcin }
355521d9a4cSLisandro Dalcin 
356c0dedaeaSBarry Smith /*@C
357c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
358c0dedaeaSBarry Smith 
359d083f849SBarry Smith    Logically Collective on dm
360c0dedaeaSBarry Smith 
361c0dedaeaSBarry Smith    Input Parameter:
362c0dedaeaSBarry Smith .  dm - the DM context
363c0dedaeaSBarry Smith 
364c0dedaeaSBarry Smith    Output Parameter:
365c0dedaeaSBarry Smith .  ctype - the matrix type
366c0dedaeaSBarry Smith 
367c0dedaeaSBarry Smith    Options Database:
368c0dedaeaSBarry Smith .   -dm_mat_type ctype
369c0dedaeaSBarry Smith 
370c0dedaeaSBarry Smith    Level: intermediate
371c0dedaeaSBarry Smith 
372a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
373c0dedaeaSBarry Smith @*/
374c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
375c0dedaeaSBarry Smith {
376c0dedaeaSBarry Smith   PetscFunctionBegin;
377c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
378c0dedaeaSBarry Smith   *ctype = dm->mattype;
379c0dedaeaSBarry Smith   PetscFunctionReturn(0);
380c0dedaeaSBarry Smith }
381c0dedaeaSBarry Smith 
382c688c046SMatthew G Knepley /*@
38334f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
384c688c046SMatthew G Knepley 
385c688c046SMatthew G Knepley   Not collective
386c688c046SMatthew G Knepley 
387c688c046SMatthew G Knepley   Input Parameter:
388c688c046SMatthew G Knepley . A - The Mat
389c688c046SMatthew G Knepley 
390c688c046SMatthew G Knepley   Output Parameter:
391c688c046SMatthew G Knepley . dm - The DM
392c688c046SMatthew G Knepley 
393c688c046SMatthew G Knepley   Level: intermediate
394c688c046SMatthew G Knepley 
3958f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3968f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
3978f1509bcSBarry Smith 
398c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
399c688c046SMatthew G Knepley @*/
400c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
401c688c046SMatthew G Knepley {
402c688c046SMatthew G Knepley   PetscErrorCode ierr;
403c688c046SMatthew G Knepley 
404c688c046SMatthew G Knepley   PetscFunctionBegin;
405c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
406c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
407c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
408c688c046SMatthew G Knepley   PetscFunctionReturn(0);
409c688c046SMatthew G Knepley }
410c688c046SMatthew G Knepley 
411c688c046SMatthew G Knepley /*@
412c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
413c688c046SMatthew G Knepley 
414c688c046SMatthew G Knepley   Not collective
415c688c046SMatthew G Knepley 
416c688c046SMatthew G Knepley   Input Parameters:
417c688c046SMatthew G Knepley + A - The Mat
418c688c046SMatthew G Knepley - dm - The DM
419c688c046SMatthew G Knepley 
420c688c046SMatthew G Knepley   Level: intermediate
421c688c046SMatthew G Knepley 
4228f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4238f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4248f1509bcSBarry Smith 
4258f1509bcSBarry Smith 
426c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
427c688c046SMatthew G Knepley @*/
428c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
429c688c046SMatthew G Knepley {
430c688c046SMatthew G Knepley   PetscErrorCode ierr;
431c688c046SMatthew G Knepley 
432c688c046SMatthew G Knepley   PetscFunctionBegin;
433c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4348865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
435c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
436c688c046SMatthew G Knepley   PetscFunctionReturn(0);
437c688c046SMatthew G Knepley }
438c688c046SMatthew G Knepley 
4399a42bb27SBarry Smith /*@C
4409a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4416757b960SDave May    DM options in the database.
4429a42bb27SBarry Smith 
443d083f849SBarry Smith    Logically Collective on dm
4449a42bb27SBarry Smith 
4459a42bb27SBarry Smith    Input Parameter:
4468353ddbbSDave May +  da - the DM context
4479a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4489a42bb27SBarry Smith 
4499a42bb27SBarry Smith    Notes:
4509a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4519a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4529a42bb27SBarry Smith 
4539a42bb27SBarry Smith    Level: advanced
4549a42bb27SBarry Smith 
4559a42bb27SBarry Smith .seealso: DMSetFromOptions()
4569a42bb27SBarry Smith @*/
4577087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4589a42bb27SBarry Smith {
4599a42bb27SBarry Smith   PetscErrorCode ierr;
4609a42bb27SBarry Smith 
4619a42bb27SBarry Smith   PetscFunctionBegin;
4629a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4639a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
464691be533SLawrence Mitchell   if (dm->sf) {
465691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
466691be533SLawrence Mitchell   }
4671bb6d2a8SBarry Smith   if (dm->sectionSF) {
4681bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
469691be533SLawrence Mitchell   }
4709a42bb27SBarry Smith   PetscFunctionReturn(0);
4719a42bb27SBarry Smith }
4729a42bb27SBarry Smith 
47331697293SDave May /*@C
47431697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47531697293SDave May    DM options in the database.
47631697293SDave May 
477d083f849SBarry Smith    Logically Collective on dm
47831697293SDave May 
47931697293SDave May    Input Parameters:
48031697293SDave May +  dm - the DM context
48131697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48231697293SDave May 
48331697293SDave May    Notes:
48431697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48531697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48631697293SDave May 
48731697293SDave May    Level: advanced
48831697293SDave May 
48931697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49031697293SDave May @*/
49131697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49231697293SDave May {
49331697293SDave May   PetscErrorCode ierr;
49431697293SDave May 
49531697293SDave May   PetscFunctionBegin;
49631697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49731697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
49831697293SDave May   PetscFunctionReturn(0);
49931697293SDave May }
50031697293SDave May 
50131697293SDave May /*@C
50231697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50331697293SDave May    DM options in the database.
50431697293SDave May 
50531697293SDave May    Not Collective
50631697293SDave May 
50731697293SDave May    Input Parameters:
50831697293SDave May .  dm - the DM context
50931697293SDave May 
51031697293SDave May    Output Parameters:
51131697293SDave May .  prefix - pointer to the prefix string used is returned
51231697293SDave May 
51395452b02SPatrick Sanan    Notes:
51495452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51531697293SDave May    sufficient length to hold the prefix.
51631697293SDave May 
51731697293SDave May    Level: advanced
51831697293SDave May 
51931697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52031697293SDave May @*/
52131697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52231697293SDave May {
52331697293SDave May   PetscErrorCode ierr;
52431697293SDave May 
52531697293SDave May   PetscFunctionBegin;
52631697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52731697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
52831697293SDave May   PetscFunctionReturn(0);
52931697293SDave May }
53031697293SDave May 
53188bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53288bdff64SToby Isaac {
53388bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
53488bdff64SToby Isaac   DMNamedVecLink nlink;
53588bdff64SToby Isaac   PetscErrorCode ierr;
53688bdff64SToby Isaac 
53788bdff64SToby Isaac   PetscFunctionBegin;
538aab5bcd8SJed Brown   *ncrefct = 0;
53988bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
54088bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
54188bdff64SToby Isaac     if (dm->localin[i])  refct--;
54288bdff64SToby Isaac     if (dm->globalin[i]) refct--;
54388bdff64SToby Isaac   }
54488bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
54588bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
54688bdff64SToby Isaac   if (dm->x) {
54788bdff64SToby Isaac     DM obj;
54888bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
54988bdff64SToby Isaac     if (obj == dm) refct--;
55088bdff64SToby Isaac   }
55188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55288bdff64SToby Isaac     refct--;
55388bdff64SToby Isaac     if (recurseCoarse) {
55488bdff64SToby Isaac       PetscInt coarseCount;
55588bdff64SToby Isaac 
55688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55788bdff64SToby Isaac       refct += coarseCount;
55888bdff64SToby Isaac     }
55988bdff64SToby Isaac   }
56088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56188bdff64SToby Isaac     refct--;
56288bdff64SToby Isaac     if (recurseFine) {
56388bdff64SToby Isaac       PetscInt fineCount;
56488bdff64SToby Isaac 
56588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56688bdff64SToby Isaac       refct += fineCount;
56788bdff64SToby Isaac     }
56888bdff64SToby Isaac   }
56988bdff64SToby Isaac   *ncrefct = refct;
57088bdff64SToby Isaac   PetscFunctionReturn(0);
57188bdff64SToby Isaac }
57288bdff64SToby Isaac 
573f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
574354557abSToby Isaac {
5755d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
576354557abSToby Isaac   PetscErrorCode ierr;
577354557abSToby Isaac 
578354557abSToby Isaac   PetscFunctionBegin;
579354557abSToby Isaac   /* destroy the labels */
580354557abSToby Isaac   while (next) {
581354557abSToby Isaac     DMLabelLink tmp = next->next;
582354557abSToby Isaac 
5835d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
584*ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
585354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
586354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
587354557abSToby Isaac     next = tmp;
588354557abSToby Isaac   }
5895d80c0bfSVaclav Hapla   dm->labels = NULL;
590354557abSToby Isaac   PetscFunctionReturn(0);
591354557abSToby Isaac }
592354557abSToby Isaac 
59347c6ae99SBarry Smith /*@
5948472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59547c6ae99SBarry Smith 
596d083f849SBarry Smith     Collective on dm
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith     Input Parameter:
59947c6ae99SBarry Smith .   dm - the DM object to destroy
60047c6ae99SBarry Smith 
60147c6ae99SBarry Smith     Level: developer
60247c6ae99SBarry Smith 
603e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith @*/
606fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60747c6ae99SBarry Smith {
60888bdff64SToby Isaac   PetscInt       i, cnt;
609dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61047c6ae99SBarry Smith   PetscErrorCode ierr;
61147c6ae99SBarry Smith 
61247c6ae99SBarry Smith   PetscFunctionBegin;
6136bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6146bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61587e657c6SBarry Smith 
61688bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61788bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61888bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
61988bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
620732e2eb9SMatthew G Knepley   /*
621732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
622732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
623732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
624732e2eb9SMatthew G Knepley   */
6256bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6266bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
627732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6286bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6296bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
630732e2eb9SMatthew G Knepley   }
631f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6320298fd71SBarry Smith   (*dm)->namedglobal = NULL;
633f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6342348bcf4SPeter Brune     nnext = nlink->next;
6352348bcf4SPeter 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);
6362348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6372348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6382348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6392348bcf4SPeter Brune   }
640f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6410298fd71SBarry Smith   (*dm)->namedlocal = NULL;
642f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
643f490541aSPeter Brune     nnext = nlink->next;
644f490541aSPeter 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);
645f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
646f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
647f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
648f490541aSPeter Brune   }
6492348bcf4SPeter Brune 
650b17ce1afSJed Brown   /* Destroy the list of hooks */
651c833c3b5SJed Brown   {
652c833c3b5SJed Brown     DMCoarsenHookLink link,next;
653b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
654b17ce1afSJed Brown       next = link->next;
655b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
656b17ce1afSJed Brown     }
6570298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
658c833c3b5SJed Brown   }
659c833c3b5SJed Brown   {
660c833c3b5SJed Brown     DMRefineHookLink link,next;
661c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
662c833c3b5SJed Brown       next = link->next;
663c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
664c833c3b5SJed Brown     }
6650298fd71SBarry Smith     (*dm)->refinehook = NULL;
666c833c3b5SJed Brown   }
667be081cd6SPeter Brune   {
668be081cd6SPeter Brune     DMSubDomainHookLink link,next;
669be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
670be081cd6SPeter Brune       next = link->next;
671be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
672be081cd6SPeter Brune     }
6730298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
674be081cd6SPeter Brune   }
675baf369e7SPeter Brune   {
676baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
677baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
678baf369e7SPeter Brune       next = link->next;
679baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
680baf369e7SPeter Brune     }
6810298fd71SBarry Smith     (*dm)->gtolhook = NULL;
682baf369e7SPeter Brune   }
683d4d07f1eSToby Isaac   {
684d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
685d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
686d4d07f1eSToby Isaac       next = link->next;
687d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
688d4d07f1eSToby Isaac     }
689d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
690d4d07f1eSToby Isaac   }
691aa1993deSMatthew G Knepley   /* Destroy the work arrays */
692aa1993deSMatthew G Knepley   {
693aa1993deSMatthew G Knepley     DMWorkLink link,next;
694aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
695aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
696aa1993deSMatthew G Knepley       next = link->next;
697aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
698aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
699aa1993deSMatthew G Knepley     }
7000298fd71SBarry Smith     (*dm)->workin = NULL;
701aa1993deSMatthew G Knepley   }
702c58f1c22SToby Isaac   /* destroy the labels */
703f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
704f4cdcedcSVaclav Hapla   /* destroy the fields */
705e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
706f4cdcedcSVaclav Hapla   /* destroy the boundaries */
707e6f8dbb6SToby Isaac   {
708e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
709e6f8dbb6SToby Isaac     while (next) {
710e6f8dbb6SToby Isaac       DMBoundary b = next;
711e6f8dbb6SToby Isaac 
712e6f8dbb6SToby Isaac       next = b->next;
713e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
714e6f8dbb6SToby Isaac     }
715e6f8dbb6SToby Isaac   }
716b17ce1afSJed Brown 
71752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
71852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
71952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72052536dc3SBarry Smith 
7211a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7221a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7231a266240SBarry Smith   }
72487e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
72571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7264dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7276bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7286bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
729073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
73088ed4aceSMatthew G Knepley 
7311bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7321bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7338b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
734fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
735fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
73688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7371bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
738736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
739736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7408e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
741736995cdSBlaise Bourdin     }
742736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
743736995cdSBlaise Bourdin   }
74488bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
74588bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
74688bdff64SToby Isaac   }
747a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
74888bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
74988bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
75088bdff64SToby Isaac   }
75188bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
752f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7536636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7546636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7556636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7565dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
757ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
758ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
759ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7606636e97aSMatthew G Knepley 
761e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
76214f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
763e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
764e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
765732e2eb9SMatthew G Knepley 
766ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7676bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
768ed3c66a1SDave May   }
769c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
770435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
771732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
77247c6ae99SBarry Smith   PetscFunctionReturn(0);
77347c6ae99SBarry Smith }
77447c6ae99SBarry Smith 
775d7bf68aeSBarry Smith /*@
776d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
777d7bf68aeSBarry Smith 
778d083f849SBarry Smith     Collective on dm
779d7bf68aeSBarry Smith 
780d7bf68aeSBarry Smith     Input Parameter:
781d7bf68aeSBarry Smith .   dm - the DM object to setup
782d7bf68aeSBarry Smith 
783d7bf68aeSBarry Smith     Level: developer
784d7bf68aeSBarry Smith 
785e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
786d7bf68aeSBarry Smith 
787d7bf68aeSBarry Smith @*/
7887087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
789d7bf68aeSBarry Smith {
790d7bf68aeSBarry Smith   PetscErrorCode ierr;
791d7bf68aeSBarry Smith 
792d7bf68aeSBarry Smith   PetscFunctionBegin;
793171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7948387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
795d7bf68aeSBarry Smith   if (dm->ops->setup) {
796d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
797d7bf68aeSBarry Smith   }
7988387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
799d7bf68aeSBarry Smith   PetscFunctionReturn(0);
800d7bf68aeSBarry Smith }
801d7bf68aeSBarry Smith 
802d7bf68aeSBarry Smith /*@
803d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
804d7bf68aeSBarry Smith 
805d083f849SBarry Smith     Collective on dm
806d7bf68aeSBarry Smith 
807d7bf68aeSBarry Smith     Input Parameter:
808d7bf68aeSBarry Smith .   dm - the DM object to set options for
809d7bf68aeSBarry Smith 
810732e2eb9SMatthew G Knepley     Options Database:
8114164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8124164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8134164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8148f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
815732e2eb9SMatthew G Knepley 
816384a6580SVaclav Hapla     DMPLEX Specific Checks
817384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
818384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
819384a6580SVaclav 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()
820384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
821384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
822384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
823384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
824d7bf68aeSBarry Smith 
82595eb5ee5SVaclav Hapla     Level: intermediate
82695eb5ee5SVaclav Hapla 
82795eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
82895eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
829d7bf68aeSBarry Smith 
830d7bf68aeSBarry Smith @*/
8317087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
832d7bf68aeSBarry Smith {
8337781c08eSBarry Smith   char           typeName[256];
834ca266f36SBarry Smith   PetscBool      flg;
835d7bf68aeSBarry Smith   PetscErrorCode ierr;
836d7bf68aeSBarry Smith 
837d7bf68aeSBarry Smith   PetscFunctionBegin;
838171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83949be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
84049be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8411bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8423194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8430298fd71SBarry 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);
844a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
845f9ba7244SBarry Smith   if (flg) {
846f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
847f9ba7244SBarry Smith   }
848a264d7a6SBarry 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);
849073dac72SJed Brown   if (flg) {
850521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
851073dac72SJed Brown   }
8528f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
853f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
854e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
855f9ba7244SBarry Smith   }
856f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8570633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
85882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
859d7bf68aeSBarry Smith   PetscFunctionReturn(0);
860d7bf68aeSBarry Smith }
861d7bf68aeSBarry Smith 
862fc9bc008SSatish Balay /*@C
863fe2efc57SMark    DMViewFromOptions - View from Options
864fe2efc57SMark 
865fe2efc57SMark    Collective on DM
866fe2efc57SMark 
867fe2efc57SMark    Input Parameters:
868fe2efc57SMark +  dm - the DM object
869736c3998SJose E. Roman .  obj - Optional object
870736c3998SJose E. Roman -  name - command line option
871fe2efc57SMark 
872fe2efc57SMark    Level: intermediate
873fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
874fe2efc57SMark @*/
875fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
876fe2efc57SMark {
877fe2efc57SMark   PetscErrorCode ierr;
878fe2efc57SMark 
879fe2efc57SMark   PetscFunctionBegin;
880fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
881fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
882fe2efc57SMark   PetscFunctionReturn(0);
883fe2efc57SMark }
884fe2efc57SMark 
885fe2efc57SMark /*@C
886224748a4SBarry Smith     DMView - Views a DM
88747c6ae99SBarry Smith 
888d083f849SBarry Smith     Collective on dm
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith     Input Parameter:
89147c6ae99SBarry Smith +   dm - the DM object to view
89247c6ae99SBarry Smith -   v - the viewer
89347c6ae99SBarry Smith 
894224748a4SBarry Smith     Level: beginner
89547c6ae99SBarry Smith 
896e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith @*/
8997087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
90047c6ae99SBarry Smith {
90147c6ae99SBarry Smith   PetscErrorCode    ierr;
90232c0f0efSBarry Smith   PetscBool         isbinary;
90376a8abe0SBarry Smith   PetscMPIInt       size;
90476a8abe0SBarry Smith   PetscViewerFormat format;
90547c6ae99SBarry Smith 
90647c6ae99SBarry Smith   PetscFunctionBegin;
907171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9083014e516SBarry Smith   if (!v) {
909ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9103014e516SBarry Smith   }
911b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
91274903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
91374903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
91474903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
91574903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
91674903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
91774903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
91874903a4fSStefano Zampini      in an error here */
91974903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9208bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
921b1b135c8SBarry Smith 
92276a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
92376a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
92476a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
92598c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
92632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
92732c0f0efSBarry Smith   if (isbinary) {
92855849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
92932c0f0efSBarry Smith     char     type[256];
93032c0f0efSBarry Smith 
93132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
93232c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
93332c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
93432c0f0efSBarry Smith   }
9350c010503SBarry Smith   if (dm->ops->view) {
9360c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
93747c6ae99SBarry Smith   }
93847c6ae99SBarry Smith   PetscFunctionReturn(0);
93947c6ae99SBarry Smith }
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith /*@
9428472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
94347c6ae99SBarry Smith 
944d083f849SBarry Smith     Collective on dm
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith     Input Parameter:
94747c6ae99SBarry Smith .   dm - the DM object
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith     Output Parameter:
95047c6ae99SBarry Smith .   vec - the global vector
95147c6ae99SBarry Smith 
952073dac72SJed Brown     Level: beginner
95347c6ae99SBarry Smith 
954e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
95547c6ae99SBarry Smith 
95647c6ae99SBarry Smith @*/
9577087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
95847c6ae99SBarry Smith {
95947c6ae99SBarry Smith   PetscErrorCode ierr;
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith   PetscFunctionBegin;
962171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
963b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
964b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
96547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
96647c6ae99SBarry Smith   PetscFunctionReturn(0);
96747c6ae99SBarry Smith }
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith /*@
9708472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
97147c6ae99SBarry Smith 
97247c6ae99SBarry Smith     Not Collective
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith     Input Parameter:
97547c6ae99SBarry Smith .   dm - the DM object
97647c6ae99SBarry Smith 
97747c6ae99SBarry Smith     Output Parameter:
97847c6ae99SBarry Smith .   vec - the local vector
97947c6ae99SBarry Smith 
980073dac72SJed Brown     Level: beginner
98147c6ae99SBarry Smith 
982e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith @*/
9857087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
98647c6ae99SBarry Smith {
98747c6ae99SBarry Smith   PetscErrorCode ierr;
98847c6ae99SBarry Smith 
98947c6ae99SBarry Smith   PetscFunctionBegin;
990171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
991b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
992b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
99347c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
99447c6ae99SBarry Smith   PetscFunctionReturn(0);
99547c6ae99SBarry Smith }
99647c6ae99SBarry Smith 
9971411c6eeSJed Brown /*@
9981411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9991411c6eeSJed Brown 
1000d083f849SBarry Smith    Collective on dm
10011411c6eeSJed Brown 
10021411c6eeSJed Brown    Input Parameter:
10031411c6eeSJed Brown .  dm - the DM that provides the mapping
10041411c6eeSJed Brown 
10051411c6eeSJed Brown    Output Parameter:
10061411c6eeSJed Brown .  ltog - the mapping
10071411c6eeSJed Brown 
10081411c6eeSJed Brown    Level: intermediate
10091411c6eeSJed Brown 
10101411c6eeSJed Brown    Notes:
10111411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10121411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10131411c6eeSJed Brown 
1014fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10151411c6eeSJed Brown @*/
10167087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10171411c6eeSJed Brown {
10180be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10191411c6eeSJed Brown   PetscErrorCode ierr;
10201411c6eeSJed Brown 
10211411c6eeSJed Brown   PetscFunctionBegin;
10221411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10231411c6eeSJed Brown   PetscValidPointer(ltog,2);
10241411c6eeSJed Brown   if (!dm->ltogmap) {
102537d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
102637d0c07bSMatthew G Knepley 
102792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
102837d0c07bSMatthew G Knepley     if (section) {
1029a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
103037d0c07bSMatthew G Knepley       PetscInt       *ltog;
1031ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
103237d0c07bSMatthew G Knepley 
1033e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103437d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1035ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1036ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
103737d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1038a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
103937d0c07bSMatthew G Knepley 
104037d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
104137d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1042a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1043a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
104437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10451a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10461a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10471a7dc684SMatthew G. Knepley         if (dof) {
1048b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1049b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10501a7dc684SMatthew G. Knepley         }
105137d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1052a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1053a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
105437d0c07bSMatthew G Knepley         }
105537d0c07bSMatthew G Knepley       }
1056bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10570be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10580be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10590be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10600be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10617591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10627591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1063ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1064ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1065ccf3bd66SMatthew G. Knepley         n /= bs;
1066ccf3bd66SMatthew G. Knepley       }
1067ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10683bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
106937d0c07bSMatthew G Knepley     } else {
1070b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1071184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10721411c6eeSJed Brown     }
107337d0c07bSMatthew G Knepley   }
10741411c6eeSJed Brown   *ltog = dm->ltogmap;
10751411c6eeSJed Brown   PetscFunctionReturn(0);
10761411c6eeSJed Brown }
10771411c6eeSJed Brown 
10781411c6eeSJed Brown /*@
10791411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10801411c6eeSJed Brown 
10811411c6eeSJed Brown    Not Collective
10821411c6eeSJed Brown 
10831411c6eeSJed Brown    Input Parameter:
10841411c6eeSJed Brown .  dm - the DM with block structure
10851411c6eeSJed Brown 
10861411c6eeSJed Brown    Output Parameter:
10871411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10881411c6eeSJed Brown 
10891411c6eeSJed Brown    Level: intermediate
10901411c6eeSJed Brown 
1091fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10921411c6eeSJed Brown @*/
10937087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10941411c6eeSJed Brown {
10951411c6eeSJed Brown   PetscFunctionBegin;
10961411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1097534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
10981411c6eeSJed 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");
10991411c6eeSJed Brown   *bs = dm->bs;
11001411c6eeSJed Brown   PetscFunctionReturn(0);
11011411c6eeSJed Brown }
11021411c6eeSJed Brown 
110347c6ae99SBarry Smith /*@
11048472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
110547c6ae99SBarry Smith 
1106d083f849SBarry Smith     Collective on dm1
110747c6ae99SBarry Smith 
110847c6ae99SBarry Smith     Input Parameter:
110947c6ae99SBarry Smith +   dm1 - the DM object
111047c6ae99SBarry Smith -   dm2 - the second, finer DM object
111147c6ae99SBarry Smith 
111247c6ae99SBarry Smith     Output Parameter:
111347c6ae99SBarry Smith +  mat - the interpolation
111447c6ae99SBarry Smith -  vec - the scaling (optional)
111547c6ae99SBarry Smith 
111647c6ae99SBarry Smith     Level: developer
111747c6ae99SBarry Smith 
111895452b02SPatrick Sanan     Notes:
111995452b02SPatrick 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
112085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1121d52bd9f3SBarry Smith 
11221f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1123d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
112485afcc9aSBarry Smith 
112585afcc9aSBarry Smith 
11263ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
112747c6ae99SBarry Smith 
112847c6ae99SBarry Smith @*/
1129e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
113047c6ae99SBarry Smith {
113147c6ae99SBarry Smith   PetscErrorCode ierr;
113247c6ae99SBarry Smith 
113347c6ae99SBarry Smith   PetscFunctionBegin;
1134171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1135171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1136c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1137b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dm1)->type_name);
1138cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
113925296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1140cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
114147c6ae99SBarry Smith   PetscFunctionReturn(0);
114247c6ae99SBarry Smith }
114347c6ae99SBarry Smith 
11443ad4599aSBarry Smith /*@
11453ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11463ad4599aSBarry Smith 
1147d083f849SBarry Smith     Collective on dm1
11483ad4599aSBarry Smith 
11493ad4599aSBarry Smith     Input Parameter:
11503ad4599aSBarry Smith +   dm1 - the DM object
11513ad4599aSBarry Smith -   dm2 - the second, finer DM object
11523ad4599aSBarry Smith 
11533ad4599aSBarry Smith     Output Parameter:
11543ad4599aSBarry Smith .  mat - the restriction
11553ad4599aSBarry Smith 
11563ad4599aSBarry Smith 
11573ad4599aSBarry Smith     Level: developer
11583ad4599aSBarry Smith 
115995452b02SPatrick Sanan     Notes:
116095452b02SPatrick 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
11613ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11623ad4599aSBarry Smith 
11633ad4599aSBarry Smith 
11643ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11653ad4599aSBarry Smith 
11663ad4599aSBarry Smith @*/
11673ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11683ad4599aSBarry Smith {
11693ad4599aSBarry Smith   PetscErrorCode ierr;
11703ad4599aSBarry Smith 
11713ad4599aSBarry Smith   PetscFunctionBegin;
11723ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11733ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11745a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1175b9d85ea2SLisandro Dalcin   if (!dm1->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dm1)->type_name);
11763ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11773ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11783ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11793ad4599aSBarry Smith   PetscFunctionReturn(0);
11803ad4599aSBarry Smith }
11813ad4599aSBarry Smith 
118247c6ae99SBarry Smith /*@
11838472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
118447c6ae99SBarry Smith 
1185d083f849SBarry Smith     Collective on dm1
118647c6ae99SBarry Smith 
118747c6ae99SBarry Smith     Input Parameter:
118847c6ae99SBarry Smith +   dm1 - the DM object
118947c6ae99SBarry Smith -   dm2 - the second, finer DM object
119047c6ae99SBarry Smith 
119147c6ae99SBarry Smith     Output Parameter:
11926dbf9973SLawrence Mitchell .   mat - the injection
119347c6ae99SBarry Smith 
119447c6ae99SBarry Smith     Level: developer
119547c6ae99SBarry Smith 
119695452b02SPatrick Sanan    Notes:
119795452b02SPatrick 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
119885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
119985afcc9aSBarry Smith 
1200e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
120147c6ae99SBarry Smith 
120247c6ae99SBarry Smith @*/
12036dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
120447c6ae99SBarry Smith {
120547c6ae99SBarry Smith   PetscErrorCode ierr;
120647c6ae99SBarry Smith 
120747c6ae99SBarry Smith   PetscFunctionBegin;
1208171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1209171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
12105a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1211b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dm1)->type_name);
12125a84ad33SLisandro Dalcin   ierr = PetscLogEventBegin(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
12135a84ad33SLisandro Dalcin   ierr = (*dm1->ops->createinjection)(dm1,dm2,mat);CHKERRQ(ierr);
12145a84ad33SLisandro Dalcin   ierr = PetscLogEventEnd(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
121547c6ae99SBarry Smith   PetscFunctionReturn(0);
121647c6ae99SBarry Smith }
121747c6ae99SBarry Smith 
1218b412c318SBarry Smith /*@
1219bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1220bd041c0cSMatthew G. Knepley 
1221d083f849SBarry Smith   Collective on dm1
1222bd041c0cSMatthew G. Knepley 
1223bd041c0cSMatthew G. Knepley   Input Parameter:
1224bd041c0cSMatthew G. Knepley + dm1 - the DM object
1225bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1226bd041c0cSMatthew G. Knepley 
1227bd041c0cSMatthew G. Knepley   Output Parameter:
1228bd041c0cSMatthew G. Knepley . mat - the interpolation
1229bd041c0cSMatthew G. Knepley 
1230bd041c0cSMatthew G. Knepley   Level: developer
1231bd041c0cSMatthew G. Knepley 
1232bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1233bd041c0cSMatthew G. Knepley @*/
1234bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1235bd041c0cSMatthew G. Knepley {
1236bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1237bd041c0cSMatthew G. Knepley 
1238bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1239bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1240bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
12415a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1242b9d85ea2SLisandro Dalcin   if (!dm1->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dm1)->type_name);
1243bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1244bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1245bd041c0cSMatthew G. Knepley }
1246bd041c0cSMatthew G. Knepley 
1247bd041c0cSMatthew G. Knepley /*@
1248b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
124947c6ae99SBarry Smith 
1250d083f849SBarry Smith     Collective on dm
125147c6ae99SBarry Smith 
125247c6ae99SBarry Smith     Input Parameter:
125347c6ae99SBarry Smith +   dm - the DM object
12545bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
125547c6ae99SBarry Smith 
125647c6ae99SBarry Smith     Output Parameter:
125747c6ae99SBarry Smith .   coloring - the coloring
125847c6ae99SBarry Smith 
1259ec5066bdSBarry Smith     Notes:
1260ec5066bdSBarry 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
1261ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1262ec5066bdSBarry Smith 
1263ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1264ec5066bdSBarry Smith 
126547c6ae99SBarry Smith     Level: developer
126647c6ae99SBarry Smith 
1267ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
126847c6ae99SBarry Smith 
1269aab9d709SJed Brown @*/
1270b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
127147c6ae99SBarry Smith {
127247c6ae99SBarry Smith   PetscErrorCode ierr;
127347c6ae99SBarry Smith 
127447c6ae99SBarry Smith   PetscFunctionBegin;
1275171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12765a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1277b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1278b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
127947c6ae99SBarry Smith   PetscFunctionReturn(0);
128047c6ae99SBarry Smith }
128147c6ae99SBarry Smith 
1282b412c318SBarry Smith /*@
12838472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
128447c6ae99SBarry Smith 
1285d083f849SBarry Smith     Collective on dm
128647c6ae99SBarry Smith 
128747c6ae99SBarry Smith     Input Parameter:
1288b412c318SBarry Smith .   dm - the DM object
128947c6ae99SBarry Smith 
129047c6ae99SBarry Smith     Output Parameter:
129147c6ae99SBarry Smith .   mat - the empty Jacobian
129247c6ae99SBarry Smith 
1293073dac72SJed Brown     Level: beginner
129447c6ae99SBarry Smith 
129595452b02SPatrick Sanan     Notes:
129695452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
129794013140SBarry Smith        do not need to do it yourself.
129894013140SBarry Smith 
129994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13007889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
130194013140SBarry Smith 
130294013140SBarry 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
130394013140SBarry Smith        internally by PETSc.
130494013140SBarry Smith 
130594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1306aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
130794013140SBarry Smith 
1308b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
130947c6ae99SBarry Smith 
1310aab9d709SJed Brown @*/
1311b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
131247c6ae99SBarry Smith {
131347c6ae99SBarry Smith   PetscErrorCode ierr;
131447c6ae99SBarry Smith 
131547c6ae99SBarry Smith   PetscFunctionBegin;
1316171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1317c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1318b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13195a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1320fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1321b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1322e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1323e5e52638SMatthew G. Knepley   if (dm->Nf) {
1324e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1325e571a35bSMatthew G. Knepley     PetscInt     Nf;
1326e571a35bSMatthew G. Knepley 
1327e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1328e571a35bSMatthew G. Knepley     if (Nf == 1) {
1329e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1330e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1331e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1332e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1333e571a35bSMatthew G. Knepley       }
1334e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1335e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1336e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1337e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1338e571a35bSMatthew G. Knepley       }
1339e571a35bSMatthew G. Knepley     }
1340e571a35bSMatthew G. Knepley   }
1341fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
134247c6ae99SBarry Smith   PetscFunctionReturn(0);
134347c6ae99SBarry Smith }
134447c6ae99SBarry Smith 
1345732e2eb9SMatthew G Knepley /*@
1346950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1347732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1348732e2eb9SMatthew G Knepley 
1349d083f849SBarry Smith   Logically Collective on dm
1350732e2eb9SMatthew G Knepley 
1351732e2eb9SMatthew G Knepley   Input Parameter:
1352732e2eb9SMatthew G Knepley + dm - the DM
1353732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1354732e2eb9SMatthew G Knepley 
1355732e2eb9SMatthew G Knepley   Level: developer
1356b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1357732e2eb9SMatthew G Knepley @*/
1358732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1359732e2eb9SMatthew G Knepley {
1360732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1361732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1362732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1363732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1364732e2eb9SMatthew G Knepley }
1365732e2eb9SMatthew G Knepley 
1366b06ff27eSHong Zhang /*@
1367b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1368b06ff27eSHong Zhang     but the array for values will not be allocated.
1369b06ff27eSHong Zhang 
1370d083f849SBarry Smith   Logically Collective on dm
1371b06ff27eSHong Zhang 
1372b06ff27eSHong Zhang   Input Parameter:
1373b06ff27eSHong Zhang + dm - the DM
1374b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1375b06ff27eSHong Zhang 
1376b06ff27eSHong Zhang   Level: developer
1377b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1378b06ff27eSHong Zhang @*/
1379b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1380b06ff27eSHong Zhang {
1381b06ff27eSHong Zhang   PetscFunctionBegin;
1382b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1383b06ff27eSHong Zhang   dm->structure_only = only;
1384b06ff27eSHong Zhang   PetscFunctionReturn(0);
1385b06ff27eSHong Zhang }
1386b06ff27eSHong Zhang 
1387a89ea682SMatthew G Knepley /*@C
1388aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1389a89ea682SMatthew G Knepley 
1390a89ea682SMatthew G Knepley   Not Collective
1391a89ea682SMatthew G Knepley 
1392a89ea682SMatthew G Knepley   Input Parameters:
1393a89ea682SMatthew G Knepley + dm - the DM object
1394aa1993deSMatthew G Knepley . count - The minium size
139569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1396a89ea682SMatthew G Knepley 
1397a89ea682SMatthew G Knepley   Output Parameter:
1398a89ea682SMatthew G Knepley . array - the work array
1399a89ea682SMatthew G Knepley 
1400a89ea682SMatthew G Knepley   Level: developer
1401a89ea682SMatthew G Knepley 
1402a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1403a89ea682SMatthew G Knepley @*/
140469291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1405a89ea682SMatthew G Knepley {
1406a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1407aa1993deSMatthew G Knepley   DMWorkLink     link;
140869291d52SBarry Smith   PetscMPIInt    dsize;
1409a89ea682SMatthew G Knepley 
1410a89ea682SMatthew G Knepley   PetscFunctionBegin;
1411a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1412aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1413aa1993deSMatthew G Knepley   if (dm->workin) {
1414aa1993deSMatthew G Knepley     link       = dm->workin;
1415aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1416aa1993deSMatthew G Knepley   } else {
1417b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1418a89ea682SMatthew G Knepley   }
141969291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14205056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1421aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1422854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1423854ce69bSBarry Smith     link->bytes = dsize*count;
1424aa1993deSMatthew G Knepley   }
1425aa1993deSMatthew G Knepley   link->next   = dm->workout;
1426aa1993deSMatthew G Knepley   dm->workout  = link;
1427aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1428a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1429a89ea682SMatthew G Knepley }
1430a89ea682SMatthew G Knepley 
1431aa1993deSMatthew G Knepley /*@C
1432aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1433aa1993deSMatthew G Knepley 
1434aa1993deSMatthew G Knepley   Not Collective
1435aa1993deSMatthew G Knepley 
1436aa1993deSMatthew G Knepley   Input Parameters:
1437aa1993deSMatthew G Knepley + dm - the DM object
1438aa1993deSMatthew G Knepley . count - The minium size
143969291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1440aa1993deSMatthew G Knepley 
1441aa1993deSMatthew G Knepley   Output Parameter:
1442aa1993deSMatthew G Knepley . array - the work array
1443aa1993deSMatthew G Knepley 
1444aa1993deSMatthew G Knepley   Level: developer
1445aa1993deSMatthew G Knepley 
144695452b02SPatrick Sanan   Developer Notes:
144795452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1448aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1449aa1993deSMatthew G Knepley @*/
145069291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1451aa1993deSMatthew G Knepley {
1452aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1453aa1993deSMatthew G Knepley 
1454aa1993deSMatthew G Knepley   PetscFunctionBegin;
1455aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1456aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1457aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1458aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1459aa1993deSMatthew G Knepley       *p           = link->next;
1460aa1993deSMatthew G Knepley       link->next   = dm->workin;
1461aa1993deSMatthew G Knepley       dm->workin   = link;
14620298fd71SBarry Smith       *(void**)mem = NULL;
1463aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1464aa1993deSMatthew G Knepley     }
1465aa1993deSMatthew G Knepley   }
1466aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1467aa1993deSMatthew G Knepley }
1468e7c4fc90SDmitry Karpeev 
1469435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1470435a35e8SMatthew G Knepley {
1471435a35e8SMatthew G Knepley   PetscFunctionBegin;
1472435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
147382f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1474435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1475435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1476435a35e8SMatthew G Knepley }
1477435a35e8SMatthew G Knepley 
14780a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14790a50eb56SMatthew G. Knepley {
14800a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14810a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1482f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14830a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14840a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14850a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14860a50eb56SMatthew G. Knepley }
14870a50eb56SMatthew G. Knepley 
1488f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1489f9d4088aSMatthew G. Knepley {
1490f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1491f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1492f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1493f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1494f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1495f9d4088aSMatthew G. Knepley }
1496f9d4088aSMatthew G. Knepley 
1497f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1498f9d4088aSMatthew G. Knepley {
1499f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1500f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1501f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1502f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1503f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1504f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1505f9d4088aSMatthew G. Knepley }
1506f9d4088aSMatthew G. Knepley 
15074f3b5142SJed Brown /*@C
15084d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
15094d343eeaSMatthew G Knepley 
15104d343eeaSMatthew G Knepley   Not collective
15114d343eeaSMatthew G Knepley 
15124d343eeaSMatthew G Knepley   Input Parameter:
15134d343eeaSMatthew G Knepley . dm - the DM object
15144d343eeaSMatthew G Knepley 
15154d343eeaSMatthew G Knepley   Output Parameters:
15160298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
15170298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
15180298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
15194d343eeaSMatthew G Knepley 
15204d343eeaSMatthew G Knepley   Level: intermediate
15214d343eeaSMatthew G Knepley 
152221c9b008SJed Brown   Notes:
152321c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
152421c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
152521c9b008SJed Brown   PetscFree().
152621c9b008SJed Brown 
15274d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
15284d343eeaSMatthew G Knepley @*/
152937d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
15304d343eeaSMatthew G Knepley {
153137d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
15324d343eeaSMatthew G Knepley   PetscErrorCode ierr;
15334d343eeaSMatthew G Knepley 
15344d343eeaSMatthew G Knepley   PetscFunctionBegin;
15354d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
153669ca1f37SDmitry Karpeev   if (numFields) {
1537534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
153869ca1f37SDmitry Karpeev     *numFields = 0;
153969ca1f37SDmitry Karpeev   }
154037d0c07bSMatthew G Knepley   if (fieldNames) {
154137d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15420298fd71SBarry Smith     *fieldNames = NULL;
154369ca1f37SDmitry Karpeev   }
154469ca1f37SDmitry Karpeev   if (fields) {
154569ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15460298fd71SBarry Smith     *fields = NULL;
154769ca1f37SDmitry Karpeev   }
154892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
154937d0c07bSMatthew G Knepley   if (section) {
15503a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
155137d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
155237d0c07bSMatthew G Knepley 
1553e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
155437d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15553a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
155637d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
155737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
155837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15593a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
156037d0c07bSMatthew G Knepley     }
156137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
156237d0c07bSMatthew G Knepley       PetscInt gdof;
156337d0c07bSMatthew G Knepley 
156437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
156537d0c07bSMatthew G Knepley       if (gdof > 0) {
156637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15673a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
156837d0c07bSMatthew G Knepley 
156937d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
157037d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15713a544194SStefano Zampini           fpdof = fdof-fcdof;
15723a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15733a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15743a544194SStefano Zampini             fieldNc[f] = 1;
15753a544194SStefano Zampini           }
15763a544194SStefano Zampini           fieldSizes[f] += fpdof;
157737d0c07bSMatthew G Knepley         }
157837d0c07bSMatthew G Knepley       }
157937d0c07bSMatthew G Knepley     }
158037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1581785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
158237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
158337d0c07bSMatthew G Knepley     }
158437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
158537d0c07bSMatthew G Knepley       PetscInt gdof, goff;
158637d0c07bSMatthew G Knepley 
158737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
158837d0c07bSMatthew G Knepley       if (gdof > 0) {
158937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
159037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
159137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
159237d0c07bSMatthew G Knepley 
159337d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
159437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
159537d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
159637d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
159737d0c07bSMatthew G Knepley           }
159837d0c07bSMatthew G Knepley         }
159937d0c07bSMatthew G Knepley       }
160037d0c07bSMatthew G Knepley     }
16018865f1eaSKarl Rupp     if (numFields) *numFields = nF;
160237d0c07bSMatthew G Knepley     if (fieldNames) {
1603785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
160437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
160537d0c07bSMatthew G Knepley         const char *fieldName;
160637d0c07bSMatthew G Knepley 
160737d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
160837d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
160937d0c07bSMatthew G Knepley       }
161037d0c07bSMatthew G Knepley     }
161137d0c07bSMatthew G Knepley     if (fields) {
1612785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
161337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
16143a544194SStefano Zampini         PetscInt bs, in[2], out[2];
16153a544194SStefano Zampini 
161682f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
16173a544194SStefano Zampini         in[0] = -fieldNc[f];
16183a544194SStefano Zampini         in[1] = fieldNc[f];
16193a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
16203a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
16213a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
162237d0c07bSMatthew G Knepley       }
162337d0c07bSMatthew G Knepley     }
16243a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
16258865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
16268865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
162769ca1f37SDmitry Karpeev   }
16284d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
16294d343eeaSMatthew G Knepley }
16304d343eeaSMatthew G Knepley 
163116621825SDmitry Karpeev 
163216621825SDmitry Karpeev /*@C
163316621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
163416621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
163516621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1636e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1637e7c4fc90SDmitry Karpeev 
1638e7c4fc90SDmitry Karpeev   Not collective
1639e7c4fc90SDmitry Karpeev 
1640e7c4fc90SDmitry Karpeev   Input Parameter:
1641e7c4fc90SDmitry Karpeev . dm - the DM object
1642e7c4fc90SDmitry Karpeev 
1643e7c4fc90SDmitry Karpeev   Output Parameters:
16440298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16450298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16460298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16470298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1648e7c4fc90SDmitry Karpeev 
1649e7c4fc90SDmitry Karpeev   Level: intermediate
1650e7c4fc90SDmitry Karpeev 
1651e7c4fc90SDmitry Karpeev   Notes:
1652e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1653e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1654e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1655e7c4fc90SDmitry Karpeev 
1656e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1657e7c4fc90SDmitry Karpeev @*/
165816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1659e7c4fc90SDmitry Karpeev {
1660e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1661e7c4fc90SDmitry Karpeev 
1662e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1663e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16648865f1eaSKarl Rupp   if (len) {
1665534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
16668865f1eaSKarl Rupp     *len = 0;
16678865f1eaSKarl Rupp   }
16688865f1eaSKarl Rupp   if (namelist) {
16698865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16708865f1eaSKarl Rupp     *namelist = 0;
16718865f1eaSKarl Rupp   }
16728865f1eaSKarl Rupp   if (islist) {
16738865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16748865f1eaSKarl Rupp     *islist = 0;
16758865f1eaSKarl Rupp   }
16768865f1eaSKarl Rupp   if (dmlist) {
16778865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16788865f1eaSKarl Rupp     *dmlist = 0;
16798865f1eaSKarl Rupp   }
1680f3f0edfdSDmitry Karpeev   /*
1681f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1682f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1683f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1684f3f0edfdSDmitry Karpeev    */
1685ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
168616621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1687435a35e8SMatthew G Knepley     PetscSection section;
1688435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1689435a35e8SMatthew G Knepley 
169092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1691435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1692435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1693f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
169403dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
169503dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
169603dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1697435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1698435a35e8SMatthew G Knepley         const char *fieldName;
1699435a35e8SMatthew G Knepley 
170003dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
170103dc3394SMatthew G. Knepley         if (namelist) {
1702435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1703435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1704435a35e8SMatthew G Knepley         }
170503dc3394SMatthew G. Knepley       }
1706435a35e8SMatthew G Knepley     } else {
170769ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1708e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
17090298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1710e7c4fc90SDmitry Karpeev     }
17118865f1eaSKarl Rupp   } else {
171216621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
171316621825SDmitry Karpeev   }
171416621825SDmitry Karpeev   PetscFunctionReturn(0);
171516621825SDmitry Karpeev }
171616621825SDmitry Karpeev 
1717564cec59SMatthew G. Knepley /*@
1718435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1719435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1720435a35e8SMatthew G Knepley 
1721435a35e8SMatthew G Knepley   Not collective
1722435a35e8SMatthew G Knepley 
1723435a35e8SMatthew G Knepley   Input Parameters:
17242adcc780SMatthew G. Knepley + dm        - The DM object
17252adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
17262adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1727435a35e8SMatthew G Knepley 
1728435a35e8SMatthew G Knepley   Output Parameters:
17292adcc780SMatthew G. Knepley + is - The global indices for the subproblem
17302adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1731435a35e8SMatthew G Knepley 
17325d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17335d3b26e6SMatthew G. Knepley 
1734435a35e8SMatthew G Knepley   Level: intermediate
1735435a35e8SMatthew G Knepley 
17365d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1737435a35e8SMatthew G Knepley @*/
173837bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1739435a35e8SMatthew G Knepley {
1740435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1741435a35e8SMatthew G Knepley 
1742435a35e8SMatthew G Knepley   PetscFunctionBegin;
1743435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1744435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17458865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17468865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1747b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1748435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1749435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1750435a35e8SMatthew G Knepley }
1751435a35e8SMatthew G Knepley 
17522adcc780SMatthew G. Knepley /*@C
17532adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17542adcc780SMatthew G. Knepley 
17552adcc780SMatthew G. Knepley   Not collective
17562adcc780SMatthew G. Knepley 
17572adcc780SMatthew G. Knepley   Input Parameter:
17582adcc780SMatthew G. Knepley + dms - The DM objects
17592adcc780SMatthew G. Knepley - len - The number of DMs
17602adcc780SMatthew G. Knepley 
17612adcc780SMatthew G. Knepley   Output Parameters:
1762a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17632adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17642adcc780SMatthew G. Knepley 
17655d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17665d3b26e6SMatthew G. Knepley 
17672adcc780SMatthew G. Knepley   Level: intermediate
17682adcc780SMatthew G. Knepley 
17695d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17702adcc780SMatthew G. Knepley @*/
17712adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17722adcc780SMatthew G. Knepley {
17732adcc780SMatthew G. Knepley   PetscInt       i;
17742adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17752adcc780SMatthew G. Knepley 
17762adcc780SMatthew G. Knepley   PetscFunctionBegin;
17772adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17782adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17792adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1780a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17812adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17822adcc780SMatthew G. Knepley   if (len) {
1783b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1784b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1785b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
17862adcc780SMatthew G. Knepley   }
17872adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17882adcc780SMatthew G. Knepley }
17892adcc780SMatthew G. Knepley 
179016621825SDmitry Karpeev 
179116621825SDmitry Karpeev /*@C
17928d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17938d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17948d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17958d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17968d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
179716621825SDmitry Karpeev 
179816621825SDmitry Karpeev   Not collective
179916621825SDmitry Karpeev 
180016621825SDmitry Karpeev   Input Parameter:
180116621825SDmitry Karpeev . dm - the DM object
180216621825SDmitry Karpeev 
180316621825SDmitry Karpeev   Output Parameters:
18040298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
18050298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
18060298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
18070298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
18080298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
180916621825SDmitry Karpeev 
181016621825SDmitry Karpeev   Level: intermediate
181116621825SDmitry Karpeev 
181216621825SDmitry Karpeev   Notes:
181316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
181416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
181516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
181616621825SDmitry Karpeev 
18178d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
181816621825SDmitry Karpeev @*/
18198d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
182016621825SDmitry Karpeev {
182116621825SDmitry Karpeev   PetscErrorCode      ierr;
1822be081cd6SPeter Brune   DMSubDomainHookLink link;
1823be081cd6SPeter Brune   PetscInt            i,l;
182416621825SDmitry Karpeev 
182516621825SDmitry Karpeev   PetscFunctionBegin;
182616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
182714a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
18280298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
18290298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
18300298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
18310298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1832f3f0edfdSDmitry Karpeev   /*
1833f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1834f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1835f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1836f3f0edfdSDmitry Karpeev    */
1837ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
183816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1839be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
184014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1841f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1842be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1843be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1844be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1845be081cd6SPeter Brune         }
1846648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1847e7c4fc90SDmitry Karpeev       }
184814a18fd3SPeter Brune     }
184914a18fd3SPeter Brune     if (len) *len = l;
185014a18fd3SPeter Brune   }
1851e30e807fSPeter Brune   PetscFunctionReturn(0);
1852e30e807fSPeter Brune }
1853e30e807fSPeter Brune 
1854e30e807fSPeter Brune 
1855e30e807fSPeter Brune /*@C
1856e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1857e30e807fSPeter Brune 
1858e30e807fSPeter Brune   Not collective
1859e30e807fSPeter Brune 
1860e30e807fSPeter Brune   Input Parameters:
1861e30e807fSPeter Brune + dm - the DM object
1862e30e807fSPeter Brune . n  - the number of subdomain scatters
1863e30e807fSPeter Brune - subdms - the local subdomains
1864e30e807fSPeter Brune 
1865e30e807fSPeter Brune   Output Parameters:
1866e30e807fSPeter Brune + n     - the number of scatters returned
1867e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1868e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1869e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1870e30e807fSPeter Brune 
187195452b02SPatrick Sanan   Notes:
187295452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1873e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1874e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1875e30e807fSPeter Brune   solution and residual data.
1876e30e807fSPeter Brune 
1877e30e807fSPeter Brune   Level: developer
1878e30e807fSPeter Brune 
1879e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1880e30e807fSPeter Brune @*/
1881e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1882e30e807fSPeter Brune {
1883e30e807fSPeter Brune   PetscErrorCode ierr;
1884e30e807fSPeter Brune 
1885e30e807fSPeter Brune   PetscFunctionBegin;
1886e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1887e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1888b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1889e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1890e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1891e7c4fc90SDmitry Karpeev }
1892e7c4fc90SDmitry Karpeev 
189347c6ae99SBarry Smith /*@
189447c6ae99SBarry Smith   DMRefine - Refines a DM object
189547c6ae99SBarry Smith 
1896d083f849SBarry Smith   Collective on dm
189747c6ae99SBarry Smith 
189847c6ae99SBarry Smith   Input Parameter:
189947c6ae99SBarry Smith + dm   - the DM object
190091d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
190147c6ae99SBarry Smith 
190247c6ae99SBarry Smith   Output Parameter:
19030298fd71SBarry Smith . dmf - the refined DM, or NULL
1904ae0a1c52SMatthew G Knepley 
19050298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
190647c6ae99SBarry Smith 
190747c6ae99SBarry Smith   Level: developer
190847c6ae99SBarry Smith 
1909e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
191047c6ae99SBarry Smith @*/
19117087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
191247c6ae99SBarry Smith {
191347c6ae99SBarry Smith   PetscErrorCode   ierr;
1914c833c3b5SJed Brown   DMRefineHookLink link;
191547c6ae99SBarry Smith 
191647c6ae99SBarry Smith   PetscFunctionBegin;
1917732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1918b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
19191ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
192047c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
19214057135bSMatthew G Knepley   if (*dmf) {
192243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
19238865f1eaSKarl Rupp 
19248cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
19258865f1eaSKarl Rupp 
1926644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
19270598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1928656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
19298865f1eaSKarl Rupp 
1930e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1931c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
19328865f1eaSKarl Rupp       if (link->refinehook) {
19338865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19348865f1eaSKarl Rupp       }
1935c833c3b5SJed Brown     }
1936c833c3b5SJed Brown   }
19371ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1938c833c3b5SJed Brown   PetscFunctionReturn(0);
1939c833c3b5SJed Brown }
1940c833c3b5SJed Brown 
1941bb9467b5SJed Brown /*@C
1942c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1943c833c3b5SJed Brown 
1944c833c3b5SJed Brown    Logically Collective
1945c833c3b5SJed Brown 
1946c833c3b5SJed Brown    Input Arguments:
1947c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1948c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1949c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19500298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1951c833c3b5SJed Brown 
1952c833c3b5SJed Brown    Calling sequence of refinehook:
1953c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1954c833c3b5SJed Brown 
1955c833c3b5SJed Brown +  coarse - coarse level DM
1956c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1957c833c3b5SJed Brown -  ctx - optional user-defined function context
1958c833c3b5SJed Brown 
1959c833c3b5SJed Brown    Calling sequence for interphook:
1960c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1961c833c3b5SJed Brown 
1962c833c3b5SJed Brown +  coarse - coarse level DM
1963c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1964c833c3b5SJed Brown .  fine - fine level DM to update
1965c833c3b5SJed Brown -  ctx - optional user-defined function context
1966c833c3b5SJed Brown 
1967c833c3b5SJed Brown    Level: advanced
1968c833c3b5SJed Brown 
1969c833c3b5SJed Brown    Notes:
1970c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1971c833c3b5SJed Brown 
1972c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1973c833c3b5SJed Brown 
1974bb9467b5SJed Brown    This function is currently not available from Fortran.
1975bb9467b5SJed Brown 
1976c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1977c833c3b5SJed Brown @*/
1978c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1979c833c3b5SJed Brown {
1980c833c3b5SJed Brown   PetscErrorCode   ierr;
1981c833c3b5SJed Brown   DMRefineHookLink link,*p;
1982c833c3b5SJed Brown 
1983c833c3b5SJed Brown   PetscFunctionBegin;
1984c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19853d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19863d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19873d8e3701SJed Brown   }
198895dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1989c833c3b5SJed Brown   link->refinehook = refinehook;
1990c833c3b5SJed Brown   link->interphook = interphook;
1991c833c3b5SJed Brown   link->ctx        = ctx;
19920298fd71SBarry Smith   link->next       = NULL;
1993c833c3b5SJed Brown   *p               = link;
1994c833c3b5SJed Brown   PetscFunctionReturn(0);
1995c833c3b5SJed Brown }
1996c833c3b5SJed Brown 
19973d8e3701SJed Brown /*@C
19983d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19993d8e3701SJed Brown 
20003d8e3701SJed Brown    Logically Collective
20013d8e3701SJed Brown 
20023d8e3701SJed Brown    Input Arguments:
20033d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
20043d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
20053d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20063d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20073d8e3701SJed Brown 
20083d8e3701SJed Brown    Level: advanced
20093d8e3701SJed Brown 
20103d8e3701SJed Brown    Notes:
20113d8e3701SJed Brown    This function does nothing if the hook is not in the list.
20123d8e3701SJed Brown 
20133d8e3701SJed Brown    This function is currently not available from Fortran.
20143d8e3701SJed Brown 
20153d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20163d8e3701SJed Brown @*/
20173d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
20183d8e3701SJed Brown {
20193d8e3701SJed Brown   PetscErrorCode   ierr;
20203d8e3701SJed Brown   DMRefineHookLink link,*p;
20213d8e3701SJed Brown 
20223d8e3701SJed Brown   PetscFunctionBegin;
20233d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20243d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
20253d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
20263d8e3701SJed Brown       link = *p;
20273d8e3701SJed Brown       *p = link->next;
20283d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
20293d8e3701SJed Brown       break;
20303d8e3701SJed Brown     }
20313d8e3701SJed Brown   }
20323d8e3701SJed Brown   PetscFunctionReturn(0);
20333d8e3701SJed Brown }
20343d8e3701SJed Brown 
2035c833c3b5SJed Brown /*@
2036c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2037c833c3b5SJed Brown 
2038c833c3b5SJed Brown    Collective if any hooks are
2039c833c3b5SJed Brown 
2040c833c3b5SJed Brown    Input Arguments:
2041c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2042e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2043c833c3b5SJed Brown -  fine - finer DM to update
2044c833c3b5SJed Brown 
2045c833c3b5SJed Brown    Level: developer
2046c833c3b5SJed Brown 
2047c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2048c833c3b5SJed Brown @*/
2049c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2050c833c3b5SJed Brown {
2051c833c3b5SJed Brown   PetscErrorCode   ierr;
2052c833c3b5SJed Brown   DMRefineHookLink link;
2053c833c3b5SJed Brown 
2054c833c3b5SJed Brown   PetscFunctionBegin;
2055c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20568865f1eaSKarl Rupp     if (link->interphook) {
20578865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20588865f1eaSKarl Rupp     }
20594057135bSMatthew G Knepley   }
206047c6ae99SBarry Smith   PetscFunctionReturn(0);
206147c6ae99SBarry Smith }
206247c6ae99SBarry Smith 
2063eb3f98d2SBarry Smith /*@
2064eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2065eb3f98d2SBarry Smith 
2066eb3f98d2SBarry Smith     Not Collective
2067eb3f98d2SBarry Smith 
2068eb3f98d2SBarry Smith     Input Parameter:
2069eb3f98d2SBarry Smith .   dm - the DM object
2070eb3f98d2SBarry Smith 
2071eb3f98d2SBarry Smith     Output Parameter:
2072eb3f98d2SBarry Smith .   level - number of refinements
2073eb3f98d2SBarry Smith 
2074eb3f98d2SBarry Smith     Level: developer
2075eb3f98d2SBarry Smith 
20766a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2077eb3f98d2SBarry Smith 
2078eb3f98d2SBarry Smith @*/
2079eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2080eb3f98d2SBarry Smith {
2081eb3f98d2SBarry Smith   PetscFunctionBegin;
2082eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2083eb3f98d2SBarry Smith   *level = dm->levelup;
2084eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2085eb3f98d2SBarry Smith }
2086eb3f98d2SBarry Smith 
2087fef3a512SBarry Smith /*@
2088fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2089fef3a512SBarry Smith 
2090fef3a512SBarry Smith     Not Collective
2091fef3a512SBarry Smith 
2092fef3a512SBarry Smith     Input Parameter:
2093fef3a512SBarry Smith +   dm - the DM object
2094fef3a512SBarry Smith -   level - number of refinements
2095fef3a512SBarry Smith 
2096fef3a512SBarry Smith     Level: advanced
2097fef3a512SBarry Smith 
209895452b02SPatrick Sanan     Notes:
209995452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2100fef3a512SBarry Smith 
2101fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2102fef3a512SBarry Smith 
2103fef3a512SBarry Smith @*/
2104fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2105fef3a512SBarry Smith {
2106fef3a512SBarry Smith   PetscFunctionBegin;
2107fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2108fef3a512SBarry Smith   dm->levelup = level;
2109fef3a512SBarry Smith   PetscFunctionReturn(0);
2110fef3a512SBarry Smith }
2111fef3a512SBarry Smith 
2112ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2113ca3d3a14SMatthew G. Knepley {
2114ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2115ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2116ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2117ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2118ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2119ca3d3a14SMatthew G. Knepley }
2120ca3d3a14SMatthew G. Knepley 
2121ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2122ca3d3a14SMatthew G. Knepley {
2123ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2124ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2125ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2126ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2127ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2128ca3d3a14SMatthew G. Knepley }
2129ca3d3a14SMatthew G. Knepley 
2130ca3d3a14SMatthew G. Knepley /*@
2131c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2132ca3d3a14SMatthew G. Knepley 
2133ca3d3a14SMatthew G. Knepley   Input Parameter:
2134ca3d3a14SMatthew G. Knepley . dm - The DM
2135ca3d3a14SMatthew G. Knepley 
2136ca3d3a14SMatthew G. Knepley   Output Parameter:
2137ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2138ca3d3a14SMatthew G. Knepley 
2139ca3d3a14SMatthew G. Knepley   Level: developer
2140ca3d3a14SMatthew G. Knepley 
2141ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2142ca3d3a14SMatthew G. Knepley @*/
2143ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2144ca3d3a14SMatthew G. Knepley {
2145ca3d3a14SMatthew G. Knepley   Vec            tv;
2146ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2147ca3d3a14SMatthew G. Knepley 
2148ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2149ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2150534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2151ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2152ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2153ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2154ca3d3a14SMatthew G. Knepley }
2155ca3d3a14SMatthew G. Knepley 
2156ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2157ca3d3a14SMatthew G. Knepley {
2158ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2159ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2160ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2161ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2162ca3d3a14SMatthew G. Knepley 
2163ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2164ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
216592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2166ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2167ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2168ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
216992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2170ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2171ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2172ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2173ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2174ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2175ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2176ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2177ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2178ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2179ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2180ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2181ca3d3a14SMatthew G. Knepley     }
2182ca3d3a14SMatthew G. Knepley   }
2183ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2184ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2185ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2186ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2187ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2188ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2189ca3d3a14SMatthew G. Knepley       if (dof) {
2190ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2191ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2192ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2193ca3d3a14SMatthew G. Knepley 
2194ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2195ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2196ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2197580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2198ca3d3a14SMatthew G. Knepley       }
2199ca3d3a14SMatthew G. Knepley     }
2200ca3d3a14SMatthew G. Knepley   }
2201ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2202ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2203ca3d3a14SMatthew G. Knepley }
2204ca3d3a14SMatthew G. Knepley 
2205ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2206ca3d3a14SMatthew G. Knepley {
2207ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2208ca3d3a14SMatthew G. Knepley 
2209ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2210ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2211ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2212ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2213ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2214ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2215ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2216ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2217ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2218ca3d3a14SMatthew G. Knepley }
2219ca3d3a14SMatthew G. Knepley 
2220bb9467b5SJed Brown /*@C
2221baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2222baf369e7SPeter Brune 
2223baf369e7SPeter Brune    Logically Collective
2224baf369e7SPeter Brune 
2225baf369e7SPeter Brune    Input Arguments:
2226baf369e7SPeter Brune +  dm - the DM
2227baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2228baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
22290298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2230baf369e7SPeter Brune 
2231baf369e7SPeter Brune    Calling sequence for beginhook:
2232baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2233baf369e7SPeter Brune 
2234baf369e7SPeter Brune +  dm - global DM
2235baf369e7SPeter Brune .  g - global vector
2236baf369e7SPeter Brune .  mode - mode
2237baf369e7SPeter Brune .  l - local vector
2238baf369e7SPeter Brune -  ctx - optional user-defined function context
2239baf369e7SPeter Brune 
2240baf369e7SPeter Brune 
2241baf369e7SPeter Brune    Calling sequence for endhook:
2242ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2243baf369e7SPeter Brune 
2244baf369e7SPeter Brune +  global - global DM
2245baf369e7SPeter Brune -  ctx - optional user-defined function context
2246baf369e7SPeter Brune 
2247baf369e7SPeter Brune    Level: advanced
2248baf369e7SPeter Brune 
2249baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2250baf369e7SPeter Brune @*/
2251baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2252baf369e7SPeter Brune {
2253baf369e7SPeter Brune   PetscErrorCode          ierr;
2254baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2255baf369e7SPeter Brune 
2256baf369e7SPeter Brune   PetscFunctionBegin;
2257baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2258baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
225995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2260baf369e7SPeter Brune   link->beginhook = beginhook;
2261baf369e7SPeter Brune   link->endhook   = endhook;
2262baf369e7SPeter Brune   link->ctx       = ctx;
22630298fd71SBarry Smith   link->next      = NULL;
2264baf369e7SPeter Brune   *p              = link;
2265baf369e7SPeter Brune   PetscFunctionReturn(0);
2266baf369e7SPeter Brune }
2267baf369e7SPeter Brune 
22684c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
22694c274da1SToby Isaac {
22704c274da1SToby Isaac   Mat cMat;
22714c274da1SToby Isaac   Vec cVec;
22724c274da1SToby Isaac   PetscSection section, cSec;
22734c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22744c274da1SToby Isaac   PetscErrorCode ierr;
22754c274da1SToby Isaac 
22764c274da1SToby Isaac   PetscFunctionBegin;
22774c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22784c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22794c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
22805db9a05bSToby Isaac     PetscInt nRows;
22815db9a05bSToby Isaac 
22825db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22835db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
228492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
22857711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22864c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
22874c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22884c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22894c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22904c274da1SToby Isaac       if (dof) {
22914c274da1SToby Isaac         PetscScalar *vals;
22924c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
22934c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
22944c274da1SToby Isaac       }
22954c274da1SToby Isaac     }
22964c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22974c274da1SToby Isaac   }
22984c274da1SToby Isaac   PetscFunctionReturn(0);
22994c274da1SToby Isaac }
23004c274da1SToby Isaac 
230147c6ae99SBarry Smith /*@
230201729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
230301729b5cSPatrick Sanan 
2304d083f849SBarry Smith     Neighbor-wise Collective on dm
230501729b5cSPatrick Sanan 
230601729b5cSPatrick Sanan     Input Parameters:
230701729b5cSPatrick Sanan +   dm - the DM object
230801729b5cSPatrick Sanan .   g - the global vector
230901729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
231001729b5cSPatrick Sanan -   l - the local vector
231101729b5cSPatrick Sanan 
231201729b5cSPatrick Sanan     Notes:
231301729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
231401729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
231501729b5cSPatrick Sanan 
231601729b5cSPatrick Sanan     Level: beginner
231701729b5cSPatrick Sanan 
231801729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
231901729b5cSPatrick Sanan 
232001729b5cSPatrick Sanan @*/
232101729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
232201729b5cSPatrick Sanan {
232301729b5cSPatrick Sanan   PetscErrorCode ierr;
232401729b5cSPatrick Sanan 
232501729b5cSPatrick Sanan   PetscFunctionBegin;
232601729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
232701729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
232801729b5cSPatrick Sanan   PetscFunctionReturn(0);
232901729b5cSPatrick Sanan }
233001729b5cSPatrick Sanan 
233101729b5cSPatrick Sanan /*@
233247c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
233347c6ae99SBarry Smith 
2334d083f849SBarry Smith     Neighbor-wise Collective on dm
233547c6ae99SBarry Smith 
233647c6ae99SBarry Smith     Input Parameters:
233747c6ae99SBarry Smith +   dm - the DM object
233847c6ae99SBarry Smith .   g - the global vector
233947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
234047c6ae99SBarry Smith -   l - the local vector
234147c6ae99SBarry Smith 
234201729b5cSPatrick Sanan     Level: intermediate
234347c6ae99SBarry Smith 
234401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
234547c6ae99SBarry Smith 
234647c6ae99SBarry Smith @*/
23477087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
234847c6ae99SBarry Smith {
23497128ae9fSMatthew G Knepley   PetscSF                 sf;
235047c6ae99SBarry Smith   PetscErrorCode          ierr;
2351baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
235247c6ae99SBarry Smith 
235347c6ae99SBarry Smith   PetscFunctionBegin;
2354171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2355baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23568865f1eaSKarl Rupp     if (link->beginhook) {
23578865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23588865f1eaSKarl Rupp     }
2359baf369e7SPeter Brune   }
23601bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
23617128ae9fSMatthew G Knepley   if (sf) {
2362ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2363ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
23647128ae9fSMatthew G Knepley 
236582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23667128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2367ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23687128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23697128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2370ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
23717128ae9fSMatthew G Knepley   } else {
237233907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2373843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23747128ae9fSMatthew G Knepley   }
237547c6ae99SBarry Smith   PetscFunctionReturn(0);
237647c6ae99SBarry Smith }
237747c6ae99SBarry Smith 
237847c6ae99SBarry Smith /*@
237947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
238047c6ae99SBarry Smith 
2381d083f849SBarry Smith     Neighbor-wise Collective on dm
238247c6ae99SBarry Smith 
238347c6ae99SBarry Smith     Input Parameters:
238447c6ae99SBarry Smith +   dm - the DM object
238547c6ae99SBarry Smith .   g - the global vector
238647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
238747c6ae99SBarry Smith -   l - the local vector
238847c6ae99SBarry Smith 
238901729b5cSPatrick Sanan     Level: intermediate
239047c6ae99SBarry Smith 
239101729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
239247c6ae99SBarry Smith 
239347c6ae99SBarry Smith @*/
23947087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
239547c6ae99SBarry Smith {
23967128ae9fSMatthew G Knepley   PetscSF                 sf;
239747c6ae99SBarry Smith   PetscErrorCode          ierr;
2398ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2399ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2400ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2401baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
240247c6ae99SBarry Smith 
240347c6ae99SBarry Smith   PetscFunctionBegin;
2404171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24051bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2406ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
24077128ae9fSMatthew G Knepley   if (sf) {
240882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24097128ae9fSMatthew G Knepley 
24107128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2411ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
24127128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
24137128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2414ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2415ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
24167128ae9fSMatthew G Knepley   } else {
241733907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2418843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24197128ae9fSMatthew G Knepley   }
24204c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2421baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2422baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2423baf369e7SPeter Brune   }
242447c6ae99SBarry Smith   PetscFunctionReturn(0);
242547c6ae99SBarry Smith }
242647c6ae99SBarry Smith 
2427d4d07f1eSToby Isaac /*@C
2428d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2429d4d07f1eSToby Isaac 
2430d4d07f1eSToby Isaac    Logically Collective
2431d4d07f1eSToby Isaac 
2432d4d07f1eSToby Isaac    Input Arguments:
2433d4d07f1eSToby Isaac +  dm - the DM
2434d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2435d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2436d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2437d4d07f1eSToby Isaac 
2438d4d07f1eSToby Isaac    Calling sequence for beginhook:
2439d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2440d4d07f1eSToby Isaac 
2441d4d07f1eSToby Isaac +  dm - global DM
2442d4d07f1eSToby Isaac .  l - local vector
2443d4d07f1eSToby Isaac .  mode - mode
2444d4d07f1eSToby Isaac .  g - global vector
2445d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2446d4d07f1eSToby Isaac 
2447d4d07f1eSToby Isaac 
2448d4d07f1eSToby Isaac    Calling sequence for endhook:
2449d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2450d4d07f1eSToby Isaac 
2451d4d07f1eSToby Isaac +  global - global DM
2452d4d07f1eSToby Isaac .  l - local vector
2453d4d07f1eSToby Isaac .  mode - mode
2454d4d07f1eSToby Isaac .  g - global vector
2455d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2456d4d07f1eSToby Isaac 
2457d4d07f1eSToby Isaac    Level: advanced
2458d4d07f1eSToby Isaac 
2459d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2460d4d07f1eSToby Isaac @*/
2461d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2462d4d07f1eSToby Isaac {
2463d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2464d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2465d4d07f1eSToby Isaac 
2466d4d07f1eSToby Isaac   PetscFunctionBegin;
2467d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2468d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
246995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2470d4d07f1eSToby Isaac   link->beginhook = beginhook;
2471d4d07f1eSToby Isaac   link->endhook   = endhook;
2472d4d07f1eSToby Isaac   link->ctx       = ctx;
2473d4d07f1eSToby Isaac   link->next      = NULL;
2474d4d07f1eSToby Isaac   *p              = link;
2475d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2476d4d07f1eSToby Isaac }
2477d4d07f1eSToby Isaac 
24784c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
24794c274da1SToby Isaac {
24804c274da1SToby Isaac   Mat cMat;
24814c274da1SToby Isaac   Vec cVec;
24824c274da1SToby Isaac   PetscSection section, cSec;
24834c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24844c274da1SToby Isaac   PetscErrorCode ierr;
24854c274da1SToby Isaac 
24864c274da1SToby Isaac   PetscFunctionBegin;
24874c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24884c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24894c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
24905db9a05bSToby Isaac     PetscInt nRows;
24915db9a05bSToby Isaac 
24925db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24935db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
249492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
24957711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24964c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24974c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24984c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24994c274da1SToby Isaac       if (dof) {
25004c274da1SToby Isaac         PetscInt d;
25014c274da1SToby Isaac         PetscScalar *vals;
25024c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
25034c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
25044c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
25054c274da1SToby Isaac          * we just extracted */
25064c274da1SToby Isaac         for (d = 0; d < dof; d++) {
25074c274da1SToby Isaac           vals[d] = 0.;
25084c274da1SToby Isaac         }
25094c274da1SToby Isaac       }
25104c274da1SToby Isaac     }
25114c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
25124c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25134c274da1SToby Isaac   }
25144c274da1SToby Isaac   PetscFunctionReturn(0);
25154c274da1SToby Isaac }
251601729b5cSPatrick Sanan /*@
251701729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
251801729b5cSPatrick Sanan 
2519d083f849SBarry Smith     Neighbor-wise Collective on dm
252001729b5cSPatrick Sanan 
252101729b5cSPatrick Sanan     Input Parameters:
252201729b5cSPatrick Sanan +   dm - the DM object
252301729b5cSPatrick Sanan .   l - the local vector
252401729b5cSPatrick 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.
252501729b5cSPatrick Sanan -   g - the global vector
252601729b5cSPatrick Sanan 
252701729b5cSPatrick Sanan     Notes:
252801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
252901729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
253001729b5cSPatrick Sanan 
253101729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
253201729b5cSPatrick 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.
253301729b5cSPatrick Sanan 
253401729b5cSPatrick Sanan     Level: beginner
253501729b5cSPatrick Sanan 
253601729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
253701729b5cSPatrick Sanan 
253801729b5cSPatrick Sanan @*/
253901729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
254001729b5cSPatrick Sanan {
254101729b5cSPatrick Sanan   PetscErrorCode ierr;
254201729b5cSPatrick Sanan 
254301729b5cSPatrick Sanan   PetscFunctionBegin;
254401729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
254501729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
254601729b5cSPatrick Sanan   PetscFunctionReturn(0);
254701729b5cSPatrick Sanan }
25484c274da1SToby Isaac 
254947c6ae99SBarry Smith /*@
255001729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25519a42bb27SBarry Smith 
2552d083f849SBarry Smith     Neighbor-wise Collective on dm
25539a42bb27SBarry Smith 
25549a42bb27SBarry Smith     Input Parameters:
25559a42bb27SBarry Smith +   dm - the DM object
2556f6813fd5SJed Brown .   l - the local vector
25571eb28f2eSBarry 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.
25581eb28f2eSBarry Smith -   g - the global vector
25599a42bb27SBarry Smith 
256095452b02SPatrick Sanan     Notes:
256195452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
256284330215SMatthew 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.
25639a42bb27SBarry Smith 
256401729b5cSPatrick Sanan     Level: intermediate
25659a42bb27SBarry Smith 
256601729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
25679a42bb27SBarry Smith 
25689a42bb27SBarry Smith @*/
25697087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
25709a42bb27SBarry Smith {
25717128ae9fSMatthew G Knepley   PetscSF                 sf;
257284330215SMatthew G. Knepley   PetscSection            s, gs;
2573d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2574ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2575ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2576ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2577ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
257884330215SMatthew G. Knepley   PetscErrorCode          ierr;
25799a42bb27SBarry Smith 
25809a42bb27SBarry Smith   PetscFunctionBegin;
2581171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2582d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2583d4d07f1eSToby Isaac     if (link->beginhook) {
2584d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2585d4d07f1eSToby Isaac     }
2586d4d07f1eSToby Isaac   }
25874c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
25881bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
258992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
25907128ae9fSMatthew G Knepley   switch (mode) {
25917128ae9fSMatthew G Knepley   case INSERT_VALUES:
25927128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2593304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
259484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25957128ae9fSMatthew G Knepley   case ADD_VALUES:
25967128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2597304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
259884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25997128ae9fSMatthew G Knepley   default:
260082f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26017128ae9fSMatthew G Knepley   }
2602ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2603ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2604ca3d3a14SMatthew G. Knepley     if (transform) {
2605ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2606ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2607ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2608ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2609ca3d3a14SMatthew G. Knepley     } else {
2610ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2611ca3d3a14SMatthew G. Knepley     }
26127128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2613ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2614a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
261584330215SMatthew G. Knepley     } else if (s && isInsert) {
261684330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
261784330215SMatthew G. Knepley 
2618e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
261984330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
262084330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
262184330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2622b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
262384330215SMatthew G. Knepley 
262484330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
262503442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
262684330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2627b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
262884330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
262984330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2630b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
263103442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2632b3b16f48SMatthew 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);
2633b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2634b3b16f48SMatthew G. Knepley         if (!gcdof) {
263584330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2636b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2637b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
263884330215SMatthew G. Knepley           const PetscInt *cdofs;
263984330215SMatthew G. Knepley           PetscInt        cind = 0;
264084330215SMatthew G. Knepley 
264184330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2642b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
264384330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2644b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
264584330215SMatthew G. Knepley           }
2646b3b16f48SMatthew 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);
264784330215SMatthew G. Knepley       }
2648ca3d3a14SMatthew G. Knepley     }
26497128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2650ca3d3a14SMatthew G. Knepley     if (transform) {
2651ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2652ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2653ca3d3a14SMatthew G. Knepley     } else {
2654ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2655ca3d3a14SMatthew G. Knepley     }
26567128ae9fSMatthew G Knepley   } else {
2657b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2658843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26597128ae9fSMatthew G Knepley   }
26609a42bb27SBarry Smith   PetscFunctionReturn(0);
26619a42bb27SBarry Smith }
26629a42bb27SBarry Smith 
26639a42bb27SBarry Smith /*@
26649a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
266547c6ae99SBarry Smith 
2666d083f849SBarry Smith     Neighbor-wise Collective on dm
266747c6ae99SBarry Smith 
266847c6ae99SBarry Smith     Input Parameters:
266947c6ae99SBarry Smith +   dm - the DM object
2670f6813fd5SJed Brown .   l - the local vector
267147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2672f6813fd5SJed Brown -   g - the global vector
267347c6ae99SBarry Smith 
267401729b5cSPatrick Sanan     Level: intermediate
267547c6ae99SBarry Smith 
2676e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
267747c6ae99SBarry Smith 
267847c6ae99SBarry Smith @*/
26797087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
268047c6ae99SBarry Smith {
26817128ae9fSMatthew G Knepley   PetscSF                 sf;
268284330215SMatthew G. Knepley   PetscSection            s;
2683d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2684ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
268584330215SMatthew G. Knepley   PetscErrorCode          ierr;
268647c6ae99SBarry Smith 
268747c6ae99SBarry Smith   PetscFunctionBegin;
2688171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26891bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
269092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26917128ae9fSMatthew G Knepley   switch (mode) {
26927128ae9fSMatthew G Knepley   case INSERT_VALUES:
26937128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
269484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26957128ae9fSMatthew G Knepley   case ADD_VALUES:
26967128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
269784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26987128ae9fSMatthew G Knepley   default:
269982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27007128ae9fSMatthew G Knepley   }
270184330215SMatthew G. Knepley   if (sf && !isInsert) {
2702ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2703ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2704ca3d3a14SMatthew G. Knepley     Vec                tmpl;
270584330215SMatthew G. Knepley 
2706ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2707ca3d3a14SMatthew G. Knepley     if (transform) {
2708ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2709ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2710ca3d3a14SMatthew G. Knepley     } else {
2711ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2712ca3d3a14SMatthew G. Knepley     }
27137128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2714a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2715ca3d3a14SMatthew G. Knepley     if (transform) {
2716ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2717ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2718ca3d3a14SMatthew G. Knepley     } else {
2719ae5cfb4aSMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2720ca3d3a14SMatthew G. Knepley     }
27217128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
272284330215SMatthew G. Knepley   } else if (s && isInsert) {
27237128ae9fSMatthew G Knepley   } else {
2724b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2725843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27267128ae9fSMatthew G Knepley   }
2727d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2728d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2729d4d07f1eSToby Isaac   }
273047c6ae99SBarry Smith   PetscFunctionReturn(0);
273147c6ae99SBarry Smith }
273247c6ae99SBarry Smith 
2733f089877aSRichard Tran Mills /*@
2734bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2735bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2736d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2737f089877aSRichard Tran Mills 
2738d083f849SBarry Smith    Neighbor-wise Collective on dm
2739f089877aSRichard Tran Mills 
2740f089877aSRichard Tran Mills    Input Parameters:
2741f089877aSRichard Tran Mills +  dm - the DM object
2742bc0a1609SRichard Tran Mills .  g - the original local vector
2743bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2744f089877aSRichard Tran Mills 
2745bc0a1609SRichard Tran Mills    Output Parameter:
2746bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2747f089877aSRichard Tran Mills 
2748f089877aSRichard Tran Mills    Level: intermediate
2749f089877aSRichard Tran Mills 
2750bc0a1609SRichard Tran Mills    Notes:
2751bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2752bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2753bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2754bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2755bc0a1609SRichard Tran Mills 
2756bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2757f089877aSRichard Tran Mills 
2758f089877aSRichard Tran Mills @*/
2759f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2760f089877aSRichard Tran Mills {
2761f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2762f089877aSRichard Tran Mills 
2763f089877aSRichard Tran Mills   PetscFunctionBegin;
2764f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2765bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2766f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2767f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2768f089877aSRichard Tran Mills }
2769f089877aSRichard Tran Mills 
2770f089877aSRichard Tran Mills /*@
2771bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2772bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2773d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2774f089877aSRichard Tran Mills 
2775d083f849SBarry Smith    Neighbor-wise Collective on dm
2776f089877aSRichard Tran Mills 
2777f089877aSRichard Tran Mills    Input Parameters:
2778bc0a1609SRichard Tran Mills +  da - the DM object
2779bc0a1609SRichard Tran Mills .  g - the original local vector
2780bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2781f089877aSRichard Tran Mills 
2782bc0a1609SRichard Tran Mills    Output Parameter:
2783bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2784f089877aSRichard Tran Mills 
2785f089877aSRichard Tran Mills    Level: intermediate
2786f089877aSRichard Tran Mills 
2787bc0a1609SRichard Tran Mills    Notes:
2788bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2789bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2790bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2791bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2792bc0a1609SRichard Tran Mills 
2793bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2794f089877aSRichard Tran Mills 
2795f089877aSRichard Tran Mills @*/
2796f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2797f089877aSRichard Tran Mills {
2798f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2799f089877aSRichard Tran Mills 
2800f089877aSRichard Tran Mills   PetscFunctionBegin;
2801f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2802bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2803f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2804f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2805f089877aSRichard Tran Mills }
2806f089877aSRichard Tran Mills 
2807f089877aSRichard Tran Mills 
280847c6ae99SBarry Smith /*@
280947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
281047c6ae99SBarry Smith 
2811d083f849SBarry Smith     Collective on dm
281247c6ae99SBarry Smith 
281347c6ae99SBarry Smith     Input Parameter:
281447c6ae99SBarry Smith +   dm - the DM object
281591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
281647c6ae99SBarry Smith 
281747c6ae99SBarry Smith     Output Parameter:
281847c6ae99SBarry Smith .   dmc - the coarsened DM
281947c6ae99SBarry Smith 
282047c6ae99SBarry Smith     Level: developer
282147c6ae99SBarry Smith 
2822e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
282347c6ae99SBarry Smith 
282447c6ae99SBarry Smith @*/
28257087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
282647c6ae99SBarry Smith {
282747c6ae99SBarry Smith   PetscErrorCode    ierr;
2828b17ce1afSJed Brown   DMCoarsenHookLink link;
282947c6ae99SBarry Smith 
283047c6ae99SBarry Smith   PetscFunctionBegin;
2831171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2832b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
283347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
283447c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2835b9d85ea2SLisandro Dalcin   if (*dmc) {
2836a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
283743842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28388cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2839644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
28400598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2841656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2842e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2843b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2844b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2845b17ce1afSJed Brown     }
2846b9d85ea2SLisandro Dalcin   }
284747a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2848b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2849b17ce1afSJed Brown   PetscFunctionReturn(0);
2850b17ce1afSJed Brown }
2851b17ce1afSJed Brown 
2852bb9467b5SJed Brown /*@C
2853b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2854b17ce1afSJed Brown 
2855b17ce1afSJed Brown    Logically Collective
2856b17ce1afSJed Brown 
2857b17ce1afSJed Brown    Input Arguments:
2858b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2859b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2860b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
28610298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2862b17ce1afSJed Brown 
2863b17ce1afSJed Brown    Calling sequence of coarsenhook:
2864b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2865b17ce1afSJed Brown 
2866b17ce1afSJed Brown +  fine - fine level DM
2867b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2868b17ce1afSJed Brown -  ctx - optional user-defined function context
2869b17ce1afSJed Brown 
2870b17ce1afSJed Brown    Calling sequence for restricthook:
2871c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2872b17ce1afSJed Brown 
2873b17ce1afSJed Brown +  fine - fine level DM
2874b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2875c833c3b5SJed Brown .  rscale - scaling vector for restriction
2876c833c3b5SJed Brown .  inject - matrix restricting by injection
2877b17ce1afSJed Brown .  coarse - coarse level DM to update
2878b17ce1afSJed Brown -  ctx - optional user-defined function context
2879b17ce1afSJed Brown 
2880b17ce1afSJed Brown    Level: advanced
2881b17ce1afSJed Brown 
2882b17ce1afSJed Brown    Notes:
2883b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2884b17ce1afSJed Brown 
2885b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2886b17ce1afSJed Brown 
2887b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2888b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2889b17ce1afSJed Brown 
2890bb9467b5SJed Brown    This function is currently not available from Fortran.
2891bb9467b5SJed Brown 
2892dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2893b17ce1afSJed Brown @*/
2894b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2895b17ce1afSJed Brown {
2896b17ce1afSJed Brown   PetscErrorCode    ierr;
2897b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2898b17ce1afSJed Brown 
2899b17ce1afSJed Brown   PetscFunctionBegin;
2900b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
29011e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
29021e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
29031e3d8eccSJed Brown   }
290495dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2905b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2906b17ce1afSJed Brown   link->restricthook = restricthook;
2907b17ce1afSJed Brown   link->ctx          = ctx;
29080298fd71SBarry Smith   link->next         = NULL;
2909b17ce1afSJed Brown   *p                 = link;
2910b17ce1afSJed Brown   PetscFunctionReturn(0);
2911b17ce1afSJed Brown }
2912b17ce1afSJed Brown 
2913dc822a44SJed Brown /*@C
2914dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2915dc822a44SJed Brown 
2916dc822a44SJed Brown    Logically Collective
2917dc822a44SJed Brown 
2918dc822a44SJed Brown    Input Arguments:
2919dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2920dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2921dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2922dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2923dc822a44SJed Brown 
2924dc822a44SJed Brown    Level: advanced
2925dc822a44SJed Brown 
2926dc822a44SJed Brown    Notes:
2927dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2928dc822a44SJed Brown 
2929dc822a44SJed Brown    This function is currently not available from Fortran.
2930dc822a44SJed Brown 
2931dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2932dc822a44SJed Brown @*/
2933dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2934dc822a44SJed Brown {
2935dc822a44SJed Brown   PetscErrorCode    ierr;
2936dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2937dc822a44SJed Brown 
2938dc822a44SJed Brown   PetscFunctionBegin;
2939dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2940dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2941dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2942dc822a44SJed Brown       link = *p;
2943dc822a44SJed Brown       *p = link->next;
2944dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2945dc822a44SJed Brown       break;
2946dc822a44SJed Brown     }
2947dc822a44SJed Brown   }
2948dc822a44SJed Brown   PetscFunctionReturn(0);
2949dc822a44SJed Brown }
2950dc822a44SJed Brown 
2951dc822a44SJed Brown 
2952b17ce1afSJed Brown /*@
2953b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2954b17ce1afSJed Brown 
2955b17ce1afSJed Brown    Collective if any hooks are
2956b17ce1afSJed Brown 
2957b17ce1afSJed Brown    Input Arguments:
2958b17ce1afSJed Brown +  fine - finer DM to use as a base
2959b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2960e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2961b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2962e91eccc2SStefano Zampini -  coarse - coarser DM to update
2963b17ce1afSJed Brown 
2964b17ce1afSJed Brown    Level: developer
2965b17ce1afSJed Brown 
2966b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2967b17ce1afSJed Brown @*/
2968b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2969b17ce1afSJed Brown {
2970b17ce1afSJed Brown   PetscErrorCode    ierr;
2971b17ce1afSJed Brown   DMCoarsenHookLink link;
2972b17ce1afSJed Brown 
2973b17ce1afSJed Brown   PetscFunctionBegin;
2974b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
29758865f1eaSKarl Rupp     if (link->restricthook) {
29768865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
29778865f1eaSKarl Rupp     }
2978b17ce1afSJed Brown   }
297947c6ae99SBarry Smith   PetscFunctionReturn(0);
298047c6ae99SBarry Smith }
298147c6ae99SBarry Smith 
2982bb9467b5SJed Brown /*@C
2983be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
29845dbd56e3SPeter Brune 
2985d083f849SBarry Smith    Logically Collective on global
29865dbd56e3SPeter Brune 
29875dbd56e3SPeter Brune    Input Arguments:
29885dbd56e3SPeter Brune +  global - global DM
2989ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
29905dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
29910298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
29925dbd56e3SPeter Brune 
2993ec4806b8SPeter Brune 
2994ec4806b8SPeter Brune    Calling sequence for ddhook:
2995ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2996ec4806b8SPeter Brune 
2997ec4806b8SPeter Brune +  global - global DM
2998ec4806b8SPeter Brune .  block  - block DM
2999ec4806b8SPeter Brune -  ctx - optional user-defined function context
3000ec4806b8SPeter Brune 
30015dbd56e3SPeter Brune    Calling sequence for restricthook:
3002ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
30035dbd56e3SPeter Brune 
30045dbd56e3SPeter Brune +  global - global DM
30055dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
30065dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3007ec4806b8SPeter Brune .  block  - block DM
30085dbd56e3SPeter Brune -  ctx - optional user-defined function context
30095dbd56e3SPeter Brune 
30105dbd56e3SPeter Brune    Level: advanced
30115dbd56e3SPeter Brune 
30125dbd56e3SPeter Brune    Notes:
3013ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
30145dbd56e3SPeter Brune 
30155dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
30165dbd56e3SPeter Brune 
30175dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3018ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
30195dbd56e3SPeter Brune 
3020bb9467b5SJed Brown    This function is currently not available from Fortran.
3021bb9467b5SJed Brown 
30225dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
30235dbd56e3SPeter Brune @*/
3024be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
30255dbd56e3SPeter Brune {
30265dbd56e3SPeter Brune   PetscErrorCode      ierr;
3027be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
30285dbd56e3SPeter Brune 
30295dbd56e3SPeter Brune   PetscFunctionBegin;
30305dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3031b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3032b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3033b3a6b972SJed Brown   }
303495dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30355dbd56e3SPeter Brune   link->restricthook = restricthook;
3036be081cd6SPeter Brune   link->ddhook       = ddhook;
30375dbd56e3SPeter Brune   link->ctx          = ctx;
30380298fd71SBarry Smith   link->next         = NULL;
30395dbd56e3SPeter Brune   *p                 = link;
30405dbd56e3SPeter Brune   PetscFunctionReturn(0);
30415dbd56e3SPeter Brune }
30425dbd56e3SPeter Brune 
3043b3a6b972SJed Brown /*@C
3044b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3045b3a6b972SJed Brown 
3046b3a6b972SJed Brown    Logically Collective
3047b3a6b972SJed Brown 
3048b3a6b972SJed Brown    Input Arguments:
3049b3a6b972SJed Brown +  global - global DM
3050b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3051b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3052b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3053b3a6b972SJed Brown 
3054b3a6b972SJed Brown    Level: advanced
3055b3a6b972SJed Brown 
3056b3a6b972SJed Brown    Notes:
3057b3a6b972SJed Brown 
3058b3a6b972SJed Brown    This function is currently not available from Fortran.
3059b3a6b972SJed Brown 
3060b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3061b3a6b972SJed Brown @*/
3062b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3063b3a6b972SJed Brown {
3064b3a6b972SJed Brown   PetscErrorCode      ierr;
3065b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3066b3a6b972SJed Brown 
3067b3a6b972SJed Brown   PetscFunctionBegin;
3068b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3069b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3070b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3071b3a6b972SJed Brown       link = *p;
3072b3a6b972SJed Brown       *p = link->next;
3073b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3074b3a6b972SJed Brown       break;
3075b3a6b972SJed Brown     }
3076b3a6b972SJed Brown   }
3077b3a6b972SJed Brown   PetscFunctionReturn(0);
3078b3a6b972SJed Brown }
3079b3a6b972SJed Brown 
30805dbd56e3SPeter Brune /*@
3081be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
30825dbd56e3SPeter Brune 
30835dbd56e3SPeter Brune    Collective if any hooks are
30845dbd56e3SPeter Brune 
30855dbd56e3SPeter Brune    Input Arguments:
30865dbd56e3SPeter Brune +  fine - finer DM to use as a base
3087be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3088be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
30895dbd56e3SPeter Brune -  coarse - coarer DM to update
30905dbd56e3SPeter Brune 
30915dbd56e3SPeter Brune    Level: developer
30925dbd56e3SPeter Brune 
30935dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
30945dbd56e3SPeter Brune @*/
3095be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
30965dbd56e3SPeter Brune {
30975dbd56e3SPeter Brune   PetscErrorCode      ierr;
3098be081cd6SPeter Brune   DMSubDomainHookLink link;
30995dbd56e3SPeter Brune 
31005dbd56e3SPeter Brune   PetscFunctionBegin;
3101be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
31028865f1eaSKarl Rupp     if (link->restricthook) {
31038865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
31048865f1eaSKarl Rupp     }
31055dbd56e3SPeter Brune   }
31065dbd56e3SPeter Brune   PetscFunctionReturn(0);
31075dbd56e3SPeter Brune }
31085dbd56e3SPeter Brune 
31095fe1f584SPeter Brune /*@
31106a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
31115fe1f584SPeter Brune 
31125fe1f584SPeter Brune     Not Collective
31135fe1f584SPeter Brune 
31145fe1f584SPeter Brune     Input Parameter:
31155fe1f584SPeter Brune .   dm - the DM object
31165fe1f584SPeter Brune 
31175fe1f584SPeter Brune     Output Parameter:
31186a7d9d85SPeter Brune .   level - number of coarsenings
31195fe1f584SPeter Brune 
31205fe1f584SPeter Brune     Level: developer
31215fe1f584SPeter Brune 
31226a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31235fe1f584SPeter Brune 
31245fe1f584SPeter Brune @*/
31255fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
31265fe1f584SPeter Brune {
31275fe1f584SPeter Brune   PetscFunctionBegin;
31285fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3129b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
31305fe1f584SPeter Brune   *level = dm->leveldown;
31315fe1f584SPeter Brune   PetscFunctionReturn(0);
31325fe1f584SPeter Brune }
31335fe1f584SPeter Brune 
31349a64c4a8SMatthew G. Knepley /*@
31359a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31369a64c4a8SMatthew G. Knepley 
31379a64c4a8SMatthew G. Knepley     Not Collective
31389a64c4a8SMatthew G. Knepley 
31399a64c4a8SMatthew G. Knepley     Input Parameters:
31409a64c4a8SMatthew G. Knepley +   dm - the DM object
31419a64c4a8SMatthew G. Knepley -   level - number of coarsenings
31429a64c4a8SMatthew G. Knepley 
31439a64c4a8SMatthew G. Knepley     Level: developer
31449a64c4a8SMatthew G. Knepley 
31459a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31469a64c4a8SMatthew G. Knepley @*/
31479a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
31489a64c4a8SMatthew G. Knepley {
31499a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
31509a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31519a64c4a8SMatthew G. Knepley   dm->leveldown = level;
31529a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
31539a64c4a8SMatthew G. Knepley }
31549a64c4a8SMatthew G. Knepley 
31555fe1f584SPeter Brune 
31565fe1f584SPeter Brune 
315747c6ae99SBarry Smith /*@C
315847c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
315947c6ae99SBarry Smith 
3160d083f849SBarry Smith     Collective on dm
316147c6ae99SBarry Smith 
316247c6ae99SBarry Smith     Input Parameter:
316347c6ae99SBarry Smith +   dm - the DM object
316447c6ae99SBarry Smith -   nlevels - the number of levels of refinement
316547c6ae99SBarry Smith 
316647c6ae99SBarry Smith     Output Parameter:
316747c6ae99SBarry Smith .   dmf - the refined DM hierarchy
316847c6ae99SBarry Smith 
316947c6ae99SBarry Smith     Level: developer
317047c6ae99SBarry Smith 
3171e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
317247c6ae99SBarry Smith 
317347c6ae99SBarry Smith @*/
31747087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
317547c6ae99SBarry Smith {
317647c6ae99SBarry Smith   PetscErrorCode ierr;
317747c6ae99SBarry Smith 
317847c6ae99SBarry Smith   PetscFunctionBegin;
3179171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3180ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
318147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3182b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
318347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
318447c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
318547c6ae99SBarry Smith   } else if (dm->ops->refine) {
318647c6ae99SBarry Smith     PetscInt i;
318747c6ae99SBarry Smith 
3188ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
318947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3190ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
319147c6ae99SBarry Smith     }
3192ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
319347c6ae99SBarry Smith   PetscFunctionReturn(0);
319447c6ae99SBarry Smith }
319547c6ae99SBarry Smith 
319647c6ae99SBarry Smith /*@C
319747c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
319847c6ae99SBarry Smith 
3199d083f849SBarry Smith     Collective on dm
320047c6ae99SBarry Smith 
320147c6ae99SBarry Smith     Input Parameter:
320247c6ae99SBarry Smith +   dm - the DM object
320347c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
320447c6ae99SBarry Smith 
320547c6ae99SBarry Smith     Output Parameter:
320647c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
320747c6ae99SBarry Smith 
320847c6ae99SBarry Smith     Level: developer
320947c6ae99SBarry Smith 
3210e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
321147c6ae99SBarry Smith 
321247c6ae99SBarry Smith @*/
32137087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
321447c6ae99SBarry Smith {
321547c6ae99SBarry Smith   PetscErrorCode ierr;
321647c6ae99SBarry Smith 
321747c6ae99SBarry Smith   PetscFunctionBegin;
3218171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3219ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
322047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
322147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
322247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
322347c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
322447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
322547c6ae99SBarry Smith     PetscInt i;
322647c6ae99SBarry Smith 
3227ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
322847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3229ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
323047c6ae99SBarry Smith     }
3231ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
323247c6ae99SBarry Smith   PetscFunctionReturn(0);
323347c6ae99SBarry Smith }
323447c6ae99SBarry Smith 
32351a266240SBarry Smith /*@C
32361a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32371a266240SBarry Smith 
32381a266240SBarry Smith     Not Collective
32391a266240SBarry Smith 
32401a266240SBarry Smith     Input Parameters:
32411a266240SBarry Smith +   dm - the DM object
32421a266240SBarry Smith -   destroy - the destroy function
32431a266240SBarry Smith 
32441a266240SBarry Smith     Level: intermediate
32451a266240SBarry Smith 
3246e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
32471a266240SBarry Smith 
3248f07f9ceaSJed Brown @*/
32491a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
32501a266240SBarry Smith {
32511a266240SBarry Smith   PetscFunctionBegin;
3252171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32531a266240SBarry Smith   dm->ctxdestroy = destroy;
32541a266240SBarry Smith   PetscFunctionReturn(0);
32551a266240SBarry Smith }
32561a266240SBarry Smith 
3257b07ff414SBarry Smith /*@
32581b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
325947c6ae99SBarry Smith 
326047c6ae99SBarry Smith     Not Collective
326147c6ae99SBarry Smith 
326247c6ae99SBarry Smith     Input Parameters:
326347c6ae99SBarry Smith +   dm - the DM object
326447c6ae99SBarry Smith -   ctx - the user context
326547c6ae99SBarry Smith 
326647c6ae99SBarry Smith     Level: intermediate
326747c6ae99SBarry Smith 
3268e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
326947c6ae99SBarry Smith 
327047c6ae99SBarry Smith @*/
32711b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
327247c6ae99SBarry Smith {
327347c6ae99SBarry Smith   PetscFunctionBegin;
3274171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
327547c6ae99SBarry Smith   dm->ctx = ctx;
327647c6ae99SBarry Smith   PetscFunctionReturn(0);
327747c6ae99SBarry Smith }
327847c6ae99SBarry Smith 
327947c6ae99SBarry Smith /*@
32801b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
328147c6ae99SBarry Smith 
328247c6ae99SBarry Smith     Not Collective
328347c6ae99SBarry Smith 
328447c6ae99SBarry Smith     Input Parameter:
328547c6ae99SBarry Smith .   dm - the DM object
328647c6ae99SBarry Smith 
328747c6ae99SBarry Smith     Output Parameter:
328847c6ae99SBarry Smith .   ctx - the user context
328947c6ae99SBarry Smith 
329047c6ae99SBarry Smith     Level: intermediate
329147c6ae99SBarry Smith 
3292e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
329347c6ae99SBarry Smith 
329447c6ae99SBarry Smith @*/
32951b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
329647c6ae99SBarry Smith {
329747c6ae99SBarry Smith   PetscFunctionBegin;
3298171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32991b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
330047c6ae99SBarry Smith   PetscFunctionReturn(0);
330147c6ae99SBarry Smith }
330247c6ae99SBarry Smith 
330308da532bSDmitry Karpeev /*@C
3304df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
330508da532bSDmitry Karpeev 
3306d083f849SBarry Smith     Logically Collective on dm
330708da532bSDmitry Karpeev 
330808da532bSDmitry Karpeev     Input Parameter:
330908da532bSDmitry Karpeev +   dm - the DM object
33100298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
331108da532bSDmitry Karpeev 
331208da532bSDmitry Karpeev     Level: intermediate
331308da532bSDmitry Karpeev 
3314835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
331508da532bSDmitry Karpeev          DMSetJacobian()
331608da532bSDmitry Karpeev 
331708da532bSDmitry Karpeev @*/
331808da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
331908da532bSDmitry Karpeev {
332008da532bSDmitry Karpeev   PetscFunctionBegin;
33215a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
332208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
332308da532bSDmitry Karpeev   PetscFunctionReturn(0);
332408da532bSDmitry Karpeev }
332508da532bSDmitry Karpeev 
332608da532bSDmitry Karpeev /*@
332708da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
332808da532bSDmitry Karpeev 
332908da532bSDmitry Karpeev     Not Collective
333008da532bSDmitry Karpeev 
333108da532bSDmitry Karpeev     Input Parameter:
333208da532bSDmitry Karpeev .   dm - the DM object to destroy
333308da532bSDmitry Karpeev 
333408da532bSDmitry Karpeev     Output Parameter:
333508da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
333608da532bSDmitry Karpeev 
333708da532bSDmitry Karpeev     Level: developer
333808da532bSDmitry Karpeev 
333974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
334008da532bSDmitry Karpeev 
334108da532bSDmitry Karpeev @*/
334208da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
334308da532bSDmitry Karpeev {
334408da532bSDmitry Karpeev   PetscFunctionBegin;
33455a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3346534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
334708da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
334808da532bSDmitry Karpeev   PetscFunctionReturn(0);
334908da532bSDmitry Karpeev }
335008da532bSDmitry Karpeev 
335108da532bSDmitry Karpeev /*@C
335208da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
335308da532bSDmitry Karpeev 
3354d083f849SBarry Smith     Logically Collective on dm
335508da532bSDmitry Karpeev 
335608da532bSDmitry Karpeev     Input Parameters:
3357907376e6SBarry Smith .   dm - the DM object
335808da532bSDmitry Karpeev 
335908da532bSDmitry Karpeev     Output parameters:
336008da532bSDmitry Karpeev +   xl - lower bound
336108da532bSDmitry Karpeev -   xu - upper bound
336208da532bSDmitry Karpeev 
3363907376e6SBarry Smith     Level: advanced
3364907376e6SBarry Smith 
336595452b02SPatrick Sanan     Notes:
336695452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
336708da532bSDmitry Karpeev 
336874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
336908da532bSDmitry Karpeev 
337008da532bSDmitry Karpeev @*/
337108da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
337208da532bSDmitry Karpeev {
337308da532bSDmitry Karpeev   PetscErrorCode ierr;
33745fd66863SKarl Rupp 
337508da532bSDmitry Karpeev   PetscFunctionBegin;
33765a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
337708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
33785a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3379b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
338008da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
338108da532bSDmitry Karpeev   PetscFunctionReturn(0);
338208da532bSDmitry Karpeev }
338308da532bSDmitry Karpeev 
3384b0ae01b7SPeter Brune /*@
3385b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3386b0ae01b7SPeter Brune 
3387b0ae01b7SPeter Brune     Not Collective
3388b0ae01b7SPeter Brune 
3389b0ae01b7SPeter Brune     Input Parameter:
3390b0ae01b7SPeter Brune .   dm - the DM object
3391b0ae01b7SPeter Brune 
3392b0ae01b7SPeter Brune     Output Parameter:
3393b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3394b0ae01b7SPeter Brune 
3395b0ae01b7SPeter Brune     Level: developer
3396b0ae01b7SPeter Brune 
33971565f0a7SPatrick Sanan .seealso DMCreateColoring()
3398b0ae01b7SPeter Brune 
3399b0ae01b7SPeter Brune @*/
3400b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3401b0ae01b7SPeter Brune {
3402b0ae01b7SPeter Brune   PetscFunctionBegin;
34035a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3404534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3405b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3406b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3407b0ae01b7SPeter Brune }
3408b0ae01b7SPeter Brune 
34093ad4599aSBarry Smith /*@
34103ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
34113ad4599aSBarry Smith 
34123ad4599aSBarry Smith     Not Collective
34133ad4599aSBarry Smith 
34143ad4599aSBarry Smith     Input Parameter:
34153ad4599aSBarry Smith .   dm - the DM object
34163ad4599aSBarry Smith 
34173ad4599aSBarry Smith     Output Parameter:
34183ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
34193ad4599aSBarry Smith 
34203ad4599aSBarry Smith     Level: developer
34213ad4599aSBarry Smith 
34221565f0a7SPatrick Sanan .seealso DMCreateRestriction()
34233ad4599aSBarry Smith 
34243ad4599aSBarry Smith @*/
34253ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
34263ad4599aSBarry Smith {
34273ad4599aSBarry Smith   PetscFunctionBegin;
34285a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3429534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34303ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34313ad4599aSBarry Smith   PetscFunctionReturn(0);
34323ad4599aSBarry Smith }
34333ad4599aSBarry Smith 
3434a7058e45SLawrence Mitchell 
3435a7058e45SLawrence Mitchell /*@
3436a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3437a7058e45SLawrence Mitchell 
3438a7058e45SLawrence Mitchell     Not Collective
3439a7058e45SLawrence Mitchell 
3440a7058e45SLawrence Mitchell     Input Parameter:
3441a7058e45SLawrence Mitchell .   dm - the DM object
3442a7058e45SLawrence Mitchell 
3443a7058e45SLawrence Mitchell     Output Parameter:
3444a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3445a7058e45SLawrence Mitchell 
3446a7058e45SLawrence Mitchell     Level: developer
3447a7058e45SLawrence Mitchell 
34481565f0a7SPatrick Sanan .seealso DMCreateInjection()
3449a7058e45SLawrence Mitchell 
3450a7058e45SLawrence Mitchell @*/
3451a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3452a7058e45SLawrence Mitchell {
34534a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
34545a84ad33SLisandro Dalcin 
3455a7058e45SLawrence Mitchell   PetscFunctionBegin;
34565a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3457534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34585a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
34594a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
34605a84ad33SLisandro Dalcin   } else {
34615a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
34625a84ad33SLisandro Dalcin   }
3463a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3464a7058e45SLawrence Mitchell }
3465a7058e45SLawrence Mitchell 
34665a84ad33SLisandro Dalcin 
3467748fac09SDmitry Karpeev /*@C
346808da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
346908da532bSDmitry Karpeev 
3470d083f849SBarry Smith     Collective on dm
347108da532bSDmitry Karpeev 
347208da532bSDmitry Karpeev     Input Parameter:
347308da532bSDmitry Karpeev +   dm - the DM object
34740298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
347508da532bSDmitry Karpeev 
347608da532bSDmitry Karpeev     Level: developer
347708da532bSDmitry Karpeev 
347874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
347908da532bSDmitry Karpeev 
348008da532bSDmitry Karpeev @*/
348108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
348208da532bSDmitry Karpeev {
348308da532bSDmitry Karpeev   PetscErrorCode ierr;
34845fd66863SKarl Rupp 
348508da532bSDmitry Karpeev   PetscFunctionBegin;
3486b9d85ea2SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
348708da532bSDmitry Karpeev   if (x) {
348808da532bSDmitry Karpeev     if (!dm->x) {
348908da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
349008da532bSDmitry Karpeev     }
349108da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
34928865f1eaSKarl Rupp   } else if (dm->x) {
349308da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
349408da532bSDmitry Karpeev   }
349508da532bSDmitry Karpeev   PetscFunctionReturn(0);
349608da532bSDmitry Karpeev }
349708da532bSDmitry Karpeev 
34980298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3499264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3500264ace61SBarry Smith 
3501264ace61SBarry Smith /*@C
3502264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3503264ace61SBarry Smith 
3504d083f849SBarry Smith   Collective on dm
3505264ace61SBarry Smith 
3506264ace61SBarry Smith   Input Parameters:
3507264ace61SBarry Smith + dm     - The DM object
3508264ace61SBarry Smith - method - The name of the DM type
3509264ace61SBarry Smith 
3510264ace61SBarry Smith   Options Database Key:
3511264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3512264ace61SBarry Smith 
3513264ace61SBarry Smith   Notes:
3514e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3515264ace61SBarry Smith 
3516264ace61SBarry Smith   Level: intermediate
3517264ace61SBarry Smith 
3518264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3519264ace61SBarry Smith @*/
352019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3521264ace61SBarry Smith {
3522264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3523264ace61SBarry Smith   PetscBool      match;
3524264ace61SBarry Smith   PetscErrorCode ierr;
3525264ace61SBarry Smith 
3526264ace61SBarry Smith   PetscFunctionBegin;
3527264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3528251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3529264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3530264ace61SBarry Smith 
35310f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35321c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3533ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3534264ace61SBarry Smith 
3535264ace61SBarry Smith   if (dm->ops->destroy) {
3536264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3537264ace61SBarry Smith   }
3538d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3539264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3540d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3541264ace61SBarry Smith   PetscFunctionReturn(0);
3542264ace61SBarry Smith }
3543264ace61SBarry Smith 
3544264ace61SBarry Smith /*@C
3545264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3546264ace61SBarry Smith 
3547264ace61SBarry Smith   Not Collective
3548264ace61SBarry Smith 
3549264ace61SBarry Smith   Input Parameter:
3550264ace61SBarry Smith . dm  - The DM
3551264ace61SBarry Smith 
3552264ace61SBarry Smith   Output Parameter:
3553264ace61SBarry Smith . type - The DM type name
3554264ace61SBarry Smith 
3555264ace61SBarry Smith   Level: intermediate
3556264ace61SBarry Smith 
3557264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3558264ace61SBarry Smith @*/
355919fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3560264ace61SBarry Smith {
3561264ace61SBarry Smith   PetscErrorCode ierr;
3562264ace61SBarry Smith 
3563264ace61SBarry Smith   PetscFunctionBegin;
3564264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3565c959eef4SJed Brown   PetscValidPointer(type,2);
3566607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3567264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3568264ace61SBarry Smith   PetscFunctionReturn(0);
3569264ace61SBarry Smith }
3570264ace61SBarry Smith 
357167a56275SMatthew G Knepley /*@C
357267a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
357367a56275SMatthew G Knepley 
3574d083f849SBarry Smith   Collective on dm
357567a56275SMatthew G Knepley 
357667a56275SMatthew G Knepley   Input Parameters:
357767a56275SMatthew G Knepley + dm - the DM
357867a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
357967a56275SMatthew G Knepley 
358067a56275SMatthew G Knepley   Output Parameter:
358167a56275SMatthew G Knepley . M - pointer to new DM
358267a56275SMatthew G Knepley 
358367a56275SMatthew G Knepley   Notes:
358467a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
358567a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
358667a56275SMatthew G Knepley   of the input DM.
358767a56275SMatthew G Knepley 
358867a56275SMatthew G Knepley   Level: intermediate
358967a56275SMatthew G Knepley 
359067a56275SMatthew G Knepley .seealso: DMCreate()
359167a56275SMatthew G Knepley @*/
359219fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
359367a56275SMatthew G Knepley {
359467a56275SMatthew G Knepley   DM             B;
359567a56275SMatthew G Knepley   char           convname[256];
3596c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
359767a56275SMatthew G Knepley   PetscErrorCode ierr;
359867a56275SMatthew G Knepley 
359967a56275SMatthew G Knepley   PetscFunctionBegin;
360067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
360167a56275SMatthew G Knepley   PetscValidType(dm,1);
360267a56275SMatthew G Knepley   PetscValidPointer(M,3);
3603251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3604c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3605c067b6caSMatthew G. Knepley   if (sametype) {
3606c067b6caSMatthew G. Knepley     *M   = dm;
3607c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3608c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3609c067b6caSMatthew G. Knepley   } else {
36100298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
361167a56275SMatthew G Knepley 
361267a56275SMatthew G Knepley     /*
361367a56275SMatthew G Knepley        Order of precedence:
361467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
361567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
361667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
361767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
361867a56275SMatthew G Knepley        5) Use a really basic converter.
361967a56275SMatthew G Knepley     */
362067a56275SMatthew G Knepley 
362167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3622a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3623a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3624a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3625a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3626a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36270005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
362867a56275SMatthew G Knepley     if (conv) goto foundconv;
362967a56275SMatthew G Knepley 
363067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
363182f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
363267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3633a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3634a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3635a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3636a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3637a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36380005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
363967a56275SMatthew G Knepley     if (conv) {
3640fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
364167a56275SMatthew G Knepley       goto foundconv;
364267a56275SMatthew G Knepley     }
364367a56275SMatthew G Knepley 
364467a56275SMatthew G Knepley #if 0
364567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
364667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3647fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
364867a56275SMatthew G Knepley     if (conv) goto foundconv;
364967a56275SMatthew G Knepley 
365067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
365167a56275SMatthew G Knepley     if (dm->ops->convert) {
365267a56275SMatthew G Knepley       conv = dm->ops->convert;
365367a56275SMatthew G Knepley     }
365467a56275SMatthew G Knepley     if (conv) goto foundconv;
365567a56275SMatthew G Knepley #endif
365667a56275SMatthew G Knepley 
365767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
365882f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
365967a56275SMatthew G Knepley 
366067a56275SMatthew G Knepley foundconv:
366167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
366267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
366312fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
366490b157c4SStefano Zampini     {
366590b157c4SStefano Zampini       PetscBool             isper;
366612fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
366712fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
366890b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
366990b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
367012fa691eSMatthew G. Knepley     }
367167a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
367267a56275SMatthew G Knepley   }
367367a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
367467a56275SMatthew G Knepley   PetscFunctionReturn(0);
367567a56275SMatthew G Knepley }
3676264ace61SBarry Smith 
3677264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3678264ace61SBarry Smith 
3679264ace61SBarry Smith /*@C
36801c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
36811c84c290SBarry Smith 
36821c84c290SBarry Smith   Not Collective
36831c84c290SBarry Smith 
36841c84c290SBarry Smith   Input Parameters:
36851c84c290SBarry Smith + name        - The name of a new user-defined creation routine
36861c84c290SBarry Smith - create_func - The creation routine itself
36871c84c290SBarry Smith 
36881c84c290SBarry Smith   Notes:
36891c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
36901c84c290SBarry Smith 
36911c84c290SBarry Smith 
36921c84c290SBarry Smith   Sample usage:
36931c84c290SBarry Smith .vb
3694bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
36951c84c290SBarry Smith .ve
36961c84c290SBarry Smith 
36971c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
36981c84c290SBarry Smith .vb
36991c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
37001c84c290SBarry Smith     DMSetType(DM,"my_da");
37011c84c290SBarry Smith .ve
37021c84c290SBarry Smith    or at runtime via the option
37031c84c290SBarry Smith .vb
37041c84c290SBarry Smith     -da_type my_da
37051c84c290SBarry Smith .ve
3706264ace61SBarry Smith 
3707264ace61SBarry Smith   Level: advanced
37081c84c290SBarry Smith 
3709bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
37101c84c290SBarry Smith 
3711264ace61SBarry Smith @*/
3712bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3713264ace61SBarry Smith {
3714264ace61SBarry Smith   PetscErrorCode ierr;
3715264ace61SBarry Smith 
3716264ace61SBarry Smith   PetscFunctionBegin;
37171d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3718a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3719264ace61SBarry Smith   PetscFunctionReturn(0);
3720264ace61SBarry Smith }
3721264ace61SBarry Smith 
3722b859378eSBarry Smith /*@C
372355849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3724b859378eSBarry Smith 
3725d083f849SBarry Smith   Collective on viewer
3726b859378eSBarry Smith 
3727b859378eSBarry Smith   Input Parameters:
3728b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3729b859378eSBarry Smith            some related function before a call to DMLoad().
3730b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3731b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3732b859378eSBarry Smith 
3733b859378eSBarry Smith    Level: intermediate
3734b859378eSBarry Smith 
3735b859378eSBarry Smith   Notes:
373655849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3737b859378eSBarry Smith 
3738b859378eSBarry Smith   Notes for advanced users:
3739b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3740b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3741b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3742b859378eSBarry Smith   format is
3743b859378eSBarry Smith .vb
3744b859378eSBarry Smith      has not yet been determined
3745b859378eSBarry Smith .ve
3746b859378eSBarry Smith 
3747b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3748b859378eSBarry Smith @*/
3749b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3750b859378eSBarry Smith {
37519331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3752b859378eSBarry Smith   PetscErrorCode ierr;
3753b859378eSBarry Smith 
3754b859378eSBarry Smith   PetscFunctionBegin;
3755b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3756b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3757fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
375832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37599331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
376058cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
37619331c7a4SMatthew G. Knepley   if (isbinary) {
37629331c7a4SMatthew G. Knepley     PetscInt classid;
37639331c7a4SMatthew G. Knepley     char     type[256];
3764b859378eSBarry Smith 
3765060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37669200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3767060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
376832c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37699331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37709331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37719331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37729331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
377358cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3774b859378eSBarry Smith   PetscFunctionReturn(0);
3775b859378eSBarry Smith }
3776b859378eSBarry Smith 
3777b2e4378dSMatthew G. Knepley /*@
3778b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3779b2e4378dSMatthew G. Knepley 
3780b2e4378dSMatthew G. Knepley   Not collective
3781b2e4378dSMatthew G. Knepley 
3782b2e4378dSMatthew G. Knepley   Input Parameter:
3783b2e4378dSMatthew G. Knepley . dm - the DM
3784b2e4378dSMatthew G. Knepley 
3785b2e4378dSMatthew G. Knepley   Output Parameters:
3786b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3787b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3788b2e4378dSMatthew G. Knepley 
3789b2e4378dSMatthew G. Knepley   Level: beginner
3790b2e4378dSMatthew G. Knepley 
3791b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3792b2e4378dSMatthew G. Knepley 
3793b2e4378dSMatthew G. Knepley 
3794b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3795b2e4378dSMatthew G. Knepley @*/
3796b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3797b2e4378dSMatthew G. Knepley {
3798b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3799b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3800b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3801b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3802b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3803b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3804b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3805b2e4378dSMatthew G. Knepley 
3806b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3807b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3808b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3809b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3810b2e4378dSMatthew G. Knepley   if (coords) {
3811b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3812b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3813b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3814b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3815b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3816b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3817b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3818b2e4378dSMatthew G. Knepley       }
3819b2e4378dSMatthew G. Knepley     }
3820b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3821b2e4378dSMatthew G. Knepley   } else {
3822b2e4378dSMatthew G. Knepley     PetscBool isda;
3823b2e4378dSMatthew G. Knepley 
3824b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3825b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3826b2e4378dSMatthew G. Knepley   }
3827b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3828b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3829b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3830b2e4378dSMatthew G. Knepley }
3831b2e4378dSMatthew G. Knepley 
3832b2e4378dSMatthew G. Knepley /*@
3833b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3834b2e4378dSMatthew G. Knepley 
3835b2e4378dSMatthew G. Knepley   Collective
3836b2e4378dSMatthew G. Knepley 
3837b2e4378dSMatthew G. Knepley   Input Parameter:
3838b2e4378dSMatthew G. Knepley . dm - the DM
3839b2e4378dSMatthew G. Knepley 
3840b2e4378dSMatthew G. Knepley   Output Parameters:
3841b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3842b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3843b2e4378dSMatthew G. Knepley 
3844b2e4378dSMatthew G. Knepley   Level: beginner
3845b2e4378dSMatthew G. Knepley 
3846b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3847b2e4378dSMatthew G. Knepley @*/
3848b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3849b2e4378dSMatthew G. Knepley {
3850b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
38516ce308c4SMatthew G. Knepley   PetscInt       cdim;
38526ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3853b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3854b2e4378dSMatthew G. Knepley 
3855b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3856b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3857b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3858b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3859b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3860b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3861b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3862b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3863b2e4378dSMatthew G. Knepley }
3864b2e4378dSMatthew G. Knepley 
38657da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38667da65231SMatthew G Knepley 
3867a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3868a6dfd86eSKarl Rupp {
38691d47ebbbSSatish Balay   PetscInt       f;
38701b30c384SMatthew G Knepley   PetscErrorCode ierr;
38711b30c384SMatthew G Knepley 
38727da65231SMatthew G Knepley   PetscFunctionBegin;
387374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38741d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
387557622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
38767da65231SMatthew G Knepley   }
38777da65231SMatthew G Knepley   PetscFunctionReturn(0);
38787da65231SMatthew G Knepley }
38797da65231SMatthew G Knepley 
3880a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3881a6dfd86eSKarl Rupp {
38821b30c384SMatthew G Knepley   PetscInt       f, g;
38837da65231SMatthew G Knepley   PetscErrorCode ierr;
38847da65231SMatthew G Knepley 
38857da65231SMatthew G Knepley   PetscFunctionBegin;
388674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38871d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
388874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
38891d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3890e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
38917da65231SMatthew G Knepley     }
389274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
38937da65231SMatthew G Knepley   }
38947da65231SMatthew G Knepley   PetscFunctionReturn(0);
38957da65231SMatthew G Knepley }
3896e7c4fc90SDmitry Karpeev 
38976113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3898e759306cSMatthew G. Knepley {
38990c5b8624SToby Isaac   PetscInt          localSize, bs;
39000c5b8624SToby Isaac   PetscMPIInt       size;
39010c5b8624SToby Isaac   Vec               x, xglob;
39020c5b8624SToby Isaac   const PetscScalar *xarray;
3903e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3904e759306cSMatthew G. Knepley 
3905e759306cSMatthew G. Knepley   PetscFunctionBegin;
39069852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3907e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3908e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
39096113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
39100c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
39110c5b8624SToby Isaac   if (size > 1) {
39120c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
39130c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
39140c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
39150c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
39160c5b8624SToby Isaac   } else {
39170c5b8624SToby Isaac     xglob = x;
39180c5b8624SToby Isaac   }
39190c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
39200c5b8624SToby Isaac   if (size > 1) {
39210c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
39220c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
39230c5b8624SToby Isaac   }
3924e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3925e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3926e759306cSMatthew G. Knepley }
3927e759306cSMatthew G. Knepley 
392888ed4aceSMatthew G Knepley /*@
39291bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3930061576a5SJed Brown 
3931061576a5SJed Brown   Input Parameter:
3932061576a5SJed Brown . dm - The DM
3933061576a5SJed Brown 
3934061576a5SJed Brown   Output Parameter:
3935061576a5SJed Brown . section - The PetscSection
3936061576a5SJed Brown 
3937061576a5SJed Brown   Options Database Keys:
3938061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3939061576a5SJed Brown 
3940061576a5SJed Brown   Level: advanced
3941061576a5SJed Brown 
3942061576a5SJed Brown   Notes:
3943061576a5SJed Brown   Use DMGetLocalSection() in new code.
3944061576a5SJed Brown 
3945061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3946061576a5SJed Brown 
3947061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3948061576a5SJed Brown @*/
3949061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3950061576a5SJed Brown {
3951061576a5SJed Brown   PetscErrorCode ierr;
3952061576a5SJed Brown 
3953061576a5SJed Brown   PetscFunctionBegin;
3954061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3955061576a5SJed Brown   PetscFunctionReturn(0);
3956061576a5SJed Brown }
3957061576a5SJed Brown 
3958061576a5SJed Brown /*@
3959061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
396088ed4aceSMatthew G Knepley 
396188ed4aceSMatthew G Knepley   Input Parameter:
396288ed4aceSMatthew G Knepley . dm - The DM
396388ed4aceSMatthew G Knepley 
396488ed4aceSMatthew G Knepley   Output Parameter:
396588ed4aceSMatthew G Knepley . section - The PetscSection
396688ed4aceSMatthew G Knepley 
3967e5893cccSMatthew G. Knepley   Options Database Keys:
3968e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3969e5893cccSMatthew G. Knepley 
397088ed4aceSMatthew G Knepley   Level: intermediate
397188ed4aceSMatthew G Knepley 
397288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
397388ed4aceSMatthew G Knepley 
3974061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
397588ed4aceSMatthew G Knepley @*/
3976061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
39770adebc6cSBarry Smith {
3978fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3979fd59a867SMatthew G. Knepley 
398088ed4aceSMatthew G Knepley   PetscFunctionBegin;
398188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
398288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
39831bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
3984e5e52638SMatthew G. Knepley     PetscInt d;
3985e5e52638SMatthew G. Knepley 
3986e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
39871bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
39881bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
39892f0f8703SMatthew G. Knepley   }
39901bb6d2a8SBarry Smith   *section = dm->localSection;
399188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
399288ed4aceSMatthew G Knepley }
399388ed4aceSMatthew G Knepley 
399488ed4aceSMatthew G Knepley /*@
39951bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
3996061576a5SJed Brown 
3997061576a5SJed Brown   Input Parameters:
3998061576a5SJed Brown + dm - The DM
3999061576a5SJed Brown - section - The PetscSection
4000061576a5SJed Brown 
4001061576a5SJed Brown   Level: advanced
4002061576a5SJed Brown 
4003061576a5SJed Brown   Notes:
4004061576a5SJed Brown   Use DMSetLocalSection() in new code.
4005061576a5SJed Brown 
4006061576a5SJed Brown   Any existing Section will be destroyed
4007061576a5SJed Brown 
4008061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4009061576a5SJed Brown @*/
4010061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4011061576a5SJed Brown {
4012061576a5SJed Brown   PetscErrorCode ierr;
4013061576a5SJed Brown 
4014061576a5SJed Brown   PetscFunctionBegin;
4015061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4016061576a5SJed Brown   PetscFunctionReturn(0);
4017061576a5SJed Brown }
4018061576a5SJed Brown 
4019061576a5SJed Brown /*@
4020061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
402188ed4aceSMatthew G Knepley 
402288ed4aceSMatthew G Knepley   Input Parameters:
402388ed4aceSMatthew G Knepley + dm - The DM
402488ed4aceSMatthew G Knepley - section - The PetscSection
402588ed4aceSMatthew G Knepley 
402688ed4aceSMatthew G Knepley   Level: intermediate
402788ed4aceSMatthew G Knepley 
402888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
402988ed4aceSMatthew G Knepley 
4030061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
403188ed4aceSMatthew G Knepley @*/
4032061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
40330adebc6cSBarry Smith {
4034c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4035af122d2aSMatthew G Knepley   PetscInt       f;
403688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
403788ed4aceSMatthew G Knepley 
403888ed4aceSMatthew G Knepley   PetscFunctionBegin;
403988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4040b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40411d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40421bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
40431bb6d2a8SBarry Smith   dm->localSection = section;
40441bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4045af122d2aSMatthew G Knepley   if (numFields) {
4046af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4047af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40480f21e855SMatthew G. Knepley       PetscObject disc;
4049af122d2aSMatthew G Knepley       const char *name;
4050af122d2aSMatthew G Knepley 
40511bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
405244a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40530f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4054af122d2aSMatthew G Knepley     }
4055af122d2aSMatthew G Knepley   }
4056e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40571bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
405888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
405988ed4aceSMatthew G Knepley }
406088ed4aceSMatthew G Knepley 
40619435951eSToby Isaac /*@
4062b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40639435951eSToby Isaac 
4064e228b242SToby Isaac   not collective
4065e228b242SToby Isaac 
40669435951eSToby Isaac   Input Parameter:
40679435951eSToby Isaac . dm - The DM
40689435951eSToby Isaac 
40699435951eSToby Isaac   Output Parameter:
40709435951eSToby 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.
40719435951eSToby 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.
40729435951eSToby Isaac 
40739435951eSToby Isaac   Level: advanced
40749435951eSToby Isaac 
40759435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
40769435951eSToby Isaac 
40779435951eSToby Isaac .seealso: DMSetDefaultConstraints()
40789435951eSToby Isaac @*/
40799435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
40809435951eSToby Isaac {
40819435951eSToby Isaac   PetscErrorCode ierr;
40829435951eSToby Isaac 
40839435951eSToby Isaac   PetscFunctionBegin;
40849435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40859435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
408645a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
408745a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
40889435951eSToby Isaac   PetscFunctionReturn(0);
40899435951eSToby Isaac }
40909435951eSToby Isaac 
40919435951eSToby Isaac /*@
4092b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
40939435951eSToby Isaac 
40949435951eSToby 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().
40959435951eSToby Isaac 
40969435951eSToby 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.
40979435951eSToby Isaac 
4098e228b242SToby Isaac   collective on dm
4099e228b242SToby Isaac 
41009435951eSToby Isaac   Input Parameters:
41019435951eSToby Isaac + dm - The DM
4102e228b242SToby 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).
4103e228b242SToby 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).
41049435951eSToby Isaac 
41059435951eSToby Isaac   Level: advanced
41069435951eSToby Isaac 
41079435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
41089435951eSToby Isaac 
41099435951eSToby Isaac .seealso: DMGetDefaultConstraints()
41109435951eSToby Isaac @*/
41119435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
41129435951eSToby Isaac {
4113e228b242SToby Isaac   PetscMPIInt result;
41149435951eSToby Isaac   PetscErrorCode ierr;
41159435951eSToby Isaac 
41169435951eSToby Isaac   PetscFunctionBegin;
41179435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4118e228b242SToby Isaac   if (section) {
4119e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4120e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4121f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4122e228b242SToby Isaac   }
4123e228b242SToby Isaac   if (mat) {
4124e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4125e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4126f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4127e228b242SToby Isaac   }
41289435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41299435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
41309435951eSToby Isaac   dm->defaultConstraintSection = section;
41319435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
41329435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
41339435951eSToby Isaac   dm->defaultConstraintMat = mat;
41349435951eSToby Isaac   PetscFunctionReturn(0);
41359435951eSToby Isaac }
41369435951eSToby Isaac 
4137497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4138507e4973SMatthew G. Knepley /*
4139507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4140507e4973SMatthew G. Knepley 
4141507e4973SMatthew G. Knepley   Input Parameters:
4142507e4973SMatthew G. Knepley + dm - The DM
4143507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4144507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4145507e4973SMatthew G. Knepley 
4146507e4973SMatthew G. Knepley   Level: intermediate
4147507e4973SMatthew G. Knepley 
41481bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4149507e4973SMatthew G. Knepley */
4150f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4151507e4973SMatthew G. Knepley {
4152507e4973SMatthew G. Knepley   MPI_Comm        comm;
4153507e4973SMatthew G. Knepley   PetscLayout     layout;
4154507e4973SMatthew G. Knepley   const PetscInt *ranges;
4155507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4156507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4157507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4158507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4159507e4973SMatthew G. Knepley 
4160507e4973SMatthew G. Knepley   PetscFunctionBegin;
4161507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4162507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4163507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4164507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4165507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4166507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4167507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4168507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4169507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4170507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4171507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4172507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4173f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4174507e4973SMatthew G. Knepley 
4175507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4176507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4177507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4178507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4179507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4180507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4181507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4182507e4973SMatthew 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;}
4183507e4973SMatthew 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;}
4184507e4973SMatthew G. Knepley     if (gdof < 0) {
4185507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4186507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4187507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4188507e4973SMatthew G. Knepley 
4189507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4190507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4191507e4973SMatthew 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;}
4192507e4973SMatthew G. Knepley       }
4193507e4973SMatthew G. Knepley     }
4194507e4973SMatthew G. Knepley   }
4195507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4196507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4197b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4198507e4973SMatthew G. Knepley   if (!gvalid) {
4199507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4200507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4201507e4973SMatthew G. Knepley   }
4202507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4203507e4973SMatthew G. Knepley }
4204f741bcd2SMatthew G. Knepley #endif
4205507e4973SMatthew G. Knepley 
420688ed4aceSMatthew G Knepley /*@
4207e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
420888ed4aceSMatthew G Knepley 
4209d083f849SBarry Smith   Collective on dm
42108b1ab98fSJed Brown 
421188ed4aceSMatthew G Knepley   Input Parameter:
421288ed4aceSMatthew G Knepley . dm - The DM
421388ed4aceSMatthew G Knepley 
421488ed4aceSMatthew G Knepley   Output Parameter:
421588ed4aceSMatthew G Knepley . section - The PetscSection
421688ed4aceSMatthew G Knepley 
421788ed4aceSMatthew G Knepley   Level: intermediate
421888ed4aceSMatthew G Knepley 
421988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
422088ed4aceSMatthew G Knepley 
422192fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
422288ed4aceSMatthew G Knepley @*/
4223e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
42240adebc6cSBarry Smith {
422588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
422688ed4aceSMatthew G Knepley 
422788ed4aceSMatthew G Knepley   PetscFunctionBegin;
422888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
422988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42301bb6d2a8SBarry Smith   if (!dm->globalSection) {
4231fd59a867SMatthew G. Knepley     PetscSection s;
4232fd59a867SMatthew G. Knepley 
423392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4234fd59a867SMatthew 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");
423533907cc2SStefano 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");
42361bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4237cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
42381bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
42391bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
424088ed4aceSMatthew G Knepley   }
42411bb6d2a8SBarry Smith   *section = dm->globalSection;
424288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
424388ed4aceSMatthew G Knepley }
424488ed4aceSMatthew G Knepley 
4245b21d0597SMatthew G Knepley /*@
4246e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4247b21d0597SMatthew G Knepley 
4248b21d0597SMatthew G Knepley   Input Parameters:
4249b21d0597SMatthew G Knepley + dm - The DM
42505080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4251b21d0597SMatthew G Knepley 
4252b21d0597SMatthew G Knepley   Level: intermediate
4253b21d0597SMatthew G Knepley 
4254b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4255b21d0597SMatthew G Knepley 
425692fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4257b21d0597SMatthew G Knepley @*/
4258e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42590adebc6cSBarry Smith {
4260b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4261b21d0597SMatthew G Knepley 
4262b21d0597SMatthew G Knepley   PetscFunctionBegin;
4263b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42645080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42651d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42661bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
42671bb6d2a8SBarry Smith   dm->globalSection = section;
4268497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
42691bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4270507e4973SMatthew G. Knepley #endif
4271b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4272b21d0597SMatthew G Knepley }
4273b21d0597SMatthew G Knepley 
427488ed4aceSMatthew G Knepley /*@
42751bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
427688ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
427788ed4aceSMatthew G Knepley 
427888ed4aceSMatthew G Knepley   Input Parameter:
427988ed4aceSMatthew G Knepley . dm - The DM
428088ed4aceSMatthew G Knepley 
428188ed4aceSMatthew G Knepley   Output Parameter:
428288ed4aceSMatthew G Knepley . sf - The PetscSF
428388ed4aceSMatthew G Knepley 
428488ed4aceSMatthew G Knepley   Level: intermediate
428588ed4aceSMatthew G Knepley 
428688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
428788ed4aceSMatthew G Knepley 
42881bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
428988ed4aceSMatthew G Knepley @*/
42901bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
42910adebc6cSBarry Smith {
429288ed4aceSMatthew G Knepley   PetscInt       nroots;
429388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
429488ed4aceSMatthew G Knepley 
429588ed4aceSMatthew G Knepley   PetscFunctionBegin;
429688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
429788ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
42981bb6d2a8SBarry Smith   if (!dm->sectionSF) {
42991bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
430033907cc2SStefano Zampini   }
43011bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
430288ed4aceSMatthew G Knepley   if (nroots < 0) {
430388ed4aceSMatthew G Knepley     PetscSection section, gSection;
430488ed4aceSMatthew G Knepley 
430592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
430631ea6d37SMatthew G Knepley     if (section) {
4307e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
43081bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
430931ea6d37SMatthew G Knepley     } else {
43100298fd71SBarry Smith       *sf = NULL;
431131ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
431231ea6d37SMatthew G Knepley     }
431388ed4aceSMatthew G Knepley   }
43141bb6d2a8SBarry Smith   *sf = dm->sectionSF;
431588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
431688ed4aceSMatthew G Knepley }
431788ed4aceSMatthew G Knepley 
431888ed4aceSMatthew G Knepley /*@
43191bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
432088ed4aceSMatthew G Knepley 
432188ed4aceSMatthew G Knepley   Input Parameters:
432288ed4aceSMatthew G Knepley + dm - The DM
432388ed4aceSMatthew G Knepley - sf - The PetscSF
432488ed4aceSMatthew G Knepley 
432588ed4aceSMatthew G Knepley   Level: intermediate
432688ed4aceSMatthew G Knepley 
432788ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
432888ed4aceSMatthew G Knepley 
43291bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
433088ed4aceSMatthew G Knepley @*/
43311bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
43320adebc6cSBarry Smith {
433388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
433488ed4aceSMatthew G Knepley 
433588ed4aceSMatthew G Knepley   PetscFunctionBegin;
433688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4337b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
433833907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
43391bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
43401bb6d2a8SBarry Smith   dm->sectionSF = sf;
434188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
434288ed4aceSMatthew G Knepley }
434388ed4aceSMatthew G Knepley 
434488ed4aceSMatthew G Knepley /*@C
43451bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
434688ed4aceSMatthew G Knepley   describing the data layout.
434788ed4aceSMatthew G Knepley 
434888ed4aceSMatthew G Knepley   Input Parameters:
434988ed4aceSMatthew G Knepley + dm - The DM
435088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
435188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
435288ed4aceSMatthew G Knepley 
43531bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
435488ed4aceSMatthew G Knepley 
43551bb6d2a8SBarry Smith   Level: developer
43561bb6d2a8SBarry Smith 
43571bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43581bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43591bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43601bb6d2a8SBarry Smith 
43611bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
436288ed4aceSMatthew G Knepley @*/
43631bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
436488ed4aceSMatthew G Knepley {
436582f516ccSBarry Smith   MPI_Comm       comm;
436688ed4aceSMatthew G Knepley   PetscLayout    layout;
436788ed4aceSMatthew G Knepley   const PetscInt *ranges;
436888ed4aceSMatthew G Knepley   PetscInt       *local;
436988ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4370ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
437188ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
437288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
437388ed4aceSMatthew G Knepley 
437488ed4aceSMatthew G Knepley   PetscFunctionBegin;
437588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4376367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
437788ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
437888ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
437988ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
438088ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
438188ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
438288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
438388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
438488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
438588ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4386ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
43876636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
438888ed4aceSMatthew G Knepley 
43896636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
43906636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4391235fbf56SMatthew G. Knepley     if (gcdof > (gdof < 0 ? -(gdof+1) : gdof)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d has %d constraints > %d dof", p, gcdof, (gdof < 0 ? -(gdof+1) : gdof));
43926636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
439388ed4aceSMatthew G Knepley   }
4394785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4395785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
439688ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
43971f588964SMatthew G Knepley     const PetscInt *cind;
43986636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
439988ed4aceSMatthew G Knepley 
440088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
440188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
440288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
440388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
440488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44056636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
440688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
44076636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
44086636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
44096636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4410057b4bcdSMatthew G Knepley       if (gsize != dof) SETERRQ4(comm, PETSC_ERR_ARG_WRONG, "Global dof %d for point %d is neither the constrained size %d, nor the unconstrained %d", gsize, p, dof-cdof, dof);
44116636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
44126636e97aSMatthew G Knepley     }
441388ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
441488ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
441588ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
441688ed4aceSMatthew G Knepley     }
441788ed4aceSMatthew G Knepley     if (gdof < 0) {
44186636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
441988ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
442088ed4aceSMatthew G Knepley 
442105376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
442231d3f06eSJed Brown         if (r < 0) r = -(r+2);
442305376888SMatthew G. Knepley         if ((r < 0) || (r >= size)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d mapped to invalid process %d (%d, %d)", p, r, gdof, goff);
442488ed4aceSMatthew G Knepley         remote[l].rank  = r;
442588ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
442688ed4aceSMatthew G Knepley       }
442788ed4aceSMatthew G Knepley     } else {
44286636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
442988ed4aceSMatthew G Knepley         remote[l].rank  = rank;
443088ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
443188ed4aceSMatthew G Knepley       }
443288ed4aceSMatthew G Knepley     }
443388ed4aceSMatthew G Knepley   }
44346636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
443588ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
44361bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
443788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
443888ed4aceSMatthew G Knepley }
4439af122d2aSMatthew G Knepley 
4440b21d0597SMatthew G Knepley /*@
4441b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4442b21d0597SMatthew G Knepley 
4443b21d0597SMatthew G Knepley   Input Parameter:
4444b21d0597SMatthew G Knepley . dm - The DM
4445b21d0597SMatthew G Knepley 
4446b21d0597SMatthew G Knepley   Output Parameter:
4447b21d0597SMatthew G Knepley . sf - The PetscSF
4448b21d0597SMatthew G Knepley 
4449b21d0597SMatthew G Knepley   Level: intermediate
4450b21d0597SMatthew G Knepley 
4451b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4452b21d0597SMatthew G Knepley 
44531bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4454b21d0597SMatthew G Knepley @*/
44550adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44560adebc6cSBarry Smith {
4457b21d0597SMatthew G Knepley   PetscFunctionBegin;
4458b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4459b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4460b21d0597SMatthew G Knepley   *sf = dm->sf;
4461b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4462b21d0597SMatthew G Knepley }
4463b21d0597SMatthew G Knepley 
4464057b4bcdSMatthew G Knepley /*@
4465057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4466057b4bcdSMatthew G Knepley 
4467057b4bcdSMatthew G Knepley   Input Parameters:
4468057b4bcdSMatthew G Knepley + dm - The DM
4469057b4bcdSMatthew G Knepley - sf - The PetscSF
4470057b4bcdSMatthew G Knepley 
4471057b4bcdSMatthew G Knepley   Level: intermediate
4472057b4bcdSMatthew G Knepley 
44731bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4474057b4bcdSMatthew G Knepley @*/
44750adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
44760adebc6cSBarry Smith {
4477057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4478057b4bcdSMatthew G Knepley 
4479057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4480057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4481b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4482057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
448333907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4484057b4bcdSMatthew G Knepley   dm->sf = sf;
4485057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4486057b4bcdSMatthew G Knepley }
4487057b4bcdSMatthew G Knepley 
448834aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
448934aa8a36SMatthew G. Knepley {
449034aa8a36SMatthew G. Knepley   PetscClassId   id;
449134aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
449234aa8a36SMatthew G. Knepley 
449334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
449434aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
449534aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
449634aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
449734aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
449834aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
449917c1d62eSMatthew G. Knepley   } else {
450017c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
450134aa8a36SMatthew G. Knepley   }
450234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
450334aa8a36SMatthew G. Knepley }
450434aa8a36SMatthew G. Knepley 
450544a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
450644a7f3ddSMatthew G. Knepley {
450744a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
450844a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
450944a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
451044a7f3ddSMatthew G. Knepley 
451144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
451244a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
451344a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
451444a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
451544a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
451644a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
451744a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
451844a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
451944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
452044a7f3ddSMatthew G. Knepley }
452144a7f3ddSMatthew G. Knepley 
452244a7f3ddSMatthew G. Knepley /*@
452344a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
452444a7f3ddSMatthew G. Knepley 
4525d083f849SBarry Smith   Logically collective on dm
452644a7f3ddSMatthew G. Knepley 
452744a7f3ddSMatthew G. Knepley   Input Parameter:
452844a7f3ddSMatthew G. Knepley . dm - The DM
452944a7f3ddSMatthew G. Knepley 
453044a7f3ddSMatthew G. Knepley   Level: intermediate
453144a7f3ddSMatthew G. Knepley 
453244a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
453344a7f3ddSMatthew G. Knepley @*/
453444a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
453544a7f3ddSMatthew G. Knepley {
453644a7f3ddSMatthew G. Knepley   PetscInt       f;
453744a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
453844a7f3ddSMatthew G. Knepley 
453944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
454044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
454144a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
454244a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
454344a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
454444a7f3ddSMatthew G. Knepley   }
454544a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
454644a7f3ddSMatthew G. Knepley   dm->fields = NULL;
454744a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
454844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
454944a7f3ddSMatthew G. Knepley }
455044a7f3ddSMatthew G. Knepley 
4551689b5837SMatthew G. Knepley /*@
4552689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4553689b5837SMatthew G. Knepley 
4554689b5837SMatthew G. Knepley   Not collective
4555689b5837SMatthew G. Knepley 
4556689b5837SMatthew G. Knepley   Input Parameter:
4557689b5837SMatthew G. Knepley . dm - The DM
4558689b5837SMatthew G. Knepley 
4559689b5837SMatthew G. Knepley   Output Parameter:
4560689b5837SMatthew G. Knepley . Nf - The number of fields
4561689b5837SMatthew G. Knepley 
4562689b5837SMatthew G. Knepley   Level: intermediate
4563689b5837SMatthew G. Knepley 
4564689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4565689b5837SMatthew G. Knepley @*/
45660f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
45670f21e855SMatthew G. Knepley {
45680f21e855SMatthew G. Knepley   PetscFunctionBegin;
45690f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4570534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
457144a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4572af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4573af122d2aSMatthew G Knepley }
4574af122d2aSMatthew G Knepley 
4575689b5837SMatthew G. Knepley /*@
4576689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4577689b5837SMatthew G. Knepley 
4578d083f849SBarry Smith   Logically collective on dm
4579689b5837SMatthew G. Knepley 
4580689b5837SMatthew G. Knepley   Input Parameters:
4581689b5837SMatthew G. Knepley + dm - The DM
4582689b5837SMatthew G. Knepley - Nf - The number of fields
4583689b5837SMatthew G. Knepley 
4584689b5837SMatthew G. Knepley   Level: intermediate
4585689b5837SMatthew G. Knepley 
4586689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4587689b5837SMatthew G. Knepley @*/
4588af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4589af122d2aSMatthew G Knepley {
45900f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4591af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4592af122d2aSMatthew G Knepley 
4593af122d2aSMatthew G Knepley   PetscFunctionBegin;
4594af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
459544a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
45960f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
45970f21e855SMatthew G. Knepley     PetscContainer obj;
45980f21e855SMatthew G. Knepley 
45990f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
460044a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
46010f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4602af122d2aSMatthew G Knepley   }
4603af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4604af122d2aSMatthew G Knepley }
4605af122d2aSMatthew G Knepley 
4606c1929be8SMatthew G. Knepley /*@
4607c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4608c1929be8SMatthew G. Knepley 
4609c1929be8SMatthew G. Knepley   Not collective
4610c1929be8SMatthew G. Knepley 
4611c1929be8SMatthew G. Knepley   Input Parameters:
4612c1929be8SMatthew G. Knepley + dm - The DM
4613c1929be8SMatthew G. Knepley - f  - The field number
4614c1929be8SMatthew G. Knepley 
461544a7f3ddSMatthew G. Knepley   Output Parameters:
461644a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
461744a7f3ddSMatthew G. Knepley - field - The discretization object
4618c1929be8SMatthew G. Knepley 
461944a7f3ddSMatthew G. Knepley   Level: intermediate
4620c1929be8SMatthew G. Knepley 
462144a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4622c1929be8SMatthew G. Knepley @*/
462344a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4624af122d2aSMatthew G Knepley {
4625af122d2aSMatthew G Knepley   PetscFunctionBegin;
4626af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
462744a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
462844a7f3ddSMatthew 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);
462944a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
463044a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4631decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4632decb47aaSMatthew G. Knepley }
4633decb47aaSMatthew G. Knepley 
4634c1929be8SMatthew G. Knepley /*@
4635c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4636c1929be8SMatthew G. Knepley 
4637d083f849SBarry Smith   Logically collective on dm
4638c1929be8SMatthew G. Knepley 
4639c1929be8SMatthew G. Knepley   Input Parameters:
4640c1929be8SMatthew G. Knepley + dm    - The DM
4641c1929be8SMatthew G. Knepley . f     - The field number
464244a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4643c1929be8SMatthew G. Knepley - field - The discretization object
4644c1929be8SMatthew G. Knepley 
464544a7f3ddSMatthew G. Knepley   Level: intermediate
4646c1929be8SMatthew G. Knepley 
464744a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4648c1929be8SMatthew G. Knepley @*/
464944a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4650decb47aaSMatthew G. Knepley {
4651decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4652decb47aaSMatthew G. Knepley 
4653decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4654decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4655e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
465644a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4657e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
465844a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
465944a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
466044a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
466144a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
466244a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
466344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
466444a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
466534aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
46662df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
466744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
466844a7f3ddSMatthew G. Knepley }
466944a7f3ddSMatthew G. Knepley 
467044a7f3ddSMatthew G. Knepley /*@
467144a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
467244a7f3ddSMatthew G. Knepley 
4673d083f849SBarry Smith   Logically collective on dm
467444a7f3ddSMatthew G. Knepley 
467544a7f3ddSMatthew G. Knepley   Input Parameters:
467644a7f3ddSMatthew G. Knepley + dm    - The DM
467744a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
467844a7f3ddSMatthew G. Knepley - field - The discretization object
467944a7f3ddSMatthew G. Knepley 
468044a7f3ddSMatthew G. Knepley   Level: intermediate
468144a7f3ddSMatthew G. Knepley 
468244a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
468344a7f3ddSMatthew G. Knepley @*/
468444a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
468544a7f3ddSMatthew G. Knepley {
468644a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
468744a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
468844a7f3ddSMatthew G. Knepley 
468944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
469044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
469144a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
469244a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
469344a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
469444a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
469544a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
469644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
469744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
469834aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
46992df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4700af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4701af122d2aSMatthew G Knepley }
47026636e97aSMatthew G Knepley 
4703e5e52638SMatthew G. Knepley /*@
4704e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4705e5e52638SMatthew G. Knepley 
4706d083f849SBarry Smith   Collective on dm
4707e5e52638SMatthew G. Knepley 
4708e5e52638SMatthew G. Knepley   Input Parameter:
4709e5e52638SMatthew G. Knepley . dm - The DM
4710e5e52638SMatthew G. Knepley 
4711e5e52638SMatthew G. Knepley   Output Parameter:
4712e5e52638SMatthew G. Knepley . newdm - The DM
4713e5e52638SMatthew G. Knepley 
4714e5e52638SMatthew G. Knepley   Level: advanced
4715e5e52638SMatthew G. Knepley 
4716e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4717e5e52638SMatthew G. Knepley @*/
4718e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4719e5e52638SMatthew G. Knepley {
4720e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4721e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4722e5e52638SMatthew G. Knepley 
4723e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4724e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4725e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4726e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4727e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4728e5e52638SMatthew G. Knepley     DMLabel     label;
4729e5e52638SMatthew G. Knepley     PetscObject field;
473034aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4731e5e52638SMatthew G. Knepley 
4732e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4733e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
473434aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
473534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
473634aa8a36SMatthew G. Knepley   }
473734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
473834aa8a36SMatthew G. Knepley }
473934aa8a36SMatthew G. Knepley 
474034aa8a36SMatthew G. Knepley /*@
474134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
474234aa8a36SMatthew G. Knepley 
474334aa8a36SMatthew G. Knepley   Not collective
474434aa8a36SMatthew G. Knepley 
474534aa8a36SMatthew G. Knepley   Input Parameters:
474634aa8a36SMatthew G. Knepley + dm - The DM object
474734aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
474834aa8a36SMatthew G. Knepley 
474934aa8a36SMatthew G. Knepley   Output Parameter:
475034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
475134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
475234aa8a36SMatthew G. Knepley 
475334aa8a36SMatthew G. Knepley   Notes:
475434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
475534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
475634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4757979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
475834aa8a36SMatthew G. Knepley 
475934aa8a36SMatthew G. Knepley   Level: developer
476034aa8a36SMatthew G. Knepley 
476134aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
476234aa8a36SMatthew G. Knepley @*/
476334aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
476434aa8a36SMatthew G. Knepley {
476534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
476634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4767534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4768534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
476934aa8a36SMatthew G. Knepley   if (f < 0) {
477034aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
477134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
477234aa8a36SMatthew G. Knepley   } else {
477334aa8a36SMatthew G. Knepley     PetscInt       Nf;
477434aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
477534aa8a36SMatthew G. Knepley 
477634aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
477734aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
477834aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
477934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
478034aa8a36SMatthew G. Knepley   }
478134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
478234aa8a36SMatthew G. Knepley }
478334aa8a36SMatthew G. Knepley 
478434aa8a36SMatthew G. Knepley /*@
478534aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
478634aa8a36SMatthew G. Knepley 
478734aa8a36SMatthew G. Knepley   Not collective
478834aa8a36SMatthew G. Knepley 
478934aa8a36SMatthew G. Knepley   Input Parameters:
479034aa8a36SMatthew G. Knepley + dm         - The DM object
479134aa8a36SMatthew G. Knepley . f          - The field number
479234aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
479334aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
479434aa8a36SMatthew G. Knepley 
479534aa8a36SMatthew G. Knepley   Notes:
479634aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
479734aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
479834aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4799979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
480034aa8a36SMatthew G. Knepley 
480134aa8a36SMatthew G. Knepley   Level: developer
480234aa8a36SMatthew G. Knepley 
480334aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
480434aa8a36SMatthew G. Knepley @*/
480534aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
480634aa8a36SMatthew G. Knepley {
480734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
480834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
480934aa8a36SMatthew G. Knepley   if (f < 0) {
481034aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
481134aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
481234aa8a36SMatthew G. Knepley   } else {
481334aa8a36SMatthew G. Knepley     PetscInt       Nf;
481434aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
481534aa8a36SMatthew G. Knepley 
481634aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
481734aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
481834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
481934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4820e5e52638SMatthew G. Knepley   }
4821e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4822e5e52638SMatthew G. Knepley }
4823e5e52638SMatthew G. Knepley 
4824b0441da4SMatthew G. Knepley /*@
4825b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4826b0441da4SMatthew G. Knepley 
4827b0441da4SMatthew G. Knepley   Not collective
4828b0441da4SMatthew G. Knepley 
4829b0441da4SMatthew G. Knepley   Input Parameters:
4830b0441da4SMatthew G. Knepley . dm - The DM object
4831b0441da4SMatthew G. Knepley 
4832b0441da4SMatthew G. Knepley   Output Parameter:
4833b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4834b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4835b0441da4SMatthew G. Knepley 
4836b0441da4SMatthew G. Knepley   Notes:
4837b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4838b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4839b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4840b0441da4SMatthew G. Knepley 
4841b0441da4SMatthew G. Knepley   Level: developer
4842b0441da4SMatthew G. Knepley 
4843b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4844b0441da4SMatthew G. Knepley @*/
4845b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4846b0441da4SMatthew G. Knepley {
4847b0441da4SMatthew G. Knepley   PetscInt       Nf;
4848b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4849b0441da4SMatthew G. Knepley 
4850b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4851b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4852534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4853534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4854b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4855b0441da4SMatthew G. Knepley   if (!Nf) {
4856b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4857b0441da4SMatthew G. Knepley   } else {
4858b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4859b0441da4SMatthew G. Knepley   }
4860b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4861b0441da4SMatthew G. Knepley }
4862b0441da4SMatthew G. Knepley 
4863b0441da4SMatthew G. Knepley /*@
4864b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4865b0441da4SMatthew G. Knepley 
4866b0441da4SMatthew G. Knepley   Not collective
4867b0441da4SMatthew G. Knepley 
4868b0441da4SMatthew G. Knepley   Input Parameters:
4869b0441da4SMatthew G. Knepley + dm         - The DM object
4870b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4871b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4872b0441da4SMatthew G. Knepley 
4873b0441da4SMatthew G. Knepley   Notes:
4874b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4875b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4876b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4877b0441da4SMatthew G. Knepley 
4878b0441da4SMatthew G. Knepley   Level: developer
4879b0441da4SMatthew G. Knepley 
4880b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4881b0441da4SMatthew G. Knepley @*/
4882b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4883b0441da4SMatthew G. Knepley {
4884b0441da4SMatthew G. Knepley   PetscInt       Nf;
4885b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4886b0441da4SMatthew G. Knepley 
4887b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4888b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4889b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4890b0441da4SMatthew G. Knepley   if (!Nf) {
4891b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4892b0441da4SMatthew G. Knepley   } else {
4893b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4894e5e52638SMatthew G. Knepley   }
4895e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4896e5e52638SMatthew G. Knepley }
4897e5e52638SMatthew G. Knepley 
4898e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4899e5e52638SMatthew G. Knepley {
4900e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4901e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4902e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4903e5e52638SMatthew G. Knepley 
4904e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4905e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4906e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4907e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4908b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4909e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4910e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4911e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4912e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4913e5e52638SMatthew G. Knepley }
4914e5e52638SMatthew G. Knepley 
4915e5e52638SMatthew G. Knepley /*@
4916e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4917e5e52638SMatthew G. Knepley 
4918e5e52638SMatthew G. Knepley   Not collective
4919e5e52638SMatthew G. Knepley 
4920e5e52638SMatthew G. Knepley   Input Parameter:
4921e5e52638SMatthew G. Knepley . dm - The DM
4922e5e52638SMatthew G. Knepley 
4923e5e52638SMatthew G. Knepley   Output Parameter:
4924e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4925e5e52638SMatthew G. Knepley 
4926e5e52638SMatthew G. Knepley   Level: intermediate
4927e5e52638SMatthew G. Knepley 
4928e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4929e5e52638SMatthew G. Knepley @*/
4930e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4931e5e52638SMatthew G. Knepley {
4932e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4933e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4934534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
4935e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4936e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4937e5e52638SMatthew G. Knepley }
4938e5e52638SMatthew G. Knepley 
4939e5e52638SMatthew G. Knepley /*@
4940e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4941e5e52638SMatthew G. Knepley 
4942d083f849SBarry Smith   Logically collective on dm
4943e5e52638SMatthew G. Knepley 
4944e5e52638SMatthew G. Knepley   Input Parameter:
4945e5e52638SMatthew G. Knepley . dm - The DM
4946e5e52638SMatthew G. Knepley 
4947e5e52638SMatthew G. Knepley   Level: intermediate
4948e5e52638SMatthew G. Knepley 
4949e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4950e5e52638SMatthew G. Knepley @*/
4951e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4952e5e52638SMatthew G. Knepley {
4953e5e52638SMatthew G. Knepley   PetscInt       s;
4954e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4955e5e52638SMatthew G. Knepley 
4956e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4957e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4958e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4959e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4960e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4961b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4962e5e52638SMatthew G. Knepley   }
4963e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4964e5e52638SMatthew G. Knepley   dm->probs = NULL;
4965e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4966e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4967e5e52638SMatthew G. Knepley }
4968e5e52638SMatthew G. Knepley 
4969e5e52638SMatthew G. Knepley /*@
4970e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4971e5e52638SMatthew G. Knepley 
4972e5e52638SMatthew G. Knepley   Not collective
4973e5e52638SMatthew G. Knepley 
4974e5e52638SMatthew G. Knepley   Input Parameter:
4975e5e52638SMatthew G. Knepley . dm    - The DM
4976e5e52638SMatthew G. Knepley 
4977e5e52638SMatthew G. Knepley   Output Parameter:
4978e5e52638SMatthew G. Knepley . prob - The default PetscDS
4979e5e52638SMatthew G. Knepley 
4980e5e52638SMatthew G. Knepley   Level: intermediate
4981e5e52638SMatthew G. Knepley 
4982e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4983e5e52638SMatthew G. Knepley @*/
4984e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4985e5e52638SMatthew G. Knepley {
4986b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4987b0143b4dSMatthew G. Knepley 
4988e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4989e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4990e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4991b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4992b0143b4dSMatthew G. Knepley     PetscDS ds;
4993b0143b4dSMatthew G. Knepley 
4994b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4995b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
4996b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4997b0143b4dSMatthew G. Knepley   }
4998b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4999e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5000e5e52638SMatthew G. Knepley }
5001e5e52638SMatthew G. Knepley 
5002e5e52638SMatthew G. Knepley /*@
5003e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5004e5e52638SMatthew G. Knepley 
5005e5e52638SMatthew G. Knepley   Not collective
5006e5e52638SMatthew G. Knepley 
5007e5e52638SMatthew G. Knepley   Input Parameters:
5008e5e52638SMatthew G. Knepley + dm    - The DM
5009e5e52638SMatthew G. Knepley - point - Cell for the DS
5010e5e52638SMatthew G. Knepley 
5011e5e52638SMatthew G. Knepley   Output Parameter:
5012e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5013e5e52638SMatthew G. Knepley 
5014e5e52638SMatthew G. Knepley   Level: developer
5015e5e52638SMatthew G. Knepley 
5016b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5017e5e52638SMatthew G. Knepley @*/
5018e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5019e5e52638SMatthew G. Knepley {
5020e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5021e5e52638SMatthew G. Knepley   PetscInt       s;
5022e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5023e5e52638SMatthew G. Knepley 
5024e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5025e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5026e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5027e5e52638SMatthew G. Knepley   *prob = NULL;
5028e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5029e5e52638SMatthew G. Knepley     PetscInt val;
5030e5e52638SMatthew G. Knepley 
5031e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5032e5e52638SMatthew G. Knepley     else {
5033e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5034e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5035e5e52638SMatthew G. Knepley     }
5036e5e52638SMatthew G. Knepley   }
5037e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5038e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5039e5e52638SMatthew G. Knepley }
5040e5e52638SMatthew G. Knepley 
5041e5e52638SMatthew G. Knepley /*@
5042e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5043e5e52638SMatthew G. Knepley 
5044e5e52638SMatthew G. Knepley   Not collective
5045e5e52638SMatthew G. Knepley 
5046e5e52638SMatthew G. Knepley   Input Parameters:
5047e5e52638SMatthew G. Knepley + dm    - The DM
5048e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5049e5e52638SMatthew G. Knepley 
5050b3cf3223SMatthew G. Knepley   Output Parameters:
5051b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5052b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5053e5e52638SMatthew G. Knepley 
5054e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5055e5e52638SMatthew G. Knepley 
5056e5e52638SMatthew G. Knepley   Level: advanced
5057e5e52638SMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5059e5e52638SMatthew G. Knepley @*/
5060b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5061e5e52638SMatthew G. Knepley {
5062e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5063e5e52638SMatthew G. Knepley 
5064e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5065e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5066e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5067b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5068b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5069e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5070b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5071b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5072b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5073b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5074b3cf3223SMatthew G. Knepley     }
5075e5e52638SMatthew G. Knepley   }
50762df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5077e5e52638SMatthew G. Knepley }
5078e5e52638SMatthew G. Knepley 
5079e5e52638SMatthew G. Knepley /*@
5080e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5081e5e52638SMatthew G. Knepley 
5082e5e52638SMatthew G. Knepley   Not collective
5083e5e52638SMatthew G. Knepley 
5084e5e52638SMatthew G. Knepley   Input Parameters:
5085e5e52638SMatthew G. Knepley + dm  - The DM
5086e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5087e5e52638SMatthew G. Knepley 
5088e5e52638SMatthew G. Knepley   Output Parameters:
5089b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5090b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5091b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
5092e5e52638SMatthew G. Knepley 
5093e5e52638SMatthew G. Knepley   Level: advanced
5094e5e52638SMatthew G. Knepley 
5095e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5096e5e52638SMatthew G. Knepley @*/
5097b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5098e5e52638SMatthew G. Knepley {
5099e5e52638SMatthew G. Knepley   PetscInt       Nds;
5100e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5101e5e52638SMatthew G. Knepley 
5102e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5103e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5104e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5105e5e52638SMatthew 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);
5106e5e52638SMatthew G. Knepley   if (label) {
5107e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5108e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5109e5e52638SMatthew G. Knepley   }
5110b3cf3223SMatthew G. Knepley   if (fields) {
5111b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5112b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5113b3cf3223SMatthew G. Knepley   }
5114e5e52638SMatthew G. Knepley   if (ds) {
5115b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5116e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5117e5e52638SMatthew G. Knepley   }
5118e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5119e5e52638SMatthew G. Knepley }
5120e5e52638SMatthew G. Knepley 
5121e5e52638SMatthew G. Knepley /*@
5122e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5123e5e52638SMatthew G. Knepley 
5124d083f849SBarry Smith   Collective on dm
5125e5e52638SMatthew G. Knepley 
5126e5e52638SMatthew G. Knepley   Input Parameters:
5127e5e52638SMatthew G. Knepley + dm     - The DM
5128e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5129b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5130e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5131e5e52638SMatthew G. Knepley 
5132b3cf3223SMatthew 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,
5133b3cf3223SMatthew G. Knepley   the fields argument is ignored.
5134e5e52638SMatthew G. Knepley 
5135e5e52638SMatthew G. Knepley   Level: advanced
5136e5e52638SMatthew G. Knepley 
5137e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
5138e5e52638SMatthew G. Knepley @*/
5139b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5140e5e52638SMatthew G. Knepley {
5141e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5142e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5143e5e52638SMatthew G. Knepley 
5144e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5145e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5146e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5147e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5148e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5149e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
5150b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5151e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
5152e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
5153e5e52638SMatthew G. Knepley     }
5154e5e52638SMatthew G. Knepley   }
51551b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5156e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5157b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5158e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5159e5e52638SMatthew G. Knepley   if (!label) {
5160e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5161e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5162e5e52638SMatthew G. Knepley     Nds = 0;
5163e5e52638SMatthew G. Knepley   }
5164e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
5165b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5166e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5167e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5168e5e52638SMatthew G. Knepley }
5169e5e52638SMatthew G. Knepley 
5170e5e52638SMatthew G. Knepley /*@
5171e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5172e5e52638SMatthew G. Knepley 
5173d083f849SBarry Smith   Collective on dm
5174e5e52638SMatthew G. Knepley 
5175e5e52638SMatthew G. Knepley   Input Parameter:
5176e5e52638SMatthew G. Knepley . dm - The DM
5177e5e52638SMatthew G. Knepley 
5178e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5179e5e52638SMatthew G. Knepley 
5180e5e52638SMatthew G. Knepley   Level: intermediate
5181e5e52638SMatthew G. Knepley 
5182e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5183e5e52638SMatthew G. Knepley @*/
5184e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5185e5e52638SMatthew G. Knepley {
5186e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5187e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5188b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5189e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5190e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5191e5e52638SMatthew G. Knepley 
5192e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5193e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5194e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5195e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5196e5e52638SMatthew G. Knepley    1) NULL
5197e5e52638SMatthew G. Knepley    2) Hybrid cells
5198e5e52638SMatthew G. Knepley   */
5199e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5200e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5201e5e52638SMatthew G. Knepley   /* Create default DS */
5202b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
52032df9ee95SMatthew G. Knepley   if (!prob) {
5204b3cf3223SMatthew G. Knepley     IS        fields;
5205b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5206b3cf3223SMatthew G. Knepley 
5207b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5208b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5209b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
521088f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
521188f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
521288f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
521388f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
521488f0c812SMatthew G. Knepley 
52152df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5216b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
52172df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5218b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5219b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
52202df9ee95SMatthew G. Knepley   }
5221e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5222e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5223b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5224e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5225e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5226e5e52638SMatthew G. Knepley 
5227e5e52638SMatthew G. Knepley     if (label) {
52280122748bSMatthew G. Knepley       DM        plex;
5229a2d47024SMatthew G. Knepley       IS        fields;
5230a2d47024SMatthew G. Knepley       PetscInt *fld;
52310122748bSMatthew G. Knepley       PetscInt  depth, pMax[4];
52320122748bSMatthew G. Knepley 
52330122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
52340122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
52350122748bSMatthew G. Knepley       ierr = DMPlexGetHybridBounds(plex, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
52360122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
52370122748bSMatthew G. Knepley 
5238e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5239e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
5240e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5241a2d47024SMatthew G. Knepley       ierr = PetscMalloc1(1, &fld);CHKERRQ(ierr);
5242a2d47024SMatthew G. Knepley       fld[0] = f;
5243a2d47024SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5244a2d47024SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5245a2d47024SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5246a2d47024SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, 1, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5247a2d47024SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, fields, probh);CHKERRQ(ierr);
5248a2d47024SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
5249e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5250e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5251e5e52638SMatthew G. Knepley       break;
5252e5e52638SMatthew G. Knepley     }
5253e5e52638SMatthew G. Knepley   }
5254e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5255b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5256e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5257e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5258e5e52638SMatthew G. Knepley 
5259e5e52638SMatthew G. Knepley     if (!label) {
5260e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5261e5e52638SMatthew G. Knepley       if (probh) {
5262e5e52638SMatthew G. Knepley         PetscFE subfe;
5263e5e52638SMatthew G. Knepley 
5264e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5265e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5266e5e52638SMatthew G. Knepley       }
5267e5e52638SMatthew G. Knepley     } else {
5268e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5269e5e52638SMatthew G. Knepley     }
5270e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5271e5e52638SMatthew G. Knepley     {
5272e5e52638SMatthew G. Knepley       PetscClassId id;
5273e5e52638SMatthew G. Knepley 
5274e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5275e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5276e5e52638SMatthew G. Knepley     }
5277e5e52638SMatthew G. Knepley   }
5278e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5279e5e52638SMatthew G. Knepley   /* Setup DSes */
5280e5e52638SMatthew G. Knepley   if (doSetup) {
5281e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5282e5e52638SMatthew G. Knepley   }
5283e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5284e5e52638SMatthew G. Knepley }
5285e5e52638SMatthew G. Knepley 
5286e5e52638SMatthew G. Knepley /*@
5287e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5288e5e52638SMatthew G. Knepley 
5289d083f849SBarry Smith   Collective on dm
5290e5e52638SMatthew G. Knepley 
5291e5e52638SMatthew G. Knepley   Input Parameter:
5292e5e52638SMatthew G. Knepley . dm - The DM
5293e5e52638SMatthew G. Knepley 
5294e5e52638SMatthew G. Knepley   Output Parameter:
5295e5e52638SMatthew G. Knepley . newdm - The DM
5296e5e52638SMatthew G. Knepley 
5297e5e52638SMatthew G. Knepley   Level: advanced
5298e5e52638SMatthew G. Knepley 
5299e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5300e5e52638SMatthew G. Knepley @*/
5301e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5302e5e52638SMatthew G. Knepley {
5303e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5304e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5305e5e52638SMatthew G. Knepley 
5306e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5307e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5308e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5309e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5310e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5311e5e52638SMatthew G. Knepley     DMLabel label;
5312b3cf3223SMatthew G. Knepley     IS      fields;
5313e5e52638SMatthew G. Knepley     PetscDS ds;
5314e5e52638SMatthew G. Knepley 
5315b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5316b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5317e5e52638SMatthew G. Knepley   }
5318e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5319e5e52638SMatthew G. Knepley }
5320e5e52638SMatthew G. Knepley 
5321e5e52638SMatthew G. Knepley /*@
5322e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5323e5e52638SMatthew G. Knepley 
5324d083f849SBarry Smith   Collective on dm
5325e5e52638SMatthew G. Knepley 
5326e5e52638SMatthew G. Knepley   Input Parameter:
5327e5e52638SMatthew G. Knepley . dm - The DM
5328e5e52638SMatthew G. Knepley 
5329e5e52638SMatthew G. Knepley   Output Parameter:
5330e5e52638SMatthew G. Knepley . newdm - The DM
5331e5e52638SMatthew G. Knepley 
5332e5e52638SMatthew G. Knepley   Level: advanced
5333e5e52638SMatthew G. Knepley 
5334e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5335e5e52638SMatthew G. Knepley @*/
5336e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5337e5e52638SMatthew G. Knepley {
5338e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5339e5e52638SMatthew G. Knepley 
5340e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5341e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5342e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5343e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5344e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5345e5e52638SMatthew G. Knepley }
5346e5e52638SMatthew G. Knepley 
5347b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5348b64e0483SPeter Brune {
5349b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5350b64e0483SPeter Brune   PetscErrorCode ierr;
5351b64e0483SPeter Brune   Vec coords,ccoords;
53526dbf9973SLawrence Mitchell   Mat inject;
5353b64e0483SPeter Brune   PetscFunctionBegin;
5354b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5355b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5356b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5357b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5358b64e0483SPeter Brune   if (coords && !ccoords) {
5359b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
53606668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
53616dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
53622adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
53636dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5364b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5365b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5366b64e0483SPeter Brune   }
5367b64e0483SPeter Brune   PetscFunctionReturn(0);
5368b64e0483SPeter Brune }
5369b64e0483SPeter Brune 
537003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
537103dadc2fSPeter Brune {
537203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
537303dadc2fSPeter Brune   PetscErrorCode ierr;
537403dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
537503dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
537603dadc2fSPeter Brune   PetscFunctionBegin;
537703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
537803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
537903dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
538003dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
538103dadc2fSPeter Brune   if (coords && !ccoords) {
538203dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
53836668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
538403dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
538524640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
538603dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
538703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
538803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
53891ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
539003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
539103dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
539203dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
539303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
539403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
539503dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
539603dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
539703dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
539803dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
539903dadc2fSPeter Brune   }
540003dadc2fSPeter Brune   PetscFunctionReturn(0);
540103dadc2fSPeter Brune }
540203dadc2fSPeter Brune 
5403c73cfb54SMatthew G. Knepley /*@
5404c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5405c73cfb54SMatthew G. Knepley 
5406c73cfb54SMatthew G. Knepley   Not collective
5407c73cfb54SMatthew G. Knepley 
5408c73cfb54SMatthew G. Knepley   Input Parameter:
5409c73cfb54SMatthew G. Knepley . dm - The DM
5410c73cfb54SMatthew G. Knepley 
5411c73cfb54SMatthew G. Knepley   Output Parameter:
5412c73cfb54SMatthew G. Knepley . dim - The topological dimension
5413c73cfb54SMatthew G. Knepley 
5414c73cfb54SMatthew G. Knepley   Level: beginner
5415c73cfb54SMatthew G. Knepley 
5416c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5417c73cfb54SMatthew G. Knepley @*/
5418c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5419c73cfb54SMatthew G. Knepley {
5420c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5421c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5422534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5423c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5424c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5425c73cfb54SMatthew G. Knepley }
5426c73cfb54SMatthew G. Knepley 
5427c73cfb54SMatthew G. Knepley /*@
5428c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5429c73cfb54SMatthew G. Knepley 
5430c73cfb54SMatthew G. Knepley   Collective on dm
5431c73cfb54SMatthew G. Knepley 
5432c73cfb54SMatthew G. Knepley   Input Parameters:
5433c73cfb54SMatthew G. Knepley + dm - The DM
5434c73cfb54SMatthew G. Knepley - dim - The topological dimension
5435c73cfb54SMatthew G. Knepley 
5436c73cfb54SMatthew G. Knepley   Level: beginner
5437c73cfb54SMatthew G. Knepley 
5438c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5439c73cfb54SMatthew G. Knepley @*/
5440c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5441c73cfb54SMatthew G. Knepley {
5442e5e52638SMatthew G. Knepley   PetscDS        ds;
5443f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5444f17e8794SMatthew G. Knepley 
5445c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5446c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5447c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5448c73cfb54SMatthew G. Knepley   dm->dim = dim;
5449e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5450e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5451c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5452c73cfb54SMatthew G. Knepley }
5453c73cfb54SMatthew G. Knepley 
5454793f3fe5SMatthew G. Knepley /*@
5455793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5456793f3fe5SMatthew G. Knepley 
5457d083f849SBarry Smith   Collective on dm
5458793f3fe5SMatthew G. Knepley 
5459793f3fe5SMatthew G. Knepley   Input Parameters:
5460793f3fe5SMatthew G. Knepley + dm - the DM
5461793f3fe5SMatthew G. Knepley - dim - the dimension
5462793f3fe5SMatthew G. Knepley 
5463793f3fe5SMatthew G. Knepley   Output Parameters:
5464793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5465aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5466793f3fe5SMatthew G. Knepley 
5467793f3fe5SMatthew G. Knepley   Note:
5468793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5469a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5470793f3fe5SMatthew G. Knepley   then the interval is empty.
5471793f3fe5SMatthew G. Knepley 
5472793f3fe5SMatthew G. Knepley   Level: intermediate
5473793f3fe5SMatthew G. Knepley 
5474793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5475793f3fe5SMatthew G. Knepley @*/
5476793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5477793f3fe5SMatthew G. Knepley {
5478793f3fe5SMatthew G. Knepley   PetscInt       d;
5479793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5480793f3fe5SMatthew G. Knepley 
5481793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5482793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5483793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5484793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5485b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5486793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5487793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5488793f3fe5SMatthew G. Knepley }
5489793f3fe5SMatthew G. Knepley 
54906636e97aSMatthew G Knepley /*@
54916636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
54926636e97aSMatthew G Knepley 
5493d083f849SBarry Smith   Collective on dm
54946636e97aSMatthew G Knepley 
54956636e97aSMatthew G Knepley   Input Parameters:
54966636e97aSMatthew G Knepley + dm - the DM
54976636e97aSMatthew G Knepley - c - coordinate vector
54986636e97aSMatthew G Knepley 
54992dd40e9bSPatrick Sanan   Notes:
55002dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
55012dd40e9bSPatrick Sanan 
55022dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
55036636e97aSMatthew G Knepley 
55046636e97aSMatthew G Knepley   Level: intermediate
55056636e97aSMatthew G Knepley 
55062dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
55076636e97aSMatthew G Knepley @*/
55086636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
55096636e97aSMatthew G Knepley {
55106636e97aSMatthew G Knepley   PetscErrorCode ierr;
55116636e97aSMatthew G Knepley 
55126636e97aSMatthew G Knepley   PetscFunctionBegin;
55136636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55146636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55156636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55166636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55176636e97aSMatthew G Knepley   dm->coordinates = c;
55186636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5519b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
552003dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
55216636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55226636e97aSMatthew G Knepley }
55236636e97aSMatthew G Knepley 
55246636e97aSMatthew G Knepley /*@
55256636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
55266636e97aSMatthew G Knepley 
55277058e716SVaclav Hapla   Not collective
55286636e97aSMatthew G Knepley 
55296636e97aSMatthew G Knepley    Input Parameters:
55306636e97aSMatthew G Knepley +  dm - the DM
55316636e97aSMatthew G Knepley -  c - coordinate vector
55326636e97aSMatthew G Knepley 
55332dd40e9bSPatrick Sanan   Notes:
55346636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
55356636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
55366636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
55376636e97aSMatthew G Knepley 
55382dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
55392dd40e9bSPatrick Sanan 
55406636e97aSMatthew G Knepley   Level: intermediate
55416636e97aSMatthew G Knepley 
55426636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
55436636e97aSMatthew G Knepley @*/
55446636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
55456636e97aSMatthew G Knepley {
55466636e97aSMatthew G Knepley   PetscErrorCode ierr;
55476636e97aSMatthew G Knepley 
55486636e97aSMatthew G Knepley   PetscFunctionBegin;
55496636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55506636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55516636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55526636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
55538865f1eaSKarl Rupp 
55546636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
55558865f1eaSKarl Rupp 
55566636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55576636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55586636e97aSMatthew G Knepley }
55596636e97aSMatthew G Knepley 
55606636e97aSMatthew G Knepley /*@
55616636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
55626636e97aSMatthew G Knepley 
5563d083f849SBarry Smith   Collective on dm
55646636e97aSMatthew G Knepley 
55656636e97aSMatthew G Knepley   Input Parameter:
55666636e97aSMatthew G Knepley . dm - the DM
55676636e97aSMatthew G Knepley 
55686636e97aSMatthew G Knepley   Output Parameter:
55696636e97aSMatthew G Knepley . c - global coordinate vector
55706636e97aSMatthew G Knepley 
55716636e97aSMatthew G Knepley   Note:
55726636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
55736636e97aSMatthew G Knepley 
55746636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
55756636e97aSMatthew G Knepley 
55766636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
55776636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
55786636e97aSMatthew G Knepley 
55796636e97aSMatthew G Knepley   Level: intermediate
55806636e97aSMatthew G Knepley 
55816636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
55826636e97aSMatthew G Knepley @*/
55836636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
55846636e97aSMatthew G Knepley {
55856636e97aSMatthew G Knepley   PetscErrorCode ierr;
55866636e97aSMatthew G Knepley 
55876636e97aSMatthew G Knepley   PetscFunctionBegin;
55886636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55896636e97aSMatthew G Knepley   PetscValidPointer(c,2);
55901f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
55910298fd71SBarry Smith     DM        cdm = NULL;
55921970a576SMatthew G. Knepley     PetscBool localized;
55936636e97aSMatthew G Knepley 
55946636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
55956636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
55961970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
55971970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
55981970a576SMatthew G. Knepley     if (localized) {
55991970a576SMatthew G. Knepley       PetscInt cdim;
56001970a576SMatthew G. Knepley 
56011970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
56021970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
56031970a576SMatthew G. Knepley     }
56046636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
56056636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
56066636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
56076636e97aSMatthew G Knepley   }
56086636e97aSMatthew G Knepley   *c = dm->coordinates;
56096636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56106636e97aSMatthew G Knepley }
56116636e97aSMatthew G Knepley 
56126636e97aSMatthew G Knepley /*@
561381e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
561481e9a530SVaclav Hapla 
5615d083f849SBarry Smith   Collective on dm
561681e9a530SVaclav Hapla 
561781e9a530SVaclav Hapla   Input Parameter:
561881e9a530SVaclav Hapla . dm - the DM
561981e9a530SVaclav Hapla 
562081e9a530SVaclav Hapla   Level: advanced
562181e9a530SVaclav Hapla 
562281e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
562381e9a530SVaclav Hapla @*/
562481e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
562581e9a530SVaclav Hapla {
562681e9a530SVaclav Hapla   PetscErrorCode ierr;
562781e9a530SVaclav Hapla 
562881e9a530SVaclav Hapla   PetscFunctionBegin;
562981e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
563081e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
56311970a576SMatthew G. Knepley     DM        cdm = NULL;
56321970a576SMatthew G. Knepley     PetscBool localized;
56331970a576SMatthew G. Knepley 
563481e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
563581e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
56361970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
56371970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
56381970a576SMatthew G. Knepley     if (localized) {
56391970a576SMatthew G. Knepley       PetscInt cdim;
56401970a576SMatthew G. Knepley 
56411970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
56421970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
56431970a576SMatthew G. Knepley     }
564481e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
564581e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
564681e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
564781e9a530SVaclav Hapla   }
564881e9a530SVaclav Hapla   PetscFunctionReturn(0);
564981e9a530SVaclav Hapla }
565081e9a530SVaclav Hapla 
565181e9a530SVaclav Hapla /*@
56526636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
56536636e97aSMatthew G Knepley 
5654d083f849SBarry Smith   Collective on dm
56556636e97aSMatthew G Knepley 
56566636e97aSMatthew G Knepley   Input Parameter:
56576636e97aSMatthew G Knepley . dm - the DM
56586636e97aSMatthew G Knepley 
56596636e97aSMatthew G Knepley   Output Parameter:
56606636e97aSMatthew G Knepley . c - coordinate vector
56616636e97aSMatthew G Knepley 
56626636e97aSMatthew G Knepley   Note:
56636636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
56646636e97aSMatthew G Knepley 
56656636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
56666636e97aSMatthew G Knepley 
56676636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56686636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56696636e97aSMatthew G Knepley 
56706636e97aSMatthew G Knepley   Level: intermediate
56716636e97aSMatthew G Knepley 
567281e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
56736636e97aSMatthew G Knepley @*/
56746636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
56756636e97aSMatthew G Knepley {
56766636e97aSMatthew G Knepley   PetscErrorCode ierr;
56776636e97aSMatthew G Knepley 
56786636e97aSMatthew G Knepley   PetscFunctionBegin;
56796636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56806636e97aSMatthew G Knepley   PetscValidPointer(c,2);
568181e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
56826636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
56836636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56846636e97aSMatthew G Knepley }
56856636e97aSMatthew G Knepley 
568681e9a530SVaclav Hapla /*@
568781e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
568881e9a530SVaclav Hapla 
568981e9a530SVaclav Hapla   Not collective
569081e9a530SVaclav Hapla 
569181e9a530SVaclav Hapla   Input Parameter:
569281e9a530SVaclav Hapla . dm - the DM
569381e9a530SVaclav Hapla 
569481e9a530SVaclav Hapla   Output Parameter:
569581e9a530SVaclav Hapla . c - coordinate vector
569681e9a530SVaclav Hapla 
569781e9a530SVaclav Hapla   Level: advanced
569881e9a530SVaclav Hapla 
569981e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
570081e9a530SVaclav Hapla @*/
570181e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
570281e9a530SVaclav Hapla {
570381e9a530SVaclav Hapla   PetscFunctionBegin;
570481e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
570581e9a530SVaclav Hapla   PetscValidPointer(c,2);
570681e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
57076636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
57086636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57096636e97aSMatthew G Knepley }
57106636e97aSMatthew G Knepley 
57112db98f8dSVaclav Hapla /*@
57122db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
57132db98f8dSVaclav Hapla 
57142db98f8dSVaclav Hapla   Not collective
57152db98f8dSVaclav Hapla 
57162db98f8dSVaclav Hapla   Input Parameter:
57172db98f8dSVaclav Hapla + dm - the DM
57182db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
57192db98f8dSVaclav Hapla 
57202db98f8dSVaclav Hapla   Output Parameter:
57212db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
57222db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
57232db98f8dSVaclav Hapla 
57242db98f8dSVaclav Hapla   Note:
57252db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
57262db98f8dSVaclav Hapla 
57272db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
57282db98f8dSVaclav Hapla 
57292db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
57302db98f8dSVaclav Hapla 
57312db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
57322db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
57332db98f8dSVaclav Hapla 
57342db98f8dSVaclav Hapla   Level: advanced
57352db98f8dSVaclav Hapla 
57362db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
57372db98f8dSVaclav Hapla @*/
57382db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
57392db98f8dSVaclav Hapla {
57402db98f8dSVaclav Hapla   PetscSection        cs, newcs;
57412db98f8dSVaclav Hapla   Vec                 coords;
57422db98f8dSVaclav Hapla   const PetscScalar   *arr;
57432db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
57442db98f8dSVaclav Hapla   PetscInt            n;
57452db98f8dSVaclav Hapla   PetscErrorCode      ierr;
57462db98f8dSVaclav Hapla 
57472db98f8dSVaclav Hapla   PetscFunctionBegin;
57482db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57492db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
57502db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
57512db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
57522db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
57531bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
57541bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
57552db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
57562db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
57572db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
57582db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
57592db98f8dSVaclav Hapla   if (pCoord) {
57602db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
57612db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
57622db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
57632db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5764ad9ac99dSVaclav Hapla   } else {
5765ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
57662db98f8dSVaclav Hapla   }
5767ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5768ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
57692db98f8dSVaclav Hapla   PetscFunctionReturn(0);
57702db98f8dSVaclav Hapla }
57712db98f8dSVaclav Hapla 
5772f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5773f19dbd58SToby Isaac {
5774f19dbd58SToby Isaac   PetscErrorCode ierr;
5775f19dbd58SToby Isaac 
5776f19dbd58SToby Isaac   PetscFunctionBegin;
5777f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5778f19dbd58SToby Isaac   PetscValidPointer(field,2);
5779f19dbd58SToby Isaac   if (!dm->coordinateField) {
5780f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5781f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5782f19dbd58SToby Isaac     }
5783f19dbd58SToby Isaac   }
5784f19dbd58SToby Isaac   *field = dm->coordinateField;
5785f19dbd58SToby Isaac   PetscFunctionReturn(0);
5786f19dbd58SToby Isaac }
5787f19dbd58SToby Isaac 
5788f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5789f19dbd58SToby Isaac {
5790f19dbd58SToby Isaac   PetscErrorCode ierr;
5791f19dbd58SToby Isaac 
5792f19dbd58SToby Isaac   PetscFunctionBegin;
5793f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5794f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5795c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5796f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5797f19dbd58SToby Isaac   dm->coordinateField = field;
5798f19dbd58SToby Isaac   PetscFunctionReturn(0);
5799f19dbd58SToby Isaac }
5800f19dbd58SToby Isaac 
58016636e97aSMatthew G Knepley /*@
58021cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
58036636e97aSMatthew G Knepley 
5804d083f849SBarry Smith   Collective on dm
58056636e97aSMatthew G Knepley 
58066636e97aSMatthew G Knepley   Input Parameter:
58076636e97aSMatthew G Knepley . dm - the DM
58086636e97aSMatthew G Knepley 
58096636e97aSMatthew G Knepley   Output Parameter:
58106636e97aSMatthew G Knepley . cdm - coordinate DM
58116636e97aSMatthew G Knepley 
58126636e97aSMatthew G Knepley   Level: intermediate
58136636e97aSMatthew G Knepley 
58141cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58156636e97aSMatthew G Knepley @*/
58166636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
58176636e97aSMatthew G Knepley {
58186636e97aSMatthew G Knepley   PetscErrorCode ierr;
58196636e97aSMatthew G Knepley 
58206636e97aSMatthew G Knepley   PetscFunctionBegin;
58216636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58226636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
58236636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5824308f8a94SToby Isaac     DM cdm;
5825308f8a94SToby Isaac 
582682f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5827308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5828308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5829308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5830308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5831308f8a94SToby Isaac     dm->coordinateDM = cdm;
58326636e97aSMatthew G Knepley   }
58336636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
58346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58356636e97aSMatthew G Knepley }
5836e87bb0d3SMatthew G Knepley 
58371cfe2091SMatthew G. Knepley /*@
58381cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
58391cfe2091SMatthew G. Knepley 
5840d083f849SBarry Smith   Logically Collective on dm
58411cfe2091SMatthew G. Knepley 
58421cfe2091SMatthew G. Knepley   Input Parameters:
58431cfe2091SMatthew G. Knepley + dm - the DM
58441cfe2091SMatthew G. Knepley - cdm - coordinate DM
58451cfe2091SMatthew G. Knepley 
58461cfe2091SMatthew G. Knepley   Level: intermediate
58471cfe2091SMatthew G. Knepley 
58481cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58491cfe2091SMatthew G. Knepley @*/
58501cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
58511cfe2091SMatthew G. Knepley {
58521cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
58531cfe2091SMatthew G. Knepley 
58541cfe2091SMatthew G. Knepley   PetscFunctionBegin;
58551cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58561cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5857f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
58581cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
58591cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
58601cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
58611cfe2091SMatthew G. Knepley }
58621cfe2091SMatthew G. Knepley 
586346e270d4SMatthew G. Knepley /*@
586446e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
586546e270d4SMatthew G. Knepley 
586646e270d4SMatthew G. Knepley   Not Collective
586746e270d4SMatthew G. Knepley 
586846e270d4SMatthew G. Knepley   Input Parameter:
586946e270d4SMatthew G. Knepley . dm - The DM object
587046e270d4SMatthew G. Knepley 
587146e270d4SMatthew G. Knepley   Output Parameter:
587246e270d4SMatthew G. Knepley . dim - The embedding dimension
587346e270d4SMatthew G. Knepley 
587446e270d4SMatthew G. Knepley   Level: intermediate
587546e270d4SMatthew G. Knepley 
587692fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
587746e270d4SMatthew G. Knepley @*/
587846e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
587946e270d4SMatthew G. Knepley {
588046e270d4SMatthew G. Knepley   PetscFunctionBegin;
588146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5882534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
58839a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
58849a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
58859a9a41abSToby Isaac   }
588646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
588746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
588846e270d4SMatthew G. Knepley }
588946e270d4SMatthew G. Knepley 
589046e270d4SMatthew G. Knepley /*@
589146e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
589246e270d4SMatthew G. Knepley 
589346e270d4SMatthew G. Knepley   Not Collective
589446e270d4SMatthew G. Knepley 
589546e270d4SMatthew G. Knepley   Input Parameters:
589646e270d4SMatthew G. Knepley + dm  - The DM object
589746e270d4SMatthew G. Knepley - dim - The embedding dimension
589846e270d4SMatthew G. Knepley 
589946e270d4SMatthew G. Knepley   Level: intermediate
590046e270d4SMatthew G. Knepley 
590192fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
590246e270d4SMatthew G. Knepley @*/
590346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
590446e270d4SMatthew G. Knepley {
5905e5e52638SMatthew G. Knepley   PetscDS        ds;
5906f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5907f17e8794SMatthew G. Knepley 
590846e270d4SMatthew G. Knepley   PetscFunctionBegin;
590946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
591046e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5911e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5912e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
591346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
591446e270d4SMatthew G. Knepley }
591546e270d4SMatthew G. Knepley 
5916e8abe2deSMatthew G. Knepley /*@
5917e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5918e8abe2deSMatthew G. Knepley 
5919d083f849SBarry Smith   Collective on dm
5920e8abe2deSMatthew G. Knepley 
5921e8abe2deSMatthew G. Knepley   Input Parameter:
5922e8abe2deSMatthew G. Knepley . dm - The DM object
5923e8abe2deSMatthew G. Knepley 
5924e8abe2deSMatthew G. Knepley   Output Parameter:
5925e8abe2deSMatthew G. Knepley . section - The PetscSection object
5926e8abe2deSMatthew G. Knepley 
5927e8abe2deSMatthew G. Knepley   Level: intermediate
5928e8abe2deSMatthew G. Knepley 
592992fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
5930e8abe2deSMatthew G. Knepley @*/
5931e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5932e8abe2deSMatthew G. Knepley {
5933e8abe2deSMatthew G. Knepley   DM             cdm;
5934e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5935e8abe2deSMatthew G. Knepley 
5936e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5937e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5938e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5939e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
594092fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
5941e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5942e8abe2deSMatthew G. Knepley }
5943e8abe2deSMatthew G. Knepley 
5944e8abe2deSMatthew G. Knepley /*@
5945e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5946e8abe2deSMatthew G. Knepley 
5947e8abe2deSMatthew G. Knepley   Not Collective
5948e8abe2deSMatthew G. Knepley 
5949e8abe2deSMatthew G. Knepley   Input Parameters:
5950e8abe2deSMatthew G. Knepley + dm      - The DM object
595146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5952e8abe2deSMatthew G. Knepley - section - The PetscSection object
5953e8abe2deSMatthew G. Knepley 
5954e8abe2deSMatthew G. Knepley   Level: intermediate
5955e8abe2deSMatthew G. Knepley 
595692fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
5957e8abe2deSMatthew G. Knepley @*/
595846e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5959e8abe2deSMatthew G. Knepley {
5960e8abe2deSMatthew G. Knepley   DM             cdm;
5961e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5962e8abe2deSMatthew G. Knepley 
5963e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5964e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
596546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5966e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
596792fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
596846e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
59694c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
597046e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
597146e270d4SMatthew G. Knepley 
597246e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
597346e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
597446e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
597546e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
597646e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
597746e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
597846e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
597946e270d4SMatthew G. Knepley     }
5980ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
598146e270d4SMatthew G. Knepley   }
5982e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5983e8abe2deSMatthew G. Knepley }
5984e8abe2deSMatthew G. Knepley 
59855dc8c3f7SMatthew G. Knepley /*@C
598690b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
59875dc8c3f7SMatthew G. Knepley 
59885dc8c3f7SMatthew G. Knepley   Input Parameters:
598990b157c4SStefano Zampini . dm      - The DM object
599090b157c4SStefano Zampini 
599190b157c4SStefano Zampini   Output Parameters:
599290b157c4SStefano Zampini + per     - Whether the DM is periodic or not
59935dc8c3f7SMatthew 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
59945dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
59955dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
59965dc8c3f7SMatthew G. Knepley 
59975dc8c3f7SMatthew G. Knepley   Level: developer
59985dc8c3f7SMatthew G. Knepley 
59995dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
60005dc8c3f7SMatthew G. Knepley @*/
600190b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6002c6b900c6SMatthew G. Knepley {
6003c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6004c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
600590b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6006c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6007c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
60085dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6009c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6010c6b900c6SMatthew G. Knepley }
6011c6b900c6SMatthew G. Knepley 
60125dc8c3f7SMatthew G. Knepley /*@C
60135dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
60145dc8c3f7SMatthew G. Knepley 
60155dc8c3f7SMatthew G. Knepley   Input Parameters:
60165dc8c3f7SMatthew G. Knepley + dm      - The DM object
601790b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
60185dc8c3f7SMatthew 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
60195dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
60205dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
60215dc8c3f7SMatthew G. Knepley 
60225dc8c3f7SMatthew G. Knepley   Level: developer
60235dc8c3f7SMatthew G. Knepley 
60245dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
60255dc8c3f7SMatthew G. Knepley @*/
602690b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6027c6b900c6SMatthew G. Knepley {
6028c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6029c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6030c6b900c6SMatthew G. Knepley 
6031c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6032c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
603390b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
603490b157c4SStefano Zampini   if (maxCell) {
6035534a8f05SLisandro Dalcin     PetscValidRealPointer(maxCell,3);
6036534a8f05SLisandro Dalcin     PetscValidRealPointer(L,4);
603790b157c4SStefano Zampini     PetscValidPointer(bd,5);
603890b157c4SStefano Zampini   }
60395dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
60405dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
604190b157c4SStefano Zampini   if (maxCell) {
60425dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
60435dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
604490b157c4SStefano Zampini   }
6045072d7d67SStefano Zampini   dm->periodic = per;
6046c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6047c6b900c6SMatthew G. Knepley }
6048c6b900c6SMatthew G. Knepley 
60492e17dfb7SMatthew G. Knepley /*@
60502e17dfb7SMatthew 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.
60512e17dfb7SMatthew G. Knepley 
60522e17dfb7SMatthew G. Knepley   Input Parameters:
60532e17dfb7SMatthew G. Knepley + dm     - The DM
605465da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
605565da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
60562e17dfb7SMatthew G. Knepley 
60572e17dfb7SMatthew G. Knepley   Output Parameter:
60582e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60592e17dfb7SMatthew G. Knepley 
60602e17dfb7SMatthew G. Knepley   Level: developer
60612e17dfb7SMatthew G. Knepley 
60622e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60632e17dfb7SMatthew G. Knepley @*/
606465da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
60652e17dfb7SMatthew G. Knepley {
60662e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
60672e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
60682e17dfb7SMatthew G. Knepley 
60692e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60702e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
60712e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60722e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60732e17dfb7SMatthew G. Knepley   } else {
607465da65dcSMatthew G. Knepley     if (endpoint) {
607565da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6076da3333bfSMatthew 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)) {
6077da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
607865da65dcSMatthew G. Knepley         } else {
6079da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
608065da65dcSMatthew G. Knepley         }
608165da65dcSMatthew G. Knepley       }
608265da65dcSMatthew G. Knepley     } else {
60832e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
60841118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
60852e17dfb7SMatthew G. Knepley       }
60862e17dfb7SMatthew G. Knepley     }
608765da65dcSMatthew G. Knepley   }
60882e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60892e17dfb7SMatthew G. Knepley }
60902e17dfb7SMatthew G. Knepley 
60912e17dfb7SMatthew G. Knepley /*
60922e17dfb7SMatthew 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.
60932e17dfb7SMatthew G. Knepley 
60942e17dfb7SMatthew G. Knepley   Input Parameters:
60952e17dfb7SMatthew G. Knepley + dm     - The DM
60962e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
60972e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
60982e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
60992e17dfb7SMatthew G. Knepley 
61002e17dfb7SMatthew G. Knepley   Output Parameter:
61012e17dfb7SMatthew G. Knepley . out - The localized coordinate point
61022e17dfb7SMatthew G. Knepley 
61032e17dfb7SMatthew G. Knepley   Level: developer
61042e17dfb7SMatthew G. Knepley 
61052e17dfb7SMatthew 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
61062e17dfb7SMatthew G. Knepley 
61072e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
61082e17dfb7SMatthew G. Knepley */
61092e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
61102e17dfb7SMatthew G. Knepley {
61112e17dfb7SMatthew G. Knepley   PetscInt d;
61122e17dfb7SMatthew G. Knepley 
61132e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61142e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61152e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61162e17dfb7SMatthew G. Knepley   } else {
61172e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6118908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
61192e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
61202e17dfb7SMatthew G. Knepley       } else {
61212e17dfb7SMatthew G. Knepley         out[d] = in[d];
61222e17dfb7SMatthew G. Knepley       }
61232e17dfb7SMatthew G. Knepley     }
61242e17dfb7SMatthew G. Knepley   }
61252e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61262e17dfb7SMatthew G. Knepley }
61272e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
61282e17dfb7SMatthew G. Knepley {
61292e17dfb7SMatthew G. Knepley   PetscInt d;
61302e17dfb7SMatthew G. Knepley 
61312e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61322e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61332e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61342e17dfb7SMatthew G. Knepley   } else {
61352e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6136908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
61372e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
61382e17dfb7SMatthew G. Knepley       } else {
61392e17dfb7SMatthew G. Knepley         out[d] = in[d];
61402e17dfb7SMatthew G. Knepley       }
61412e17dfb7SMatthew G. Knepley     }
61422e17dfb7SMatthew G. Knepley   }
61432e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61442e17dfb7SMatthew G. Knepley }
61452e17dfb7SMatthew G. Knepley 
61462e17dfb7SMatthew G. Knepley /*
61472e17dfb7SMatthew 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.
61482e17dfb7SMatthew G. Knepley 
61492e17dfb7SMatthew G. Knepley   Input Parameters:
61502e17dfb7SMatthew G. Knepley + dm     - The DM
61512e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
61522e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
61532e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
61542e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
61552e17dfb7SMatthew G. Knepley 
61562e17dfb7SMatthew G. Knepley   Output Parameter:
61572e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
61582e17dfb7SMatthew G. Knepley 
61592e17dfb7SMatthew G. Knepley   Level: developer
61602e17dfb7SMatthew G. Knepley 
61612e17dfb7SMatthew 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
61622e17dfb7SMatthew G. Knepley 
61632e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
61642e17dfb7SMatthew G. Knepley */
61652e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
61662e17dfb7SMatthew G. Knepley {
61672e17dfb7SMatthew G. Knepley   PetscInt d;
61682e17dfb7SMatthew G. Knepley 
61692e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61702e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61712e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
61722e17dfb7SMatthew G. Knepley   } else {
61732e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6174908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
61752e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
61762e17dfb7SMatthew G. Knepley       } else {
61772e17dfb7SMatthew G. Knepley         out[d] += in[d];
61782e17dfb7SMatthew G. Knepley       }
61792e17dfb7SMatthew G. Knepley     }
61802e17dfb7SMatthew G. Knepley   }
61812e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61822e17dfb7SMatthew G. Knepley }
61832e17dfb7SMatthew G. Knepley 
618436447a5eSToby Isaac /*@
61858f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
61868f700142SStefano Zampini 
61878f700142SStefano Zampini   Not collective
618836447a5eSToby Isaac 
618936447a5eSToby Isaac   Input Parameter:
619036447a5eSToby Isaac . dm - The DM
619136447a5eSToby Isaac 
619236447a5eSToby Isaac   Output Parameter:
619336447a5eSToby Isaac   areLocalized - True if localized
619436447a5eSToby Isaac 
619536447a5eSToby Isaac   Level: developer
619636447a5eSToby Isaac 
61978f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
619836447a5eSToby Isaac @*/
61998f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
620036447a5eSToby Isaac {
620136447a5eSToby Isaac   DM             cdm;
620236447a5eSToby Isaac   PetscSection   coordSection;
620346a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
620446a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
620536447a5eSToby Isaac   PetscErrorCode ierr;
620636447a5eSToby Isaac 
620736447a5eSToby Isaac   PetscFunctionBegin;
620836447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6209534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62108b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
621146a3a80fSLisandro Dalcin 
621236447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
621336447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
621446a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
62159f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
621646a3a80fSLisandro Dalcin 
62179f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
621846a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
621936447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
622036447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
622146a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
622246a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
622336447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
622446a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
622536447a5eSToby Isaac   }
62268f700142SStefano Zampini   *areLocalized = alreadyLocalized;
622736447a5eSToby Isaac   PetscFunctionReturn(0);
622836447a5eSToby Isaac }
622936447a5eSToby Isaac 
62308f700142SStefano Zampini /*@
62318f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
62328f700142SStefano Zampini 
62338f700142SStefano Zampini   Collective on dm
62348f700142SStefano Zampini 
62358f700142SStefano Zampini   Input Parameter:
62368f700142SStefano Zampini . dm - The DM
62378f700142SStefano Zampini 
62388f700142SStefano Zampini   Output Parameter:
62398f700142SStefano Zampini   areLocalized - True if localized
62408f700142SStefano Zampini 
62418f700142SStefano Zampini   Level: developer
62428f700142SStefano Zampini 
62438f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
62448f700142SStefano Zampini @*/
62458f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
62468f700142SStefano Zampini {
62478f700142SStefano Zampini   PetscBool      localized;
62488f700142SStefano Zampini   PetscErrorCode ierr;
62498f700142SStefano Zampini 
62508f700142SStefano Zampini   PetscFunctionBegin;
62518f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6252534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62538f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
62548f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
62558f700142SStefano Zampini   PetscFunctionReturn(0);
62568f700142SStefano Zampini }
625736447a5eSToby Isaac 
62582e17dfb7SMatthew G. Knepley /*@
6259492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
62602e17dfb7SMatthew G. Knepley 
62618f700142SStefano Zampini   Collective on dm
62628f700142SStefano Zampini 
62632e17dfb7SMatthew G. Knepley   Input Parameter:
62642e17dfb7SMatthew G. Knepley . dm - The DM
62652e17dfb7SMatthew G. Knepley 
62662e17dfb7SMatthew G. Knepley   Level: developer
62672e17dfb7SMatthew G. Knepley 
62688f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
62692e17dfb7SMatthew G. Knepley @*/
62702e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
62712e17dfb7SMatthew G. Knepley {
62722e17dfb7SMatthew G. Knepley   DM             cdm;
62732e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
62742e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
62753e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
62763e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6277e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
62783e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
62793e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
62802e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
62812e17dfb7SMatthew G. Knepley 
62822e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
62832e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
628492c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6285f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6286f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6287f7cbd40bSStefano Zampini 
62882e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
62892e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62902e17dfb7SMatthew G. Knepley   {
62912e17dfb7SMatthew G. Knepley     PetscBool isplex;
62922e17dfb7SMatthew G. Knepley 
62932e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
62942e17dfb7SMatthew G. Knepley     if (isplex) {
62952e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
62963e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
629769291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
62983e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
62993e922f36SToby Isaac       newStart = vStart;
63003e922f36SToby Isaac       newEnd   = vEnd;
63013e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
63023e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
63033e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
63043e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
63053e922f36SToby Isaac       }
63062e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
63072e17dfb7SMatthew G. Knepley   }
63082e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
630943eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
63102e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
63113e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6312e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
63133e922f36SToby Isaac 
63142e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
63152e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
63162e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
63172e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
63183e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
63193e922f36SToby Isaac 
632069291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63213e922f36SToby Isaac   localized = &anchor[bs];
63223e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
63233e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63243e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63253e922f36SToby Isaac 
63263e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
63273e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
63283e922f36SToby Isaac       PetscInt     b;
63293e922f36SToby Isaac 
63303e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
63313e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63323e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63333e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
63343e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
63353e922f36SToby Isaac         for (b = 0; b < bs; b++) {
63363e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
63373e922f36SToby Isaac         }
63383e922f36SToby Isaac         if (b < bs) break;
63393e922f36SToby Isaac       }
63403e922f36SToby Isaac       if (d < dof/bs) {
63413e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
63423e922f36SToby Isaac           PetscInt cdof;
63433e922f36SToby Isaac 
63443e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
63453e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
63463e922f36SToby Isaac         }
63473e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
63483e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
63493e922f36SToby Isaac       }
63503e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63513e922f36SToby Isaac     }
63523e922f36SToby Isaac   }
63533e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
63543e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
635569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63563e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
635769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
63583e922f36SToby Isaac     PetscFunctionReturn(0);
63593e922f36SToby Isaac   }
63602e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63612e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63622e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
63632e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
63642e17dfb7SMatthew G. Knepley   }
63652e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
63662e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6367c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
63682e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
63692e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
63702e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
63712e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6372c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63732e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
63742e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63752e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63762e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
63772e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
63782e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
63792e17dfb7SMatthew G. Knepley   }
63803e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63813e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63823e922f36SToby Isaac 
63832e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
63842e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
63853e922f36SToby Isaac       PetscInt     b, cdof;
63862e17dfb7SMatthew G. Knepley 
63873e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
63883e922f36SToby Isaac       if (!cdof) continue;
63892e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63902e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
63912e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63922e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
63932e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63942e17dfb7SMatthew G. Knepley     }
63953e922f36SToby Isaac   }
639669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
639769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6398c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63992e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
64002e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
64012e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
64022e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
64032e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
64042e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
64052e17dfb7SMatthew G. Knepley }
64062e17dfb7SMatthew G. Knepley 
6407e87bb0d3SMatthew G Knepley /*@
64083a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6409e87bb0d3SMatthew G Knepley 
6410d083f849SBarry Smith   Collective on v (see explanation below)
6411e87bb0d3SMatthew G Knepley 
6412e87bb0d3SMatthew G Knepley   Input Parameters:
6413e87bb0d3SMatthew G Knepley + dm - The DM
64143a93e3b7SToby Isaac . v - The Vec of points
641562a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
64163a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6417e87bb0d3SMatthew G Knepley 
641861e3bb9bSMatthew G Knepley   Output Parameter:
641962a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
642062a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
64213a93e3b7SToby Isaac 
6422e87bb0d3SMatthew G Knepley 
6423e87bb0d3SMatthew G Knepley   Level: developer
642461e3bb9bSMatthew G Knepley 
642562a38674SMatthew G. Knepley   Notes:
64263a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
642762a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
64283a93e3b7SToby Isaac 
64293a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
643062a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
64313a93e3b7SToby Isaac 
64323a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
64333a93e3b7SToby Isaac 
643462a38674SMatthew G. Knepley $    const PetscSFNode *cells;
643562a38674SMatthew G. Knepley $    PetscInt           nFound;
6436a6216909SToby Isaac $    const PetscInt    *found;
643762a38674SMatthew G. Knepley $
6438a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
64393a93e3b7SToby Isaac 
64403a93e3b7SToby 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
64413a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
64423a93e3b7SToby Isaac 
644362a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
644461e3bb9bSMatthew G Knepley @*/
644562a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6446e87bb0d3SMatthew G Knepley {
6447735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6448735aa83eSMatthew G Knepley 
6449e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6450e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6451e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6452e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
64533a93e3b7SToby Isaac   if (*cellSF) {
64543a93e3b7SToby Isaac     PetscMPIInt result;
64553a93e3b7SToby Isaac 
6456e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6457a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
64583a93e3b7SToby 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");
6459e0fc9d1bSMatthew G. Knepley   } else {
64603a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
64613a93e3b7SToby Isaac   }
6462b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
646347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
646462a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
646547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6466e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6467e87bb0d3SMatthew G Knepley }
646814f150ffSMatthew G. Knepley 
6469f4d763aaSMatthew G. Knepley /*@
6470f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6471f4d763aaSMatthew G. Knepley 
64728f700142SStefano Zampini   Collective on dm
64738f700142SStefano Zampini 
6474f4d763aaSMatthew G. Knepley   Input Parameter:
6475f4d763aaSMatthew G. Knepley . dm - The original DM
6476f4d763aaSMatthew G. Knepley 
6477f4d763aaSMatthew G. Knepley   Output Parameter:
6478f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6479f4d763aaSMatthew G. Knepley 
6480f4d763aaSMatthew G. Knepley   Level: intermediate
6481f4d763aaSMatthew G. Knepley 
6482e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6483f4d763aaSMatthew G. Knepley @*/
648414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
648514f150ffSMatthew G. Knepley {
6486c26acbdeSMatthew G. Knepley   PetscSection   section;
64872d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
648814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
648914f150ffSMatthew G. Knepley 
649014f150ffSMatthew G. Knepley   PetscFunctionBegin;
649114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
649214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
649392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6494c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6495127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
64962d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6497c26acbdeSMatthew G. Knepley     *odm = dm;
6498c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6499c26acbdeSMatthew G. Knepley   }
650014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6501c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
650214f150ffSMatthew G. Knepley     PetscSF      sf;
650314f150ffSMatthew G. Knepley 
650414f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6505e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
650614f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
650792fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
650814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
650914f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
651015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6511e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
651214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
651314f150ffSMatthew G. Knepley   }
651414f150ffSMatthew G. Knepley   *odm = dm->dmBC;
651514f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
651614f150ffSMatthew G. Knepley }
6517f4d763aaSMatthew G. Knepley 
6518f4d763aaSMatthew G. Knepley /*@
6519cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6520f4d763aaSMatthew G. Knepley 
6521f4d763aaSMatthew G. Knepley   Input Parameter:
6522f4d763aaSMatthew G. Knepley . dm - The original DM
6523f4d763aaSMatthew G. Knepley 
6524cdb7a50dSMatthew G. Knepley   Output Parameters:
6525cdb7a50dSMatthew G. Knepley + num - The output sequence number
6526cdb7a50dSMatthew G. Knepley - val - The output sequence value
6527f4d763aaSMatthew G. Knepley 
6528f4d763aaSMatthew G. Knepley   Level: intermediate
6529f4d763aaSMatthew G. Knepley 
6530f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6531f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6532f4d763aaSMatthew G. Knepley 
6533f4d763aaSMatthew G. Knepley .seealso: VecView()
6534f4d763aaSMatthew G. Knepley @*/
6535cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6536f4d763aaSMatthew G. Knepley {
6537f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6538f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6539534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6540534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6541f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6542f4d763aaSMatthew G. Knepley }
6543f4d763aaSMatthew G. Knepley 
6544f4d763aaSMatthew G. Knepley /*@
6545cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6546f4d763aaSMatthew G. Knepley 
6547f4d763aaSMatthew G. Knepley   Input Parameters:
6548f4d763aaSMatthew G. Knepley + dm - The original DM
6549cdb7a50dSMatthew G. Knepley . num - The output sequence number
6550cdb7a50dSMatthew G. Knepley - val - The output sequence value
6551f4d763aaSMatthew G. Knepley 
6552f4d763aaSMatthew G. Knepley   Level: intermediate
6553f4d763aaSMatthew G. Knepley 
6554f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6555f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6556f4d763aaSMatthew G. Knepley 
6557f4d763aaSMatthew G. Knepley .seealso: VecView()
6558f4d763aaSMatthew G. Knepley @*/
6559cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6560f4d763aaSMatthew G. Knepley {
6561f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6562f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6563f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6564cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6565cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6566cdb7a50dSMatthew G. Knepley }
6567cdb7a50dSMatthew G. Knepley 
6568cdb7a50dSMatthew G. Knepley /*@C
6569cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6570cdb7a50dSMatthew G. Knepley 
6571cdb7a50dSMatthew G. Knepley   Input Parameters:
6572cdb7a50dSMatthew G. Knepley + dm   - The original DM
6573cdb7a50dSMatthew G. Knepley . name - The sequence name
6574cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6575cdb7a50dSMatthew G. Knepley 
6576cdb7a50dSMatthew G. Knepley   Output Parameter:
6577cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6578cdb7a50dSMatthew G. Knepley 
6579cdb7a50dSMatthew G. Knepley   Level: intermediate
6580cdb7a50dSMatthew G. Knepley 
6581cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6582cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6583cdb7a50dSMatthew G. Knepley 
6584cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6585cdb7a50dSMatthew G. Knepley @*/
6586cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6587cdb7a50dSMatthew G. Knepley {
6588cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6589cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6590cdb7a50dSMatthew G. Knepley 
6591cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6592cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6593cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6594534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6595cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6596cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6597cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6598cdb7a50dSMatthew G. Knepley     PetscScalar value;
6599cdb7a50dSMatthew G. Knepley 
660039d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
66014aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6602cdb7a50dSMatthew G. Knepley #endif
6603cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6604f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6605f4d763aaSMatthew G. Knepley }
66068e4ac7eaSMatthew G. Knepley 
66078e4ac7eaSMatthew G. Knepley /*@
66088e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
66098e4ac7eaSMatthew G. Knepley 
66108e4ac7eaSMatthew G. Knepley   Not collective
66118e4ac7eaSMatthew G. Knepley 
66128e4ac7eaSMatthew G. Knepley   Input Parameter:
66138e4ac7eaSMatthew G. Knepley . dm - The DM
66148e4ac7eaSMatthew G. Knepley 
66158e4ac7eaSMatthew G. Knepley   Output Parameter:
66168e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
66178e4ac7eaSMatthew G. Knepley 
66188e4ac7eaSMatthew G. Knepley   Level: beginner
66198e4ac7eaSMatthew G. Knepley 
66208e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
66218e4ac7eaSMatthew G. Knepley @*/
66228e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
66238e4ac7eaSMatthew G. Knepley {
66248e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66258e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6626534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
66278e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66288e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66298e4ac7eaSMatthew G. Knepley }
66308e4ac7eaSMatthew G. Knepley 
66318e4ac7eaSMatthew G. Knepley /*@
66325d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
66338e4ac7eaSMatthew G. Knepley 
66348e4ac7eaSMatthew G. Knepley   Collective on dm
66358e4ac7eaSMatthew G. Knepley 
66368e4ac7eaSMatthew G. Knepley   Input Parameters:
66378e4ac7eaSMatthew G. Knepley + dm - The DM
66388e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
66398e4ac7eaSMatthew G. Knepley 
66405d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
66415d3b26e6SMatthew G. Knepley 
66428e4ac7eaSMatthew G. Knepley   Level: beginner
66438e4ac7eaSMatthew G. Knepley 
66445d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
66458e4ac7eaSMatthew G. Knepley @*/
66468e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
66478e4ac7eaSMatthew G. Knepley {
66488e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66498e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66508833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66518e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66528e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66538e4ac7eaSMatthew G. Knepley }
6654c58f1c22SToby Isaac 
6655c58f1c22SToby Isaac 
6656c58f1c22SToby Isaac /*@C
6657c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6658c58f1c22SToby Isaac 
6659c58f1c22SToby Isaac   Not Collective
6660c58f1c22SToby Isaac 
6661c58f1c22SToby Isaac   Input Parameters:
6662c58f1c22SToby Isaac + dm   - The DM object
6663c58f1c22SToby Isaac - name - The label name
6664c58f1c22SToby Isaac 
6665c58f1c22SToby Isaac   Level: intermediate
6666c58f1c22SToby Isaac 
6667c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6668c58f1c22SToby Isaac @*/
6669c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6670c58f1c22SToby Isaac {
66715d80c0bfSVaclav Hapla   PetscBool      flg;
66725d80c0bfSVaclav Hapla   DMLabel        label;
6673c58f1c22SToby Isaac   PetscErrorCode ierr;
6674c58f1c22SToby Isaac 
6675c58f1c22SToby Isaac   PetscFunctionBegin;
6676c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6677c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66785d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
6679c58f1c22SToby Isaac   if (!flg) {
66805d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
66815d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
66825d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
6683c58f1c22SToby Isaac   }
6684c58f1c22SToby Isaac   PetscFunctionReturn(0);
6685c58f1c22SToby Isaac }
6686c58f1c22SToby Isaac 
6687c58f1c22SToby Isaac /*@C
6688c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6689c58f1c22SToby Isaac 
6690c58f1c22SToby Isaac   Not Collective
6691c58f1c22SToby Isaac 
6692c58f1c22SToby Isaac   Input Parameters:
6693c58f1c22SToby Isaac + dm   - The DM object
6694c58f1c22SToby Isaac . name - The label name
6695c58f1c22SToby Isaac - point - The mesh point
6696c58f1c22SToby Isaac 
6697c58f1c22SToby Isaac   Output Parameter:
6698c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6699c58f1c22SToby Isaac 
6700c58f1c22SToby Isaac   Level: beginner
6701c58f1c22SToby Isaac 
6702c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6703c58f1c22SToby Isaac @*/
6704c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6705c58f1c22SToby Isaac {
6706c58f1c22SToby Isaac   DMLabel        label;
6707c58f1c22SToby Isaac   PetscErrorCode ierr;
6708c58f1c22SToby Isaac 
6709c58f1c22SToby Isaac   PetscFunctionBegin;
6710c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6711c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6712c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
671313903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6714c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6715c58f1c22SToby Isaac   PetscFunctionReturn(0);
6716c58f1c22SToby Isaac }
6717c58f1c22SToby Isaac 
6718c58f1c22SToby Isaac /*@C
6719c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6720c58f1c22SToby Isaac 
6721c58f1c22SToby Isaac   Not Collective
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac   Input Parameters:
6724c58f1c22SToby Isaac + dm   - The DM object
6725c58f1c22SToby Isaac . name - The label name
6726c58f1c22SToby Isaac . point - The mesh point
6727c58f1c22SToby Isaac - value - The label value for this point
6728c58f1c22SToby Isaac 
6729c58f1c22SToby Isaac   Output Parameter:
6730c58f1c22SToby Isaac 
6731c58f1c22SToby Isaac   Level: beginner
6732c58f1c22SToby Isaac 
6733c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6734c58f1c22SToby Isaac @*/
6735c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6736c58f1c22SToby Isaac {
6737c58f1c22SToby Isaac   DMLabel        label;
6738c58f1c22SToby Isaac   PetscErrorCode ierr;
6739c58f1c22SToby Isaac 
6740c58f1c22SToby Isaac   PetscFunctionBegin;
6741c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6742c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6743c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6744c58f1c22SToby Isaac   if (!label) {
6745c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6746c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6747c58f1c22SToby Isaac   }
6748c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6749c58f1c22SToby Isaac   PetscFunctionReturn(0);
6750c58f1c22SToby Isaac }
6751c58f1c22SToby Isaac 
6752c58f1c22SToby Isaac /*@C
6753c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6754c58f1c22SToby Isaac 
6755c58f1c22SToby Isaac   Not Collective
6756c58f1c22SToby Isaac 
6757c58f1c22SToby Isaac   Input Parameters:
6758c58f1c22SToby Isaac + dm   - The DM object
6759c58f1c22SToby Isaac . name - The label name
6760c58f1c22SToby Isaac . point - The mesh point
6761c58f1c22SToby Isaac - value - The label value for this point
6762c58f1c22SToby Isaac 
6763c58f1c22SToby Isaac   Output Parameter:
6764c58f1c22SToby Isaac 
6765c58f1c22SToby Isaac   Level: beginner
6766c58f1c22SToby Isaac 
6767c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6768c58f1c22SToby Isaac @*/
6769c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6770c58f1c22SToby Isaac {
6771c58f1c22SToby Isaac   DMLabel        label;
6772c58f1c22SToby Isaac   PetscErrorCode ierr;
6773c58f1c22SToby Isaac 
6774c58f1c22SToby Isaac   PetscFunctionBegin;
6775c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6776c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6777c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6778c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6779c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6780c58f1c22SToby Isaac   PetscFunctionReturn(0);
6781c58f1c22SToby Isaac }
6782c58f1c22SToby Isaac 
6783c58f1c22SToby Isaac /*@C
6784c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6785c58f1c22SToby Isaac 
6786c58f1c22SToby Isaac   Not Collective
6787c58f1c22SToby Isaac 
6788c58f1c22SToby Isaac   Input Parameters:
6789c58f1c22SToby Isaac + dm   - The DM object
6790c58f1c22SToby Isaac - name - The label name
6791c58f1c22SToby Isaac 
6792c58f1c22SToby Isaac   Output Parameter:
6793c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6794c58f1c22SToby Isaac 
6795c58f1c22SToby Isaac   Level: beginner
6796c58f1c22SToby Isaac 
6797df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6798c58f1c22SToby Isaac @*/
6799c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6800c58f1c22SToby Isaac {
6801c58f1c22SToby Isaac   DMLabel        label;
6802c58f1c22SToby Isaac   PetscErrorCode ierr;
6803c58f1c22SToby Isaac 
6804c58f1c22SToby Isaac   PetscFunctionBegin;
6805c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6806c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6807534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
6808c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6809c58f1c22SToby Isaac   *size = 0;
6810c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6811c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6812c58f1c22SToby Isaac   PetscFunctionReturn(0);
6813c58f1c22SToby Isaac }
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac /*@C
6816c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6817c58f1c22SToby Isaac 
6818c58f1c22SToby Isaac   Not Collective
6819c58f1c22SToby Isaac 
6820c58f1c22SToby Isaac   Input Parameters:
6821c58f1c22SToby Isaac + mesh - The DM object
6822c58f1c22SToby Isaac - name - The label name
6823c58f1c22SToby Isaac 
6824c58f1c22SToby Isaac   Output Parameter:
6825c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6826c58f1c22SToby Isaac 
6827c58f1c22SToby Isaac   Level: beginner
6828c58f1c22SToby Isaac 
6829c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6830c58f1c22SToby Isaac @*/
6831c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6832c58f1c22SToby Isaac {
6833c58f1c22SToby Isaac   DMLabel        label;
6834c58f1c22SToby Isaac   PetscErrorCode ierr;
6835c58f1c22SToby Isaac 
6836c58f1c22SToby Isaac   PetscFunctionBegin;
6837c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6838c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6839c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6840c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6841c58f1c22SToby Isaac   *ids = NULL;
6842dab2e251SBlaise Bourdin  if (label) {
6843c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6844dab2e251SBlaise Bourdin   } else {
6845dab2e251SBlaise Bourdin     /* returning an empty IS */
6846dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6847dab2e251SBlaise Bourdin   }
6848c58f1c22SToby Isaac   PetscFunctionReturn(0);
6849c58f1c22SToby Isaac }
6850c58f1c22SToby Isaac 
6851c58f1c22SToby Isaac /*@C
6852c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6853c58f1c22SToby Isaac 
6854c58f1c22SToby Isaac   Not Collective
6855c58f1c22SToby Isaac 
6856c58f1c22SToby Isaac   Input Parameters:
6857c58f1c22SToby Isaac + dm - The DM object
6858c58f1c22SToby Isaac . name - The label name
6859c58f1c22SToby Isaac - value - The stratum value
6860c58f1c22SToby Isaac 
6861c58f1c22SToby Isaac   Output Parameter:
6862c58f1c22SToby Isaac . size - The stratum size
6863c58f1c22SToby Isaac 
6864c58f1c22SToby Isaac   Level: beginner
6865c58f1c22SToby Isaac 
6866c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6867c58f1c22SToby Isaac @*/
6868c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6869c58f1c22SToby Isaac {
6870c58f1c22SToby Isaac   DMLabel        label;
6871c58f1c22SToby Isaac   PetscErrorCode ierr;
6872c58f1c22SToby Isaac 
6873c58f1c22SToby Isaac   PetscFunctionBegin;
6874c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6875c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6876534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
6877c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6878c58f1c22SToby Isaac   *size = 0;
6879c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6880c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6881c58f1c22SToby Isaac   PetscFunctionReturn(0);
6882c58f1c22SToby Isaac }
6883c58f1c22SToby Isaac 
6884c58f1c22SToby Isaac /*@C
6885c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6886c58f1c22SToby Isaac 
6887c58f1c22SToby Isaac   Not Collective
6888c58f1c22SToby Isaac 
6889c58f1c22SToby Isaac   Input Parameters:
6890c58f1c22SToby Isaac + dm - The DM object
6891c58f1c22SToby Isaac . name - The label name
6892c58f1c22SToby Isaac - value - The stratum value
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   Output Parameter:
6895c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6896c58f1c22SToby Isaac 
6897c58f1c22SToby Isaac   Level: beginner
6898c58f1c22SToby Isaac 
6899c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6900c58f1c22SToby Isaac @*/
6901c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6902c58f1c22SToby Isaac {
6903c58f1c22SToby Isaac   DMLabel        label;
6904c58f1c22SToby Isaac   PetscErrorCode ierr;
6905c58f1c22SToby Isaac 
6906c58f1c22SToby Isaac   PetscFunctionBegin;
6907c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6908c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6909c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6910c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6911c58f1c22SToby Isaac   *points = NULL;
6912c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6913c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6914c58f1c22SToby Isaac   PetscFunctionReturn(0);
6915c58f1c22SToby Isaac }
6916c58f1c22SToby Isaac 
69174de306b1SToby Isaac /*@C
69189044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69194de306b1SToby Isaac 
69204de306b1SToby Isaac   Not Collective
69214de306b1SToby Isaac 
69224de306b1SToby Isaac   Input Parameters:
69234de306b1SToby Isaac + dm - The DM object
69244de306b1SToby Isaac . name - The label name
69254de306b1SToby Isaac . value - The stratum value
69264de306b1SToby Isaac - points - The stratum points
69274de306b1SToby Isaac 
69284de306b1SToby Isaac   Level: beginner
69294de306b1SToby Isaac 
69304de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
69314de306b1SToby Isaac @*/
69324de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
69334de306b1SToby Isaac {
69344de306b1SToby Isaac   DMLabel        label;
69354de306b1SToby Isaac   PetscErrorCode ierr;
69364de306b1SToby Isaac 
69374de306b1SToby Isaac   PetscFunctionBegin;
69384de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69394de306b1SToby Isaac   PetscValidCharPointer(name, 2);
69404de306b1SToby Isaac   PetscValidPointer(points, 4);
69414de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
69424de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
69434de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
69444de306b1SToby Isaac   PetscFunctionReturn(0);
69454de306b1SToby Isaac }
69464de306b1SToby Isaac 
6947c58f1c22SToby Isaac /*@C
6948c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6949c58f1c22SToby Isaac 
6950c58f1c22SToby Isaac   Not Collective
6951c58f1c22SToby Isaac 
6952c58f1c22SToby Isaac   Input Parameters:
6953c58f1c22SToby Isaac + dm   - The DM object
6954c58f1c22SToby Isaac . name - The label name
6955c58f1c22SToby Isaac - value - The label value for this point
6956c58f1c22SToby Isaac 
6957c58f1c22SToby Isaac   Output Parameter:
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Level: beginner
6960c58f1c22SToby Isaac 
6961c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6962c58f1c22SToby Isaac @*/
6963c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6964c58f1c22SToby Isaac {
6965c58f1c22SToby Isaac   DMLabel        label;
6966c58f1c22SToby Isaac   PetscErrorCode ierr;
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   PetscFunctionBegin;
6969c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6970c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6971c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6972c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6973c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6974c58f1c22SToby Isaac   PetscFunctionReturn(0);
6975c58f1c22SToby Isaac }
6976c58f1c22SToby Isaac 
6977c58f1c22SToby Isaac /*@
6978c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6979c58f1c22SToby Isaac 
6980c58f1c22SToby Isaac   Not Collective
6981c58f1c22SToby Isaac 
6982c58f1c22SToby Isaac   Input Parameter:
6983c58f1c22SToby Isaac . dm   - The DM object
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac   Output Parameter:
6986c58f1c22SToby Isaac . numLabels - the number of Labels
6987c58f1c22SToby Isaac 
6988c58f1c22SToby Isaac   Level: intermediate
6989c58f1c22SToby Isaac 
6990c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6991c58f1c22SToby Isaac @*/
6992c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6993c58f1c22SToby Isaac {
69945d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6995c58f1c22SToby Isaac   PetscInt  n    = 0;
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac   PetscFunctionBegin;
6998c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6999534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7000c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7001c58f1c22SToby Isaac   *numLabels = n;
7002c58f1c22SToby Isaac   PetscFunctionReturn(0);
7003c58f1c22SToby Isaac }
7004c58f1c22SToby Isaac 
7005c58f1c22SToby Isaac /*@C
7006c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7007c58f1c22SToby Isaac 
7008c58f1c22SToby Isaac   Not Collective
7009c58f1c22SToby Isaac 
7010c58f1c22SToby Isaac   Input Parameters:
7011c58f1c22SToby Isaac + dm - The DM object
7012c58f1c22SToby Isaac - n  - the label number
7013c58f1c22SToby Isaac 
7014c58f1c22SToby Isaac   Output Parameter:
7015c58f1c22SToby Isaac . name - the label name
7016c58f1c22SToby Isaac 
7017c58f1c22SToby Isaac   Level: intermediate
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7020c58f1c22SToby Isaac @*/
7021c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7022c58f1c22SToby Isaac {
70235d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7024c58f1c22SToby Isaac   PetscInt       l    = 0;
7025d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7026c58f1c22SToby Isaac 
7027c58f1c22SToby Isaac   PetscFunctionBegin;
7028c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7029c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7030c58f1c22SToby Isaac   while (next) {
7031c58f1c22SToby Isaac     if (l == n) {
7032d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7033c58f1c22SToby Isaac       PetscFunctionReturn(0);
7034c58f1c22SToby Isaac     }
7035c58f1c22SToby Isaac     ++l;
7036c58f1c22SToby Isaac     next = next->next;
7037c58f1c22SToby Isaac   }
7038c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7039c58f1c22SToby Isaac }
7040c58f1c22SToby Isaac 
7041c58f1c22SToby Isaac /*@C
7042c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7043c58f1c22SToby Isaac 
7044c58f1c22SToby Isaac   Not Collective
7045c58f1c22SToby Isaac 
7046c58f1c22SToby Isaac   Input Parameters:
7047c58f1c22SToby Isaac + dm   - The DM object
7048c58f1c22SToby Isaac - name - The label name
7049c58f1c22SToby Isaac 
7050c58f1c22SToby Isaac   Output Parameter:
7051c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7052c58f1c22SToby Isaac 
7053c58f1c22SToby Isaac   Level: intermediate
7054c58f1c22SToby Isaac 
7055c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7056c58f1c22SToby Isaac @*/
7057c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7058c58f1c22SToby Isaac {
70595d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7060d67d17b1SMatthew G. Knepley   const char    *lname;
7061c58f1c22SToby Isaac   PetscErrorCode ierr;
7062c58f1c22SToby Isaac 
7063c58f1c22SToby Isaac   PetscFunctionBegin;
7064c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7065c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7066534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7067c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7068c58f1c22SToby Isaac   while (next) {
7069d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7070d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7071c58f1c22SToby Isaac     if (*hasLabel) break;
7072c58f1c22SToby Isaac     next = next->next;
7073c58f1c22SToby Isaac   }
7074c58f1c22SToby Isaac   PetscFunctionReturn(0);
7075c58f1c22SToby Isaac }
7076c58f1c22SToby Isaac 
7077c58f1c22SToby Isaac /*@C
7078c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7079c58f1c22SToby Isaac 
7080c58f1c22SToby Isaac   Not Collective
7081c58f1c22SToby Isaac 
7082c58f1c22SToby Isaac   Input Parameters:
7083c58f1c22SToby Isaac + dm   - The DM object
7084c58f1c22SToby Isaac - name - The label name
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac   Output Parameter:
7087c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7088c58f1c22SToby Isaac 
7089c58f1c22SToby Isaac   Level: intermediate
7090c58f1c22SToby Isaac 
7091c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7092c58f1c22SToby Isaac @*/
7093c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7094c58f1c22SToby Isaac {
70955d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7096c58f1c22SToby Isaac   PetscBool      hasLabel;
7097d67d17b1SMatthew G. Knepley   const char    *lname;
7098c58f1c22SToby Isaac   PetscErrorCode ierr;
7099c58f1c22SToby Isaac 
7100c58f1c22SToby Isaac   PetscFunctionBegin;
7101c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7102c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7103c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7104c58f1c22SToby Isaac   *label = NULL;
7105c58f1c22SToby Isaac   while (next) {
7106d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7107d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7108c58f1c22SToby Isaac     if (hasLabel) {
7109c58f1c22SToby Isaac       *label = next->label;
7110c58f1c22SToby Isaac       break;
7111c58f1c22SToby Isaac     }
7112c58f1c22SToby Isaac     next = next->next;
7113c58f1c22SToby Isaac   }
7114c58f1c22SToby Isaac   PetscFunctionReturn(0);
7115c58f1c22SToby Isaac }
7116c58f1c22SToby Isaac 
7117c58f1c22SToby Isaac /*@C
7118c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7119c58f1c22SToby Isaac 
7120c58f1c22SToby Isaac   Not Collective
7121c58f1c22SToby Isaac 
7122c58f1c22SToby Isaac   Input Parameters:
7123c58f1c22SToby Isaac + dm - The DM object
7124c58f1c22SToby Isaac - n  - the label number
7125c58f1c22SToby Isaac 
7126c58f1c22SToby Isaac   Output Parameter:
7127c58f1c22SToby Isaac . label - the label
7128c58f1c22SToby Isaac 
7129c58f1c22SToby Isaac   Level: intermediate
7130c58f1c22SToby Isaac 
7131c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7132c58f1c22SToby Isaac @*/
7133c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7134c58f1c22SToby Isaac {
71355d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7136c58f1c22SToby Isaac   PetscInt    l    = 0;
7137c58f1c22SToby Isaac 
7138c58f1c22SToby Isaac   PetscFunctionBegin;
7139c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7140c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7141c58f1c22SToby Isaac   while (next) {
7142c58f1c22SToby Isaac     if (l == n) {
7143c58f1c22SToby Isaac       *label = next->label;
7144c58f1c22SToby Isaac       PetscFunctionReturn(0);
7145c58f1c22SToby Isaac     }
7146c58f1c22SToby Isaac     ++l;
7147c58f1c22SToby Isaac     next = next->next;
7148c58f1c22SToby Isaac   }
7149c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7150c58f1c22SToby Isaac }
7151c58f1c22SToby Isaac 
7152c58f1c22SToby Isaac /*@C
7153c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7154c58f1c22SToby Isaac 
7155c58f1c22SToby Isaac   Not Collective
7156c58f1c22SToby Isaac 
7157c58f1c22SToby Isaac   Input Parameters:
7158c58f1c22SToby Isaac + dm   - The DM object
7159c58f1c22SToby Isaac - label - The DMLabel
7160c58f1c22SToby Isaac 
7161c58f1c22SToby Isaac   Level: developer
7162c58f1c22SToby Isaac 
7163c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7164c58f1c22SToby Isaac @*/
7165c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7166c58f1c22SToby Isaac {
71675d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7168c58f1c22SToby Isaac   PetscBool      hasLabel;
7169d67d17b1SMatthew G. Knepley   const char    *lname;
71705d80c0bfSVaclav Hapla   PetscBool      flg;
7171c58f1c22SToby Isaac   PetscErrorCode ierr;
7172c58f1c22SToby Isaac 
7173c58f1c22SToby Isaac   PetscFunctionBegin;
7174c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7175d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7176d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7177d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7178c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7179c58f1c22SToby Isaac   tmpLabel->label  = label;
7180c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
71815d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
71825d80c0bfSVaclav Hapla   *p = tmpLabel;
718308f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
71845d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
71855d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7186*ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7187*ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7188c58f1c22SToby Isaac   PetscFunctionReturn(0);
7189c58f1c22SToby Isaac }
7190c58f1c22SToby Isaac 
7191c58f1c22SToby Isaac /*@C
7192e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7193c58f1c22SToby Isaac 
7194c58f1c22SToby Isaac   Not Collective
7195c58f1c22SToby Isaac 
7196c58f1c22SToby Isaac   Input Parameters:
7197c58f1c22SToby Isaac + dm   - The DM object
7198c58f1c22SToby Isaac - name - The label name
7199c58f1c22SToby Isaac 
7200c58f1c22SToby Isaac   Output Parameter:
7201c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7202c58f1c22SToby Isaac 
7203c58f1c22SToby Isaac   Level: developer
7204c58f1c22SToby Isaac 
7205e5472504SVaclav Hapla   Notes:
7206e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7207e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7208e5472504SVaclav Hapla 
7209e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7210e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7211e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7212e5472504SVaclav Hapla 
7213e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7214c58f1c22SToby Isaac @*/
7215c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7216c58f1c22SToby Isaac {
721795d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7218c58f1c22SToby Isaac   PetscBool      hasLabel;
7219d67d17b1SMatthew G. Knepley   const char    *lname;
7220c58f1c22SToby Isaac   PetscErrorCode ierr;
7221c58f1c22SToby Isaac 
7222c58f1c22SToby Isaac   PetscFunctionBegin;
7223c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7224e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7225e5472504SVaclav Hapla   if (label) {
7226e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7227c58f1c22SToby Isaac     *label = NULL;
7228e5472504SVaclav Hapla   }
72295d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
723095d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7231d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7232c58f1c22SToby Isaac     if (hasLabel) {
723395d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7234c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
723595d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7236*ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7237*ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
723895d578d6SVaclav Hapla       if (label) *label = link->label;
723995d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
724095d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7241c58f1c22SToby Isaac       break;
7242c58f1c22SToby Isaac     }
7243c58f1c22SToby Isaac   }
7244c58f1c22SToby Isaac   PetscFunctionReturn(0);
7245c58f1c22SToby Isaac }
7246c58f1c22SToby Isaac 
7247306894acSVaclav Hapla /*@
7248306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7249306894acSVaclav Hapla 
7250306894acSVaclav Hapla   Not Collective
7251306894acSVaclav Hapla 
7252306894acSVaclav Hapla   Input Parameters:
7253306894acSVaclav Hapla + dm   - The DM object
7254306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7255306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7256306894acSVaclav Hapla 
7257306894acSVaclav Hapla   Level: developer
7258306894acSVaclav Hapla 
7259306894acSVaclav Hapla   Notes:
7260306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7261306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7262306894acSVaclav Hapla   *label nullified.
7263306894acSVaclav Hapla 
7264306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7265306894acSVaclav Hapla @*/
7266306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7267306894acSVaclav Hapla {
726843e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7269306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7270306894acSVaclav Hapla   PetscErrorCode ierr;
7271306894acSVaclav Hapla 
7272306894acSVaclav Hapla   PetscFunctionBegin;
7273306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7274306894acSVaclav Hapla   PetscValidPointer(label, 2);
7275f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7276306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7277306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
72785d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
727943e45a93SVaclav Hapla     if (*label == link->label) {
7280306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
728143e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7282306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7283*ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
728443e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
728543e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
728643e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7287306894acSVaclav Hapla       break;
7288306894acSVaclav Hapla     }
7289306894acSVaclav Hapla   }
7290306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7291306894acSVaclav Hapla   PetscFunctionReturn(0);
7292306894acSVaclav Hapla }
7293306894acSVaclav Hapla 
7294c58f1c22SToby Isaac /*@C
7295c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7296c58f1c22SToby Isaac 
7297c58f1c22SToby Isaac   Not Collective
7298c58f1c22SToby Isaac 
7299c58f1c22SToby Isaac   Input Parameters:
7300c58f1c22SToby Isaac + dm   - The DM object
7301c58f1c22SToby Isaac - name - The label name
7302c58f1c22SToby Isaac 
7303c58f1c22SToby Isaac   Output Parameter:
7304c58f1c22SToby Isaac . output - The flag for output
7305c58f1c22SToby Isaac 
7306c58f1c22SToby Isaac   Level: developer
7307c58f1c22SToby Isaac 
7308c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7309c58f1c22SToby Isaac @*/
7310c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7311c58f1c22SToby Isaac {
73125d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7313d67d17b1SMatthew G. Knepley   const char    *lname;
7314c58f1c22SToby Isaac   PetscErrorCode ierr;
7315c58f1c22SToby Isaac 
7316c58f1c22SToby Isaac   PetscFunctionBegin;
7317c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7318c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7319c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7320c58f1c22SToby Isaac   while (next) {
7321c58f1c22SToby Isaac     PetscBool flg;
7322c58f1c22SToby Isaac 
7323d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7324d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7325c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7326c58f1c22SToby Isaac     next = next->next;
7327c58f1c22SToby Isaac   }
7328c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7329c58f1c22SToby Isaac }
7330c58f1c22SToby Isaac 
7331c58f1c22SToby Isaac /*@C
7332c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7333c58f1c22SToby Isaac 
7334c58f1c22SToby Isaac   Not Collective
7335c58f1c22SToby Isaac 
7336c58f1c22SToby Isaac   Input Parameters:
7337c58f1c22SToby Isaac + dm     - The DM object
7338c58f1c22SToby Isaac . name   - The label name
7339c58f1c22SToby Isaac - output - The flag for output
7340c58f1c22SToby Isaac 
7341c58f1c22SToby Isaac   Level: developer
7342c58f1c22SToby Isaac 
7343c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7344c58f1c22SToby Isaac @*/
7345c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7346c58f1c22SToby Isaac {
73475d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7348d67d17b1SMatthew G. Knepley   const char    *lname;
7349c58f1c22SToby Isaac   PetscErrorCode ierr;
7350c58f1c22SToby Isaac 
7351c58f1c22SToby Isaac   PetscFunctionBegin;
7352c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7353534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7354c58f1c22SToby Isaac   while (next) {
7355c58f1c22SToby Isaac     PetscBool flg;
7356c58f1c22SToby Isaac 
7357d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7358d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7359c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7360c58f1c22SToby Isaac     next = next->next;
7361c58f1c22SToby Isaac   }
7362c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7363c58f1c22SToby Isaac }
7364c58f1c22SToby Isaac 
7365c58f1c22SToby Isaac /*@
7366c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7367c58f1c22SToby Isaac 
7368d083f849SBarry Smith   Collective on dmA
7369c58f1c22SToby Isaac 
7370c58f1c22SToby Isaac   Input Parameter:
73715d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
73722e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
73735d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7374*ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7375c58f1c22SToby Isaac 
7376c58f1c22SToby Isaac   Level: intermediate
7377c58f1c22SToby Isaac 
7378c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7379c58f1c22SToby Isaac 
73805d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7381c58f1c22SToby Isaac @*/
73825d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7383c58f1c22SToby Isaac {
7384c58f1c22SToby Isaac   DMLabel        label, labelNew;
7385c58f1c22SToby Isaac   const char    *name;
7386c58f1c22SToby Isaac   PetscBool      flg;
73875d80c0bfSVaclav Hapla   DMLabelLink    link;
73885d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7389c58f1c22SToby Isaac 
73905d80c0bfSVaclav Hapla   PetscFunctionBegin;
73915d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
73925d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
73935d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
73945d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
73955d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
73965d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
73975d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
73985d80c0bfSVaclav Hapla     label=link->label;
73995d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
74005d80c0bfSVaclav Hapla     if (!all) {
7401c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7402c58f1c22SToby Isaac       if (flg) continue;
74037d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
74047d5acc75SStefano Zampini       if (flg) continue;
7405*ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7406*ba2698f1SMatthew G. Knepley       if (flg) continue;
74075d80c0bfSVaclav Hapla     } else {
74085d80c0bfSVaclav Hapla       dmB->depthLabel    = dmA->depthLabel;
7409*ba2698f1SMatthew G. Knepley       dmB->celltypeLabel = dmA->celltypeLabel;
74105d80c0bfSVaclav Hapla     }
74115d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7412c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
74135d80c0bfSVaclav Hapla     } else {
74145d80c0bfSVaclav Hapla       labelNew = label;
74155d80c0bfSVaclav Hapla     }
7416c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
74175d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7418c58f1c22SToby Isaac   }
7419c58f1c22SToby Isaac   PetscFunctionReturn(0);
7420c58f1c22SToby Isaac }
7421a8fb8f29SToby Isaac 
7422a8fb8f29SToby Isaac /*@
7423a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7424a8fb8f29SToby Isaac 
7425a8fb8f29SToby Isaac   Input Parameter:
7426a8fb8f29SToby Isaac . dm - The DM object
7427a8fb8f29SToby Isaac 
7428a8fb8f29SToby Isaac   Output Parameter:
7429a8fb8f29SToby Isaac . cdm - The coarse DM
7430a8fb8f29SToby Isaac 
7431a8fb8f29SToby Isaac   Level: intermediate
7432a8fb8f29SToby Isaac 
7433a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7434a8fb8f29SToby Isaac @*/
7435a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7436a8fb8f29SToby Isaac {
7437a8fb8f29SToby Isaac   PetscFunctionBegin;
7438a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7439a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7440a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7441a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7442a8fb8f29SToby Isaac }
7443a8fb8f29SToby Isaac 
7444a8fb8f29SToby Isaac /*@
7445a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7446a8fb8f29SToby Isaac 
7447a8fb8f29SToby Isaac   Input Parameters:
7448a8fb8f29SToby Isaac + dm - The DM object
7449a8fb8f29SToby Isaac - cdm - The coarse DM
7450a8fb8f29SToby Isaac 
7451a8fb8f29SToby Isaac   Level: intermediate
7452a8fb8f29SToby Isaac 
7453a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7454a8fb8f29SToby Isaac @*/
7455a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7456a8fb8f29SToby Isaac {
7457a8fb8f29SToby Isaac   PetscErrorCode ierr;
7458a8fb8f29SToby Isaac 
7459a8fb8f29SToby Isaac   PetscFunctionBegin;
7460a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7461a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7462a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7463a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7464a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7465a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7466a8fb8f29SToby Isaac }
7467a8fb8f29SToby Isaac 
746888bdff64SToby Isaac /*@
746988bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
747088bdff64SToby Isaac 
747188bdff64SToby Isaac   Input Parameter:
747288bdff64SToby Isaac . dm - The DM object
747388bdff64SToby Isaac 
747488bdff64SToby Isaac   Output Parameter:
747588bdff64SToby Isaac . fdm - The fine DM
747688bdff64SToby Isaac 
747788bdff64SToby Isaac   Level: intermediate
747888bdff64SToby Isaac 
747988bdff64SToby Isaac .seealso: DMSetFineDM()
748088bdff64SToby Isaac @*/
748188bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
748288bdff64SToby Isaac {
748388bdff64SToby Isaac   PetscFunctionBegin;
748488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
748588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
748688bdff64SToby Isaac   *fdm = dm->fineMesh;
748788bdff64SToby Isaac   PetscFunctionReturn(0);
748888bdff64SToby Isaac }
748988bdff64SToby Isaac 
749088bdff64SToby Isaac /*@
749188bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
749288bdff64SToby Isaac 
749388bdff64SToby Isaac   Input Parameters:
749488bdff64SToby Isaac + dm - The DM object
749588bdff64SToby Isaac - fdm - The fine DM
749688bdff64SToby Isaac 
749788bdff64SToby Isaac   Level: intermediate
749888bdff64SToby Isaac 
749988bdff64SToby Isaac .seealso: DMGetFineDM()
750088bdff64SToby Isaac @*/
750188bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
750288bdff64SToby Isaac {
750388bdff64SToby Isaac   PetscErrorCode ierr;
750488bdff64SToby Isaac 
750588bdff64SToby Isaac   PetscFunctionBegin;
750688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
750788bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
750888bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
750988bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
751088bdff64SToby Isaac   dm->fineMesh = fdm;
751188bdff64SToby Isaac   PetscFunctionReturn(0);
751288bdff64SToby Isaac }
751388bdff64SToby Isaac 
7514a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7515a6ba4734SToby Isaac 
7516a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7517a6ba4734SToby Isaac {
7518e5e52638SMatthew G. Knepley   PetscInt       d;
7519a6ba4734SToby Isaac   PetscErrorCode ierr;
7520a6ba4734SToby Isaac 
7521a6ba4734SToby Isaac   PetscFunctionBegin;
7522e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7523e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7524e5e52638SMatthew G. Knepley   }
7525a6ba4734SToby Isaac   PetscFunctionReturn(0);
7526a6ba4734SToby Isaac }
7527a6ba4734SToby Isaac 
7528a6ba4734SToby Isaac /*@C
7529a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7530a6ba4734SToby Isaac 
7531a6ba4734SToby Isaac   Input Parameters:
75324c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7533f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7534a6ba4734SToby Isaac . name        - The BC name
7535a6ba4734SToby Isaac . labelname   - The label defining constrained points
7536a6ba4734SToby Isaac . field       - The field to constrain
7537e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7538a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7539a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7540a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7541a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7542a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7543a6ba4734SToby Isaac 
7544a6ba4734SToby Isaac   Options Database Keys:
7545a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7546a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7547a6ba4734SToby Isaac 
7548a6ba4734SToby Isaac   Level: developer
7549a6ba4734SToby Isaac 
7550a6ba4734SToby Isaac .seealso: DMGetBoundary()
7551a6ba4734SToby Isaac @*/
7552a30ec4eaSSatish Balay PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(void), PetscInt numids, const PetscInt *ids, void *ctx)
7553a6ba4734SToby Isaac {
7554e5e52638SMatthew G. Knepley   PetscDS        ds;
7555a6ba4734SToby Isaac   PetscErrorCode ierr;
7556a6ba4734SToby Isaac 
7557a6ba4734SToby Isaac   PetscFunctionBegin;
7558a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7559e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7560e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7561a6ba4734SToby Isaac   PetscFunctionReturn(0);
7562a6ba4734SToby Isaac }
7563a6ba4734SToby Isaac 
7564a6ba4734SToby Isaac /*@
7565a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7566a6ba4734SToby Isaac 
7567a6ba4734SToby Isaac   Input Parameters:
7568a6ba4734SToby Isaac . dm - The mesh object
7569a6ba4734SToby Isaac 
7570a6ba4734SToby Isaac   Output Parameters:
7571a6ba4734SToby Isaac . numBd - The number of BC
7572a6ba4734SToby Isaac 
7573a6ba4734SToby Isaac   Level: intermediate
7574a6ba4734SToby Isaac 
7575a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7576a6ba4734SToby Isaac @*/
7577a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7578a6ba4734SToby Isaac {
7579e5e52638SMatthew G. Knepley   PetscDS        ds;
758058ebd649SToby Isaac   PetscErrorCode ierr;
7581a6ba4734SToby Isaac 
7582a6ba4734SToby Isaac   PetscFunctionBegin;
7583a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7584e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7585e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7586a6ba4734SToby Isaac   PetscFunctionReturn(0);
7587a6ba4734SToby Isaac }
7588a6ba4734SToby Isaac 
7589a6ba4734SToby Isaac /*@C
75901c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7591a6ba4734SToby Isaac 
7592a6ba4734SToby Isaac   Input Parameters:
7593a6ba4734SToby Isaac + dm          - The mesh object
7594a6ba4734SToby Isaac - bd          - The BC number
7595a6ba4734SToby Isaac 
7596a6ba4734SToby Isaac   Output Parameters:
7597f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7598a6ba4734SToby Isaac . name        - The BC name
7599a6ba4734SToby Isaac . labelname   - The label defining constrained points
7600a6ba4734SToby Isaac . field       - The field to constrain
7601a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7602a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7603a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7604a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7605a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7606a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7607a6ba4734SToby Isaac 
7608a6ba4734SToby Isaac   Options Database Keys:
7609a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7610a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7611a6ba4734SToby Isaac 
7612a6ba4734SToby Isaac   Level: developer
7613a6ba4734SToby Isaac 
7614a6ba4734SToby Isaac .seealso: DMAddBoundary()
7615a6ba4734SToby Isaac @*/
7616a30ec4eaSSatish Balay PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(void), PetscInt *numids, const PetscInt **ids, void **ctx)
7617a6ba4734SToby Isaac {
7618e5e52638SMatthew G. Knepley   PetscDS        ds;
761958ebd649SToby Isaac   PetscErrorCode ierr;
7620a6ba4734SToby Isaac 
7621a6ba4734SToby Isaac   PetscFunctionBegin;
7622a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7623e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7624e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7625a6ba4734SToby Isaac   PetscFunctionReturn(0);
7626a6ba4734SToby Isaac }
7627a6ba4734SToby Isaac 
7628e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7629e6f8dbb6SToby Isaac {
7630e5e52638SMatthew G. Knepley   PetscDS        ds;
7631dff059c6SToby Isaac   DMBoundary    *lastnext;
7632e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7633e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7634e6f8dbb6SToby Isaac 
7635e6f8dbb6SToby Isaac   PetscFunctionBegin;
7636e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7637e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
763847a1f5adSToby Isaac   if (dm->boundary) {
763947a1f5adSToby Isaac     DMBoundary next = dm->boundary;
764047a1f5adSToby Isaac 
764147a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
764247a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
764347a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
764447a1f5adSToby Isaac     while (next) {
764547a1f5adSToby Isaac       DMBoundary b = next;
764647a1f5adSToby Isaac 
764747a1f5adSToby Isaac       next = b->next;
764847a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7649a6ba4734SToby Isaac     }
765047a1f5adSToby Isaac     dm->boundary = NULL;
7651a6ba4734SToby Isaac   }
765247a1f5adSToby Isaac 
7653dff059c6SToby Isaac   lastnext = &(dm->boundary);
7654e6f8dbb6SToby Isaac   while (dsbound) {
7655e6f8dbb6SToby Isaac     DMBoundary dmbound;
7656e6f8dbb6SToby Isaac 
7657e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7658e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7659e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7660994fe344SLisandro 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);}
766147a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7662dff059c6SToby Isaac     *lastnext = dmbound;
7663dff059c6SToby Isaac     lastnext = &(dmbound->next);
7664dff059c6SToby Isaac     dsbound = dsbound->next;
7665a6ba4734SToby Isaac   }
7666a6ba4734SToby Isaac   PetscFunctionReturn(0);
7667a6ba4734SToby Isaac }
7668a6ba4734SToby Isaac 
7669a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7670a6ba4734SToby Isaac {
7671b95f2879SToby Isaac   DMBoundary     b;
7672a6ba4734SToby Isaac   PetscErrorCode ierr;
7673a6ba4734SToby Isaac 
7674a6ba4734SToby Isaac   PetscFunctionBegin;
7675a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7676534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7677a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7678e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7679b95f2879SToby Isaac   b = dm->boundary;
7680a6ba4734SToby Isaac   while (b && !(*isBd)) {
7681e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7682e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
76833424c85cSToby Isaac 
76843424c85cSToby Isaac     if (label) {
7685a6ba4734SToby Isaac       PetscInt i;
7686a6ba4734SToby Isaac 
7687e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7688e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7689a6ba4734SToby Isaac       }
7690a6ba4734SToby Isaac     }
7691a6ba4734SToby Isaac     b = b->next;
7692a6ba4734SToby Isaac   }
7693a6ba4734SToby Isaac   PetscFunctionReturn(0);
7694a6ba4734SToby Isaac }
76954d6f44ffSToby Isaac 
76964d6f44ffSToby Isaac /*@C
7697a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
7698a6e0b375SMatthew G. Knepley 
7699a6e0b375SMatthew G. Knepley   Collective on DM
77004d6f44ffSToby Isaac 
77014d6f44ffSToby Isaac   Input Parameters:
77024d6f44ffSToby Isaac + dm      - The DM
77030709b2feSToby Isaac . time    - The time
77044d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
77054d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
77064d6f44ffSToby Isaac - mode    - The insertion mode for values
77074d6f44ffSToby Isaac 
77084d6f44ffSToby Isaac   Output Parameter:
77094d6f44ffSToby Isaac . X - vector
77104d6f44ffSToby Isaac 
77114d6f44ffSToby Isaac    Calling sequence of func:
77120709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
77134d6f44ffSToby Isaac 
77144d6f44ffSToby Isaac +  dim - The spatial dimension
77154d6f44ffSToby Isaac .  x   - The coordinates
77164d6f44ffSToby Isaac .  Nf  - The number of fields
77174d6f44ffSToby Isaac .  u   - The output field values
77184d6f44ffSToby Isaac -  ctx - optional user-defined function context
77194d6f44ffSToby Isaac 
77204d6f44ffSToby Isaac   Level: developer
77214d6f44ffSToby Isaac 
7722a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
77234d6f44ffSToby Isaac @*/
77240709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
77254d6f44ffSToby Isaac {
77264d6f44ffSToby Isaac   Vec            localX;
77274d6f44ffSToby Isaac   PetscErrorCode ierr;
77284d6f44ffSToby Isaac 
77294d6f44ffSToby Isaac   PetscFunctionBegin;
77304d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77314d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
77320709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77334d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
77344d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
77354d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
77364d6f44ffSToby Isaac   PetscFunctionReturn(0);
77374d6f44ffSToby Isaac }
77384d6f44ffSToby Isaac 
7739a6e0b375SMatthew G. Knepley /*@C
7740a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
7741a6e0b375SMatthew G. Knepley 
7742a6e0b375SMatthew G. Knepley   Not collective
7743a6e0b375SMatthew G. Knepley 
7744a6e0b375SMatthew G. Knepley   Input Parameters:
7745a6e0b375SMatthew G. Knepley + dm      - The DM
7746a6e0b375SMatthew G. Knepley . time    - The time
7747a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7748a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7749a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7750a6e0b375SMatthew G. Knepley 
7751a6e0b375SMatthew G. Knepley   Output Parameter:
7752a6e0b375SMatthew G. Knepley . localX - vector
7753a6e0b375SMatthew G. Knepley 
7754a6e0b375SMatthew G. Knepley    Calling sequence of func:
7755a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7756a6e0b375SMatthew G. Knepley 
7757a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7758a6e0b375SMatthew G. Knepley .  x   - The coordinates
7759a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7760a6e0b375SMatthew G. Knepley .  u   - The output field values
7761a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7762a6e0b375SMatthew G. Knepley 
7763a6e0b375SMatthew G. Knepley   Level: developer
7764a6e0b375SMatthew G. Knepley 
7765a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
7766a6e0b375SMatthew G. Knepley @*/
77670709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
77684d6f44ffSToby Isaac {
77694d6f44ffSToby Isaac   PetscErrorCode ierr;
77704d6f44ffSToby Isaac 
77714d6f44ffSToby Isaac   PetscFunctionBegin;
77724d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77734d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
77740918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
77750709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77764d6f44ffSToby Isaac   PetscFunctionReturn(0);
77774d6f44ffSToby Isaac }
77784d6f44ffSToby Isaac 
7779a6e0b375SMatthew G. Knepley /*@C
7780a6e0b375SMatthew 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.
7781a6e0b375SMatthew G. Knepley 
7782a6e0b375SMatthew G. Knepley   Collective on DM
7783a6e0b375SMatthew G. Knepley 
7784a6e0b375SMatthew G. Knepley   Input Parameters:
7785a6e0b375SMatthew G. Knepley + dm      - The DM
7786a6e0b375SMatthew G. Knepley . time    - The time
7787a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7788a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7789a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7790a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7791a6e0b375SMatthew G. Knepley 
7792a6e0b375SMatthew G. Knepley   Output Parameter:
7793a6e0b375SMatthew G. Knepley . X - vector
7794a6e0b375SMatthew G. Knepley 
7795a6e0b375SMatthew G. Knepley    Calling sequence of func:
7796a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7797a6e0b375SMatthew G. Knepley 
7798a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7799a6e0b375SMatthew G. Knepley .  x   - The coordinates
7800a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7801a6e0b375SMatthew G. Knepley .  u   - The output field values
7802a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7803a6e0b375SMatthew G. Knepley 
7804a6e0b375SMatthew G. Knepley   Level: developer
7805a6e0b375SMatthew G. Knepley 
7806a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
7807a6e0b375SMatthew G. Knepley @*/
78082c53366bSMatthew 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)
78092c53366bSMatthew G. Knepley {
78102c53366bSMatthew G. Knepley   Vec            localX;
78112c53366bSMatthew G. Knepley   PetscErrorCode ierr;
78122c53366bSMatthew G. Knepley 
78132c53366bSMatthew G. Knepley   PetscFunctionBegin;
78142c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78152c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
78162c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
78172c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
78182c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
78192c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
78202c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
78212c53366bSMatthew G. Knepley }
78222c53366bSMatthew G. Knepley 
7823a6e0b375SMatthew G. Knepley /*@C
7824a6e0b375SMatthew 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.
7825a6e0b375SMatthew G. Knepley 
7826a6e0b375SMatthew G. Knepley   Not collective
7827a6e0b375SMatthew G. Knepley 
7828a6e0b375SMatthew G. Knepley   Input Parameters:
7829a6e0b375SMatthew G. Knepley + dm      - The DM
7830a6e0b375SMatthew G. Knepley . time    - The time
7831a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7832a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7833a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7834a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7835a6e0b375SMatthew G. Knepley 
7836a6e0b375SMatthew G. Knepley   Output Parameter:
7837a6e0b375SMatthew G. Knepley . localX - vector
7838a6e0b375SMatthew G. Knepley 
7839a6e0b375SMatthew G. Knepley    Calling sequence of func:
7840a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7841a6e0b375SMatthew G. Knepley 
7842a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7843a6e0b375SMatthew G. Knepley .  x   - The coordinates
7844a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7845a6e0b375SMatthew G. Knepley .  u   - The output field values
7846a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7847a6e0b375SMatthew G. Knepley 
7848a6e0b375SMatthew G. Knepley   Level: developer
7849a6e0b375SMatthew G. Knepley 
7850a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
7851a6e0b375SMatthew G. Knepley @*/
78521c531cf8SMatthew 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)
78534d6f44ffSToby Isaac {
78544d6f44ffSToby Isaac   PetscErrorCode ierr;
78554d6f44ffSToby Isaac 
78564d6f44ffSToby Isaac   PetscFunctionBegin;
78574d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78584d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
78590918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
78601c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
78614d6f44ffSToby Isaac   PetscFunctionReturn(0);
78624d6f44ffSToby Isaac }
78632716604bSToby Isaac 
7864a6e0b375SMatthew G. Knepley /*@C
7865a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
7866a6e0b375SMatthew G. Knepley 
7867a6e0b375SMatthew G. Knepley   Not collective
7868a6e0b375SMatthew G. Knepley 
7869a6e0b375SMatthew G. Knepley   Input Parameters:
7870a6e0b375SMatthew G. Knepley + dm      - The DM
7871a6e0b375SMatthew G. Knepley . time    - The time
7872a6e0b375SMatthew G. Knepley . localU  - The input field vector
7873a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7874a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7875a6e0b375SMatthew G. Knepley 
7876a6e0b375SMatthew G. Knepley   Output Parameter:
7877a6e0b375SMatthew G. Knepley . localX  - The output vector
7878a6e0b375SMatthew G. Knepley 
7879a6e0b375SMatthew G. Knepley    Calling sequence of func:
7880a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7881a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7882a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7883a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7884a6e0b375SMatthew G. Knepley 
7885a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7886a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7887a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7888a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7889a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7890a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7891a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7892a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7893a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7894a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7895a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7896a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7897a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7898a6e0b375SMatthew G. Knepley .  t            - The current time
7899a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7900a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7901a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7902a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7903a6e0b375SMatthew G. Knepley 
7904a6e0b375SMatthew 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.
7905a6e0b375SMatthew 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
7906a6e0b375SMatthew 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
7907a6e0b375SMatthew 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.
7908a6e0b375SMatthew G. Knepley 
7909a6e0b375SMatthew G. Knepley   Level: intermediate
7910a6e0b375SMatthew G. Knepley 
7911a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
7912a6e0b375SMatthew G. Knepley @*/
79138c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
79148c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
79158c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79168c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7917191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
79188c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
79198c6c5593SMatthew G. Knepley {
79208c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
79218c6c5593SMatthew G. Knepley 
79228c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79238c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79248c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
79258c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
79260918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
79278c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
79288c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
79298c6c5593SMatthew G. Knepley }
79308c6c5593SMatthew G. Knepley 
7931a6e0b375SMatthew G. Knepley /*@C
7932a6e0b375SMatthew 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.
7933a6e0b375SMatthew G. Knepley 
7934a6e0b375SMatthew G. Knepley   Not collective
7935a6e0b375SMatthew G. Knepley 
7936a6e0b375SMatthew G. Knepley   Input Parameters:
7937a6e0b375SMatthew G. Knepley + dm      - The DM
7938a6e0b375SMatthew G. Knepley . time    - The time
7939a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
7940a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
7941a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
7942a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
7943a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7944a6e0b375SMatthew G. Knepley . localU  - The input field vector
7945a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7946a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7947a6e0b375SMatthew G. Knepley 
7948a6e0b375SMatthew G. Knepley   Output Parameter:
7949a6e0b375SMatthew G. Knepley . localX  - The output vector
7950a6e0b375SMatthew G. Knepley 
7951a6e0b375SMatthew G. Knepley    Calling sequence of func:
7952a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7953a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7954a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7955a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7956a6e0b375SMatthew G. Knepley 
7957a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7958a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7959a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7960a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7961a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7962a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7963a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7964a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7965a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7966a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7967a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7968a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7969a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7970a6e0b375SMatthew G. Knepley .  t            - The current time
7971a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7972a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7973a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7974a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7975a6e0b375SMatthew G. Knepley 
7976a6e0b375SMatthew 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.
7977a6e0b375SMatthew 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
7978a6e0b375SMatthew 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
7979a6e0b375SMatthew 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.
7980a6e0b375SMatthew G. Knepley 
7981a6e0b375SMatthew G. Knepley   Level: intermediate
7982a6e0b375SMatthew G. Knepley 
7983a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
7984a6e0b375SMatthew G. Knepley @*/
79851c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
79868c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
79878c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79888c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7989191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
79908c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
79918c6c5593SMatthew G. Knepley {
79928c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
79938c6c5593SMatthew G. Knepley 
79948c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79958c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79968c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
79978c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
79980918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
79991c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
80008c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
80018c6c5593SMatthew G. Knepley }
80028c6c5593SMatthew G. Knepley 
80032716604bSToby Isaac /*@C
80042716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
80052716604bSToby Isaac 
80062716604bSToby Isaac   Input Parameters:
80072716604bSToby Isaac + dm    - The DM
80080709b2feSToby Isaac . time  - The time
80092716604bSToby Isaac . funcs - The functions to evaluate for each field component
80102716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8011574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
80122716604bSToby Isaac 
80132716604bSToby Isaac   Output Parameter:
80142716604bSToby Isaac . diff - The diff ||u - u_h||_2
80152716604bSToby Isaac 
80162716604bSToby Isaac   Level: developer
80172716604bSToby Isaac 
80181189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
80192716604bSToby Isaac @*/
80200709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
80212716604bSToby Isaac {
80222716604bSToby Isaac   PetscErrorCode ierr;
80232716604bSToby Isaac 
80242716604bSToby Isaac   PetscFunctionBegin;
80252716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8026b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80270918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
80280709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
80292716604bSToby Isaac   PetscFunctionReturn(0);
80302716604bSToby Isaac }
8031b698f381SToby Isaac 
8032b698f381SToby Isaac /*@C
8033b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8034b698f381SToby Isaac 
8035d083f849SBarry Smith   Collective on dm
8036d083f849SBarry Smith 
8037b698f381SToby Isaac   Input Parameters:
8038b698f381SToby Isaac + dm    - The DM
8039b698f381SToby Isaac , time  - The time
8040b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8041b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8042574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8043b698f381SToby Isaac - n     - The vector to project along
8044b698f381SToby Isaac 
8045b698f381SToby Isaac   Output Parameter:
8046b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8047b698f381SToby Isaac 
8048b698f381SToby Isaac   Level: developer
8049b698f381SToby Isaac 
8050b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8051b698f381SToby Isaac @*/
8052b698f381SToby 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)
8053b698f381SToby Isaac {
8054b698f381SToby Isaac   PetscErrorCode ierr;
8055b698f381SToby Isaac 
8056b698f381SToby Isaac   PetscFunctionBegin;
8057b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8058b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8059b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8060b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8061b698f381SToby Isaac   PetscFunctionReturn(0);
8062b698f381SToby Isaac }
8063b698f381SToby Isaac 
80642a16baeaSToby Isaac /*@C
80652a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
80662a16baeaSToby Isaac 
8067d083f849SBarry Smith   Collective on dm
8068d083f849SBarry Smith 
80692a16baeaSToby Isaac   Input Parameters:
80702a16baeaSToby Isaac + dm    - The DM
80712a16baeaSToby Isaac . time  - The time
80722a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
80732a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8074574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
80752a16baeaSToby Isaac 
80762a16baeaSToby Isaac   Output Parameter:
80772a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
80782a16baeaSToby Isaac 
80792a16baeaSToby Isaac   Level: developer
80802a16baeaSToby Isaac 
80811189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
80822a16baeaSToby Isaac @*/
80831189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
80842a16baeaSToby Isaac {
80852a16baeaSToby Isaac   PetscErrorCode ierr;
80862a16baeaSToby Isaac 
80872a16baeaSToby Isaac   PetscFunctionBegin;
80882a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80892a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80900918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
80912a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
80922a16baeaSToby Isaac   PetscFunctionReturn(0);
80932a16baeaSToby Isaac }
80942a16baeaSToby Isaac 
8095df0b854cSToby Isaac /*@C
8096df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8097cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8098df0b854cSToby Isaac 
8099df0b854cSToby Isaac   Collective on dm
8100df0b854cSToby Isaac 
8101df0b854cSToby Isaac   Input parameters:
8102df0b854cSToby Isaac + dm - the pre-adaptation DM object
8103a1b0c543SToby Isaac - label - label with the flags
8104df0b854cSToby Isaac 
8105df0b854cSToby Isaac   Output parameters:
81060d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8107df0b854cSToby Isaac 
8108df0b854cSToby Isaac   Level: intermediate
81090d1cd5e0SMatthew G. Knepley 
81100d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8111df0b854cSToby Isaac @*/
81120d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8113df0b854cSToby Isaac {
8114df0b854cSToby Isaac   PetscErrorCode ierr;
8115df0b854cSToby Isaac 
8116df0b854cSToby Isaac   PetscFunctionBegin;
8117df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8118a1b0c543SToby Isaac   PetscValidPointer(label,2);
81190d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
81200d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
81216f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
81220d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
81230d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
81240d1cd5e0SMatthew G. Knepley }
81250d1cd5e0SMatthew G. Knepley 
81260d1cd5e0SMatthew G. Knepley /*@C
81270d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
81280d1cd5e0SMatthew G. Knepley 
81290d1cd5e0SMatthew G. Knepley   Input Parameters:
81300d1cd5e0SMatthew G. Knepley + dm - The DM object
81310d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
81326f25b0d8SLisandro 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_".
81330d1cd5e0SMatthew G. Knepley 
81340d1cd5e0SMatthew G. Knepley   Output Parameter:
81350d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
81360d1cd5e0SMatthew G. Knepley 
81370d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
81380d1cd5e0SMatthew G. Knepley 
81390d1cd5e0SMatthew G. Knepley   Level: advanced
81400d1cd5e0SMatthew G. Knepley 
81410d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
81420d1cd5e0SMatthew G. Knepley @*/
81430d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
81440d1cd5e0SMatthew G. Knepley {
81450d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
81460d1cd5e0SMatthew G. Knepley 
81470d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
81480d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81490d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
81500d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
81510d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
81526f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
81536f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
81540d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8155df0b854cSToby Isaac   PetscFunctionReturn(0);
8156df0b854cSToby Isaac }
8157c4088d22SMatthew G. Knepley 
8158502a2867SDave May /*@C
8159502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8160502a2867SDave May 
8161502a2867SDave May  Not Collective
8162502a2867SDave May 
8163502a2867SDave May  Input Parameter:
8164502a2867SDave May  . dm    - The DM
8165502a2867SDave May 
8166502a2867SDave May  Output Parameter:
8167502a2867SDave May  . nranks - the number of neighbours
8168502a2867SDave May  . ranks - the neighbors ranks
8169502a2867SDave May 
8170502a2867SDave May  Notes:
8171502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8172502a2867SDave May 
8173502a2867SDave May  Level: beginner
8174502a2867SDave May 
8175dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8176502a2867SDave May @*/
8177502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8178502a2867SDave May {
8179502a2867SDave May   PetscErrorCode ierr;
8180502a2867SDave May 
8181502a2867SDave May   PetscFunctionBegin;
8182502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81830918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8184502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8185502a2867SDave May   PetscFunctionReturn(0);
8186502a2867SDave May }
8187502a2867SDave May 
8188531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8189531c7667SBarry Smith 
8190531c7667SBarry Smith /*
8191531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8192531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8193531c7667SBarry Smith */
8194531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8195531c7667SBarry Smith {
8196531c7667SBarry Smith   PetscErrorCode ierr;
8197531c7667SBarry Smith 
8198531c7667SBarry Smith   PetscFunctionBegin;
8199531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8200531c7667SBarry Smith     Vec x1local;
8201531c7667SBarry Smith     DM  dm;
8202531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8203531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8204531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8205531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8206531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8207531c7667SBarry Smith     x1   = x1local;
8208531c7667SBarry Smith   }
8209531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8210531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8211531c7667SBarry Smith     DM  dm;
8212531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8213531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8214531c7667SBarry Smith   }
8215531c7667SBarry Smith   PetscFunctionReturn(0);
8216531c7667SBarry Smith }
8217531c7667SBarry Smith 
8218531c7667SBarry Smith /*@
8219531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8220531c7667SBarry Smith 
8221531c7667SBarry Smith     Input Parameter:
8222531c7667SBarry Smith .    coloring - the MatFDColoring object
8223531c7667SBarry Smith 
822495452b02SPatrick Sanan     Developer Notes:
822595452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8226531c7667SBarry Smith 
82271b266c99SBarry Smith     Level: advanced
82281b266c99SBarry Smith 
8229531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8230531c7667SBarry Smith @*/
8231531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8232531c7667SBarry Smith {
8233531c7667SBarry Smith   PetscFunctionBegin;
8234531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8235531c7667SBarry Smith   PetscFunctionReturn(0);
8236531c7667SBarry Smith }
82378320bc6fSPatrick Sanan 
82388320bc6fSPatrick Sanan /*@
82398320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
82408320bc6fSPatrick Sanan 
82418320bc6fSPatrick Sanan     Collective
82428320bc6fSPatrick Sanan 
82438320bc6fSPatrick Sanan     Input Parameters:
82448320bc6fSPatrick Sanan +    dm - the first DM
82458320bc6fSPatrick Sanan -    dm2 - the second DM
82468320bc6fSPatrick Sanan 
82478320bc6fSPatrick Sanan     Output Parameters:
82488320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
82498320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
82508320bc6fSPatrick Sanan 
82518320bc6fSPatrick Sanan     Notes:
82528320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
82533d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
82548320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
82553d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
82563d862458SPatrick Sanan     decomposition, but hold different data.
82578320bc6fSPatrick Sanan 
82588320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
82598320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
82608320bc6fSPatrick Sanan 
82618320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
82628320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
82638320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
82648320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
82658320bc6fSPatrick Sanan 
82668320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
82678320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
82688320bc6fSPatrick Sanan .vb
82698320bc6fSPatrick Sanan   ...
82708320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
82718320bc6fSPatrick Sanan   if (set && compatible)  {
82728320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
82738320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
82743d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
82758320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
82768320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
82778320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
82788320bc6fSPatrick Sanan       }
82798320bc6fSPatrick Sanan     }
82808320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
82818320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
82828320bc6fSPatrick Sanan   } else {
82838320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
82848320bc6fSPatrick Sanan   }
82858320bc6fSPatrick Sanan   ...
82868320bc6fSPatrick Sanan .ve
82878320bc6fSPatrick Sanan 
82888320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
82898320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
82908320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
82918320bc6fSPatrick Sanan     always check the "set" output parameter.
82928320bc6fSPatrick Sanan 
82938320bc6fSPatrick Sanan     A DM is always compatible with itself.
82948320bc6fSPatrick Sanan 
82958320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
82968320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
82978320bc6fSPatrick Sanan     incompatible.
82988320bc6fSPatrick Sanan 
82998320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
83008320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
83018320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
83028320bc6fSPatrick Sanan 
83038320bc6fSPatrick Sanan     Developer Notes:
83043d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
83053d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
83068320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
83078320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
83088320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
83093d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
83103d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
83118320bc6fSPatrick Sanan 
83128320bc6fSPatrick Sanan     Level: advanced
83138320bc6fSPatrick Sanan 
83143d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
83158320bc6fSPatrick Sanan @*/
83168320bc6fSPatrick Sanan 
83178320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
83188320bc6fSPatrick Sanan {
83198320bc6fSPatrick Sanan   PetscErrorCode ierr;
83208320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
83218320bc6fSPatrick Sanan   DMType         type,type2;
83228320bc6fSPatrick Sanan   PetscBool      sameType;
83238320bc6fSPatrick Sanan 
83248320bc6fSPatrick Sanan   PetscFunctionBegin;
83258320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83268320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83278320bc6fSPatrick Sanan 
83288320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
83298320bc6fSPatrick Sanan   if (dm == dm2) {
83308320bc6fSPatrick Sanan     *set = PETSC_TRUE;
83318320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
83328320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83338320bc6fSPatrick Sanan   }
83348320bc6fSPatrick Sanan 
83358320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
83368320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
83378320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
83388320bc6fSPatrick Sanan      determined by the implementation-specific logic */
83398320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
83408320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
83418320bc6fSPatrick Sanan     *set = PETSC_TRUE;
83428320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
83438320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83448320bc6fSPatrick Sanan   }
83458320bc6fSPatrick Sanan 
83468320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
83478320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
83488320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
8349b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
83508320bc6fSPatrick Sanan   }
83518320bc6fSPatrick Sanan 
83528320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
83538320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
83548320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
83558320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
83568320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
83578320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
83588320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
83598320bc6fSPatrick Sanan   } else {
83608320bc6fSPatrick Sanan     *set = PETSC_FALSE;
83618320bc6fSPatrick Sanan   }
83628320bc6fSPatrick Sanan   PetscFunctionReturn(0);
83638320bc6fSPatrick Sanan }
8364c0f0dcc3SMatthew G. Knepley 
8365c0f0dcc3SMatthew G. Knepley /*@C
8366c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8367c0f0dcc3SMatthew G. Knepley 
8368c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8369c0f0dcc3SMatthew G. Knepley 
8370c0f0dcc3SMatthew G. Knepley   Input Parameters:
8371c0f0dcc3SMatthew G. Knepley + DM - the DM
8372c0f0dcc3SMatthew G. Knepley . f - the monitor function
8373c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8374c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8375c0f0dcc3SMatthew G. Knepley 
8376c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8377c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8378c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8379c0f0dcc3SMatthew G. Knepley 
8380c0f0dcc3SMatthew G. Knepley   Notes:
8381c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8382c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8383c0f0dcc3SMatthew G. Knepley   order in which they were set.
8384c0f0dcc3SMatthew G. Knepley 
8385c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8386c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8387c0f0dcc3SMatthew G. Knepley 
8388c0f0dcc3SMatthew G. Knepley   Level: intermediate
8389c0f0dcc3SMatthew G. Knepley 
8390c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8391c0f0dcc3SMatthew G. Knepley @*/
8392c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8393c0f0dcc3SMatthew G. Knepley {
8394c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8395c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8396c0f0dcc3SMatthew G. Knepley 
8397c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8398c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8399c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8400c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8401c0f0dcc3SMatthew G. Knepley 
8402c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8403c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8404c0f0dcc3SMatthew G. Knepley   }
8405c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8406c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8407c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8408c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8409c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8410c0f0dcc3SMatthew G. Knepley }
8411c0f0dcc3SMatthew G. Knepley 
8412c0f0dcc3SMatthew G. Knepley /*@
8413c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8414c0f0dcc3SMatthew G. Knepley 
8415c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8416c0f0dcc3SMatthew G. Knepley 
8417c0f0dcc3SMatthew G. Knepley   Input Parameter:
8418c0f0dcc3SMatthew G. Knepley . dm - the DM
8419c0f0dcc3SMatthew G. Knepley 
8420c0f0dcc3SMatthew G. Knepley   Options Database Key:
8421c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8422c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8423c0f0dcc3SMatthew G. Knepley   set via the options database
8424c0f0dcc3SMatthew G. Knepley 
8425c0f0dcc3SMatthew G. Knepley   Notes:
8426c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8427c0f0dcc3SMatthew G. Knepley 
8428c0f0dcc3SMatthew G. Knepley   Level: intermediate
8429c0f0dcc3SMatthew G. Knepley 
8430c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8431c0f0dcc3SMatthew G. Knepley @*/
8432c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8433c0f0dcc3SMatthew G. Knepley {
8434c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8435c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8436c0f0dcc3SMatthew G. Knepley 
8437c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8438c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8439c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8440c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
8441c0f0dcc3SMatthew G. Knepley   }
8442c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8443c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8444c0f0dcc3SMatthew G. Knepley }
8445c0f0dcc3SMatthew G. Knepley 
8446c0f0dcc3SMatthew G. Knepley /*@C
8447c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8448c0f0dcc3SMatthew G. Knepley 
8449c0f0dcc3SMatthew G. Knepley   Collective on DM
8450c0f0dcc3SMatthew G. Knepley 
8451c0f0dcc3SMatthew G. Knepley   Input Parameters:
8452c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8453c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8454c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8455c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8456c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8457c0f0dcc3SMatthew 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
8458c0f0dcc3SMatthew G. Knepley 
8459c0f0dcc3SMatthew G. Knepley   Output Parameter:
8460c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8461c0f0dcc3SMatthew G. Knepley 
8462c0f0dcc3SMatthew G. Knepley   Level: developer
8463c0f0dcc3SMatthew G. Knepley 
8464c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
8465c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
8466c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
8467c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
8468c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
8469c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
8470c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
8471c0f0dcc3SMatthew G. Knepley @*/
8472c0f0dcc3SMatthew 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)
8473c0f0dcc3SMatthew G. Knepley {
8474c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8475c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8476c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
8477c0f0dcc3SMatthew G. Knepley 
8478c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8479c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8480c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
8481c0f0dcc3SMatthew G. Knepley   if (*flg) {
8482c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8483c0f0dcc3SMatthew G. Knepley 
8484c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
8485c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
8486c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
8487c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
8488c0f0dcc3SMatthew G. Knepley   }
8489c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8490c0f0dcc3SMatthew G. Knepley }
8491c0f0dcc3SMatthew G. Knepley 
8492c0f0dcc3SMatthew G. Knepley /*@
8493c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8494c0f0dcc3SMatthew G. Knepley 
8495c0f0dcc3SMatthew G. Knepley    Collective on DM
8496c0f0dcc3SMatthew G. Knepley 
8497c0f0dcc3SMatthew G. Knepley    Input Parameters:
8498c0f0dcc3SMatthew G. Knepley .  dm - The DM
8499c0f0dcc3SMatthew G. Knepley 
8500c0f0dcc3SMatthew G. Knepley    Level: developer
8501c0f0dcc3SMatthew G. Knepley 
8502c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8503c0f0dcc3SMatthew G. Knepley @*/
8504c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8505c0f0dcc3SMatthew G. Knepley {
8506c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8507c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8508c0f0dcc3SMatthew G. Knepley 
8509c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8510c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8511c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8512c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8513c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
8514c0f0dcc3SMatthew G. Knepley   }
8515c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8516c0f0dcc3SMatthew G. Knepley }
8517