xref: /petsc/src/dm/interface/dm.c (revision 6eb26441ea522d27d4d83b27f7c78c629ba0baf4)
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};
15ba2698f1SMatthew 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;
58ba2698f1SMatthew 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 
1021cb8cacdSPatrick Sanan   Notes:
1031cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1041cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1051cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1061bb6d2a8SBarry Smith 
10789706ed2SPatrick Sanan   The clone is considered set up iff the original is.
10889706ed2SPatrick Sanan 
10992cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1101bb6d2a8SBarry Smith 
11138221697SMatthew G. Knepley @*/
11238221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11338221697SMatthew G. Knepley {
11438221697SMatthew G. Knepley   PetscSF        sf;
11538221697SMatthew G. Knepley   Vec            coords;
11638221697SMatthew G. Knepley   void          *ctx;
117a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11838221697SMatthew G. Knepley   PetscErrorCode ierr;
11938221697SMatthew G. Knepley 
12038221697SMatthew G. Knepley   PetscFunctionBegin;
12138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12238221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12338221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1245d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
125ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
126ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1271de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1281de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12938221697SMatthew G. Knepley   if (dm->ops->clone) {
13038221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13138221697SMatthew G. Knepley   }
1323f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13338221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13438221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13638221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
137be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
138be4c1c3eSMatthew G. Knepley     DM           ncdm;
139be4c1c3eSMatthew G. Knepley     PetscSection cs;
1405a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
141be4c1c3eSMatthew G. Knepley 
14292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
143be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1445a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1455a0206caSToby Isaac     if (pEndMax >= 0) {
146be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
14783bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
14892fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
149a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
150be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
151be4c1c3eSMatthew G. Knepley     }
152be4c1c3eSMatthew G. Knepley   }
153a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
154a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15538221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15638221697SMatthew G. Knepley   if (coords) {
15738221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15838221697SMatthew G. Knepley   } else {
15938221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16038221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16138221697SMatthew G. Knepley   }
16290b157c4SStefano Zampini   {
16390b157c4SStefano Zampini     PetscBool             isper;
164c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1655dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16690b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16790b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
168c6b900c6SMatthew G. Knepley   }
16934aa8a36SMatthew G. Knepley   {
17034aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17134aa8a36SMatthew G. Knepley 
17234aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17434aa8a36SMatthew G. Knepley   }
17538221697SMatthew G. Knepley   PetscFunctionReturn(0);
17638221697SMatthew G. Knepley }
17738221697SMatthew G. Knepley 
1789a42bb27SBarry Smith /*@C
179564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1809a42bb27SBarry Smith 
181d083f849SBarry Smith    Logically Collective on da
1829a42bb27SBarry Smith 
1839a42bb27SBarry Smith    Input Parameter:
1849a42bb27SBarry Smith +  da - initial distributed array
185e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith    Options Database:
188dd85299cSBarry Smith .   -dm_vec_type ctype
1899a42bb27SBarry Smith 
1909a42bb27SBarry Smith    Level: intermediate
1919a42bb27SBarry Smith 
192a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1939a42bb27SBarry Smith @*/
19419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1959a42bb27SBarry Smith {
1969a42bb27SBarry Smith   PetscErrorCode ierr;
1979a42bb27SBarry Smith 
1989a42bb27SBarry Smith   PetscFunctionBegin;
1999a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2009a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2029a42bb27SBarry Smith   PetscFunctionReturn(0);
2039a42bb27SBarry Smith }
2049a42bb27SBarry Smith 
205c0dedaeaSBarry Smith /*@C
206c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
207c0dedaeaSBarry Smith 
208d083f849SBarry Smith    Logically Collective on da
209c0dedaeaSBarry Smith 
210c0dedaeaSBarry Smith    Input Parameter:
211c0dedaeaSBarry Smith .  da - initial distributed array
212c0dedaeaSBarry Smith 
213c0dedaeaSBarry Smith    Output Parameter:
214c0dedaeaSBarry Smith .  ctype - the vector type
215c0dedaeaSBarry Smith 
216c0dedaeaSBarry Smith    Level: intermediate
217c0dedaeaSBarry Smith 
218a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
219c0dedaeaSBarry Smith @*/
220c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
221c0dedaeaSBarry Smith {
222c0dedaeaSBarry Smith   PetscFunctionBegin;
223c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
224c0dedaeaSBarry Smith   *ctype = da->vectype;
225c0dedaeaSBarry Smith   PetscFunctionReturn(0);
226c0dedaeaSBarry Smith }
227c0dedaeaSBarry Smith 
2285f1ad066SMatthew G Knepley /*@
22934f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley   Not collective
2325f1ad066SMatthew G Knepley 
2335f1ad066SMatthew G Knepley   Input Parameter:
2345f1ad066SMatthew G Knepley . v - The Vec
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   Output Parameter:
2375f1ad066SMatthew G Knepley . dm - The DM
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley   Level: intermediate
2405f1ad066SMatthew G Knepley 
2415f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2425f1ad066SMatthew G Knepley @*/
2435f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2445f1ad066SMatthew G Knepley {
2455f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2465f1ad066SMatthew G Knepley 
2475f1ad066SMatthew G Knepley   PetscFunctionBegin;
2485f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2495f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2505f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2515f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2525f1ad066SMatthew G Knepley }
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley /*@
255d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2565f1ad066SMatthew G Knepley 
2575f1ad066SMatthew G Knepley   Not collective
2585f1ad066SMatthew G Knepley 
2595f1ad066SMatthew G Knepley   Input Parameters:
2605f1ad066SMatthew G Knepley + v - The Vec
2615f1ad066SMatthew G Knepley - dm - The DM
2625f1ad066SMatthew G Knepley 
263d9805387SMatthew 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.
264d9805387SMatthew G. Knepley 
2655f1ad066SMatthew G Knepley   Level: intermediate
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2685f1ad066SMatthew G Knepley @*/
2695f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2705f1ad066SMatthew G Knepley {
2715f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley   PetscFunctionBegin;
2745f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
275d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2765f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2775f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2785f1ad066SMatthew G Knepley }
2795f1ad066SMatthew G Knepley 
280521d9a4cSLisandro Dalcin /*@C
2818f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2828f1509bcSBarry Smith 
283d083f849SBarry Smith    Logically Collective on dm
2848f1509bcSBarry Smith 
2858f1509bcSBarry Smith    Input Parameters:
2868f1509bcSBarry Smith +  dm - the DM context
2878f1509bcSBarry Smith -  ctype - the matrix type
2888f1509bcSBarry Smith 
2898f1509bcSBarry Smith    Options Database:
2908f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2918f1509bcSBarry Smith 
2928f1509bcSBarry Smith    Level: intermediate
2938f1509bcSBarry Smith 
2948f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2958f1509bcSBarry Smith           DMGetISColoringType()
2968f1509bcSBarry Smith @*/
2978f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2988f1509bcSBarry Smith {
2998f1509bcSBarry Smith   PetscFunctionBegin;
3008f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3018f1509bcSBarry Smith   dm->coloringtype = ctype;
3028f1509bcSBarry Smith   PetscFunctionReturn(0);
3038f1509bcSBarry Smith }
3048f1509bcSBarry Smith 
3058f1509bcSBarry Smith /*@C
3068f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
307521d9a4cSLisandro Dalcin 
308d083f849SBarry Smith    Logically Collective on dm
309521d9a4cSLisandro Dalcin 
310521d9a4cSLisandro Dalcin    Input Parameter:
3118f1509bcSBarry Smith .  dm - the DM context
3128f1509bcSBarry Smith 
3138f1509bcSBarry Smith    Output Parameter:
3148f1509bcSBarry Smith .  ctype - the matrix type
3158f1509bcSBarry Smith 
3168f1509bcSBarry Smith    Options Database:
3178f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3188f1509bcSBarry Smith 
3198f1509bcSBarry Smith    Level: intermediate
3208f1509bcSBarry Smith 
3218f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3228f1509bcSBarry Smith           DMGetISColoringType()
3238f1509bcSBarry Smith @*/
3248f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3258f1509bcSBarry Smith {
3268f1509bcSBarry Smith   PetscFunctionBegin;
3278f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3288f1509bcSBarry Smith   *ctype = dm->coloringtype;
3298f1509bcSBarry Smith   PetscFunctionReturn(0);
3308f1509bcSBarry Smith }
3318f1509bcSBarry Smith 
3328f1509bcSBarry Smith /*@C
3338f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3348f1509bcSBarry Smith 
335d083f849SBarry Smith    Logically Collective on dm
3368f1509bcSBarry Smith 
3378f1509bcSBarry Smith    Input Parameters:
338521d9a4cSLisandro Dalcin +  dm - the DM context
339a2b5a043SBarry Smith -  ctype - the matrix type
340521d9a4cSLisandro Dalcin 
341521d9a4cSLisandro Dalcin    Options Database:
342521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
343521d9a4cSLisandro Dalcin 
344521d9a4cSLisandro Dalcin    Level: intermediate
345521d9a4cSLisandro Dalcin 
346a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
347521d9a4cSLisandro Dalcin @*/
34819fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
349521d9a4cSLisandro Dalcin {
350521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35188f0584fSBarry Smith 
352521d9a4cSLisandro Dalcin   PetscFunctionBegin;
353521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
354521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
356521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
357521d9a4cSLisandro Dalcin }
358521d9a4cSLisandro Dalcin 
359c0dedaeaSBarry Smith /*@C
360c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
361c0dedaeaSBarry Smith 
362d083f849SBarry Smith    Logically Collective on dm
363c0dedaeaSBarry Smith 
364c0dedaeaSBarry Smith    Input Parameter:
365c0dedaeaSBarry Smith .  dm - the DM context
366c0dedaeaSBarry Smith 
367c0dedaeaSBarry Smith    Output Parameter:
368c0dedaeaSBarry Smith .  ctype - the matrix type
369c0dedaeaSBarry Smith 
370c0dedaeaSBarry Smith    Options Database:
371c0dedaeaSBarry Smith .   -dm_mat_type ctype
372c0dedaeaSBarry Smith 
373c0dedaeaSBarry Smith    Level: intermediate
374c0dedaeaSBarry Smith 
375a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
376c0dedaeaSBarry Smith @*/
377c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
378c0dedaeaSBarry Smith {
379c0dedaeaSBarry Smith   PetscFunctionBegin;
380c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
381c0dedaeaSBarry Smith   *ctype = dm->mattype;
382c0dedaeaSBarry Smith   PetscFunctionReturn(0);
383c0dedaeaSBarry Smith }
384c0dedaeaSBarry Smith 
385c688c046SMatthew G Knepley /*@
38634f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
387c688c046SMatthew G Knepley 
388c688c046SMatthew G Knepley   Not collective
389c688c046SMatthew G Knepley 
390c688c046SMatthew G Knepley   Input Parameter:
391c688c046SMatthew G Knepley . A - The Mat
392c688c046SMatthew G Knepley 
393c688c046SMatthew G Knepley   Output Parameter:
394c688c046SMatthew G Knepley . dm - The DM
395c688c046SMatthew G Knepley 
396c688c046SMatthew G Knepley   Level: intermediate
397c688c046SMatthew G Knepley 
3988f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3998f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4008f1509bcSBarry Smith 
401c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
402c688c046SMatthew G Knepley @*/
403c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
404c688c046SMatthew G Knepley {
405c688c046SMatthew G Knepley   PetscErrorCode ierr;
406c688c046SMatthew G Knepley 
407c688c046SMatthew G Knepley   PetscFunctionBegin;
408c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
409c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
410c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
411c688c046SMatthew G Knepley   PetscFunctionReturn(0);
412c688c046SMatthew G Knepley }
413c688c046SMatthew G Knepley 
414c688c046SMatthew G Knepley /*@
415c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
416c688c046SMatthew G Knepley 
417c688c046SMatthew G Knepley   Not collective
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley   Input Parameters:
420c688c046SMatthew G Knepley + A - The Mat
421c688c046SMatthew G Knepley - dm - The DM
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   Level: intermediate
424c688c046SMatthew G Knepley 
4258f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4268f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4278f1509bcSBarry Smith 
4288f1509bcSBarry Smith 
429c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
430c688c046SMatthew G Knepley @*/
431c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
432c688c046SMatthew G Knepley {
433c688c046SMatthew G Knepley   PetscErrorCode ierr;
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   PetscFunctionBegin;
436c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4378865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
438c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
439c688c046SMatthew G Knepley   PetscFunctionReturn(0);
440c688c046SMatthew G Knepley }
441c688c046SMatthew G Knepley 
4429a42bb27SBarry Smith /*@C
4439a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4446757b960SDave May    DM options in the database.
4459a42bb27SBarry Smith 
446d083f849SBarry Smith    Logically Collective on dm
4479a42bb27SBarry Smith 
4489a42bb27SBarry Smith    Input Parameter:
4498353ddbbSDave May +  da - the DM context
4509a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4519a42bb27SBarry Smith 
4529a42bb27SBarry Smith    Notes:
4539a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4549a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4559a42bb27SBarry Smith 
4569a42bb27SBarry Smith    Level: advanced
4579a42bb27SBarry Smith 
4589a42bb27SBarry Smith .seealso: DMSetFromOptions()
4599a42bb27SBarry Smith @*/
4607087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4619a42bb27SBarry Smith {
4629a42bb27SBarry Smith   PetscErrorCode ierr;
4639a42bb27SBarry Smith 
4649a42bb27SBarry Smith   PetscFunctionBegin;
4659a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4669a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
467691be533SLawrence Mitchell   if (dm->sf) {
468691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
469691be533SLawrence Mitchell   }
4701bb6d2a8SBarry Smith   if (dm->sectionSF) {
4711bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
472691be533SLawrence Mitchell   }
4739a42bb27SBarry Smith   PetscFunctionReturn(0);
4749a42bb27SBarry Smith }
4759a42bb27SBarry Smith 
47631697293SDave May /*@C
47731697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47831697293SDave May    DM options in the database.
47931697293SDave May 
480d083f849SBarry Smith    Logically Collective on dm
48131697293SDave May 
48231697293SDave May    Input Parameters:
48331697293SDave May +  dm - the DM context
48431697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48531697293SDave May 
48631697293SDave May    Notes:
48731697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48831697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48931697293SDave May 
49031697293SDave May    Level: advanced
49131697293SDave May 
49231697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49331697293SDave May @*/
49431697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49531697293SDave May {
49631697293SDave May   PetscErrorCode ierr;
49731697293SDave May 
49831697293SDave May   PetscFunctionBegin;
49931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50031697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50131697293SDave May   PetscFunctionReturn(0);
50231697293SDave May }
50331697293SDave May 
50431697293SDave May /*@C
50531697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50631697293SDave May    DM options in the database.
50731697293SDave May 
50831697293SDave May    Not Collective
50931697293SDave May 
51031697293SDave May    Input Parameters:
51131697293SDave May .  dm - the DM context
51231697293SDave May 
51331697293SDave May    Output Parameters:
51431697293SDave May .  prefix - pointer to the prefix string used is returned
51531697293SDave May 
51695452b02SPatrick Sanan    Notes:
51795452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51831697293SDave May    sufficient length to hold the prefix.
51931697293SDave May 
52031697293SDave May    Level: advanced
52131697293SDave May 
52231697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52331697293SDave May @*/
52431697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52531697293SDave May {
52631697293SDave May   PetscErrorCode ierr;
52731697293SDave May 
52831697293SDave May   PetscFunctionBegin;
52931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53031697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53131697293SDave May   PetscFunctionReturn(0);
53231697293SDave May }
53331697293SDave May 
53488bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53588bdff64SToby Isaac {
536*6eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
53788bdff64SToby Isaac   PetscErrorCode ierr;
53888bdff64SToby Isaac 
53988bdff64SToby Isaac   PetscFunctionBegin;
540aab5bcd8SJed Brown   *ncrefct = 0;
54188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54288bdff64SToby Isaac     refct--;
54388bdff64SToby Isaac     if (recurseCoarse) {
54488bdff64SToby Isaac       PetscInt coarseCount;
54588bdff64SToby Isaac 
54688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
54788bdff64SToby Isaac       refct += coarseCount;
54888bdff64SToby Isaac     }
54988bdff64SToby Isaac   }
55088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55188bdff64SToby Isaac     refct--;
55288bdff64SToby Isaac     if (recurseFine) {
55388bdff64SToby Isaac       PetscInt fineCount;
55488bdff64SToby Isaac 
55588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
55688bdff64SToby Isaac       refct += fineCount;
55788bdff64SToby Isaac     }
55888bdff64SToby Isaac   }
55988bdff64SToby Isaac   *ncrefct = refct;
56088bdff64SToby Isaac   PetscFunctionReturn(0);
56188bdff64SToby Isaac }
56288bdff64SToby Isaac 
563f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
564354557abSToby Isaac {
5655d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
566354557abSToby Isaac   PetscErrorCode ierr;
567354557abSToby Isaac 
568354557abSToby Isaac   PetscFunctionBegin;
569354557abSToby Isaac   /* destroy the labels */
570354557abSToby Isaac   while (next) {
571354557abSToby Isaac     DMLabelLink tmp = next->next;
572354557abSToby Isaac 
5735d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
574ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
575354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
576354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
577354557abSToby Isaac     next = tmp;
578354557abSToby Isaac   }
5795d80c0bfSVaclav Hapla   dm->labels = NULL;
580354557abSToby Isaac   PetscFunctionReturn(0);
581354557abSToby Isaac }
582354557abSToby Isaac 
58347c6ae99SBarry Smith /*@
5848472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
58547c6ae99SBarry Smith 
586d083f849SBarry Smith     Collective on dm
58747c6ae99SBarry Smith 
58847c6ae99SBarry Smith     Input Parameter:
58947c6ae99SBarry Smith .   dm - the DM object to destroy
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith     Level: developer
59247c6ae99SBarry Smith 
593e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith @*/
596fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
59747c6ae99SBarry Smith {
598*6eb26441SStefano Zampini   PetscInt       cnt;
599dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60047c6ae99SBarry Smith   PetscErrorCode ierr;
60147c6ae99SBarry Smith 
60247c6ae99SBarry Smith   PetscFunctionBegin;
6036bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6046bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
60587e657c6SBarry Smith 
60688bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
60788bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
60888bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
60988bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
6106bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6116bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
612*6eb26441SStefano Zampini 
613*6eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
614*6eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
615*6eb26441SStefano Zampini 
616f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6170298fd71SBarry Smith   (*dm)->namedglobal = NULL;
618f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6192348bcf4SPeter Brune     nnext = nlink->next;
6202348bcf4SPeter 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);
6212348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6222348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6232348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6242348bcf4SPeter Brune   }
625f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6260298fd71SBarry Smith   (*dm)->namedlocal = NULL;
627f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
628f490541aSPeter Brune     nnext = nlink->next;
629f490541aSPeter 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);
630f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
631f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
632f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
633f490541aSPeter Brune   }
6342348bcf4SPeter Brune 
635b17ce1afSJed Brown   /* Destroy the list of hooks */
636c833c3b5SJed Brown   {
637c833c3b5SJed Brown     DMCoarsenHookLink link,next;
638b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
639b17ce1afSJed Brown       next = link->next;
640b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
641b17ce1afSJed Brown     }
6420298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
643c833c3b5SJed Brown   }
644c833c3b5SJed Brown   {
645c833c3b5SJed Brown     DMRefineHookLink link,next;
646c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
647c833c3b5SJed Brown       next = link->next;
648c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
649c833c3b5SJed Brown     }
6500298fd71SBarry Smith     (*dm)->refinehook = NULL;
651c833c3b5SJed Brown   }
652be081cd6SPeter Brune   {
653be081cd6SPeter Brune     DMSubDomainHookLink link,next;
654be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
655be081cd6SPeter Brune       next = link->next;
656be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
657be081cd6SPeter Brune     }
6580298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
659be081cd6SPeter Brune   }
660baf369e7SPeter Brune   {
661baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
662baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
663baf369e7SPeter Brune       next = link->next;
664baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
665baf369e7SPeter Brune     }
6660298fd71SBarry Smith     (*dm)->gtolhook = NULL;
667baf369e7SPeter Brune   }
668d4d07f1eSToby Isaac   {
669d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
670d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
671d4d07f1eSToby Isaac       next = link->next;
672d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
673d4d07f1eSToby Isaac     }
674d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
675d4d07f1eSToby Isaac   }
676aa1993deSMatthew G Knepley   /* Destroy the work arrays */
677aa1993deSMatthew G Knepley   {
678aa1993deSMatthew G Knepley     DMWorkLink link,next;
679aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
680aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
681aa1993deSMatthew G Knepley       next = link->next;
682aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
683aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
684aa1993deSMatthew G Knepley     }
6850298fd71SBarry Smith     (*dm)->workin = NULL;
686aa1993deSMatthew G Knepley   }
687c58f1c22SToby Isaac   /* destroy the labels */
688f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
689f4cdcedcSVaclav Hapla   /* destroy the fields */
690e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
691f4cdcedcSVaclav Hapla   /* destroy the boundaries */
692e6f8dbb6SToby Isaac   {
693e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
694e6f8dbb6SToby Isaac     while (next) {
695e6f8dbb6SToby Isaac       DMBoundary b = next;
696e6f8dbb6SToby Isaac 
697e6f8dbb6SToby Isaac       next = b->next;
698e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
699e6f8dbb6SToby Isaac     }
700e6f8dbb6SToby Isaac   }
701b17ce1afSJed Brown 
70252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
70352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
70452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
70552536dc3SBarry Smith 
7061a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7071a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7081a266240SBarry Smith   }
70971cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7106bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7116bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
712073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
71388ed4aceSMatthew G Knepley 
7141bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7151bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7168b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
717fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
718fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
71988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7201bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
721736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
722736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7238e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
724736995cdSBlaise Bourdin     }
725736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
726736995cdSBlaise Bourdin   }
72788bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
72888bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
72988bdff64SToby Isaac   }
730*6eb26441SStefano Zampini 
731a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
73288bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
73388bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
73488bdff64SToby Isaac   }
73588bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
736f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7376636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7386636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7396636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7405dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
741ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
742ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
743ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7446636e97aSMatthew G Knepley 
745e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
74614f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
747e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
748e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
749732e2eb9SMatthew G Knepley 
750ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7516bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
752ed3c66a1SDave May   }
753c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
754435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
755732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
75647c6ae99SBarry Smith   PetscFunctionReturn(0);
75747c6ae99SBarry Smith }
75847c6ae99SBarry Smith 
759d7bf68aeSBarry Smith /*@
760d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
761d7bf68aeSBarry Smith 
762d083f849SBarry Smith     Collective on dm
763d7bf68aeSBarry Smith 
764d7bf68aeSBarry Smith     Input Parameter:
765d7bf68aeSBarry Smith .   dm - the DM object to setup
766d7bf68aeSBarry Smith 
767d7bf68aeSBarry Smith     Level: developer
768d7bf68aeSBarry Smith 
769e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
770d7bf68aeSBarry Smith 
771d7bf68aeSBarry Smith @*/
7727087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
773d7bf68aeSBarry Smith {
774d7bf68aeSBarry Smith   PetscErrorCode ierr;
775d7bf68aeSBarry Smith 
776d7bf68aeSBarry Smith   PetscFunctionBegin;
777171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7788387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
779d7bf68aeSBarry Smith   if (dm->ops->setup) {
780d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
781d7bf68aeSBarry Smith   }
7828387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
783d7bf68aeSBarry Smith   PetscFunctionReturn(0);
784d7bf68aeSBarry Smith }
785d7bf68aeSBarry Smith 
786d7bf68aeSBarry Smith /*@
787d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
788d7bf68aeSBarry Smith 
789d083f849SBarry Smith     Collective on dm
790d7bf68aeSBarry Smith 
791d7bf68aeSBarry Smith     Input Parameter:
792d7bf68aeSBarry Smith .   dm - the DM object to set options for
793d7bf68aeSBarry Smith 
794732e2eb9SMatthew G Knepley     Options Database:
7954164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7964164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7974164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7988f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
799732e2eb9SMatthew G Knepley 
800384a6580SVaclav Hapla     DMPLEX Specific Checks
801384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
802384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
803384a6580SVaclav 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()
804384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
805384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
806384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
807384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
808d7bf68aeSBarry Smith 
80995eb5ee5SVaclav Hapla     Level: intermediate
81095eb5ee5SVaclav Hapla 
81195eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
81295eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
813d7bf68aeSBarry Smith 
814d7bf68aeSBarry Smith @*/
8157087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
816d7bf68aeSBarry Smith {
8177781c08eSBarry Smith   char           typeName[256];
818ca266f36SBarry Smith   PetscBool      flg;
819d7bf68aeSBarry Smith   PetscErrorCode ierr;
820d7bf68aeSBarry Smith 
821d7bf68aeSBarry Smith   PetscFunctionBegin;
822171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82349be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
82449be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8251bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8263194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8270298fd71SBarry 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);
828a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
829f9ba7244SBarry Smith   if (flg) {
830f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
831f9ba7244SBarry Smith   }
832a264d7a6SBarry 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);
833073dac72SJed Brown   if (flg) {
834521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
835073dac72SJed Brown   }
8368f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
837f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
838e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
839f9ba7244SBarry Smith   }
840f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8410633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
84282fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
843d7bf68aeSBarry Smith   PetscFunctionReturn(0);
844d7bf68aeSBarry Smith }
845d7bf68aeSBarry Smith 
846fc9bc008SSatish Balay /*@C
847fe2efc57SMark    DMViewFromOptions - View from Options
848fe2efc57SMark 
849fe2efc57SMark    Collective on DM
850fe2efc57SMark 
851fe2efc57SMark    Input Parameters:
852fe2efc57SMark +  dm - the DM object
853736c3998SJose E. Roman .  obj - Optional object
854736c3998SJose E. Roman -  name - command line option
855fe2efc57SMark 
856fe2efc57SMark    Level: intermediate
857fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
858fe2efc57SMark @*/
859fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
860fe2efc57SMark {
861fe2efc57SMark   PetscErrorCode ierr;
862fe2efc57SMark 
863fe2efc57SMark   PetscFunctionBegin;
864fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
865fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
866fe2efc57SMark   PetscFunctionReturn(0);
867fe2efc57SMark }
868fe2efc57SMark 
869fe2efc57SMark /*@C
870224748a4SBarry Smith     DMView - Views a DM
87147c6ae99SBarry Smith 
872d083f849SBarry Smith     Collective on dm
87347c6ae99SBarry Smith 
87447c6ae99SBarry Smith     Input Parameter:
87547c6ae99SBarry Smith +   dm - the DM object to view
87647c6ae99SBarry Smith -   v - the viewer
87747c6ae99SBarry Smith 
878224748a4SBarry Smith     Level: beginner
87947c6ae99SBarry Smith 
880e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith @*/
8837087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
88447c6ae99SBarry Smith {
88547c6ae99SBarry Smith   PetscErrorCode    ierr;
88632c0f0efSBarry Smith   PetscBool         isbinary;
88776a8abe0SBarry Smith   PetscMPIInt       size;
88876a8abe0SBarry Smith   PetscViewerFormat format;
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith   PetscFunctionBegin;
891171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8923014e516SBarry Smith   if (!v) {
893ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8943014e516SBarry Smith   }
895b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
89674903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
89774903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
89874903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
89974903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
90074903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
90174903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
90274903a4fSStefano Zampini      in an error here */
90374903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9048bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
905b1b135c8SBarry Smith 
90676a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
90776a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
90876a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
90998c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
91032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
91132c0f0efSBarry Smith   if (isbinary) {
91255849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
91332c0f0efSBarry Smith     char     type[256];
91432c0f0efSBarry Smith 
91532c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
91632c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
91732c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
91832c0f0efSBarry Smith   }
9190c010503SBarry Smith   if (dm->ops->view) {
9200c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
92147c6ae99SBarry Smith   }
92247c6ae99SBarry Smith   PetscFunctionReturn(0);
92347c6ae99SBarry Smith }
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith /*@
9268472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
92747c6ae99SBarry Smith 
928d083f849SBarry Smith     Collective on dm
92947c6ae99SBarry Smith 
93047c6ae99SBarry Smith     Input Parameter:
93147c6ae99SBarry Smith .   dm - the DM object
93247c6ae99SBarry Smith 
93347c6ae99SBarry Smith     Output Parameter:
93447c6ae99SBarry Smith .   vec - the global vector
93547c6ae99SBarry Smith 
936073dac72SJed Brown     Level: beginner
93747c6ae99SBarry Smith 
938e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith @*/
9417087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
94247c6ae99SBarry Smith {
94347c6ae99SBarry Smith   PetscErrorCode ierr;
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith   PetscFunctionBegin;
946171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
947b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
948b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
94947c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
95047c6ae99SBarry Smith   PetscFunctionReturn(0);
95147c6ae99SBarry Smith }
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith /*@
9548472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
95547c6ae99SBarry Smith 
95647c6ae99SBarry Smith     Not Collective
95747c6ae99SBarry Smith 
95847c6ae99SBarry Smith     Input Parameter:
95947c6ae99SBarry Smith .   dm - the DM object
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith     Output Parameter:
96247c6ae99SBarry Smith .   vec - the local vector
96347c6ae99SBarry Smith 
964073dac72SJed Brown     Level: beginner
96547c6ae99SBarry Smith 
966e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
96747c6ae99SBarry Smith 
96847c6ae99SBarry Smith @*/
9697087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
97047c6ae99SBarry Smith {
97147c6ae99SBarry Smith   PetscErrorCode ierr;
97247c6ae99SBarry Smith 
97347c6ae99SBarry Smith   PetscFunctionBegin;
974171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
975b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
976b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
97747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
97847c6ae99SBarry Smith   PetscFunctionReturn(0);
97947c6ae99SBarry Smith }
98047c6ae99SBarry Smith 
9811411c6eeSJed Brown /*@
9821411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9831411c6eeSJed Brown 
984d083f849SBarry Smith    Collective on dm
9851411c6eeSJed Brown 
9861411c6eeSJed Brown    Input Parameter:
9871411c6eeSJed Brown .  dm - the DM that provides the mapping
9881411c6eeSJed Brown 
9891411c6eeSJed Brown    Output Parameter:
9901411c6eeSJed Brown .  ltog - the mapping
9911411c6eeSJed Brown 
9921411c6eeSJed Brown    Level: intermediate
9931411c6eeSJed Brown 
9941411c6eeSJed Brown    Notes:
9951411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9961411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9971411c6eeSJed Brown 
998fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9991411c6eeSJed Brown @*/
10007087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10011411c6eeSJed Brown {
10020be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10031411c6eeSJed Brown   PetscErrorCode ierr;
10041411c6eeSJed Brown 
10051411c6eeSJed Brown   PetscFunctionBegin;
10061411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10071411c6eeSJed Brown   PetscValidPointer(ltog,2);
10081411c6eeSJed Brown   if (!dm->ltogmap) {
100937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
101037d0c07bSMatthew G Knepley 
101192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
101237d0c07bSMatthew G Knepley     if (section) {
1013a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
101437d0c07bSMatthew G Knepley       PetscInt       *ltog;
1015ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
101637d0c07bSMatthew G Knepley 
1017e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
101837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1019ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1020ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
102137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1022a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
102337d0c07bSMatthew G Knepley 
102437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
102537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1026a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1027a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
102837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10291a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10301a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10311a7dc684SMatthew G. Knepley         if (dof) {
1032b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1033b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10341a7dc684SMatthew G. Knepley         }
103537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1036a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1037a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
103837d0c07bSMatthew G Knepley         }
103937d0c07bSMatthew G Knepley       }
1040bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10410be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10420be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10430be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10440be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10457591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10467591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1047ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1048ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1049ccf3bd66SMatthew G. Knepley         n /= bs;
1050ccf3bd66SMatthew G. Knepley       }
1051ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10523bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
105337d0c07bSMatthew G Knepley     } else {
1054b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1055184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10561411c6eeSJed Brown     }
105737d0c07bSMatthew G Knepley   }
10581411c6eeSJed Brown   *ltog = dm->ltogmap;
10591411c6eeSJed Brown   PetscFunctionReturn(0);
10601411c6eeSJed Brown }
10611411c6eeSJed Brown 
10621411c6eeSJed Brown /*@
10631411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10641411c6eeSJed Brown 
10651411c6eeSJed Brown    Not Collective
10661411c6eeSJed Brown 
10671411c6eeSJed Brown    Input Parameter:
10681411c6eeSJed Brown .  dm - the DM with block structure
10691411c6eeSJed Brown 
10701411c6eeSJed Brown    Output Parameter:
10711411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10721411c6eeSJed Brown 
10731411c6eeSJed Brown    Level: intermediate
10741411c6eeSJed Brown 
1075fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10761411c6eeSJed Brown @*/
10777087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10781411c6eeSJed Brown {
10791411c6eeSJed Brown   PetscFunctionBegin;
10801411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1081534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
10821411c6eeSJed 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");
10831411c6eeSJed Brown   *bs = dm->bs;
10841411c6eeSJed Brown   PetscFunctionReturn(0);
10851411c6eeSJed Brown }
10861411c6eeSJed Brown 
108747c6ae99SBarry Smith /*@
10888472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
108947c6ae99SBarry Smith 
1090d083f849SBarry Smith     Collective on dm1
109147c6ae99SBarry Smith 
109247c6ae99SBarry Smith     Input Parameter:
109347c6ae99SBarry Smith +   dm1 - the DM object
109447c6ae99SBarry Smith -   dm2 - the second, finer DM object
109547c6ae99SBarry Smith 
109647c6ae99SBarry Smith     Output Parameter:
109747c6ae99SBarry Smith +  mat - the interpolation
109847c6ae99SBarry Smith -  vec - the scaling (optional)
109947c6ae99SBarry Smith 
110047c6ae99SBarry Smith     Level: developer
110147c6ae99SBarry Smith 
110295452b02SPatrick Sanan     Notes:
110395452b02SPatrick 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
110485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1105d52bd9f3SBarry Smith 
11061f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1107d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
110885afcc9aSBarry Smith 
110985afcc9aSBarry Smith 
11102ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
111147c6ae99SBarry Smith 
111247c6ae99SBarry Smith @*/
1113e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
111447c6ae99SBarry Smith {
111547c6ae99SBarry Smith   PetscErrorCode ierr;
111647c6ae99SBarry Smith 
111747c6ae99SBarry Smith   PetscFunctionBegin;
1118171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1119171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1120c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1121b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dm1)->type_name);
1122cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
112325296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1124cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
112547c6ae99SBarry Smith   PetscFunctionReturn(0);
112647c6ae99SBarry Smith }
112747c6ae99SBarry Smith 
11283ad4599aSBarry Smith /*@
1129d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11302ed6491fSPatrick Sanan 
11312ed6491fSPatrick Sanan   Input Parameters:
11322ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11332ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11342ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11352ed6491fSPatrick Sanan 
11362ed6491fSPatrick Sanan   Output Parameter:
11372ed6491fSPatrick Sanan .    scale - the scaled vector
11382ed6491fSPatrick Sanan 
11392ed6491fSPatrick Sanan   Level: developer
11402ed6491fSPatrick Sanan 
11412ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
11422ed6491fSPatrick Sanan 
11432ed6491fSPatrick Sanan @*/
11442ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11452ed6491fSPatrick Sanan {
11462ed6491fSPatrick Sanan   PetscErrorCode ierr;
11472ed6491fSPatrick Sanan   Vec            fine;
11482ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11492ed6491fSPatrick Sanan 
11502ed6491fSPatrick Sanan   PetscFunctionBegin;
11512ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
11522ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
11532ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
11542ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
11552ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
11562ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
11572ed6491fSPatrick Sanan   PetscFunctionReturn(0);
11582ed6491fSPatrick Sanan }
11592ed6491fSPatrick Sanan 
11602ed6491fSPatrick Sanan /*@
11613ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11623ad4599aSBarry Smith 
1163d083f849SBarry Smith     Collective on dm1
11643ad4599aSBarry Smith 
11653ad4599aSBarry Smith     Input Parameter:
11663ad4599aSBarry Smith +   dm1 - the DM object
11673ad4599aSBarry Smith -   dm2 - the second, finer DM object
11683ad4599aSBarry Smith 
11693ad4599aSBarry Smith     Output Parameter:
11703ad4599aSBarry Smith .  mat - the restriction
11713ad4599aSBarry Smith 
11723ad4599aSBarry Smith 
11733ad4599aSBarry Smith     Level: developer
11743ad4599aSBarry Smith 
117595452b02SPatrick Sanan     Notes:
117695452b02SPatrick 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
11773ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11783ad4599aSBarry Smith 
11793ad4599aSBarry Smith 
11803ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11813ad4599aSBarry Smith 
11823ad4599aSBarry Smith @*/
11833ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11843ad4599aSBarry Smith {
11853ad4599aSBarry Smith   PetscErrorCode ierr;
11863ad4599aSBarry Smith 
11873ad4599aSBarry Smith   PetscFunctionBegin;
11883ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11893ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11905a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1191b9d85ea2SLisandro Dalcin   if (!dm1->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dm1)->type_name);
11923ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11933ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11943ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11953ad4599aSBarry Smith   PetscFunctionReturn(0);
11963ad4599aSBarry Smith }
11973ad4599aSBarry Smith 
119847c6ae99SBarry Smith /*@
11998472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
120047c6ae99SBarry Smith 
1201d083f849SBarry Smith     Collective on dm1
120247c6ae99SBarry Smith 
120347c6ae99SBarry Smith     Input Parameter:
120447c6ae99SBarry Smith +   dm1 - the DM object
120547c6ae99SBarry Smith -   dm2 - the second, finer DM object
120647c6ae99SBarry Smith 
120747c6ae99SBarry Smith     Output Parameter:
12086dbf9973SLawrence Mitchell .   mat - the injection
120947c6ae99SBarry Smith 
121047c6ae99SBarry Smith     Level: developer
121147c6ae99SBarry Smith 
121295452b02SPatrick Sanan    Notes:
121395452b02SPatrick 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
121485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
121585afcc9aSBarry Smith 
1216e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
121747c6ae99SBarry Smith 
121847c6ae99SBarry Smith @*/
12196dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
122047c6ae99SBarry Smith {
122147c6ae99SBarry Smith   PetscErrorCode ierr;
122247c6ae99SBarry Smith 
122347c6ae99SBarry Smith   PetscFunctionBegin;
1224171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1225171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
12265a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1227b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dm1)->type_name);
12285a84ad33SLisandro Dalcin   ierr = PetscLogEventBegin(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
12295a84ad33SLisandro Dalcin   ierr = (*dm1->ops->createinjection)(dm1,dm2,mat);CHKERRQ(ierr);
12305a84ad33SLisandro Dalcin   ierr = PetscLogEventEnd(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
123147c6ae99SBarry Smith   PetscFunctionReturn(0);
123247c6ae99SBarry Smith }
123347c6ae99SBarry Smith 
1234b412c318SBarry Smith /*@
1235bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1236bd041c0cSMatthew G. Knepley 
1237d083f849SBarry Smith   Collective on dm1
1238bd041c0cSMatthew G. Knepley 
1239bd041c0cSMatthew G. Knepley   Input Parameter:
1240bd041c0cSMatthew G. Knepley + dm1 - the DM object
1241bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1242bd041c0cSMatthew G. Knepley 
1243bd041c0cSMatthew G. Knepley   Output Parameter:
1244bd041c0cSMatthew G. Knepley . mat - the interpolation
1245bd041c0cSMatthew G. Knepley 
1246bd041c0cSMatthew G. Knepley   Level: developer
1247bd041c0cSMatthew G. Knepley 
1248bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1249bd041c0cSMatthew G. Knepley @*/
1250bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1251bd041c0cSMatthew G. Knepley {
1252bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1253bd041c0cSMatthew G. Knepley 
1254bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1255bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1256bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
12575a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1258b9d85ea2SLisandro Dalcin   if (!dm1->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dm1)->type_name);
1259bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1260bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1261bd041c0cSMatthew G. Knepley }
1262bd041c0cSMatthew G. Knepley 
1263bd041c0cSMatthew G. Knepley /*@
1264b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
126547c6ae99SBarry Smith 
1266d083f849SBarry Smith     Collective on dm
126747c6ae99SBarry Smith 
126847c6ae99SBarry Smith     Input Parameter:
126947c6ae99SBarry Smith +   dm - the DM object
12705bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
127147c6ae99SBarry Smith 
127247c6ae99SBarry Smith     Output Parameter:
127347c6ae99SBarry Smith .   coloring - the coloring
127447c6ae99SBarry Smith 
1275ec5066bdSBarry Smith     Notes:
1276ec5066bdSBarry 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
1277ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1278ec5066bdSBarry Smith 
1279ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1280ec5066bdSBarry Smith 
128147c6ae99SBarry Smith     Level: developer
128247c6ae99SBarry Smith 
1283ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
128447c6ae99SBarry Smith 
1285aab9d709SJed Brown @*/
1286b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
128747c6ae99SBarry Smith {
128847c6ae99SBarry Smith   PetscErrorCode ierr;
128947c6ae99SBarry Smith 
129047c6ae99SBarry Smith   PetscFunctionBegin;
1291171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12925a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1293b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1294b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
129547c6ae99SBarry Smith   PetscFunctionReturn(0);
129647c6ae99SBarry Smith }
129747c6ae99SBarry Smith 
1298b412c318SBarry Smith /*@
12998472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
130047c6ae99SBarry Smith 
1301d083f849SBarry Smith     Collective on dm
130247c6ae99SBarry Smith 
130347c6ae99SBarry Smith     Input Parameter:
1304b412c318SBarry Smith .   dm - the DM object
130547c6ae99SBarry Smith 
130647c6ae99SBarry Smith     Output Parameter:
130747c6ae99SBarry Smith .   mat - the empty Jacobian
130847c6ae99SBarry Smith 
1309073dac72SJed Brown     Level: beginner
131047c6ae99SBarry Smith 
131195452b02SPatrick Sanan     Notes:
131295452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
131394013140SBarry Smith        do not need to do it yourself.
131494013140SBarry Smith 
131594013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13167889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
131794013140SBarry Smith 
131894013140SBarry 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
131994013140SBarry Smith        internally by PETSc.
132094013140SBarry Smith 
132194013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1322aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
132394013140SBarry Smith 
1324b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
132547c6ae99SBarry Smith 
1326aab9d709SJed Brown @*/
1327b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
132847c6ae99SBarry Smith {
132947c6ae99SBarry Smith   PetscErrorCode ierr;
133047c6ae99SBarry Smith 
133147c6ae99SBarry Smith   PetscFunctionBegin;
1332171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1333c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1334b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13355a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1336fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1337b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1338e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1339e5e52638SMatthew G. Knepley   if (dm->Nf) {
1340e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1341e571a35bSMatthew G. Knepley     PetscInt     Nf;
1342e571a35bSMatthew G. Knepley 
1343e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1344e571a35bSMatthew G. Knepley     if (Nf == 1) {
1345e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1346e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1347e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1348e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1349e571a35bSMatthew G. Knepley       }
1350e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1351e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1352e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1353e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1354e571a35bSMatthew G. Knepley       }
1355e571a35bSMatthew G. Knepley     }
1356e571a35bSMatthew G. Knepley   }
1357fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
135847c6ae99SBarry Smith   PetscFunctionReturn(0);
135947c6ae99SBarry Smith }
136047c6ae99SBarry Smith 
1361732e2eb9SMatthew G Knepley /*@
1362950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1363732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1364732e2eb9SMatthew G Knepley 
1365d083f849SBarry Smith   Logically Collective on dm
1366732e2eb9SMatthew G Knepley 
1367732e2eb9SMatthew G Knepley   Input Parameter:
1368732e2eb9SMatthew G Knepley + dm - the DM
1369732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1370732e2eb9SMatthew G Knepley 
1371732e2eb9SMatthew G Knepley   Level: developer
1372b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1373732e2eb9SMatthew G Knepley @*/
1374732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1375732e2eb9SMatthew G Knepley {
1376732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1377732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1378732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1379732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1380732e2eb9SMatthew G Knepley }
1381732e2eb9SMatthew G Knepley 
1382b06ff27eSHong Zhang /*@
1383b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1384b06ff27eSHong Zhang     but the array for values will not be allocated.
1385b06ff27eSHong Zhang 
1386d083f849SBarry Smith   Logically Collective on dm
1387b06ff27eSHong Zhang 
1388b06ff27eSHong Zhang   Input Parameter:
1389b06ff27eSHong Zhang + dm - the DM
1390b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1391b06ff27eSHong Zhang 
1392b06ff27eSHong Zhang   Level: developer
1393b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1394b06ff27eSHong Zhang @*/
1395b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1396b06ff27eSHong Zhang {
1397b06ff27eSHong Zhang   PetscFunctionBegin;
1398b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1399b06ff27eSHong Zhang   dm->structure_only = only;
1400b06ff27eSHong Zhang   PetscFunctionReturn(0);
1401b06ff27eSHong Zhang }
1402b06ff27eSHong Zhang 
1403a89ea682SMatthew G Knepley /*@C
1404aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1405a89ea682SMatthew G Knepley 
1406a89ea682SMatthew G Knepley   Not Collective
1407a89ea682SMatthew G Knepley 
1408a89ea682SMatthew G Knepley   Input Parameters:
1409a89ea682SMatthew G Knepley + dm - the DM object
1410aa1993deSMatthew G Knepley . count - The minium size
141169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1412a89ea682SMatthew G Knepley 
1413a89ea682SMatthew G Knepley   Output Parameter:
1414a89ea682SMatthew G Knepley . array - the work array
1415a89ea682SMatthew G Knepley 
1416a89ea682SMatthew G Knepley   Level: developer
1417a89ea682SMatthew G Knepley 
1418a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1419a89ea682SMatthew G Knepley @*/
142069291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1421a89ea682SMatthew G Knepley {
1422a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1423aa1993deSMatthew G Knepley   DMWorkLink     link;
142469291d52SBarry Smith   PetscMPIInt    dsize;
1425a89ea682SMatthew G Knepley 
1426a89ea682SMatthew G Knepley   PetscFunctionBegin;
1427a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1428aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1429aa1993deSMatthew G Knepley   if (dm->workin) {
1430aa1993deSMatthew G Knepley     link       = dm->workin;
1431aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1432aa1993deSMatthew G Knepley   } else {
1433b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1434a89ea682SMatthew G Knepley   }
143569291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14365056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1437aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1438854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1439854ce69bSBarry Smith     link->bytes = dsize*count;
1440aa1993deSMatthew G Knepley   }
1441aa1993deSMatthew G Knepley   link->next   = dm->workout;
1442aa1993deSMatthew G Knepley   dm->workout  = link;
1443aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1444a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1445a89ea682SMatthew G Knepley }
1446a89ea682SMatthew G Knepley 
1447aa1993deSMatthew G Knepley /*@C
1448aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1449aa1993deSMatthew G Knepley 
1450aa1993deSMatthew G Knepley   Not Collective
1451aa1993deSMatthew G Knepley 
1452aa1993deSMatthew G Knepley   Input Parameters:
1453aa1993deSMatthew G Knepley + dm - the DM object
1454aa1993deSMatthew G Knepley . count - The minium size
145569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1456aa1993deSMatthew G Knepley 
1457aa1993deSMatthew G Knepley   Output Parameter:
1458aa1993deSMatthew G Knepley . array - the work array
1459aa1993deSMatthew G Knepley 
1460aa1993deSMatthew G Knepley   Level: developer
1461aa1993deSMatthew G Knepley 
146295452b02SPatrick Sanan   Developer Notes:
146395452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1464aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1465aa1993deSMatthew G Knepley @*/
146669291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1467aa1993deSMatthew G Knepley {
1468aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1469aa1993deSMatthew G Knepley 
1470aa1993deSMatthew G Knepley   PetscFunctionBegin;
1471aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1472aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1473aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1474aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1475aa1993deSMatthew G Knepley       *p           = link->next;
1476aa1993deSMatthew G Knepley       link->next   = dm->workin;
1477aa1993deSMatthew G Knepley       dm->workin   = link;
14780298fd71SBarry Smith       *(void**)mem = NULL;
1479aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1480aa1993deSMatthew G Knepley     }
1481aa1993deSMatthew G Knepley   }
1482aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1483aa1993deSMatthew G Knepley }
1484e7c4fc90SDmitry Karpeev 
1485435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1486435a35e8SMatthew G Knepley {
1487435a35e8SMatthew G Knepley   PetscFunctionBegin;
1488435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
148982f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1490435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1491435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1492435a35e8SMatthew G Knepley }
1493435a35e8SMatthew G Knepley 
14940a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14950a50eb56SMatthew G. Knepley {
14960a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14970a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1498f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14990a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15000a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15010a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15020a50eb56SMatthew G. Knepley }
15030a50eb56SMatthew G. Knepley 
1504f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1505f9d4088aSMatthew G. Knepley {
1506f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1507f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1508f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1509f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1510f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1511f9d4088aSMatthew G. Knepley }
1512f9d4088aSMatthew G. Knepley 
1513f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1514f9d4088aSMatthew G. Knepley {
1515f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1516f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1517f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1518f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1519f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1520f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1521f9d4088aSMatthew G. Knepley }
1522f9d4088aSMatthew G. Knepley 
15234f3b5142SJed Brown /*@C
15244d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
15254d343eeaSMatthew G Knepley 
15264d343eeaSMatthew G Knepley   Not collective
15274d343eeaSMatthew G Knepley 
15284d343eeaSMatthew G Knepley   Input Parameter:
15294d343eeaSMatthew G Knepley . dm - the DM object
15304d343eeaSMatthew G Knepley 
15314d343eeaSMatthew G Knepley   Output Parameters:
15320298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
15330298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
15340298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
15354d343eeaSMatthew G Knepley 
15364d343eeaSMatthew G Knepley   Level: intermediate
15374d343eeaSMatthew G Knepley 
153821c9b008SJed Brown   Notes:
153921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
154021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
154121c9b008SJed Brown   PetscFree().
154221c9b008SJed Brown 
15434d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
15444d343eeaSMatthew G Knepley @*/
154537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
15464d343eeaSMatthew G Knepley {
154737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
15484d343eeaSMatthew G Knepley   PetscErrorCode ierr;
15494d343eeaSMatthew G Knepley 
15504d343eeaSMatthew G Knepley   PetscFunctionBegin;
15514d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
155269ca1f37SDmitry Karpeev   if (numFields) {
1553534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
155469ca1f37SDmitry Karpeev     *numFields = 0;
155569ca1f37SDmitry Karpeev   }
155637d0c07bSMatthew G Knepley   if (fieldNames) {
155737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15580298fd71SBarry Smith     *fieldNames = NULL;
155969ca1f37SDmitry Karpeev   }
156069ca1f37SDmitry Karpeev   if (fields) {
156169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15620298fd71SBarry Smith     *fields = NULL;
156369ca1f37SDmitry Karpeev   }
156492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
156537d0c07bSMatthew G Knepley   if (section) {
15663a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
156737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
156837d0c07bSMatthew G Knepley 
1569e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
157037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15713a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
157237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
157337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
157437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15753a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
157637d0c07bSMatthew G Knepley     }
157737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
157837d0c07bSMatthew G Knepley       PetscInt gdof;
157937d0c07bSMatthew G Knepley 
158037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
158137d0c07bSMatthew G Knepley       if (gdof > 0) {
158237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15833a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
158437d0c07bSMatthew G Knepley 
158537d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
158637d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15873a544194SStefano Zampini           fpdof = fdof-fcdof;
15883a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15893a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15903a544194SStefano Zampini             fieldNc[f] = 1;
15913a544194SStefano Zampini           }
15923a544194SStefano Zampini           fieldSizes[f] += fpdof;
159337d0c07bSMatthew G Knepley         }
159437d0c07bSMatthew G Knepley       }
159537d0c07bSMatthew G Knepley     }
159637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1597785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
159837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
159937d0c07bSMatthew G Knepley     }
160037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
160137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
160237d0c07bSMatthew G Knepley 
160337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
160437d0c07bSMatthew G Knepley       if (gdof > 0) {
160537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
160637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
160737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
160837d0c07bSMatthew G Knepley 
160937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
161037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
161137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
161237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
161337d0c07bSMatthew G Knepley           }
161437d0c07bSMatthew G Knepley         }
161537d0c07bSMatthew G Knepley       }
161637d0c07bSMatthew G Knepley     }
16178865f1eaSKarl Rupp     if (numFields) *numFields = nF;
161837d0c07bSMatthew G Knepley     if (fieldNames) {
1619785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
162037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
162137d0c07bSMatthew G Knepley         const char *fieldName;
162237d0c07bSMatthew G Knepley 
162337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
162437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
162537d0c07bSMatthew G Knepley       }
162637d0c07bSMatthew G Knepley     }
162737d0c07bSMatthew G Knepley     if (fields) {
1628785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
162937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
16303a544194SStefano Zampini         PetscInt bs, in[2], out[2];
16313a544194SStefano Zampini 
163282f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
16333a544194SStefano Zampini         in[0] = -fieldNc[f];
16343a544194SStefano Zampini         in[1] = fieldNc[f];
16353a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
16363a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
16373a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
163837d0c07bSMatthew G Knepley       }
163937d0c07bSMatthew G Knepley     }
16403a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
16418865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
16428865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
164369ca1f37SDmitry Karpeev   }
16444d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
16454d343eeaSMatthew G Knepley }
16464d343eeaSMatthew G Knepley 
164716621825SDmitry Karpeev 
164816621825SDmitry Karpeev /*@C
164916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
165016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
165116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1652e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1653e7c4fc90SDmitry Karpeev 
1654e7c4fc90SDmitry Karpeev   Not collective
1655e7c4fc90SDmitry Karpeev 
1656e7c4fc90SDmitry Karpeev   Input Parameter:
1657e7c4fc90SDmitry Karpeev . dm - the DM object
1658e7c4fc90SDmitry Karpeev 
1659e7c4fc90SDmitry Karpeev   Output Parameters:
16600298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16610298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16620298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16630298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1664e7c4fc90SDmitry Karpeev 
1665e7c4fc90SDmitry Karpeev   Level: intermediate
1666e7c4fc90SDmitry Karpeev 
1667e7c4fc90SDmitry Karpeev   Notes:
1668e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1669e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1670e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1671e7c4fc90SDmitry Karpeev 
1672e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1673e7c4fc90SDmitry Karpeev @*/
167416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1675e7c4fc90SDmitry Karpeev {
1676e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1677e7c4fc90SDmitry Karpeev 
1678e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1679e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16808865f1eaSKarl Rupp   if (len) {
1681534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
16828865f1eaSKarl Rupp     *len = 0;
16838865f1eaSKarl Rupp   }
16848865f1eaSKarl Rupp   if (namelist) {
16858865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16868865f1eaSKarl Rupp     *namelist = 0;
16878865f1eaSKarl Rupp   }
16888865f1eaSKarl Rupp   if (islist) {
16898865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16908865f1eaSKarl Rupp     *islist = 0;
16918865f1eaSKarl Rupp   }
16928865f1eaSKarl Rupp   if (dmlist) {
16938865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16948865f1eaSKarl Rupp     *dmlist = 0;
16958865f1eaSKarl Rupp   }
1696f3f0edfdSDmitry Karpeev   /*
1697f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1698f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1699f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1700f3f0edfdSDmitry Karpeev    */
1701ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
170216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1703435a35e8SMatthew G Knepley     PetscSection section;
1704435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1705435a35e8SMatthew G Knepley 
170692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1707435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1708435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1709f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
171003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
171103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
171203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1713435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1714435a35e8SMatthew G Knepley         const char *fieldName;
1715435a35e8SMatthew G Knepley 
171603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
171703dc3394SMatthew G. Knepley         if (namelist) {
1718435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1719435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1720435a35e8SMatthew G Knepley         }
172103dc3394SMatthew G. Knepley       }
1722435a35e8SMatthew G Knepley     } else {
172369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1724e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
17250298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1726e7c4fc90SDmitry Karpeev     }
17278865f1eaSKarl Rupp   } else {
172816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
172916621825SDmitry Karpeev   }
173016621825SDmitry Karpeev   PetscFunctionReturn(0);
173116621825SDmitry Karpeev }
173216621825SDmitry Karpeev 
1733564cec59SMatthew G. Knepley /*@
1734435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1735435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1736435a35e8SMatthew G Knepley 
1737435a35e8SMatthew G Knepley   Not collective
1738435a35e8SMatthew G Knepley 
1739435a35e8SMatthew G Knepley   Input Parameters:
17402adcc780SMatthew G. Knepley + dm        - The DM object
17412adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
17422adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1743435a35e8SMatthew G Knepley 
1744435a35e8SMatthew G Knepley   Output Parameters:
17452adcc780SMatthew G. Knepley + is - The global indices for the subproblem
17462adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1747435a35e8SMatthew G Knepley 
17485d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17495d3b26e6SMatthew G. Knepley 
1750435a35e8SMatthew G Knepley   Level: intermediate
1751435a35e8SMatthew G Knepley 
17525d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1753435a35e8SMatthew G Knepley @*/
175437bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1755435a35e8SMatthew G Knepley {
1756435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1757435a35e8SMatthew G Knepley 
1758435a35e8SMatthew G Knepley   PetscFunctionBegin;
1759435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1760435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17618865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17628865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1763b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1764435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1765435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1766435a35e8SMatthew G Knepley }
1767435a35e8SMatthew G Knepley 
17682adcc780SMatthew G. Knepley /*@C
17692adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17702adcc780SMatthew G. Knepley 
17712adcc780SMatthew G. Knepley   Not collective
17722adcc780SMatthew G. Knepley 
17732adcc780SMatthew G. Knepley   Input Parameter:
17742adcc780SMatthew G. Knepley + dms - The DM objects
17752adcc780SMatthew G. Knepley - len - The number of DMs
17762adcc780SMatthew G. Knepley 
17772adcc780SMatthew G. Knepley   Output Parameters:
1778a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17792adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17802adcc780SMatthew G. Knepley 
17815d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17825d3b26e6SMatthew G. Knepley 
17832adcc780SMatthew G. Knepley   Level: intermediate
17842adcc780SMatthew G. Knepley 
17855d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17862adcc780SMatthew G. Knepley @*/
17872adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17882adcc780SMatthew G. Knepley {
17892adcc780SMatthew G. Knepley   PetscInt       i;
17902adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17912adcc780SMatthew G. Knepley 
17922adcc780SMatthew G. Knepley   PetscFunctionBegin;
17932adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17942adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17952adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1796a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17972adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17982adcc780SMatthew G. Knepley   if (len) {
1799b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1800b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1801b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
18022adcc780SMatthew G. Knepley   }
18032adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
18042adcc780SMatthew G. Knepley }
18052adcc780SMatthew G. Knepley 
180616621825SDmitry Karpeev 
180716621825SDmitry Karpeev /*@C
18088d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
18098d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
18108d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
18118d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
18128d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
181316621825SDmitry Karpeev 
181416621825SDmitry Karpeev   Not collective
181516621825SDmitry Karpeev 
181616621825SDmitry Karpeev   Input Parameter:
181716621825SDmitry Karpeev . dm - the DM object
181816621825SDmitry Karpeev 
181916621825SDmitry Karpeev   Output Parameters:
18200298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
18210298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
18220298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
18230298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
18240298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
182516621825SDmitry Karpeev 
182616621825SDmitry Karpeev   Level: intermediate
182716621825SDmitry Karpeev 
182816621825SDmitry Karpeev   Notes:
182916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
183016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
183116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
183216621825SDmitry Karpeev 
18338d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
183416621825SDmitry Karpeev @*/
18358d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
183616621825SDmitry Karpeev {
183716621825SDmitry Karpeev   PetscErrorCode      ierr;
1838be081cd6SPeter Brune   DMSubDomainHookLink link;
1839be081cd6SPeter Brune   PetscInt            i,l;
184016621825SDmitry Karpeev 
184116621825SDmitry Karpeev   PetscFunctionBegin;
184216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
184314a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
18440298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
18450298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
18460298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
18470298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1848f3f0edfdSDmitry Karpeev   /*
1849f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1850f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1851f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1852f3f0edfdSDmitry Karpeev    */
1853ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
185416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1855be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
185614a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1857f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1858be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1859be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1860be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1861be081cd6SPeter Brune         }
1862648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1863e7c4fc90SDmitry Karpeev       }
186414a18fd3SPeter Brune     }
186514a18fd3SPeter Brune     if (len) *len = l;
186614a18fd3SPeter Brune   }
1867e30e807fSPeter Brune   PetscFunctionReturn(0);
1868e30e807fSPeter Brune }
1869e30e807fSPeter Brune 
1870e30e807fSPeter Brune 
1871e30e807fSPeter Brune /*@C
1872e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1873e30e807fSPeter Brune 
1874e30e807fSPeter Brune   Not collective
1875e30e807fSPeter Brune 
1876e30e807fSPeter Brune   Input Parameters:
1877e30e807fSPeter Brune + dm - the DM object
1878e30e807fSPeter Brune . n  - the number of subdomain scatters
1879e30e807fSPeter Brune - subdms - the local subdomains
1880e30e807fSPeter Brune 
1881e30e807fSPeter Brune   Output Parameters:
1882e30e807fSPeter Brune + n     - the number of scatters returned
1883e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1884e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1885e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1886e30e807fSPeter Brune 
188795452b02SPatrick Sanan   Notes:
188895452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1889e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1890e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1891e30e807fSPeter Brune   solution and residual data.
1892e30e807fSPeter Brune 
1893e30e807fSPeter Brune   Level: developer
1894e30e807fSPeter Brune 
1895e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1896e30e807fSPeter Brune @*/
1897e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1898e30e807fSPeter Brune {
1899e30e807fSPeter Brune   PetscErrorCode ierr;
1900e30e807fSPeter Brune 
1901e30e807fSPeter Brune   PetscFunctionBegin;
1902e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1903e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1904b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1905e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1906e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1907e7c4fc90SDmitry Karpeev }
1908e7c4fc90SDmitry Karpeev 
190947c6ae99SBarry Smith /*@
191047c6ae99SBarry Smith   DMRefine - Refines a DM object
191147c6ae99SBarry Smith 
1912d083f849SBarry Smith   Collective on dm
191347c6ae99SBarry Smith 
191447c6ae99SBarry Smith   Input Parameter:
191547c6ae99SBarry Smith + dm   - the DM object
191691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
191747c6ae99SBarry Smith 
191847c6ae99SBarry Smith   Output Parameter:
19190298fd71SBarry Smith . dmf - the refined DM, or NULL
1920ae0a1c52SMatthew G Knepley 
19210298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
192247c6ae99SBarry Smith 
192347c6ae99SBarry Smith   Level: developer
192447c6ae99SBarry Smith 
1925e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
192647c6ae99SBarry Smith @*/
19277087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
192847c6ae99SBarry Smith {
192947c6ae99SBarry Smith   PetscErrorCode   ierr;
1930c833c3b5SJed Brown   DMRefineHookLink link;
193147c6ae99SBarry Smith 
193247c6ae99SBarry Smith   PetscFunctionBegin;
1933732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1934b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
19351ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
193647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
19374057135bSMatthew G Knepley   if (*dmf) {
193843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
19398865f1eaSKarl Rupp 
19408cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
19418865f1eaSKarl Rupp 
1942644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
19430598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1944656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
19458865f1eaSKarl Rupp 
1946e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1947c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
19488865f1eaSKarl Rupp       if (link->refinehook) {
19498865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19508865f1eaSKarl Rupp       }
1951c833c3b5SJed Brown     }
1952c833c3b5SJed Brown   }
19531ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1954c833c3b5SJed Brown   PetscFunctionReturn(0);
1955c833c3b5SJed Brown }
1956c833c3b5SJed Brown 
1957bb9467b5SJed Brown /*@C
1958c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1959c833c3b5SJed Brown 
1960c833c3b5SJed Brown    Logically Collective
1961c833c3b5SJed Brown 
1962c833c3b5SJed Brown    Input Arguments:
1963c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1964c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1965c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19660298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1967c833c3b5SJed Brown 
1968c833c3b5SJed Brown    Calling sequence of refinehook:
1969c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1970c833c3b5SJed Brown 
1971c833c3b5SJed Brown +  coarse - coarse level DM
1972c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1973c833c3b5SJed Brown -  ctx - optional user-defined function context
1974c833c3b5SJed Brown 
1975c833c3b5SJed Brown    Calling sequence for interphook:
1976c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1977c833c3b5SJed Brown 
1978c833c3b5SJed Brown +  coarse - coarse level DM
1979c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1980c833c3b5SJed Brown .  fine - fine level DM to update
1981c833c3b5SJed Brown -  ctx - optional user-defined function context
1982c833c3b5SJed Brown 
1983c833c3b5SJed Brown    Level: advanced
1984c833c3b5SJed Brown 
1985c833c3b5SJed Brown    Notes:
1986c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1987c833c3b5SJed Brown 
1988c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1989c833c3b5SJed Brown 
1990bb9467b5SJed Brown    This function is currently not available from Fortran.
1991bb9467b5SJed Brown 
1992c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1993c833c3b5SJed Brown @*/
1994c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1995c833c3b5SJed Brown {
1996c833c3b5SJed Brown   PetscErrorCode   ierr;
1997c833c3b5SJed Brown   DMRefineHookLink link,*p;
1998c833c3b5SJed Brown 
1999c833c3b5SJed Brown   PetscFunctionBegin;
2000c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20013d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
20023d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
20033d8e3701SJed Brown   }
200495dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2005c833c3b5SJed Brown   link->refinehook = refinehook;
2006c833c3b5SJed Brown   link->interphook = interphook;
2007c833c3b5SJed Brown   link->ctx        = ctx;
20080298fd71SBarry Smith   link->next       = NULL;
2009c833c3b5SJed Brown   *p               = link;
2010c833c3b5SJed Brown   PetscFunctionReturn(0);
2011c833c3b5SJed Brown }
2012c833c3b5SJed Brown 
20133d8e3701SJed Brown /*@C
20143d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
20153d8e3701SJed Brown 
20163d8e3701SJed Brown    Logically Collective
20173d8e3701SJed Brown 
20183d8e3701SJed Brown    Input Arguments:
20193d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
20203d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
20213d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20223d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20233d8e3701SJed Brown 
20243d8e3701SJed Brown    Level: advanced
20253d8e3701SJed Brown 
20263d8e3701SJed Brown    Notes:
20273d8e3701SJed Brown    This function does nothing if the hook is not in the list.
20283d8e3701SJed Brown 
20293d8e3701SJed Brown    This function is currently not available from Fortran.
20303d8e3701SJed Brown 
20313d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20323d8e3701SJed Brown @*/
20333d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
20343d8e3701SJed Brown {
20353d8e3701SJed Brown   PetscErrorCode   ierr;
20363d8e3701SJed Brown   DMRefineHookLink link,*p;
20373d8e3701SJed Brown 
20383d8e3701SJed Brown   PetscFunctionBegin;
20393d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20403d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
20413d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
20423d8e3701SJed Brown       link = *p;
20433d8e3701SJed Brown       *p = link->next;
20443d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
20453d8e3701SJed Brown       break;
20463d8e3701SJed Brown     }
20473d8e3701SJed Brown   }
20483d8e3701SJed Brown   PetscFunctionReturn(0);
20493d8e3701SJed Brown }
20503d8e3701SJed Brown 
2051c833c3b5SJed Brown /*@
2052c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2053c833c3b5SJed Brown 
2054c833c3b5SJed Brown    Collective if any hooks are
2055c833c3b5SJed Brown 
2056c833c3b5SJed Brown    Input Arguments:
2057c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2058e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2059c833c3b5SJed Brown -  fine - finer DM to update
2060c833c3b5SJed Brown 
2061c833c3b5SJed Brown    Level: developer
2062c833c3b5SJed Brown 
2063c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2064c833c3b5SJed Brown @*/
2065c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2066c833c3b5SJed Brown {
2067c833c3b5SJed Brown   PetscErrorCode   ierr;
2068c833c3b5SJed Brown   DMRefineHookLink link;
2069c833c3b5SJed Brown 
2070c833c3b5SJed Brown   PetscFunctionBegin;
2071c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20728865f1eaSKarl Rupp     if (link->interphook) {
20738865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20748865f1eaSKarl Rupp     }
20754057135bSMatthew G Knepley   }
207647c6ae99SBarry Smith   PetscFunctionReturn(0);
207747c6ae99SBarry Smith }
207847c6ae99SBarry Smith 
2079eb3f98d2SBarry Smith /*@
2080eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2081eb3f98d2SBarry Smith 
2082eb3f98d2SBarry Smith     Not Collective
2083eb3f98d2SBarry Smith 
2084eb3f98d2SBarry Smith     Input Parameter:
2085eb3f98d2SBarry Smith .   dm - the DM object
2086eb3f98d2SBarry Smith 
2087eb3f98d2SBarry Smith     Output Parameter:
2088eb3f98d2SBarry Smith .   level - number of refinements
2089eb3f98d2SBarry Smith 
2090eb3f98d2SBarry Smith     Level: developer
2091eb3f98d2SBarry Smith 
20926a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2093eb3f98d2SBarry Smith 
2094eb3f98d2SBarry Smith @*/
2095eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2096eb3f98d2SBarry Smith {
2097eb3f98d2SBarry Smith   PetscFunctionBegin;
2098eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2099eb3f98d2SBarry Smith   *level = dm->levelup;
2100eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2101eb3f98d2SBarry Smith }
2102eb3f98d2SBarry Smith 
2103fef3a512SBarry Smith /*@
2104fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2105fef3a512SBarry Smith 
2106fef3a512SBarry Smith     Not Collective
2107fef3a512SBarry Smith 
2108fef3a512SBarry Smith     Input Parameter:
2109fef3a512SBarry Smith +   dm - the DM object
2110fef3a512SBarry Smith -   level - number of refinements
2111fef3a512SBarry Smith 
2112fef3a512SBarry Smith     Level: advanced
2113fef3a512SBarry Smith 
211495452b02SPatrick Sanan     Notes:
211595452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2116fef3a512SBarry Smith 
2117fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2118fef3a512SBarry Smith 
2119fef3a512SBarry Smith @*/
2120fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2121fef3a512SBarry Smith {
2122fef3a512SBarry Smith   PetscFunctionBegin;
2123fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2124fef3a512SBarry Smith   dm->levelup = level;
2125fef3a512SBarry Smith   PetscFunctionReturn(0);
2126fef3a512SBarry Smith }
2127fef3a512SBarry Smith 
2128ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2129ca3d3a14SMatthew G. Knepley {
2130ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2131ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2132ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2133ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2134ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2135ca3d3a14SMatthew G. Knepley }
2136ca3d3a14SMatthew G. Knepley 
2137ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2138ca3d3a14SMatthew G. Knepley {
2139ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2140ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2141ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2142ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2143ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2144ca3d3a14SMatthew G. Knepley }
2145ca3d3a14SMatthew G. Knepley 
2146ca3d3a14SMatthew G. Knepley /*@
2147c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2148ca3d3a14SMatthew G. Knepley 
2149ca3d3a14SMatthew G. Knepley   Input Parameter:
2150ca3d3a14SMatthew G. Knepley . dm - The DM
2151ca3d3a14SMatthew G. Knepley 
2152ca3d3a14SMatthew G. Knepley   Output Parameter:
2153ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2154ca3d3a14SMatthew G. Knepley 
2155ca3d3a14SMatthew G. Knepley   Level: developer
2156ca3d3a14SMatthew G. Knepley 
2157ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2158ca3d3a14SMatthew G. Knepley @*/
2159ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2160ca3d3a14SMatthew G. Knepley {
2161ca3d3a14SMatthew G. Knepley   Vec            tv;
2162ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2163ca3d3a14SMatthew G. Knepley 
2164ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2165ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2166534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2167ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2168ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2169ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2170ca3d3a14SMatthew G. Knepley }
2171ca3d3a14SMatthew G. Knepley 
2172ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2173ca3d3a14SMatthew G. Knepley {
2174ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2175ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2176ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2177ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2178ca3d3a14SMatthew G. Knepley 
2179ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2180ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
218192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2182ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2183ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2184ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
218592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2186ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2187ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2188ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2189ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2190ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2191ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2192ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2193ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2194ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2195ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2196ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2197ca3d3a14SMatthew G. Knepley     }
2198ca3d3a14SMatthew G. Knepley   }
2199ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2200ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2201ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2202ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2203ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2204ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2205ca3d3a14SMatthew G. Knepley       if (dof) {
2206ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2207ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2208ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2209ca3d3a14SMatthew G. Knepley 
2210ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2211ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2212ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2213580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2214ca3d3a14SMatthew G. Knepley       }
2215ca3d3a14SMatthew G. Knepley     }
2216ca3d3a14SMatthew G. Knepley   }
2217ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2218ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2219ca3d3a14SMatthew G. Knepley }
2220ca3d3a14SMatthew G. Knepley 
2221ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2222ca3d3a14SMatthew G. Knepley {
2223ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2224ca3d3a14SMatthew G. Knepley 
2225ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2226ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2227ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2228ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2229ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2230ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2231ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2232ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2233ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2234ca3d3a14SMatthew G. Knepley }
2235ca3d3a14SMatthew G. Knepley 
2236bb9467b5SJed Brown /*@C
2237baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2238baf369e7SPeter Brune 
2239baf369e7SPeter Brune    Logically Collective
2240baf369e7SPeter Brune 
2241baf369e7SPeter Brune    Input Arguments:
2242baf369e7SPeter Brune +  dm - the DM
2243baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2244baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
22450298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2246baf369e7SPeter Brune 
2247baf369e7SPeter Brune    Calling sequence for beginhook:
2248baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2249baf369e7SPeter Brune 
2250baf369e7SPeter Brune +  dm - global DM
2251baf369e7SPeter Brune .  g - global vector
2252baf369e7SPeter Brune .  mode - mode
2253baf369e7SPeter Brune .  l - local vector
2254baf369e7SPeter Brune -  ctx - optional user-defined function context
2255baf369e7SPeter Brune 
2256baf369e7SPeter Brune 
2257baf369e7SPeter Brune    Calling sequence for endhook:
2258ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2259baf369e7SPeter Brune 
2260baf369e7SPeter Brune +  global - global DM
2261baf369e7SPeter Brune -  ctx - optional user-defined function context
2262baf369e7SPeter Brune 
2263baf369e7SPeter Brune    Level: advanced
2264baf369e7SPeter Brune 
2265baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2266baf369e7SPeter Brune @*/
2267baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2268baf369e7SPeter Brune {
2269baf369e7SPeter Brune   PetscErrorCode          ierr;
2270baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2271baf369e7SPeter Brune 
2272baf369e7SPeter Brune   PetscFunctionBegin;
2273baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2274baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
227595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2276baf369e7SPeter Brune   link->beginhook = beginhook;
2277baf369e7SPeter Brune   link->endhook   = endhook;
2278baf369e7SPeter Brune   link->ctx       = ctx;
22790298fd71SBarry Smith   link->next      = NULL;
2280baf369e7SPeter Brune   *p              = link;
2281baf369e7SPeter Brune   PetscFunctionReturn(0);
2282baf369e7SPeter Brune }
2283baf369e7SPeter Brune 
22844c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
22854c274da1SToby Isaac {
22864c274da1SToby Isaac   Mat cMat;
22874c274da1SToby Isaac   Vec cVec;
22884c274da1SToby Isaac   PetscSection section, cSec;
22894c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22904c274da1SToby Isaac   PetscErrorCode ierr;
22914c274da1SToby Isaac 
22924c274da1SToby Isaac   PetscFunctionBegin;
22934c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22944c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22954c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
22965db9a05bSToby Isaac     PetscInt nRows;
22975db9a05bSToby Isaac 
22985db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22995db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
230092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
23017711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23024c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
23034c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23044c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23054c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23064c274da1SToby Isaac       if (dof) {
23074c274da1SToby Isaac         PetscScalar *vals;
23084c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
23094c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
23104c274da1SToby Isaac       }
23114c274da1SToby Isaac     }
23124c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23134c274da1SToby Isaac   }
23144c274da1SToby Isaac   PetscFunctionReturn(0);
23154c274da1SToby Isaac }
23164c274da1SToby Isaac 
231747c6ae99SBarry Smith /*@
231801729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
231901729b5cSPatrick Sanan 
2320d083f849SBarry Smith     Neighbor-wise Collective on dm
232101729b5cSPatrick Sanan 
232201729b5cSPatrick Sanan     Input Parameters:
232301729b5cSPatrick Sanan +   dm - the DM object
232401729b5cSPatrick Sanan .   g - the global vector
232501729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
232601729b5cSPatrick Sanan -   l - the local vector
232701729b5cSPatrick Sanan 
232801729b5cSPatrick Sanan     Notes:
232901729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
233001729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
233101729b5cSPatrick Sanan 
233201729b5cSPatrick Sanan     Level: beginner
233301729b5cSPatrick Sanan 
233401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
233501729b5cSPatrick Sanan 
233601729b5cSPatrick Sanan @*/
233701729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
233801729b5cSPatrick Sanan {
233901729b5cSPatrick Sanan   PetscErrorCode ierr;
234001729b5cSPatrick Sanan 
234101729b5cSPatrick Sanan   PetscFunctionBegin;
234201729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
234301729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
234401729b5cSPatrick Sanan   PetscFunctionReturn(0);
234501729b5cSPatrick Sanan }
234601729b5cSPatrick Sanan 
234701729b5cSPatrick Sanan /*@
234847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
234947c6ae99SBarry Smith 
2350d083f849SBarry Smith     Neighbor-wise Collective on dm
235147c6ae99SBarry Smith 
235247c6ae99SBarry Smith     Input Parameters:
235347c6ae99SBarry Smith +   dm - the DM object
235447c6ae99SBarry Smith .   g - the global vector
235547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
235647c6ae99SBarry Smith -   l - the local vector
235747c6ae99SBarry Smith 
235801729b5cSPatrick Sanan     Level: intermediate
235947c6ae99SBarry Smith 
236001729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
236147c6ae99SBarry Smith 
236247c6ae99SBarry Smith @*/
23637087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
236447c6ae99SBarry Smith {
23657128ae9fSMatthew G Knepley   PetscSF                 sf;
236647c6ae99SBarry Smith   PetscErrorCode          ierr;
2367baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
236847c6ae99SBarry Smith 
236947c6ae99SBarry Smith   PetscFunctionBegin;
2370171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2371baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23728865f1eaSKarl Rupp     if (link->beginhook) {
23738865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23748865f1eaSKarl Rupp     }
2375baf369e7SPeter Brune   }
23761bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
23777128ae9fSMatthew G Knepley   if (sf) {
2378ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2379ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
23807128ae9fSMatthew G Knepley 
238182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23827128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2383ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23847128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23857128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2386ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
23877128ae9fSMatthew G Knepley   } else {
238833907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2389843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23907128ae9fSMatthew G Knepley   }
239147c6ae99SBarry Smith   PetscFunctionReturn(0);
239247c6ae99SBarry Smith }
239347c6ae99SBarry Smith 
239447c6ae99SBarry Smith /*@
239547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
239647c6ae99SBarry Smith 
2397d083f849SBarry Smith     Neighbor-wise Collective on dm
239847c6ae99SBarry Smith 
239947c6ae99SBarry Smith     Input Parameters:
240047c6ae99SBarry Smith +   dm - the DM object
240147c6ae99SBarry Smith .   g - the global vector
240247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
240347c6ae99SBarry Smith -   l - the local vector
240447c6ae99SBarry Smith 
240501729b5cSPatrick Sanan     Level: intermediate
240647c6ae99SBarry Smith 
240701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
240847c6ae99SBarry Smith 
240947c6ae99SBarry Smith @*/
24107087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
241147c6ae99SBarry Smith {
24127128ae9fSMatthew G Knepley   PetscSF                 sf;
241347c6ae99SBarry Smith   PetscErrorCode          ierr;
2414ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2415ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2416ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2417baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
241847c6ae99SBarry Smith 
241947c6ae99SBarry Smith   PetscFunctionBegin;
2420171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24211bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2422ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
24237128ae9fSMatthew G Knepley   if (sf) {
242482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24257128ae9fSMatthew G Knepley 
24267128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2427ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
24287128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
24297128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2430ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2431ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
24327128ae9fSMatthew G Knepley   } else {
243333907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2434843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24357128ae9fSMatthew G Knepley   }
24364c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2437baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2438baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2439baf369e7SPeter Brune   }
244047c6ae99SBarry Smith   PetscFunctionReturn(0);
244147c6ae99SBarry Smith }
244247c6ae99SBarry Smith 
2443d4d07f1eSToby Isaac /*@C
2444d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2445d4d07f1eSToby Isaac 
2446d4d07f1eSToby Isaac    Logically Collective
2447d4d07f1eSToby Isaac 
2448d4d07f1eSToby Isaac    Input Arguments:
2449d4d07f1eSToby Isaac +  dm - the DM
2450d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2451d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2452d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2453d4d07f1eSToby Isaac 
2454d4d07f1eSToby Isaac    Calling sequence for beginhook:
2455d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2456d4d07f1eSToby Isaac 
2457d4d07f1eSToby Isaac +  dm - global DM
2458d4d07f1eSToby Isaac .  l - local vector
2459d4d07f1eSToby Isaac .  mode - mode
2460d4d07f1eSToby Isaac .  g - global vector
2461d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2462d4d07f1eSToby Isaac 
2463d4d07f1eSToby Isaac 
2464d4d07f1eSToby Isaac    Calling sequence for endhook:
2465d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2466d4d07f1eSToby Isaac 
2467d4d07f1eSToby Isaac +  global - global DM
2468d4d07f1eSToby Isaac .  l - local vector
2469d4d07f1eSToby Isaac .  mode - mode
2470d4d07f1eSToby Isaac .  g - global vector
2471d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2472d4d07f1eSToby Isaac 
2473d4d07f1eSToby Isaac    Level: advanced
2474d4d07f1eSToby Isaac 
2475d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2476d4d07f1eSToby Isaac @*/
2477d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2478d4d07f1eSToby Isaac {
2479d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2480d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2481d4d07f1eSToby Isaac 
2482d4d07f1eSToby Isaac   PetscFunctionBegin;
2483d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2484d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
248595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2486d4d07f1eSToby Isaac   link->beginhook = beginhook;
2487d4d07f1eSToby Isaac   link->endhook   = endhook;
2488d4d07f1eSToby Isaac   link->ctx       = ctx;
2489d4d07f1eSToby Isaac   link->next      = NULL;
2490d4d07f1eSToby Isaac   *p              = link;
2491d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2492d4d07f1eSToby Isaac }
2493d4d07f1eSToby Isaac 
24944c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
24954c274da1SToby Isaac {
24964c274da1SToby Isaac   Mat cMat;
24974c274da1SToby Isaac   Vec cVec;
24984c274da1SToby Isaac   PetscSection section, cSec;
24994c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25004c274da1SToby Isaac   PetscErrorCode ierr;
25014c274da1SToby Isaac 
25024c274da1SToby Isaac   PetscFunctionBegin;
25034c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25044c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25054c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
25065db9a05bSToby Isaac     PetscInt nRows;
25075db9a05bSToby Isaac 
25085db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25095db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
251092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25117711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25124c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25134c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25144c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25154c274da1SToby Isaac       if (dof) {
25164c274da1SToby Isaac         PetscInt d;
25174c274da1SToby Isaac         PetscScalar *vals;
25184c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
25194c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
25204c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
25214c274da1SToby Isaac          * we just extracted */
25224c274da1SToby Isaac         for (d = 0; d < dof; d++) {
25234c274da1SToby Isaac           vals[d] = 0.;
25244c274da1SToby Isaac         }
25254c274da1SToby Isaac       }
25264c274da1SToby Isaac     }
25274c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
25284c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25294c274da1SToby Isaac   }
25304c274da1SToby Isaac   PetscFunctionReturn(0);
25314c274da1SToby Isaac }
253201729b5cSPatrick Sanan /*@
253301729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
253401729b5cSPatrick Sanan 
2535d083f849SBarry Smith     Neighbor-wise Collective on dm
253601729b5cSPatrick Sanan 
253701729b5cSPatrick Sanan     Input Parameters:
253801729b5cSPatrick Sanan +   dm - the DM object
253901729b5cSPatrick Sanan .   l - the local vector
254001729b5cSPatrick 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.
254101729b5cSPatrick Sanan -   g - the global vector
254201729b5cSPatrick Sanan 
254301729b5cSPatrick Sanan     Notes:
254401729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
254501729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
254601729b5cSPatrick Sanan 
254701729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
254801729b5cSPatrick 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.
254901729b5cSPatrick Sanan 
255001729b5cSPatrick Sanan     Level: beginner
255101729b5cSPatrick Sanan 
255201729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
255301729b5cSPatrick Sanan 
255401729b5cSPatrick Sanan @*/
255501729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
255601729b5cSPatrick Sanan {
255701729b5cSPatrick Sanan   PetscErrorCode ierr;
255801729b5cSPatrick Sanan 
255901729b5cSPatrick Sanan   PetscFunctionBegin;
256001729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
256101729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
256201729b5cSPatrick Sanan   PetscFunctionReturn(0);
256301729b5cSPatrick Sanan }
25644c274da1SToby Isaac 
256547c6ae99SBarry Smith /*@
256601729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25679a42bb27SBarry Smith 
2568d083f849SBarry Smith     Neighbor-wise Collective on dm
25699a42bb27SBarry Smith 
25709a42bb27SBarry Smith     Input Parameters:
25719a42bb27SBarry Smith +   dm - the DM object
2572f6813fd5SJed Brown .   l - the local vector
25731eb28f2eSBarry 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.
25741eb28f2eSBarry Smith -   g - the global vector
25759a42bb27SBarry Smith 
257695452b02SPatrick Sanan     Notes:
257795452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
257884330215SMatthew 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.
25799a42bb27SBarry Smith 
258001729b5cSPatrick Sanan     Level: intermediate
25819a42bb27SBarry Smith 
258201729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
25839a42bb27SBarry Smith 
25849a42bb27SBarry Smith @*/
25857087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
25869a42bb27SBarry Smith {
25877128ae9fSMatthew G Knepley   PetscSF                 sf;
258884330215SMatthew G. Knepley   PetscSection            s, gs;
2589d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2590ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2591ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2592ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2593ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
259484330215SMatthew G. Knepley   PetscErrorCode          ierr;
25959a42bb27SBarry Smith 
25969a42bb27SBarry Smith   PetscFunctionBegin;
2597171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2598d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2599d4d07f1eSToby Isaac     if (link->beginhook) {
2600d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2601d4d07f1eSToby Isaac     }
2602d4d07f1eSToby Isaac   }
26034c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
26041bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
260592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26067128ae9fSMatthew G Knepley   switch (mode) {
26077128ae9fSMatthew G Knepley   case INSERT_VALUES:
26087128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2609304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
261084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26117128ae9fSMatthew G Knepley   case ADD_VALUES:
26127128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2613304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
261484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26157128ae9fSMatthew G Knepley   default:
261682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26177128ae9fSMatthew G Knepley   }
2618ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2619ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2620ca3d3a14SMatthew G. Knepley     if (transform) {
2621ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2622ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2623ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2624ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2625ca3d3a14SMatthew G. Knepley     } else {
2626ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2627ca3d3a14SMatthew G. Knepley     }
26287128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2629ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2630a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
263184330215SMatthew G. Knepley     } else if (s && isInsert) {
263284330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
263384330215SMatthew G. Knepley 
2634e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
263584330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
263684330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
263784330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2638b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
263984330215SMatthew G. Knepley 
264084330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
264103442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
264284330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2643b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
264484330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
264584330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2646b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
264703442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2648b3b16f48SMatthew 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);
2649b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2650b3b16f48SMatthew G. Knepley         if (!gcdof) {
265184330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2652b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2653b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
265484330215SMatthew G. Knepley           const PetscInt *cdofs;
265584330215SMatthew G. Knepley           PetscInt        cind = 0;
265684330215SMatthew G. Knepley 
265784330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2658b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
265984330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2660b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
266184330215SMatthew G. Knepley           }
2662b3b16f48SMatthew 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);
266384330215SMatthew G. Knepley       }
2664ca3d3a14SMatthew G. Knepley     }
26657128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2666ca3d3a14SMatthew G. Knepley     if (transform) {
2667ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2668ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2669ca3d3a14SMatthew G. Knepley     } else {
2670ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2671ca3d3a14SMatthew G. Knepley     }
26727128ae9fSMatthew G Knepley   } else {
2673b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2674843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26757128ae9fSMatthew G Knepley   }
26769a42bb27SBarry Smith   PetscFunctionReturn(0);
26779a42bb27SBarry Smith }
26789a42bb27SBarry Smith 
26799a42bb27SBarry Smith /*@
26809a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
268147c6ae99SBarry Smith 
2682d083f849SBarry Smith     Neighbor-wise Collective on dm
268347c6ae99SBarry Smith 
268447c6ae99SBarry Smith     Input Parameters:
268547c6ae99SBarry Smith +   dm - the DM object
2686f6813fd5SJed Brown .   l - the local vector
268747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2688f6813fd5SJed Brown -   g - the global vector
268947c6ae99SBarry Smith 
269001729b5cSPatrick Sanan     Level: intermediate
269147c6ae99SBarry Smith 
2692e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
269347c6ae99SBarry Smith 
269447c6ae99SBarry Smith @*/
26957087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
269647c6ae99SBarry Smith {
26977128ae9fSMatthew G Knepley   PetscSF                 sf;
269884330215SMatthew G. Knepley   PetscSection            s;
2699d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2700ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
270184330215SMatthew G. Knepley   PetscErrorCode          ierr;
270247c6ae99SBarry Smith 
270347c6ae99SBarry Smith   PetscFunctionBegin;
2704171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27051bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
270692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27077128ae9fSMatthew G Knepley   switch (mode) {
27087128ae9fSMatthew G Knepley   case INSERT_VALUES:
27097128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
271084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27117128ae9fSMatthew G Knepley   case ADD_VALUES:
27127128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
271384330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27147128ae9fSMatthew G Knepley   default:
271582f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27167128ae9fSMatthew G Knepley   }
271784330215SMatthew G. Knepley   if (sf && !isInsert) {
2718ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2719ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2720ca3d3a14SMatthew G. Knepley     Vec                tmpl;
272184330215SMatthew G. Knepley 
2722ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2723ca3d3a14SMatthew G. Knepley     if (transform) {
2724ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2725ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2726ca3d3a14SMatthew G. Knepley     } else {
2727ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2728ca3d3a14SMatthew G. Knepley     }
27297128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2730a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2731ca3d3a14SMatthew G. Knepley     if (transform) {
2732ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2733ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2734ca3d3a14SMatthew G. Knepley     } else {
2735ae5cfb4aSMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2736ca3d3a14SMatthew G. Knepley     }
27377128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
273884330215SMatthew G. Knepley   } else if (s && isInsert) {
27397128ae9fSMatthew G Knepley   } else {
2740b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2741843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27427128ae9fSMatthew G Knepley   }
2743d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2744d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2745d4d07f1eSToby Isaac   }
274647c6ae99SBarry Smith   PetscFunctionReturn(0);
274747c6ae99SBarry Smith }
274847c6ae99SBarry Smith 
2749f089877aSRichard Tran Mills /*@
2750bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2751bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2752d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2753f089877aSRichard Tran Mills 
2754d083f849SBarry Smith    Neighbor-wise Collective on dm
2755f089877aSRichard Tran Mills 
2756f089877aSRichard Tran Mills    Input Parameters:
2757f089877aSRichard Tran Mills +  dm - the DM object
2758bc0a1609SRichard Tran Mills .  g - the original local vector
2759bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2760f089877aSRichard Tran Mills 
2761bc0a1609SRichard Tran Mills    Output Parameter:
2762bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2763f089877aSRichard Tran Mills 
2764f089877aSRichard Tran Mills    Level: intermediate
2765f089877aSRichard Tran Mills 
2766bc0a1609SRichard Tran Mills    Notes:
2767bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2768bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2769bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2770bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2771bc0a1609SRichard Tran Mills 
2772bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2773f089877aSRichard Tran Mills 
2774f089877aSRichard Tran Mills @*/
2775f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2776f089877aSRichard Tran Mills {
2777f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2778f089877aSRichard Tran Mills 
2779f089877aSRichard Tran Mills   PetscFunctionBegin;
2780f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2781bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2782f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2783f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2784f089877aSRichard Tran Mills }
2785f089877aSRichard Tran Mills 
2786f089877aSRichard Tran Mills /*@
2787bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2788bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2789d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2790f089877aSRichard Tran Mills 
2791d083f849SBarry Smith    Neighbor-wise Collective on dm
2792f089877aSRichard Tran Mills 
2793f089877aSRichard Tran Mills    Input Parameters:
2794bc0a1609SRichard Tran Mills +  da - the DM object
2795bc0a1609SRichard Tran Mills .  g - the original local vector
2796bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2797f089877aSRichard Tran Mills 
2798bc0a1609SRichard Tran Mills    Output Parameter:
2799bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2800f089877aSRichard Tran Mills 
2801f089877aSRichard Tran Mills    Level: intermediate
2802f089877aSRichard Tran Mills 
2803bc0a1609SRichard Tran Mills    Notes:
2804bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2805bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2806bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2807bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2808bc0a1609SRichard Tran Mills 
2809bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2810f089877aSRichard Tran Mills 
2811f089877aSRichard Tran Mills @*/
2812f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2813f089877aSRichard Tran Mills {
2814f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2815f089877aSRichard Tran Mills 
2816f089877aSRichard Tran Mills   PetscFunctionBegin;
2817f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2818bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2819f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2820f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2821f089877aSRichard Tran Mills }
2822f089877aSRichard Tran Mills 
2823f089877aSRichard Tran Mills 
282447c6ae99SBarry Smith /*@
282547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
282647c6ae99SBarry Smith 
2827d083f849SBarry Smith     Collective on dm
282847c6ae99SBarry Smith 
282947c6ae99SBarry Smith     Input Parameter:
283047c6ae99SBarry Smith +   dm - the DM object
283191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
283247c6ae99SBarry Smith 
283347c6ae99SBarry Smith     Output Parameter:
283447c6ae99SBarry Smith .   dmc - the coarsened DM
283547c6ae99SBarry Smith 
283647c6ae99SBarry Smith     Level: developer
283747c6ae99SBarry Smith 
2838e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
283947c6ae99SBarry Smith 
284047c6ae99SBarry Smith @*/
28417087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
284247c6ae99SBarry Smith {
284347c6ae99SBarry Smith   PetscErrorCode    ierr;
2844b17ce1afSJed Brown   DMCoarsenHookLink link;
284547c6ae99SBarry Smith 
284647c6ae99SBarry Smith   PetscFunctionBegin;
2847171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2848b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
284947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
285047c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2851b9d85ea2SLisandro Dalcin   if (*dmc) {
2852a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
285343842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28548cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2855644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
28560598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2857656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2858e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2859b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2860b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2861b17ce1afSJed Brown     }
2862b9d85ea2SLisandro Dalcin   }
286347a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2864b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2865b17ce1afSJed Brown   PetscFunctionReturn(0);
2866b17ce1afSJed Brown }
2867b17ce1afSJed Brown 
2868bb9467b5SJed Brown /*@C
2869b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2870b17ce1afSJed Brown 
2871b17ce1afSJed Brown    Logically Collective
2872b17ce1afSJed Brown 
2873b17ce1afSJed Brown    Input Arguments:
2874b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2875b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2876b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
28770298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2878b17ce1afSJed Brown 
2879b17ce1afSJed Brown    Calling sequence of coarsenhook:
2880b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2881b17ce1afSJed Brown 
2882b17ce1afSJed Brown +  fine - fine level DM
2883b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2884b17ce1afSJed Brown -  ctx - optional user-defined function context
2885b17ce1afSJed Brown 
2886b17ce1afSJed Brown    Calling sequence for restricthook:
2887c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2888b17ce1afSJed Brown 
2889b17ce1afSJed Brown +  fine - fine level DM
2890b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2891c833c3b5SJed Brown .  rscale - scaling vector for restriction
2892c833c3b5SJed Brown .  inject - matrix restricting by injection
2893b17ce1afSJed Brown .  coarse - coarse level DM to update
2894b17ce1afSJed Brown -  ctx - optional user-defined function context
2895b17ce1afSJed Brown 
2896b17ce1afSJed Brown    Level: advanced
2897b17ce1afSJed Brown 
2898b17ce1afSJed Brown    Notes:
2899b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2900b17ce1afSJed Brown 
2901b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2902b17ce1afSJed Brown 
2903b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2904b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2905b17ce1afSJed Brown 
2906bb9467b5SJed Brown    This function is currently not available from Fortran.
2907bb9467b5SJed Brown 
2908dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2909b17ce1afSJed Brown @*/
2910b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2911b17ce1afSJed Brown {
2912b17ce1afSJed Brown   PetscErrorCode    ierr;
2913b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2914b17ce1afSJed Brown 
2915b17ce1afSJed Brown   PetscFunctionBegin;
2916b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
29171e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
29181e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
29191e3d8eccSJed Brown   }
292095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2921b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2922b17ce1afSJed Brown   link->restricthook = restricthook;
2923b17ce1afSJed Brown   link->ctx          = ctx;
29240298fd71SBarry Smith   link->next         = NULL;
2925b17ce1afSJed Brown   *p                 = link;
2926b17ce1afSJed Brown   PetscFunctionReturn(0);
2927b17ce1afSJed Brown }
2928b17ce1afSJed Brown 
2929dc822a44SJed Brown /*@C
2930dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2931dc822a44SJed Brown 
2932dc822a44SJed Brown    Logically Collective
2933dc822a44SJed Brown 
2934dc822a44SJed Brown    Input Arguments:
2935dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2936dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2937dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2938dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2939dc822a44SJed Brown 
2940dc822a44SJed Brown    Level: advanced
2941dc822a44SJed Brown 
2942dc822a44SJed Brown    Notes:
2943dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2944dc822a44SJed Brown 
2945dc822a44SJed Brown    This function is currently not available from Fortran.
2946dc822a44SJed Brown 
2947dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2948dc822a44SJed Brown @*/
2949dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2950dc822a44SJed Brown {
2951dc822a44SJed Brown   PetscErrorCode    ierr;
2952dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2953dc822a44SJed Brown 
2954dc822a44SJed Brown   PetscFunctionBegin;
2955dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2956dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2957dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2958dc822a44SJed Brown       link = *p;
2959dc822a44SJed Brown       *p = link->next;
2960dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2961dc822a44SJed Brown       break;
2962dc822a44SJed Brown     }
2963dc822a44SJed Brown   }
2964dc822a44SJed Brown   PetscFunctionReturn(0);
2965dc822a44SJed Brown }
2966dc822a44SJed Brown 
2967dc822a44SJed Brown 
2968b17ce1afSJed Brown /*@
2969b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2970b17ce1afSJed Brown 
2971b17ce1afSJed Brown    Collective if any hooks are
2972b17ce1afSJed Brown 
2973b17ce1afSJed Brown    Input Arguments:
2974b17ce1afSJed Brown +  fine - finer DM to use as a base
2975b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2976e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2977b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2978e91eccc2SStefano Zampini -  coarse - coarser DM to update
2979b17ce1afSJed Brown 
2980b17ce1afSJed Brown    Level: developer
2981b17ce1afSJed Brown 
2982b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2983b17ce1afSJed Brown @*/
2984b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2985b17ce1afSJed Brown {
2986b17ce1afSJed Brown   PetscErrorCode    ierr;
2987b17ce1afSJed Brown   DMCoarsenHookLink link;
2988b17ce1afSJed Brown 
2989b17ce1afSJed Brown   PetscFunctionBegin;
2990b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
29918865f1eaSKarl Rupp     if (link->restricthook) {
29928865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
29938865f1eaSKarl Rupp     }
2994b17ce1afSJed Brown   }
299547c6ae99SBarry Smith   PetscFunctionReturn(0);
299647c6ae99SBarry Smith }
299747c6ae99SBarry Smith 
2998bb9467b5SJed Brown /*@C
2999be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
30005dbd56e3SPeter Brune 
3001d083f849SBarry Smith    Logically Collective on global
30025dbd56e3SPeter Brune 
30035dbd56e3SPeter Brune    Input Arguments:
30045dbd56e3SPeter Brune +  global - global DM
3005ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
30065dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
30070298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
30085dbd56e3SPeter Brune 
3009ec4806b8SPeter Brune 
3010ec4806b8SPeter Brune    Calling sequence for ddhook:
3011ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3012ec4806b8SPeter Brune 
3013ec4806b8SPeter Brune +  global - global DM
3014ec4806b8SPeter Brune .  block  - block DM
3015ec4806b8SPeter Brune -  ctx - optional user-defined function context
3016ec4806b8SPeter Brune 
30175dbd56e3SPeter Brune    Calling sequence for restricthook:
3018ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
30195dbd56e3SPeter Brune 
30205dbd56e3SPeter Brune +  global - global DM
30215dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
30225dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3023ec4806b8SPeter Brune .  block  - block DM
30245dbd56e3SPeter Brune -  ctx - optional user-defined function context
30255dbd56e3SPeter Brune 
30265dbd56e3SPeter Brune    Level: advanced
30275dbd56e3SPeter Brune 
30285dbd56e3SPeter Brune    Notes:
3029ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
30305dbd56e3SPeter Brune 
30315dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
30325dbd56e3SPeter Brune 
30335dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3034ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
30355dbd56e3SPeter Brune 
3036bb9467b5SJed Brown    This function is currently not available from Fortran.
3037bb9467b5SJed Brown 
30385dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
30395dbd56e3SPeter Brune @*/
3040be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
30415dbd56e3SPeter Brune {
30425dbd56e3SPeter Brune   PetscErrorCode      ierr;
3043be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
30445dbd56e3SPeter Brune 
30455dbd56e3SPeter Brune   PetscFunctionBegin;
30465dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3047b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3048b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3049b3a6b972SJed Brown   }
305095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30515dbd56e3SPeter Brune   link->restricthook = restricthook;
3052be081cd6SPeter Brune   link->ddhook       = ddhook;
30535dbd56e3SPeter Brune   link->ctx          = ctx;
30540298fd71SBarry Smith   link->next         = NULL;
30555dbd56e3SPeter Brune   *p                 = link;
30565dbd56e3SPeter Brune   PetscFunctionReturn(0);
30575dbd56e3SPeter Brune }
30585dbd56e3SPeter Brune 
3059b3a6b972SJed Brown /*@C
3060b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3061b3a6b972SJed Brown 
3062b3a6b972SJed Brown    Logically Collective
3063b3a6b972SJed Brown 
3064b3a6b972SJed Brown    Input Arguments:
3065b3a6b972SJed Brown +  global - global DM
3066b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3067b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3068b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3069b3a6b972SJed Brown 
3070b3a6b972SJed Brown    Level: advanced
3071b3a6b972SJed Brown 
3072b3a6b972SJed Brown    Notes:
3073b3a6b972SJed Brown 
3074b3a6b972SJed Brown    This function is currently not available from Fortran.
3075b3a6b972SJed Brown 
3076b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3077b3a6b972SJed Brown @*/
3078b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3079b3a6b972SJed Brown {
3080b3a6b972SJed Brown   PetscErrorCode      ierr;
3081b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3082b3a6b972SJed Brown 
3083b3a6b972SJed Brown   PetscFunctionBegin;
3084b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3085b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3086b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3087b3a6b972SJed Brown       link = *p;
3088b3a6b972SJed Brown       *p = link->next;
3089b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3090b3a6b972SJed Brown       break;
3091b3a6b972SJed Brown     }
3092b3a6b972SJed Brown   }
3093b3a6b972SJed Brown   PetscFunctionReturn(0);
3094b3a6b972SJed Brown }
3095b3a6b972SJed Brown 
30965dbd56e3SPeter Brune /*@
3097be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
30985dbd56e3SPeter Brune 
30995dbd56e3SPeter Brune    Collective if any hooks are
31005dbd56e3SPeter Brune 
31015dbd56e3SPeter Brune    Input Arguments:
31025dbd56e3SPeter Brune +  fine - finer DM to use as a base
3103be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3104be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
31055dbd56e3SPeter Brune -  coarse - coarer DM to update
31065dbd56e3SPeter Brune 
31075dbd56e3SPeter Brune    Level: developer
31085dbd56e3SPeter Brune 
31095dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
31105dbd56e3SPeter Brune @*/
3111be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
31125dbd56e3SPeter Brune {
31135dbd56e3SPeter Brune   PetscErrorCode      ierr;
3114be081cd6SPeter Brune   DMSubDomainHookLink link;
31155dbd56e3SPeter Brune 
31165dbd56e3SPeter Brune   PetscFunctionBegin;
3117be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
31188865f1eaSKarl Rupp     if (link->restricthook) {
31198865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
31208865f1eaSKarl Rupp     }
31215dbd56e3SPeter Brune   }
31225dbd56e3SPeter Brune   PetscFunctionReturn(0);
31235dbd56e3SPeter Brune }
31245dbd56e3SPeter Brune 
31255fe1f584SPeter Brune /*@
31266a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
31275fe1f584SPeter Brune 
31285fe1f584SPeter Brune     Not Collective
31295fe1f584SPeter Brune 
31305fe1f584SPeter Brune     Input Parameter:
31315fe1f584SPeter Brune .   dm - the DM object
31325fe1f584SPeter Brune 
31335fe1f584SPeter Brune     Output Parameter:
31346a7d9d85SPeter Brune .   level - number of coarsenings
31355fe1f584SPeter Brune 
31365fe1f584SPeter Brune     Level: developer
31375fe1f584SPeter Brune 
31386a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31395fe1f584SPeter Brune 
31405fe1f584SPeter Brune @*/
31415fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
31425fe1f584SPeter Brune {
31435fe1f584SPeter Brune   PetscFunctionBegin;
31445fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3145b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
31465fe1f584SPeter Brune   *level = dm->leveldown;
31475fe1f584SPeter Brune   PetscFunctionReturn(0);
31485fe1f584SPeter Brune }
31495fe1f584SPeter Brune 
31509a64c4a8SMatthew G. Knepley /*@
31519a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31529a64c4a8SMatthew G. Knepley 
31539a64c4a8SMatthew G. Knepley     Not Collective
31549a64c4a8SMatthew G. Knepley 
31559a64c4a8SMatthew G. Knepley     Input Parameters:
31569a64c4a8SMatthew G. Knepley +   dm - the DM object
31579a64c4a8SMatthew G. Knepley -   level - number of coarsenings
31589a64c4a8SMatthew G. Knepley 
31599a64c4a8SMatthew G. Knepley     Level: developer
31609a64c4a8SMatthew G. Knepley 
31619a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31629a64c4a8SMatthew G. Knepley @*/
31639a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
31649a64c4a8SMatthew G. Knepley {
31659a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
31669a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31679a64c4a8SMatthew G. Knepley   dm->leveldown = level;
31689a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
31699a64c4a8SMatthew G. Knepley }
31709a64c4a8SMatthew G. Knepley 
31715fe1f584SPeter Brune 
31725fe1f584SPeter Brune 
317347c6ae99SBarry Smith /*@C
317447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
317547c6ae99SBarry Smith 
3176d083f849SBarry Smith     Collective on dm
317747c6ae99SBarry Smith 
317847c6ae99SBarry Smith     Input Parameter:
317947c6ae99SBarry Smith +   dm - the DM object
318047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
318147c6ae99SBarry Smith 
318247c6ae99SBarry Smith     Output Parameter:
318347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
318447c6ae99SBarry Smith 
318547c6ae99SBarry Smith     Level: developer
318647c6ae99SBarry Smith 
3187e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
318847c6ae99SBarry Smith 
318947c6ae99SBarry Smith @*/
31907087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
319147c6ae99SBarry Smith {
319247c6ae99SBarry Smith   PetscErrorCode ierr;
319347c6ae99SBarry Smith 
319447c6ae99SBarry Smith   PetscFunctionBegin;
3195171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3196ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
319747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3198b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
319947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
320047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
320147c6ae99SBarry Smith   } else if (dm->ops->refine) {
320247c6ae99SBarry Smith     PetscInt i;
320347c6ae99SBarry Smith 
3204ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
320547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3206ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
320747c6ae99SBarry Smith     }
3208ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
320947c6ae99SBarry Smith   PetscFunctionReturn(0);
321047c6ae99SBarry Smith }
321147c6ae99SBarry Smith 
321247c6ae99SBarry Smith /*@C
321347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
321447c6ae99SBarry Smith 
3215d083f849SBarry Smith     Collective on dm
321647c6ae99SBarry Smith 
321747c6ae99SBarry Smith     Input Parameter:
321847c6ae99SBarry Smith +   dm - the DM object
321947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
322047c6ae99SBarry Smith 
322147c6ae99SBarry Smith     Output Parameter:
322247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
322347c6ae99SBarry Smith 
322447c6ae99SBarry Smith     Level: developer
322547c6ae99SBarry Smith 
3226e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
322747c6ae99SBarry Smith 
322847c6ae99SBarry Smith @*/
32297087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
323047c6ae99SBarry Smith {
323147c6ae99SBarry Smith   PetscErrorCode ierr;
323247c6ae99SBarry Smith 
323347c6ae99SBarry Smith   PetscFunctionBegin;
3234171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3235ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
323647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
323747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
323847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
323947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
324047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
324147c6ae99SBarry Smith     PetscInt i;
324247c6ae99SBarry Smith 
3243ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
324447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3245ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
324647c6ae99SBarry Smith     }
3247ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
324847c6ae99SBarry Smith   PetscFunctionReturn(0);
324947c6ae99SBarry Smith }
325047c6ae99SBarry Smith 
32511a266240SBarry Smith /*@C
32521a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32531a266240SBarry Smith 
32541a266240SBarry Smith     Not Collective
32551a266240SBarry Smith 
32561a266240SBarry Smith     Input Parameters:
32571a266240SBarry Smith +   dm - the DM object
32581a266240SBarry Smith -   destroy - the destroy function
32591a266240SBarry Smith 
32601a266240SBarry Smith     Level: intermediate
32611a266240SBarry Smith 
3262e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
32631a266240SBarry Smith 
3264f07f9ceaSJed Brown @*/
32651a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
32661a266240SBarry Smith {
32671a266240SBarry Smith   PetscFunctionBegin;
3268171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32691a266240SBarry Smith   dm->ctxdestroy = destroy;
32701a266240SBarry Smith   PetscFunctionReturn(0);
32711a266240SBarry Smith }
32721a266240SBarry Smith 
3273b07ff414SBarry Smith /*@
32741b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
327547c6ae99SBarry Smith 
327647c6ae99SBarry Smith     Not Collective
327747c6ae99SBarry Smith 
327847c6ae99SBarry Smith     Input Parameters:
327947c6ae99SBarry Smith +   dm - the DM object
328047c6ae99SBarry Smith -   ctx - the user context
328147c6ae99SBarry Smith 
328247c6ae99SBarry Smith     Level: intermediate
328347c6ae99SBarry Smith 
3284e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
328547c6ae99SBarry Smith 
328647c6ae99SBarry Smith @*/
32871b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
328847c6ae99SBarry Smith {
328947c6ae99SBarry Smith   PetscFunctionBegin;
3290171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
329147c6ae99SBarry Smith   dm->ctx = ctx;
329247c6ae99SBarry Smith   PetscFunctionReturn(0);
329347c6ae99SBarry Smith }
329447c6ae99SBarry Smith 
329547c6ae99SBarry Smith /*@
32961b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
329747c6ae99SBarry Smith 
329847c6ae99SBarry Smith     Not Collective
329947c6ae99SBarry Smith 
330047c6ae99SBarry Smith     Input Parameter:
330147c6ae99SBarry Smith .   dm - the DM object
330247c6ae99SBarry Smith 
330347c6ae99SBarry Smith     Output Parameter:
330447c6ae99SBarry Smith .   ctx - the user context
330547c6ae99SBarry Smith 
330647c6ae99SBarry Smith     Level: intermediate
330747c6ae99SBarry Smith 
3308e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
330947c6ae99SBarry Smith 
331047c6ae99SBarry Smith @*/
33111b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
331247c6ae99SBarry Smith {
331347c6ae99SBarry Smith   PetscFunctionBegin;
3314171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33151b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
331647c6ae99SBarry Smith   PetscFunctionReturn(0);
331747c6ae99SBarry Smith }
331847c6ae99SBarry Smith 
331908da532bSDmitry Karpeev /*@C
3320df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
332108da532bSDmitry Karpeev 
3322d083f849SBarry Smith     Logically Collective on dm
332308da532bSDmitry Karpeev 
332408da532bSDmitry Karpeev     Input Parameter:
332508da532bSDmitry Karpeev +   dm - the DM object
33260298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
332708da532bSDmitry Karpeev 
332808da532bSDmitry Karpeev     Level: intermediate
332908da532bSDmitry Karpeev 
3330835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
333108da532bSDmitry Karpeev          DMSetJacobian()
333208da532bSDmitry Karpeev 
333308da532bSDmitry Karpeev @*/
333408da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
333508da532bSDmitry Karpeev {
333608da532bSDmitry Karpeev   PetscFunctionBegin;
33375a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
333808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
333908da532bSDmitry Karpeev   PetscFunctionReturn(0);
334008da532bSDmitry Karpeev }
334108da532bSDmitry Karpeev 
334208da532bSDmitry Karpeev /*@
334308da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
334408da532bSDmitry Karpeev 
334508da532bSDmitry Karpeev     Not Collective
334608da532bSDmitry Karpeev 
334708da532bSDmitry Karpeev     Input Parameter:
334808da532bSDmitry Karpeev .   dm - the DM object to destroy
334908da532bSDmitry Karpeev 
335008da532bSDmitry Karpeev     Output Parameter:
335108da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
335208da532bSDmitry Karpeev 
335308da532bSDmitry Karpeev     Level: developer
335408da532bSDmitry Karpeev 
335574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
335608da532bSDmitry Karpeev 
335708da532bSDmitry Karpeev @*/
335808da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
335908da532bSDmitry Karpeev {
336008da532bSDmitry Karpeev   PetscFunctionBegin;
33615a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3362534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
336308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
336408da532bSDmitry Karpeev   PetscFunctionReturn(0);
336508da532bSDmitry Karpeev }
336608da532bSDmitry Karpeev 
336708da532bSDmitry Karpeev /*@C
336808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
336908da532bSDmitry Karpeev 
3370d083f849SBarry Smith     Logically Collective on dm
337108da532bSDmitry Karpeev 
337208da532bSDmitry Karpeev     Input Parameters:
3373907376e6SBarry Smith .   dm - the DM object
337408da532bSDmitry Karpeev 
337508da532bSDmitry Karpeev     Output parameters:
337608da532bSDmitry Karpeev +   xl - lower bound
337708da532bSDmitry Karpeev -   xu - upper bound
337808da532bSDmitry Karpeev 
3379907376e6SBarry Smith     Level: advanced
3380907376e6SBarry Smith 
338195452b02SPatrick Sanan     Notes:
338295452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
338308da532bSDmitry Karpeev 
338474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
338508da532bSDmitry Karpeev 
338608da532bSDmitry Karpeev @*/
338708da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
338808da532bSDmitry Karpeev {
338908da532bSDmitry Karpeev   PetscErrorCode ierr;
33905fd66863SKarl Rupp 
339108da532bSDmitry Karpeev   PetscFunctionBegin;
33925a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
339308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
33945a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3395b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
339608da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
339708da532bSDmitry Karpeev   PetscFunctionReturn(0);
339808da532bSDmitry Karpeev }
339908da532bSDmitry Karpeev 
3400b0ae01b7SPeter Brune /*@
3401b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3402b0ae01b7SPeter Brune 
3403b0ae01b7SPeter Brune     Not Collective
3404b0ae01b7SPeter Brune 
3405b0ae01b7SPeter Brune     Input Parameter:
3406b0ae01b7SPeter Brune .   dm - the DM object
3407b0ae01b7SPeter Brune 
3408b0ae01b7SPeter Brune     Output Parameter:
3409b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3410b0ae01b7SPeter Brune 
3411b0ae01b7SPeter Brune     Level: developer
3412b0ae01b7SPeter Brune 
34131565f0a7SPatrick Sanan .seealso DMCreateColoring()
3414b0ae01b7SPeter Brune 
3415b0ae01b7SPeter Brune @*/
3416b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3417b0ae01b7SPeter Brune {
3418b0ae01b7SPeter Brune   PetscFunctionBegin;
34195a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3420534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3421b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3422b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3423b0ae01b7SPeter Brune }
3424b0ae01b7SPeter Brune 
34253ad4599aSBarry Smith /*@
34263ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
34273ad4599aSBarry Smith 
34283ad4599aSBarry Smith     Not Collective
34293ad4599aSBarry Smith 
34303ad4599aSBarry Smith     Input Parameter:
34313ad4599aSBarry Smith .   dm - the DM object
34323ad4599aSBarry Smith 
34333ad4599aSBarry Smith     Output Parameter:
34343ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
34353ad4599aSBarry Smith 
34363ad4599aSBarry Smith     Level: developer
34373ad4599aSBarry Smith 
34381565f0a7SPatrick Sanan .seealso DMCreateRestriction()
34393ad4599aSBarry Smith 
34403ad4599aSBarry Smith @*/
34413ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
34423ad4599aSBarry Smith {
34433ad4599aSBarry Smith   PetscFunctionBegin;
34445a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3445534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34463ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34473ad4599aSBarry Smith   PetscFunctionReturn(0);
34483ad4599aSBarry Smith }
34493ad4599aSBarry Smith 
3450a7058e45SLawrence Mitchell 
3451a7058e45SLawrence Mitchell /*@
3452a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3453a7058e45SLawrence Mitchell 
3454a7058e45SLawrence Mitchell     Not Collective
3455a7058e45SLawrence Mitchell 
3456a7058e45SLawrence Mitchell     Input Parameter:
3457a7058e45SLawrence Mitchell .   dm - the DM object
3458a7058e45SLawrence Mitchell 
3459a7058e45SLawrence Mitchell     Output Parameter:
3460a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3461a7058e45SLawrence Mitchell 
3462a7058e45SLawrence Mitchell     Level: developer
3463a7058e45SLawrence Mitchell 
34641565f0a7SPatrick Sanan .seealso DMCreateInjection()
3465a7058e45SLawrence Mitchell 
3466a7058e45SLawrence Mitchell @*/
3467a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3468a7058e45SLawrence Mitchell {
34694a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
34705a84ad33SLisandro Dalcin 
3471a7058e45SLawrence Mitchell   PetscFunctionBegin;
34725a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3473534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34745a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
34754a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
34765a84ad33SLisandro Dalcin   } else {
34775a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
34785a84ad33SLisandro Dalcin   }
3479a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3480a7058e45SLawrence Mitchell }
3481a7058e45SLawrence Mitchell 
34820298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3483264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3484264ace61SBarry Smith 
3485264ace61SBarry Smith /*@C
3486264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3487264ace61SBarry Smith 
3488d083f849SBarry Smith   Collective on dm
3489264ace61SBarry Smith 
3490264ace61SBarry Smith   Input Parameters:
3491264ace61SBarry Smith + dm     - The DM object
3492264ace61SBarry Smith - method - The name of the DM type
3493264ace61SBarry Smith 
3494264ace61SBarry Smith   Options Database Key:
3495264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3496264ace61SBarry Smith 
3497264ace61SBarry Smith   Notes:
3498e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3499264ace61SBarry Smith 
3500264ace61SBarry Smith   Level: intermediate
3501264ace61SBarry Smith 
3502264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3503264ace61SBarry Smith @*/
350419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3505264ace61SBarry Smith {
3506264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3507264ace61SBarry Smith   PetscBool      match;
3508264ace61SBarry Smith   PetscErrorCode ierr;
3509264ace61SBarry Smith 
3510264ace61SBarry Smith   PetscFunctionBegin;
3511264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3512251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3513264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3514264ace61SBarry Smith 
35150f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35161c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3517ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3518264ace61SBarry Smith 
3519264ace61SBarry Smith   if (dm->ops->destroy) {
3520264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3521264ace61SBarry Smith   }
3522d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3523264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3524d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3525264ace61SBarry Smith   PetscFunctionReturn(0);
3526264ace61SBarry Smith }
3527264ace61SBarry Smith 
3528264ace61SBarry Smith /*@C
3529264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3530264ace61SBarry Smith 
3531264ace61SBarry Smith   Not Collective
3532264ace61SBarry Smith 
3533264ace61SBarry Smith   Input Parameter:
3534264ace61SBarry Smith . dm  - The DM
3535264ace61SBarry Smith 
3536264ace61SBarry Smith   Output Parameter:
3537264ace61SBarry Smith . type - The DM type name
3538264ace61SBarry Smith 
3539264ace61SBarry Smith   Level: intermediate
3540264ace61SBarry Smith 
3541264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3542264ace61SBarry Smith @*/
354319fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3544264ace61SBarry Smith {
3545264ace61SBarry Smith   PetscErrorCode ierr;
3546264ace61SBarry Smith 
3547264ace61SBarry Smith   PetscFunctionBegin;
3548264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3549c959eef4SJed Brown   PetscValidPointer(type,2);
3550607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3551264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3552264ace61SBarry Smith   PetscFunctionReturn(0);
3553264ace61SBarry Smith }
3554264ace61SBarry Smith 
355567a56275SMatthew G Knepley /*@C
355667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
355767a56275SMatthew G Knepley 
3558d083f849SBarry Smith   Collective on dm
355967a56275SMatthew G Knepley 
356067a56275SMatthew G Knepley   Input Parameters:
356167a56275SMatthew G Knepley + dm - the DM
356267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
356367a56275SMatthew G Knepley 
356467a56275SMatthew G Knepley   Output Parameter:
356567a56275SMatthew G Knepley . M - pointer to new DM
356667a56275SMatthew G Knepley 
356767a56275SMatthew G Knepley   Notes:
356867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
356967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
357067a56275SMatthew G Knepley   of the input DM.
357167a56275SMatthew G Knepley 
357267a56275SMatthew G Knepley   Level: intermediate
357367a56275SMatthew G Knepley 
357467a56275SMatthew G Knepley .seealso: DMCreate()
357567a56275SMatthew G Knepley @*/
357619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
357767a56275SMatthew G Knepley {
357867a56275SMatthew G Knepley   DM             B;
357967a56275SMatthew G Knepley   char           convname[256];
3580c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
358167a56275SMatthew G Knepley   PetscErrorCode ierr;
358267a56275SMatthew G Knepley 
358367a56275SMatthew G Knepley   PetscFunctionBegin;
358467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
358567a56275SMatthew G Knepley   PetscValidType(dm,1);
358667a56275SMatthew G Knepley   PetscValidPointer(M,3);
3587251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3588c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3589c067b6caSMatthew G. Knepley   if (sametype) {
3590c067b6caSMatthew G. Knepley     *M   = dm;
3591c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3592c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3593c067b6caSMatthew G. Knepley   } else {
35940298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
359567a56275SMatthew G Knepley 
359667a56275SMatthew G Knepley     /*
359767a56275SMatthew G Knepley        Order of precedence:
359867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
359967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
360067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
360167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
360267a56275SMatthew G Knepley        5) Use a really basic converter.
360367a56275SMatthew G Knepley     */
360467a56275SMatthew G Knepley 
360567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3606a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3607a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3608a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3609a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3610a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36110005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
361267a56275SMatthew G Knepley     if (conv) goto foundconv;
361367a56275SMatthew G Knepley 
361467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
361582f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
361667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3617a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3618a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3619a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3620a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3621a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36220005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
362367a56275SMatthew G Knepley     if (conv) {
3624fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
362567a56275SMatthew G Knepley       goto foundconv;
362667a56275SMatthew G Knepley     }
362767a56275SMatthew G Knepley 
362867a56275SMatthew G Knepley #if 0
362967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
363067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3631fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
363267a56275SMatthew G Knepley     if (conv) goto foundconv;
363367a56275SMatthew G Knepley 
363467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
363567a56275SMatthew G Knepley     if (dm->ops->convert) {
363667a56275SMatthew G Knepley       conv = dm->ops->convert;
363767a56275SMatthew G Knepley     }
363867a56275SMatthew G Knepley     if (conv) goto foundconv;
363967a56275SMatthew G Knepley #endif
364067a56275SMatthew G Knepley 
364167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
364282f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
364367a56275SMatthew G Knepley 
364467a56275SMatthew G Knepley foundconv:
364567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
364667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
364712fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
364890b157c4SStefano Zampini     {
364990b157c4SStefano Zampini       PetscBool             isper;
365012fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
365112fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
365290b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
365390b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
365412fa691eSMatthew G. Knepley     }
365567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
365667a56275SMatthew G Knepley   }
365767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
365867a56275SMatthew G Knepley   PetscFunctionReturn(0);
365967a56275SMatthew G Knepley }
3660264ace61SBarry Smith 
3661264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3662264ace61SBarry Smith 
3663264ace61SBarry Smith /*@C
36641c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
36651c84c290SBarry Smith 
36661c84c290SBarry Smith   Not Collective
36671c84c290SBarry Smith 
36681c84c290SBarry Smith   Input Parameters:
36691c84c290SBarry Smith + name        - The name of a new user-defined creation routine
36701c84c290SBarry Smith - create_func - The creation routine itself
36711c84c290SBarry Smith 
36721c84c290SBarry Smith   Notes:
36731c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
36741c84c290SBarry Smith 
36751c84c290SBarry Smith 
36761c84c290SBarry Smith   Sample usage:
36771c84c290SBarry Smith .vb
3678bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
36791c84c290SBarry Smith .ve
36801c84c290SBarry Smith 
36811c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
36821c84c290SBarry Smith .vb
36831c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
36841c84c290SBarry Smith     DMSetType(DM,"my_da");
36851c84c290SBarry Smith .ve
36861c84c290SBarry Smith    or at runtime via the option
36871c84c290SBarry Smith .vb
36881c84c290SBarry Smith     -da_type my_da
36891c84c290SBarry Smith .ve
3690264ace61SBarry Smith 
3691264ace61SBarry Smith   Level: advanced
36921c84c290SBarry Smith 
3693bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
36941c84c290SBarry Smith 
3695264ace61SBarry Smith @*/
3696bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3697264ace61SBarry Smith {
3698264ace61SBarry Smith   PetscErrorCode ierr;
3699264ace61SBarry Smith 
3700264ace61SBarry Smith   PetscFunctionBegin;
37011d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3702a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3703264ace61SBarry Smith   PetscFunctionReturn(0);
3704264ace61SBarry Smith }
3705264ace61SBarry Smith 
3706b859378eSBarry Smith /*@C
370755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3708b859378eSBarry Smith 
3709d083f849SBarry Smith   Collective on viewer
3710b859378eSBarry Smith 
3711b859378eSBarry Smith   Input Parameters:
3712b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3713b859378eSBarry Smith            some related function before a call to DMLoad().
3714b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3715b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3716b859378eSBarry Smith 
3717b859378eSBarry Smith    Level: intermediate
3718b859378eSBarry Smith 
3719b859378eSBarry Smith   Notes:
372055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3721b859378eSBarry Smith 
3722b859378eSBarry Smith   Notes for advanced users:
3723b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3724b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3725b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3726b859378eSBarry Smith   format is
3727b859378eSBarry Smith .vb
3728b859378eSBarry Smith      has not yet been determined
3729b859378eSBarry Smith .ve
3730b859378eSBarry Smith 
3731b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3732b859378eSBarry Smith @*/
3733b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3734b859378eSBarry Smith {
37359331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3736b859378eSBarry Smith   PetscErrorCode ierr;
3737b859378eSBarry Smith 
3738b859378eSBarry Smith   PetscFunctionBegin;
3739b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3740b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3741fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
374232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37439331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
374458cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
37459331c7a4SMatthew G. Knepley   if (isbinary) {
37469331c7a4SMatthew G. Knepley     PetscInt classid;
37479331c7a4SMatthew G. Knepley     char     type[256];
3748b859378eSBarry Smith 
3749060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37509200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3751060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
375232c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37539331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37549331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37559331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37569331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
375758cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3758b859378eSBarry Smith   PetscFunctionReturn(0);
3759b859378eSBarry Smith }
3760b859378eSBarry Smith 
3761b2e4378dSMatthew G. Knepley /*@
3762b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3763b2e4378dSMatthew G. Knepley 
3764b2e4378dSMatthew G. Knepley   Not collective
3765b2e4378dSMatthew G. Knepley 
3766b2e4378dSMatthew G. Knepley   Input Parameter:
3767b2e4378dSMatthew G. Knepley . dm - the DM
3768b2e4378dSMatthew G. Knepley 
3769b2e4378dSMatthew G. Knepley   Output Parameters:
3770b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3771b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3772b2e4378dSMatthew G. Knepley 
3773b2e4378dSMatthew G. Knepley   Level: beginner
3774b2e4378dSMatthew G. Knepley 
3775b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3776b2e4378dSMatthew G. Knepley 
3777b2e4378dSMatthew G. Knepley 
3778b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3779b2e4378dSMatthew G. Knepley @*/
3780b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3781b2e4378dSMatthew G. Knepley {
3782b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3783b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3784b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3785b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3786b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3787b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3788b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3789b2e4378dSMatthew G. Knepley 
3790b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3791b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3792b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3793b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3794b2e4378dSMatthew G. Knepley   if (coords) {
3795b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3796b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3797b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3798b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3799b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3800b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3801b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3802b2e4378dSMatthew G. Knepley       }
3803b2e4378dSMatthew G. Knepley     }
3804b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3805b2e4378dSMatthew G. Knepley   } else {
3806b2e4378dSMatthew G. Knepley     PetscBool isda;
3807b2e4378dSMatthew G. Knepley 
3808b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3809b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3810b2e4378dSMatthew G. Knepley   }
3811b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3812b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3813b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3814b2e4378dSMatthew G. Knepley }
3815b2e4378dSMatthew G. Knepley 
3816b2e4378dSMatthew G. Knepley /*@
3817b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3818b2e4378dSMatthew G. Knepley 
3819b2e4378dSMatthew G. Knepley   Collective
3820b2e4378dSMatthew G. Knepley 
3821b2e4378dSMatthew G. Knepley   Input Parameter:
3822b2e4378dSMatthew G. Knepley . dm - the DM
3823b2e4378dSMatthew G. Knepley 
3824b2e4378dSMatthew G. Knepley   Output Parameters:
3825b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3826b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3827b2e4378dSMatthew G. Knepley 
3828b2e4378dSMatthew G. Knepley   Level: beginner
3829b2e4378dSMatthew G. Knepley 
3830b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3831b2e4378dSMatthew G. Knepley @*/
3832b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3833b2e4378dSMatthew G. Knepley {
3834b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
38356ce308c4SMatthew G. Knepley   PetscInt       cdim;
38366ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3837b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3838b2e4378dSMatthew G. Knepley 
3839b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3840b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3841b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3842b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3843b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3844b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3845b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3846b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3847b2e4378dSMatthew G. Knepley }
3848b2e4378dSMatthew G. Knepley 
38497da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38507da65231SMatthew G Knepley 
3851a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3852a6dfd86eSKarl Rupp {
38531d47ebbbSSatish Balay   PetscInt       f;
38541b30c384SMatthew G Knepley   PetscErrorCode ierr;
38551b30c384SMatthew G Knepley 
38567da65231SMatthew G Knepley   PetscFunctionBegin;
385774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38581d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
385957622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
38607da65231SMatthew G Knepley   }
38617da65231SMatthew G Knepley   PetscFunctionReturn(0);
38627da65231SMatthew G Knepley }
38637da65231SMatthew G Knepley 
3864a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3865a6dfd86eSKarl Rupp {
38661b30c384SMatthew G Knepley   PetscInt       f, g;
38677da65231SMatthew G Knepley   PetscErrorCode ierr;
38687da65231SMatthew G Knepley 
38697da65231SMatthew G Knepley   PetscFunctionBegin;
387074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38711d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
387274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
38731d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3874e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
38757da65231SMatthew G Knepley     }
387674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
38777da65231SMatthew G Knepley   }
38787da65231SMatthew G Knepley   PetscFunctionReturn(0);
38797da65231SMatthew G Knepley }
3880e7c4fc90SDmitry Karpeev 
38816113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3882e759306cSMatthew G. Knepley {
38830c5b8624SToby Isaac   PetscInt          localSize, bs;
38840c5b8624SToby Isaac   PetscMPIInt       size;
38850c5b8624SToby Isaac   Vec               x, xglob;
38860c5b8624SToby Isaac   const PetscScalar *xarray;
3887e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3888e759306cSMatthew G. Knepley 
3889e759306cSMatthew G. Knepley   PetscFunctionBegin;
38909852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3891e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3892e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
38936113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
38940c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
38950c5b8624SToby Isaac   if (size > 1) {
38960c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
38970c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
38980c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
38990c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
39000c5b8624SToby Isaac   } else {
39010c5b8624SToby Isaac     xglob = x;
39020c5b8624SToby Isaac   }
39030c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
39040c5b8624SToby Isaac   if (size > 1) {
39050c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
39060c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
39070c5b8624SToby Isaac   }
3908e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3909e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3910e759306cSMatthew G. Knepley }
3911e759306cSMatthew G. Knepley 
391288ed4aceSMatthew G Knepley /*@
39131bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3914061576a5SJed Brown 
3915061576a5SJed Brown   Input Parameter:
3916061576a5SJed Brown . dm - The DM
3917061576a5SJed Brown 
3918061576a5SJed Brown   Output Parameter:
3919061576a5SJed Brown . section - The PetscSection
3920061576a5SJed Brown 
3921061576a5SJed Brown   Options Database Keys:
3922061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3923061576a5SJed Brown 
3924061576a5SJed Brown   Level: advanced
3925061576a5SJed Brown 
3926061576a5SJed Brown   Notes:
3927061576a5SJed Brown   Use DMGetLocalSection() in new code.
3928061576a5SJed Brown 
3929061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3930061576a5SJed Brown 
3931061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3932061576a5SJed Brown @*/
3933061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3934061576a5SJed Brown {
3935061576a5SJed Brown   PetscErrorCode ierr;
3936061576a5SJed Brown 
3937061576a5SJed Brown   PetscFunctionBegin;
3938061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3939061576a5SJed Brown   PetscFunctionReturn(0);
3940061576a5SJed Brown }
3941061576a5SJed Brown 
3942061576a5SJed Brown /*@
3943061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
394488ed4aceSMatthew G Knepley 
394588ed4aceSMatthew G Knepley   Input Parameter:
394688ed4aceSMatthew G Knepley . dm - The DM
394788ed4aceSMatthew G Knepley 
394888ed4aceSMatthew G Knepley   Output Parameter:
394988ed4aceSMatthew G Knepley . section - The PetscSection
395088ed4aceSMatthew G Knepley 
3951e5893cccSMatthew G. Knepley   Options Database Keys:
3952e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3953e5893cccSMatthew G. Knepley 
395488ed4aceSMatthew G Knepley   Level: intermediate
395588ed4aceSMatthew G Knepley 
395688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
395788ed4aceSMatthew G Knepley 
3958061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
395988ed4aceSMatthew G Knepley @*/
3960061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
39610adebc6cSBarry Smith {
3962fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3963fd59a867SMatthew G. Knepley 
396488ed4aceSMatthew G Knepley   PetscFunctionBegin;
396588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
396688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
39671bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
3968e5e52638SMatthew G. Knepley     PetscInt d;
3969e5e52638SMatthew G. Knepley 
3970e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
39711bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
39721bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
39732f0f8703SMatthew G. Knepley   }
39741bb6d2a8SBarry Smith   *section = dm->localSection;
397588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
397688ed4aceSMatthew G Knepley }
397788ed4aceSMatthew G Knepley 
397888ed4aceSMatthew G Knepley /*@
39791bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
3980061576a5SJed Brown 
3981061576a5SJed Brown   Input Parameters:
3982061576a5SJed Brown + dm - The DM
3983061576a5SJed Brown - section - The PetscSection
3984061576a5SJed Brown 
3985061576a5SJed Brown   Level: advanced
3986061576a5SJed Brown 
3987061576a5SJed Brown   Notes:
3988061576a5SJed Brown   Use DMSetLocalSection() in new code.
3989061576a5SJed Brown 
3990061576a5SJed Brown   Any existing Section will be destroyed
3991061576a5SJed Brown 
3992061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
3993061576a5SJed Brown @*/
3994061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
3995061576a5SJed Brown {
3996061576a5SJed Brown   PetscErrorCode ierr;
3997061576a5SJed Brown 
3998061576a5SJed Brown   PetscFunctionBegin;
3999061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4000061576a5SJed Brown   PetscFunctionReturn(0);
4001061576a5SJed Brown }
4002061576a5SJed Brown 
4003061576a5SJed Brown /*@
4004061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
400588ed4aceSMatthew G Knepley 
400688ed4aceSMatthew G Knepley   Input Parameters:
400788ed4aceSMatthew G Knepley + dm - The DM
400888ed4aceSMatthew G Knepley - section - The PetscSection
400988ed4aceSMatthew G Knepley 
401088ed4aceSMatthew G Knepley   Level: intermediate
401188ed4aceSMatthew G Knepley 
401288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
401388ed4aceSMatthew G Knepley 
4014061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
401588ed4aceSMatthew G Knepley @*/
4016061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
40170adebc6cSBarry Smith {
4018c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4019af122d2aSMatthew G Knepley   PetscInt       f;
402088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
402188ed4aceSMatthew G Knepley 
402288ed4aceSMatthew G Knepley   PetscFunctionBegin;
402388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4024b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40251d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40261bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
40271bb6d2a8SBarry Smith   dm->localSection = section;
40281bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4029af122d2aSMatthew G Knepley   if (numFields) {
4030af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4031af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40320f21e855SMatthew G. Knepley       PetscObject disc;
4033af122d2aSMatthew G Knepley       const char *name;
4034af122d2aSMatthew G Knepley 
40351bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
403644a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40370f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4038af122d2aSMatthew G Knepley     }
4039af122d2aSMatthew G Knepley   }
4040e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40411bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
404288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
404388ed4aceSMatthew G Knepley }
404488ed4aceSMatthew G Knepley 
40459435951eSToby Isaac /*@
4046b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40479435951eSToby Isaac 
4048e228b242SToby Isaac   not collective
4049e228b242SToby Isaac 
40509435951eSToby Isaac   Input Parameter:
40519435951eSToby Isaac . dm - The DM
40529435951eSToby Isaac 
40539435951eSToby Isaac   Output Parameter:
40549435951eSToby 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.
40559435951eSToby 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.
40569435951eSToby Isaac 
40579435951eSToby Isaac   Level: advanced
40589435951eSToby Isaac 
40599435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
40609435951eSToby Isaac 
40619435951eSToby Isaac .seealso: DMSetDefaultConstraints()
40629435951eSToby Isaac @*/
40639435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
40649435951eSToby Isaac {
40659435951eSToby Isaac   PetscErrorCode ierr;
40669435951eSToby Isaac 
40679435951eSToby Isaac   PetscFunctionBegin;
40689435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40699435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
407045a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
407145a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
40729435951eSToby Isaac   PetscFunctionReturn(0);
40739435951eSToby Isaac }
40749435951eSToby Isaac 
40759435951eSToby Isaac /*@
4076b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
40779435951eSToby Isaac 
40789435951eSToby 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().
40799435951eSToby Isaac 
40809435951eSToby 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.
40819435951eSToby Isaac 
4082e228b242SToby Isaac   collective on dm
4083e228b242SToby Isaac 
40849435951eSToby Isaac   Input Parameters:
40859435951eSToby Isaac + dm - The DM
4086e228b242SToby 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).
4087e228b242SToby 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).
40889435951eSToby Isaac 
40899435951eSToby Isaac   Level: advanced
40909435951eSToby Isaac 
40919435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
40929435951eSToby Isaac 
40939435951eSToby Isaac .seealso: DMGetDefaultConstraints()
40949435951eSToby Isaac @*/
40959435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
40969435951eSToby Isaac {
4097e228b242SToby Isaac   PetscMPIInt result;
40989435951eSToby Isaac   PetscErrorCode ierr;
40999435951eSToby Isaac 
41009435951eSToby Isaac   PetscFunctionBegin;
41019435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4102e228b242SToby Isaac   if (section) {
4103e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4104e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4105f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4106e228b242SToby Isaac   }
4107e228b242SToby Isaac   if (mat) {
4108e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4109e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4110f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4111e228b242SToby Isaac   }
41129435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41139435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
41149435951eSToby Isaac   dm->defaultConstraintSection = section;
41159435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
41169435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
41179435951eSToby Isaac   dm->defaultConstraintMat = mat;
41189435951eSToby Isaac   PetscFunctionReturn(0);
41199435951eSToby Isaac }
41209435951eSToby Isaac 
4121497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4122507e4973SMatthew G. Knepley /*
4123507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4124507e4973SMatthew G. Knepley 
4125507e4973SMatthew G. Knepley   Input Parameters:
4126507e4973SMatthew G. Knepley + dm - The DM
4127507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4128507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4129507e4973SMatthew G. Knepley 
4130507e4973SMatthew G. Knepley   Level: intermediate
4131507e4973SMatthew G. Knepley 
41321bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4133507e4973SMatthew G. Knepley */
4134f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4135507e4973SMatthew G. Knepley {
4136507e4973SMatthew G. Knepley   MPI_Comm        comm;
4137507e4973SMatthew G. Knepley   PetscLayout     layout;
4138507e4973SMatthew G. Knepley   const PetscInt *ranges;
4139507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4140507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4141507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4142507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4143507e4973SMatthew G. Knepley 
4144507e4973SMatthew G. Knepley   PetscFunctionBegin;
4145507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4146507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4147507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4148507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4149507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4150507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4151507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4152507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4153507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4154507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4155507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4156507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4157f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4158507e4973SMatthew G. Knepley 
4159507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4160507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4161507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4162507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4163507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4164507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4165507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4166507e4973SMatthew 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;}
4167507e4973SMatthew 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;}
4168507e4973SMatthew G. Knepley     if (gdof < 0) {
4169507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4170507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4171507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4172507e4973SMatthew G. Knepley 
4173507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4174507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4175507e4973SMatthew 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;}
4176507e4973SMatthew G. Knepley       }
4177507e4973SMatthew G. Knepley     }
4178507e4973SMatthew G. Knepley   }
4179507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4180507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4181b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4182507e4973SMatthew G. Knepley   if (!gvalid) {
4183507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4184507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4185507e4973SMatthew G. Knepley   }
4186507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4187507e4973SMatthew G. Knepley }
4188f741bcd2SMatthew G. Knepley #endif
4189507e4973SMatthew G. Knepley 
419088ed4aceSMatthew G Knepley /*@
4191e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
419288ed4aceSMatthew G Knepley 
4193d083f849SBarry Smith   Collective on dm
41948b1ab98fSJed Brown 
419588ed4aceSMatthew G Knepley   Input Parameter:
419688ed4aceSMatthew G Knepley . dm - The DM
419788ed4aceSMatthew G Knepley 
419888ed4aceSMatthew G Knepley   Output Parameter:
419988ed4aceSMatthew G Knepley . section - The PetscSection
420088ed4aceSMatthew G Knepley 
420188ed4aceSMatthew G Knepley   Level: intermediate
420288ed4aceSMatthew G Knepley 
420388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
420488ed4aceSMatthew G Knepley 
420592fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
420688ed4aceSMatthew G Knepley @*/
4207e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
42080adebc6cSBarry Smith {
420988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
421088ed4aceSMatthew G Knepley 
421188ed4aceSMatthew G Knepley   PetscFunctionBegin;
421288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
421388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42141bb6d2a8SBarry Smith   if (!dm->globalSection) {
4215fd59a867SMatthew G. Knepley     PetscSection s;
4216fd59a867SMatthew G. Knepley 
421792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4218fd59a867SMatthew 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");
421933907cc2SStefano 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");
42201bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4221cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
42221bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
42231bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
422488ed4aceSMatthew G Knepley   }
42251bb6d2a8SBarry Smith   *section = dm->globalSection;
422688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
422788ed4aceSMatthew G Knepley }
422888ed4aceSMatthew G Knepley 
4229b21d0597SMatthew G Knepley /*@
4230e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4231b21d0597SMatthew G Knepley 
4232b21d0597SMatthew G Knepley   Input Parameters:
4233b21d0597SMatthew G Knepley + dm - The DM
42345080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4235b21d0597SMatthew G Knepley 
4236b21d0597SMatthew G Knepley   Level: intermediate
4237b21d0597SMatthew G Knepley 
4238b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4239b21d0597SMatthew G Knepley 
424092fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4241b21d0597SMatthew G Knepley @*/
4242e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42430adebc6cSBarry Smith {
4244b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4245b21d0597SMatthew G Knepley 
4246b21d0597SMatthew G Knepley   PetscFunctionBegin;
4247b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42485080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42491d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42501bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
42511bb6d2a8SBarry Smith   dm->globalSection = section;
4252497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
42531bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4254507e4973SMatthew G. Knepley #endif
4255b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4256b21d0597SMatthew G Knepley }
4257b21d0597SMatthew G Knepley 
425888ed4aceSMatthew G Knepley /*@
42591bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
426088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
426188ed4aceSMatthew G Knepley 
426288ed4aceSMatthew G Knepley   Input Parameter:
426388ed4aceSMatthew G Knepley . dm - The DM
426488ed4aceSMatthew G Knepley 
426588ed4aceSMatthew G Knepley   Output Parameter:
426688ed4aceSMatthew G Knepley . sf - The PetscSF
426788ed4aceSMatthew G Knepley 
426888ed4aceSMatthew G Knepley   Level: intermediate
426988ed4aceSMatthew G Knepley 
427088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
427188ed4aceSMatthew G Knepley 
42721bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
427388ed4aceSMatthew G Knepley @*/
42741bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
42750adebc6cSBarry Smith {
427688ed4aceSMatthew G Knepley   PetscInt       nroots;
427788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
427888ed4aceSMatthew G Knepley 
427988ed4aceSMatthew G Knepley   PetscFunctionBegin;
428088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
428188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
42821bb6d2a8SBarry Smith   if (!dm->sectionSF) {
42831bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
428433907cc2SStefano Zampini   }
42851bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
428688ed4aceSMatthew G Knepley   if (nroots < 0) {
428788ed4aceSMatthew G Knepley     PetscSection section, gSection;
428888ed4aceSMatthew G Knepley 
428992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
429031ea6d37SMatthew G Knepley     if (section) {
4291e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
42921bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
429331ea6d37SMatthew G Knepley     } else {
42940298fd71SBarry Smith       *sf = NULL;
429531ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
429631ea6d37SMatthew G Knepley     }
429788ed4aceSMatthew G Knepley   }
42981bb6d2a8SBarry Smith   *sf = dm->sectionSF;
429988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
430088ed4aceSMatthew G Knepley }
430188ed4aceSMatthew G Knepley 
430288ed4aceSMatthew G Knepley /*@
43031bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
430488ed4aceSMatthew G Knepley 
430588ed4aceSMatthew G Knepley   Input Parameters:
430688ed4aceSMatthew G Knepley + dm - The DM
430788ed4aceSMatthew G Knepley - sf - The PetscSF
430888ed4aceSMatthew G Knepley 
430988ed4aceSMatthew G Knepley   Level: intermediate
431088ed4aceSMatthew G Knepley 
431188ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
431288ed4aceSMatthew G Knepley 
43131bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
431488ed4aceSMatthew G Knepley @*/
43151bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
43160adebc6cSBarry Smith {
431788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
431888ed4aceSMatthew G Knepley 
431988ed4aceSMatthew G Knepley   PetscFunctionBegin;
432088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4321b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
432233907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
43231bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
43241bb6d2a8SBarry Smith   dm->sectionSF = sf;
432588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
432688ed4aceSMatthew G Knepley }
432788ed4aceSMatthew G Knepley 
432888ed4aceSMatthew G Knepley /*@C
43291bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
433088ed4aceSMatthew G Knepley   describing the data layout.
433188ed4aceSMatthew G Knepley 
433288ed4aceSMatthew G Knepley   Input Parameters:
433388ed4aceSMatthew G Knepley + dm - The DM
433488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
433588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
433688ed4aceSMatthew G Knepley 
43371bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
433888ed4aceSMatthew G Knepley 
43391bb6d2a8SBarry Smith   Level: developer
43401bb6d2a8SBarry Smith 
43411bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43421bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43431bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43441bb6d2a8SBarry Smith 
43451bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
434688ed4aceSMatthew G Knepley @*/
43471bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
434888ed4aceSMatthew G Knepley {
434982f516ccSBarry Smith   MPI_Comm       comm;
435088ed4aceSMatthew G Knepley   PetscLayout    layout;
435188ed4aceSMatthew G Knepley   const PetscInt *ranges;
435288ed4aceSMatthew G Knepley   PetscInt       *local;
435388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4354ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
435588ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
435688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
435788ed4aceSMatthew G Knepley 
435888ed4aceSMatthew G Knepley   PetscFunctionBegin;
435988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4360367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
436188ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
436288ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
436388ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
436488ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
436588ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
436688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
436788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
436888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
436988ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4370ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
43716636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
437288ed4aceSMatthew G Knepley 
43736636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
43746636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4375235fbf56SMatthew 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));
43766636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
437788ed4aceSMatthew G Knepley   }
4378785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4379785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
438088ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
43811f588964SMatthew G Knepley     const PetscInt *cind;
43826636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
438388ed4aceSMatthew G Knepley 
438488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
438588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
438688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
438788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
438888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
43896636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
439088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
43916636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
43926636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
43936636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4394057b4bcdSMatthew 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);
43956636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
43966636e97aSMatthew G Knepley     }
439788ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
439888ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
439988ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
440088ed4aceSMatthew G Knepley     }
440188ed4aceSMatthew G Knepley     if (gdof < 0) {
44026636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
440388ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
440488ed4aceSMatthew G Knepley 
440505376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
440631d3f06eSJed Brown         if (r < 0) r = -(r+2);
440705376888SMatthew 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);
440888ed4aceSMatthew G Knepley         remote[l].rank  = r;
440988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
441088ed4aceSMatthew G Knepley       }
441188ed4aceSMatthew G Knepley     } else {
44126636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
441388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
441488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
441588ed4aceSMatthew G Knepley       }
441688ed4aceSMatthew G Knepley     }
441788ed4aceSMatthew G Knepley   }
44186636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
441988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
44201bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
442188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
442288ed4aceSMatthew G Knepley }
4423af122d2aSMatthew G Knepley 
4424b21d0597SMatthew G Knepley /*@
4425b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4426b21d0597SMatthew G Knepley 
4427b21d0597SMatthew G Knepley   Input Parameter:
4428b21d0597SMatthew G Knepley . dm - The DM
4429b21d0597SMatthew G Knepley 
4430b21d0597SMatthew G Knepley   Output Parameter:
4431b21d0597SMatthew G Knepley . sf - The PetscSF
4432b21d0597SMatthew G Knepley 
4433b21d0597SMatthew G Knepley   Level: intermediate
4434b21d0597SMatthew G Knepley 
4435b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4436b21d0597SMatthew G Knepley 
44371bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4438b21d0597SMatthew G Knepley @*/
44390adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44400adebc6cSBarry Smith {
4441b21d0597SMatthew G Knepley   PetscFunctionBegin;
4442b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4443b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4444b21d0597SMatthew G Knepley   *sf = dm->sf;
4445b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4446b21d0597SMatthew G Knepley }
4447b21d0597SMatthew G Knepley 
4448057b4bcdSMatthew G Knepley /*@
4449057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4450057b4bcdSMatthew G Knepley 
4451057b4bcdSMatthew G Knepley   Input Parameters:
4452057b4bcdSMatthew G Knepley + dm - The DM
4453057b4bcdSMatthew G Knepley - sf - The PetscSF
4454057b4bcdSMatthew G Knepley 
4455057b4bcdSMatthew G Knepley   Level: intermediate
4456057b4bcdSMatthew G Knepley 
44571bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4458057b4bcdSMatthew G Knepley @*/
44590adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
44600adebc6cSBarry Smith {
4461057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4462057b4bcdSMatthew G Knepley 
4463057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4464057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4465b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4466057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
446733907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4468057b4bcdSMatthew G Knepley   dm->sf = sf;
4469057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4470057b4bcdSMatthew G Knepley }
4471057b4bcdSMatthew G Knepley 
447234aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
447334aa8a36SMatthew G. Knepley {
447434aa8a36SMatthew G. Knepley   PetscClassId   id;
447534aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
447634aa8a36SMatthew G. Knepley 
447734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
447834aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
447934aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
448034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
448134aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
448234aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
448317c1d62eSMatthew G. Knepley   } else {
448417c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
448534aa8a36SMatthew G. Knepley   }
448634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
448734aa8a36SMatthew G. Knepley }
448834aa8a36SMatthew G. Knepley 
448944a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
449044a7f3ddSMatthew G. Knepley {
449144a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
449244a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
449344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
449444a7f3ddSMatthew G. Knepley 
449544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
449644a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
449744a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
449844a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
449944a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
450044a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
450144a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
450244a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
450344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
450444a7f3ddSMatthew G. Knepley }
450544a7f3ddSMatthew G. Knepley 
450644a7f3ddSMatthew G. Knepley /*@
450744a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
450844a7f3ddSMatthew G. Knepley 
4509d083f849SBarry Smith   Logically collective on dm
451044a7f3ddSMatthew G. Knepley 
451144a7f3ddSMatthew G. Knepley   Input Parameter:
451244a7f3ddSMatthew G. Knepley . dm - The DM
451344a7f3ddSMatthew G. Knepley 
451444a7f3ddSMatthew G. Knepley   Level: intermediate
451544a7f3ddSMatthew G. Knepley 
451644a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
451744a7f3ddSMatthew G. Knepley @*/
451844a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
451944a7f3ddSMatthew G. Knepley {
452044a7f3ddSMatthew G. Knepley   PetscInt       f;
452144a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
452244a7f3ddSMatthew G. Knepley 
452344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
452444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
452544a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
452644a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
452744a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
452844a7f3ddSMatthew G. Knepley   }
452944a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
453044a7f3ddSMatthew G. Knepley   dm->fields = NULL;
453144a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
453244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
453344a7f3ddSMatthew G. Knepley }
453444a7f3ddSMatthew G. Knepley 
4535689b5837SMatthew G. Knepley /*@
4536689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4537689b5837SMatthew G. Knepley 
4538689b5837SMatthew G. Knepley   Not collective
4539689b5837SMatthew G. Knepley 
4540689b5837SMatthew G. Knepley   Input Parameter:
4541689b5837SMatthew G. Knepley . dm - The DM
4542689b5837SMatthew G. Knepley 
4543689b5837SMatthew G. Knepley   Output Parameter:
4544689b5837SMatthew G. Knepley . Nf - The number of fields
4545689b5837SMatthew G. Knepley 
4546689b5837SMatthew G. Knepley   Level: intermediate
4547689b5837SMatthew G. Knepley 
4548689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4549689b5837SMatthew G. Knepley @*/
45500f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
45510f21e855SMatthew G. Knepley {
45520f21e855SMatthew G. Knepley   PetscFunctionBegin;
45530f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4554534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
455544a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4556af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4557af122d2aSMatthew G Knepley }
4558af122d2aSMatthew G Knepley 
4559689b5837SMatthew G. Knepley /*@
4560689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4561689b5837SMatthew G. Knepley 
4562d083f849SBarry Smith   Logically collective on dm
4563689b5837SMatthew G. Knepley 
4564689b5837SMatthew G. Knepley   Input Parameters:
4565689b5837SMatthew G. Knepley + dm - The DM
4566689b5837SMatthew G. Knepley - Nf - The number of fields
4567689b5837SMatthew G. Knepley 
4568689b5837SMatthew G. Knepley   Level: intermediate
4569689b5837SMatthew G. Knepley 
4570689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4571689b5837SMatthew G. Knepley @*/
4572af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4573af122d2aSMatthew G Knepley {
45740f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4575af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4576af122d2aSMatthew G Knepley 
4577af122d2aSMatthew G Knepley   PetscFunctionBegin;
4578af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
457944a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
45800f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
45810f21e855SMatthew G. Knepley     PetscContainer obj;
45820f21e855SMatthew G. Knepley 
45830f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
458444a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
45850f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4586af122d2aSMatthew G Knepley   }
4587af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4588af122d2aSMatthew G Knepley }
4589af122d2aSMatthew G Knepley 
4590c1929be8SMatthew G. Knepley /*@
4591c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4592c1929be8SMatthew G. Knepley 
4593c1929be8SMatthew G. Knepley   Not collective
4594c1929be8SMatthew G. Knepley 
4595c1929be8SMatthew G. Knepley   Input Parameters:
4596c1929be8SMatthew G. Knepley + dm - The DM
4597c1929be8SMatthew G. Knepley - f  - The field number
4598c1929be8SMatthew G. Knepley 
459944a7f3ddSMatthew G. Knepley   Output Parameters:
460044a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
460144a7f3ddSMatthew G. Knepley - field - The discretization object
4602c1929be8SMatthew G. Knepley 
460344a7f3ddSMatthew G. Knepley   Level: intermediate
4604c1929be8SMatthew G. Knepley 
460544a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4606c1929be8SMatthew G. Knepley @*/
460744a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4608af122d2aSMatthew G Knepley {
4609af122d2aSMatthew G Knepley   PetscFunctionBegin;
4610af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
461144a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
461244a7f3ddSMatthew 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);
461344a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
461444a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4615decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4616decb47aaSMatthew G. Knepley }
4617decb47aaSMatthew G. Knepley 
4618c1929be8SMatthew G. Knepley /*@
4619c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4620c1929be8SMatthew G. Knepley 
4621d083f849SBarry Smith   Logically collective on dm
4622c1929be8SMatthew G. Knepley 
4623c1929be8SMatthew G. Knepley   Input Parameters:
4624c1929be8SMatthew G. Knepley + dm    - The DM
4625c1929be8SMatthew G. Knepley . f     - The field number
462644a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4627c1929be8SMatthew G. Knepley - field - The discretization object
4628c1929be8SMatthew G. Knepley 
462944a7f3ddSMatthew G. Knepley   Level: intermediate
4630c1929be8SMatthew G. Knepley 
463144a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4632c1929be8SMatthew G. Knepley @*/
463344a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4634decb47aaSMatthew G. Knepley {
4635decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4636decb47aaSMatthew G. Knepley 
4637decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4638decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4639e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
464044a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4641e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
464244a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
464344a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
464444a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
464544a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
464644a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
464744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
464844a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
464934aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
46502df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
465144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
465244a7f3ddSMatthew G. Knepley }
465344a7f3ddSMatthew G. Knepley 
465444a7f3ddSMatthew G. Knepley /*@
465544a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
465644a7f3ddSMatthew G. Knepley 
4657d083f849SBarry Smith   Logically collective on dm
465844a7f3ddSMatthew G. Knepley 
465944a7f3ddSMatthew G. Knepley   Input Parameters:
466044a7f3ddSMatthew G. Knepley + dm    - The DM
466144a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
466244a7f3ddSMatthew G. Knepley - field - The discretization object
466344a7f3ddSMatthew G. Knepley 
466444a7f3ddSMatthew G. Knepley   Level: intermediate
466544a7f3ddSMatthew G. Knepley 
466644a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
466744a7f3ddSMatthew G. Knepley @*/
466844a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
466944a7f3ddSMatthew G. Knepley {
467044a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
467144a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
467244a7f3ddSMatthew G. Knepley 
467344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
467444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
467544a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
467644a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
467744a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
467844a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
467944a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
468044a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
468144a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
468234aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
46832df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4684af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4685af122d2aSMatthew G Knepley }
46866636e97aSMatthew G Knepley 
4687e5e52638SMatthew G. Knepley /*@
4688e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4689e5e52638SMatthew G. Knepley 
4690d083f849SBarry Smith   Collective on dm
4691e5e52638SMatthew G. Knepley 
4692e5e52638SMatthew G. Knepley   Input Parameter:
4693e5e52638SMatthew G. Knepley . dm - The DM
4694e5e52638SMatthew G. Knepley 
4695e5e52638SMatthew G. Knepley   Output Parameter:
4696e5e52638SMatthew G. Knepley . newdm - The DM
4697e5e52638SMatthew G. Knepley 
4698e5e52638SMatthew G. Knepley   Level: advanced
4699e5e52638SMatthew G. Knepley 
4700e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4701e5e52638SMatthew G. Knepley @*/
4702e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4703e5e52638SMatthew G. Knepley {
4704e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4705e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4706e5e52638SMatthew G. Knepley 
4707e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4708e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4709e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4710e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4711e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4712e5e52638SMatthew G. Knepley     DMLabel     label;
4713e5e52638SMatthew G. Knepley     PetscObject field;
471434aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4715e5e52638SMatthew G. Knepley 
4716e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4717e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
471834aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
471934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
472034aa8a36SMatthew G. Knepley   }
472134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
472234aa8a36SMatthew G. Knepley }
472334aa8a36SMatthew G. Knepley 
472434aa8a36SMatthew G. Knepley /*@
472534aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
472634aa8a36SMatthew G. Knepley 
472734aa8a36SMatthew G. Knepley   Not collective
472834aa8a36SMatthew G. Knepley 
472934aa8a36SMatthew G. Knepley   Input Parameters:
473034aa8a36SMatthew G. Knepley + dm - The DM object
473134aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
473234aa8a36SMatthew G. Knepley 
473334aa8a36SMatthew G. Knepley   Output Parameter:
473434aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
473534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
473634aa8a36SMatthew G. Knepley 
473734aa8a36SMatthew G. Knepley   Notes:
473834aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
473934aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
474034aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4741979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
474234aa8a36SMatthew G. Knepley 
474334aa8a36SMatthew G. Knepley   Level: developer
474434aa8a36SMatthew G. Knepley 
474534aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
474634aa8a36SMatthew G. Knepley @*/
474734aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
474834aa8a36SMatthew G. Knepley {
474934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
475034aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4751534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4752534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
475334aa8a36SMatthew G. Knepley   if (f < 0) {
475434aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
475534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
475634aa8a36SMatthew G. Knepley   } else {
475734aa8a36SMatthew G. Knepley     PetscInt       Nf;
475834aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
475934aa8a36SMatthew G. Knepley 
476034aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
476134aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
476234aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
476334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
476434aa8a36SMatthew G. Knepley   }
476534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
476634aa8a36SMatthew G. Knepley }
476734aa8a36SMatthew G. Knepley 
476834aa8a36SMatthew G. Knepley /*@
476934aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
477034aa8a36SMatthew G. Knepley 
477134aa8a36SMatthew G. Knepley   Not collective
477234aa8a36SMatthew G. Knepley 
477334aa8a36SMatthew G. Knepley   Input Parameters:
477434aa8a36SMatthew G. Knepley + dm         - The DM object
477534aa8a36SMatthew G. Knepley . f          - The field number
477634aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
477734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
477834aa8a36SMatthew G. Knepley 
477934aa8a36SMatthew G. Knepley   Notes:
478034aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
478134aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
478234aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4783979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
478434aa8a36SMatthew G. Knepley 
478534aa8a36SMatthew G. Knepley   Level: developer
478634aa8a36SMatthew G. Knepley 
478734aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
478834aa8a36SMatthew G. Knepley @*/
478934aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
479034aa8a36SMatthew G. Knepley {
479134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
479234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
479334aa8a36SMatthew G. Knepley   if (f < 0) {
479434aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
479534aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
479634aa8a36SMatthew G. Knepley   } else {
479734aa8a36SMatthew G. Knepley     PetscInt       Nf;
479834aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
479934aa8a36SMatthew G. Knepley 
480034aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
480134aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
480234aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
480334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4804e5e52638SMatthew G. Knepley   }
4805e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4806e5e52638SMatthew G. Knepley }
4807e5e52638SMatthew G. Knepley 
4808b0441da4SMatthew G. Knepley /*@
4809b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4810b0441da4SMatthew G. Knepley 
4811b0441da4SMatthew G. Knepley   Not collective
4812b0441da4SMatthew G. Knepley 
4813b0441da4SMatthew G. Knepley   Input Parameters:
4814b0441da4SMatthew G. Knepley . dm - The DM object
4815b0441da4SMatthew G. Knepley 
4816b0441da4SMatthew G. Knepley   Output Parameter:
4817b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4818b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4819b0441da4SMatthew G. Knepley 
4820b0441da4SMatthew G. Knepley   Notes:
4821b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4822b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4823b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4824b0441da4SMatthew G. Knepley 
4825b0441da4SMatthew G. Knepley   Level: developer
4826b0441da4SMatthew G. Knepley 
4827b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4828b0441da4SMatthew G. Knepley @*/
4829b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4830b0441da4SMatthew G. Knepley {
4831b0441da4SMatthew G. Knepley   PetscInt       Nf;
4832b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4833b0441da4SMatthew G. Knepley 
4834b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4835b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4836534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4837534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4838b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4839b0441da4SMatthew G. Knepley   if (!Nf) {
4840b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4841b0441da4SMatthew G. Knepley   } else {
4842b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4843b0441da4SMatthew G. Knepley   }
4844b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4845b0441da4SMatthew G. Knepley }
4846b0441da4SMatthew G. Knepley 
4847b0441da4SMatthew G. Knepley /*@
4848b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4849b0441da4SMatthew G. Knepley 
4850b0441da4SMatthew G. Knepley   Not collective
4851b0441da4SMatthew G. Knepley 
4852b0441da4SMatthew G. Knepley   Input Parameters:
4853b0441da4SMatthew G. Knepley + dm         - The DM object
4854b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4855b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4856b0441da4SMatthew G. Knepley 
4857b0441da4SMatthew G. Knepley   Notes:
4858b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4859b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4860b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4861b0441da4SMatthew G. Knepley 
4862b0441da4SMatthew G. Knepley   Level: developer
4863b0441da4SMatthew G. Knepley 
4864b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4865b0441da4SMatthew G. Knepley @*/
4866b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4867b0441da4SMatthew G. Knepley {
4868b0441da4SMatthew G. Knepley   PetscInt       Nf;
4869b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4870b0441da4SMatthew G. Knepley 
4871b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4872b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4873b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4874b0441da4SMatthew G. Knepley   if (!Nf) {
4875b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4876b0441da4SMatthew G. Knepley   } else {
4877b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4878e5e52638SMatthew G. Knepley   }
4879e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4880e5e52638SMatthew G. Knepley }
4881e5e52638SMatthew G. Knepley 
4882e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4883e5e52638SMatthew G. Knepley {
4884e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4885e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4886e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4887e5e52638SMatthew G. Knepley 
4888e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4889e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4890e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4891e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4892b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4893e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4894e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4895e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4896e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4897e5e52638SMatthew G. Knepley }
4898e5e52638SMatthew G. Knepley 
4899e5e52638SMatthew G. Knepley /*@
4900e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4901e5e52638SMatthew G. Knepley 
4902e5e52638SMatthew G. Knepley   Not collective
4903e5e52638SMatthew G. Knepley 
4904e5e52638SMatthew G. Knepley   Input Parameter:
4905e5e52638SMatthew G. Knepley . dm - The DM
4906e5e52638SMatthew G. Knepley 
4907e5e52638SMatthew G. Knepley   Output Parameter:
4908e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4909e5e52638SMatthew G. Knepley 
4910e5e52638SMatthew G. Knepley   Level: intermediate
4911e5e52638SMatthew G. Knepley 
4912e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4913e5e52638SMatthew G. Knepley @*/
4914e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4915e5e52638SMatthew G. Knepley {
4916e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4917e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4918534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
4919e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4920e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4921e5e52638SMatthew G. Knepley }
4922e5e52638SMatthew G. Knepley 
4923e5e52638SMatthew G. Knepley /*@
4924e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4925e5e52638SMatthew G. Knepley 
4926d083f849SBarry Smith   Logically collective on dm
4927e5e52638SMatthew G. Knepley 
4928e5e52638SMatthew G. Knepley   Input Parameter:
4929e5e52638SMatthew G. Knepley . dm - The DM
4930e5e52638SMatthew G. Knepley 
4931e5e52638SMatthew G. Knepley   Level: intermediate
4932e5e52638SMatthew G. Knepley 
4933e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4934e5e52638SMatthew G. Knepley @*/
4935e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4936e5e52638SMatthew G. Knepley {
4937e5e52638SMatthew G. Knepley   PetscInt       s;
4938e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4939e5e52638SMatthew G. Knepley 
4940e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4941e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4942e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4943e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4944e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4945b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4946e5e52638SMatthew G. Knepley   }
4947e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4948e5e52638SMatthew G. Knepley   dm->probs = NULL;
4949e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4950e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4951e5e52638SMatthew G. Knepley }
4952e5e52638SMatthew G. Knepley 
4953e5e52638SMatthew G. Knepley /*@
4954e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4955e5e52638SMatthew G. Knepley 
4956e5e52638SMatthew G. Knepley   Not collective
4957e5e52638SMatthew G. Knepley 
4958e5e52638SMatthew G. Knepley   Input Parameter:
4959e5e52638SMatthew G. Knepley . dm    - The DM
4960e5e52638SMatthew G. Knepley 
4961e5e52638SMatthew G. Knepley   Output Parameter:
4962e5e52638SMatthew G. Knepley . prob - The default PetscDS
4963e5e52638SMatthew G. Knepley 
4964e5e52638SMatthew G. Knepley   Level: intermediate
4965e5e52638SMatthew G. Knepley 
4966e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4967e5e52638SMatthew G. Knepley @*/
4968e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4969e5e52638SMatthew G. Knepley {
4970b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4971b0143b4dSMatthew G. Knepley 
4972e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4973e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4974e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4975b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4976b0143b4dSMatthew G. Knepley     PetscDS ds;
4977b0143b4dSMatthew G. Knepley 
4978b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4979b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
4980b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4981b0143b4dSMatthew G. Knepley   }
4982b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4983e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4984e5e52638SMatthew G. Knepley }
4985e5e52638SMatthew G. Knepley 
4986e5e52638SMatthew G. Knepley /*@
4987e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
4988e5e52638SMatthew G. Knepley 
4989e5e52638SMatthew G. Knepley   Not collective
4990e5e52638SMatthew G. Knepley 
4991e5e52638SMatthew G. Knepley   Input Parameters:
4992e5e52638SMatthew G. Knepley + dm    - The DM
4993e5e52638SMatthew G. Knepley - point - Cell for the DS
4994e5e52638SMatthew G. Knepley 
4995e5e52638SMatthew G. Knepley   Output Parameter:
4996e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
4997e5e52638SMatthew G. Knepley 
4998e5e52638SMatthew G. Knepley   Level: developer
4999e5e52638SMatthew G. Knepley 
5000b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5001e5e52638SMatthew G. Knepley @*/
5002e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5003e5e52638SMatthew G. Knepley {
5004e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5005e5e52638SMatthew G. Knepley   PetscInt       s;
5006e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5007e5e52638SMatthew G. Knepley 
5008e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5009e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5010e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5011e5e52638SMatthew G. Knepley   *prob = NULL;
5012e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5013e5e52638SMatthew G. Knepley     PetscInt val;
5014e5e52638SMatthew G. Knepley 
5015e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5016e5e52638SMatthew G. Knepley     else {
5017e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5018e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5019e5e52638SMatthew G. Knepley     }
5020e5e52638SMatthew G. Knepley   }
5021e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5022e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5023e5e52638SMatthew G. Knepley }
5024e5e52638SMatthew G. Knepley 
5025e5e52638SMatthew G. Knepley /*@
5026e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5027e5e52638SMatthew G. Knepley 
5028e5e52638SMatthew G. Knepley   Not collective
5029e5e52638SMatthew G. Knepley 
5030e5e52638SMatthew G. Knepley   Input Parameters:
5031e5e52638SMatthew G. Knepley + dm    - The DM
5032e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5033e5e52638SMatthew G. Knepley 
5034b3cf3223SMatthew G. Knepley   Output Parameters:
5035b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5036b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5037e5e52638SMatthew G. Knepley 
5038e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5039e5e52638SMatthew G. Knepley 
5040e5e52638SMatthew G. Knepley   Level: advanced
5041e5e52638SMatthew G. Knepley 
5042e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5043e5e52638SMatthew G. Knepley @*/
5044b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5045e5e52638SMatthew G. Knepley {
5046e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5047e5e52638SMatthew G. Knepley 
5048e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5049e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5050e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5051b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5052b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5053e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5054b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5055b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5056b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5057b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5058b3cf3223SMatthew G. Knepley     }
5059e5e52638SMatthew G. Knepley   }
50602df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5061e5e52638SMatthew G. Knepley }
5062e5e52638SMatthew G. Knepley 
5063e5e52638SMatthew G. Knepley /*@
5064e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5065e5e52638SMatthew G. Knepley 
5066e5e52638SMatthew G. Knepley   Not collective
5067e5e52638SMatthew G. Knepley 
5068e5e52638SMatthew G. Knepley   Input Parameters:
5069e5e52638SMatthew G. Knepley + dm  - The DM
5070e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5071e5e52638SMatthew G. Knepley 
5072e5e52638SMatthew G. Knepley   Output Parameters:
5073b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5074b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5075b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
5076e5e52638SMatthew G. Knepley 
5077e5e52638SMatthew G. Knepley   Level: advanced
5078e5e52638SMatthew G. Knepley 
5079e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5080e5e52638SMatthew G. Knepley @*/
5081b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5082e5e52638SMatthew G. Knepley {
5083e5e52638SMatthew G. Knepley   PetscInt       Nds;
5084e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5085e5e52638SMatthew G. Knepley 
5086e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5087e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5088e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5089e5e52638SMatthew 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);
5090e5e52638SMatthew G. Knepley   if (label) {
5091e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5092e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5093e5e52638SMatthew G. Knepley   }
5094b3cf3223SMatthew G. Knepley   if (fields) {
5095b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5096b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5097b3cf3223SMatthew G. Knepley   }
5098e5e52638SMatthew G. Knepley   if (ds) {
5099b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5100e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5101e5e52638SMatthew G. Knepley   }
5102e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5103e5e52638SMatthew G. Knepley }
5104e5e52638SMatthew G. Knepley 
5105e5e52638SMatthew G. Knepley /*@
5106e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5107e5e52638SMatthew G. Knepley 
5108d083f849SBarry Smith   Collective on dm
5109e5e52638SMatthew G. Knepley 
5110e5e52638SMatthew G. Knepley   Input Parameters:
5111e5e52638SMatthew G. Knepley + dm     - The DM
5112e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5113b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5114e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5115e5e52638SMatthew G. Knepley 
5116b3cf3223SMatthew 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,
5117b3cf3223SMatthew G. Knepley   the fields argument is ignored.
5118e5e52638SMatthew G. Knepley 
5119e5e52638SMatthew G. Knepley   Level: advanced
5120e5e52638SMatthew G. Knepley 
5121e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
5122e5e52638SMatthew G. Knepley @*/
5123b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5124e5e52638SMatthew G. Knepley {
5125e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5126e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5127e5e52638SMatthew G. Knepley 
5128e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5129e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5130e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5131e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5132e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5133e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
5134b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5135e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
5136e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
5137e5e52638SMatthew G. Knepley     }
5138e5e52638SMatthew G. Knepley   }
51391b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5140e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5141b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5142e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5143e5e52638SMatthew G. Knepley   if (!label) {
5144e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5145e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5146e5e52638SMatthew G. Knepley     Nds = 0;
5147e5e52638SMatthew G. Knepley   }
5148e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
5149b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5150e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5151e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5152e5e52638SMatthew G. Knepley }
5153e5e52638SMatthew G. Knepley 
5154e5e52638SMatthew G. Knepley /*@
5155e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5156e5e52638SMatthew G. Knepley 
5157d083f849SBarry Smith   Collective on dm
5158e5e52638SMatthew G. Knepley 
5159e5e52638SMatthew G. Knepley   Input Parameter:
5160e5e52638SMatthew G. Knepley . dm - The DM
5161e5e52638SMatthew G. Knepley 
5162e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5163e5e52638SMatthew G. Knepley 
5164e5e52638SMatthew G. Knepley   Level: intermediate
5165e5e52638SMatthew G. Knepley 
5166e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5167e5e52638SMatthew G. Knepley @*/
5168e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5169e5e52638SMatthew G. Knepley {
5170e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5171e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5172b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5173e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5174e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5175e5e52638SMatthew G. Knepley 
5176e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5177e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5178e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5179e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5180e5e52638SMatthew G. Knepley    1) NULL
5181e5e52638SMatthew G. Knepley    2) Hybrid cells
5182e5e52638SMatthew G. Knepley   */
5183e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5184e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5185e5e52638SMatthew G. Knepley   /* Create default DS */
5186b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
51872df9ee95SMatthew G. Knepley   if (!prob) {
5188b3cf3223SMatthew G. Knepley     IS        fields;
5189b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5190b3cf3223SMatthew G. Knepley 
5191b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5192b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5193b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
519488f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
519588f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
519688f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
519788f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
519888f0c812SMatthew G. Knepley 
51992df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5200b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
52012df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5202b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5203b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
52042df9ee95SMatthew G. Knepley   }
5205e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5206e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5207b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5208e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5209e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5210e5e52638SMatthew G. Knepley 
5211e5e52638SMatthew G. Knepley     if (label) {
52120122748bSMatthew G. Knepley       DM        plex;
5213a2d47024SMatthew G. Knepley       IS        fields;
5214a2d47024SMatthew G. Knepley       PetscInt *fld;
52150122748bSMatthew G. Knepley       PetscInt  depth, pMax[4];
52160122748bSMatthew G. Knepley 
52170122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
52180122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
52190122748bSMatthew 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);
52200122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
52210122748bSMatthew G. Knepley 
5222e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5223e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
5224e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5225a2d47024SMatthew G. Knepley       ierr = PetscMalloc1(1, &fld);CHKERRQ(ierr);
5226a2d47024SMatthew G. Knepley       fld[0] = f;
5227a2d47024SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5228a2d47024SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5229a2d47024SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5230a2d47024SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, 1, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5231a2d47024SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, fields, probh);CHKERRQ(ierr);
5232a2d47024SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
5233e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5234e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5235e5e52638SMatthew G. Knepley       break;
5236e5e52638SMatthew G. Knepley     }
5237e5e52638SMatthew G. Knepley   }
5238e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5239b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5240e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5241e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5242e5e52638SMatthew G. Knepley 
5243e5e52638SMatthew G. Knepley     if (!label) {
5244e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5245e5e52638SMatthew G. Knepley       if (probh) {
5246e5e52638SMatthew G. Knepley         PetscFE subfe;
5247e5e52638SMatthew G. Knepley 
5248e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5249e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5250e5e52638SMatthew G. Knepley       }
5251e5e52638SMatthew G. Knepley     } else {
5252e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5253e5e52638SMatthew G. Knepley     }
5254e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5255e5e52638SMatthew G. Knepley     {
5256e5e52638SMatthew G. Knepley       PetscClassId id;
5257e5e52638SMatthew G. Knepley 
5258e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5259e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5260e5e52638SMatthew G. Knepley     }
5261e5e52638SMatthew G. Knepley   }
5262e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5263e5e52638SMatthew G. Knepley   /* Setup DSes */
5264e5e52638SMatthew G. Knepley   if (doSetup) {
5265e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5266e5e52638SMatthew G. Knepley   }
5267e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5268e5e52638SMatthew G. Knepley }
5269e5e52638SMatthew G. Knepley 
5270e5e52638SMatthew G. Knepley /*@
5271e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5272e5e52638SMatthew G. Knepley 
5273d083f849SBarry Smith   Collective on dm
5274e5e52638SMatthew G. Knepley 
5275e5e52638SMatthew G. Knepley   Input Parameter:
5276e5e52638SMatthew G. Knepley . dm - The DM
5277e5e52638SMatthew G. Knepley 
5278e5e52638SMatthew G. Knepley   Output Parameter:
5279e5e52638SMatthew G. Knepley . newdm - The DM
5280e5e52638SMatthew G. Knepley 
5281e5e52638SMatthew G. Knepley   Level: advanced
5282e5e52638SMatthew G. Knepley 
5283e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5284e5e52638SMatthew G. Knepley @*/
5285e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5286e5e52638SMatthew G. Knepley {
5287e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5288e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5289e5e52638SMatthew G. Knepley 
5290e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5291e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5292e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5293e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5294e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5295e5e52638SMatthew G. Knepley     DMLabel label;
5296b3cf3223SMatthew G. Knepley     IS      fields;
5297e5e52638SMatthew G. Knepley     PetscDS ds;
5298e5e52638SMatthew G. Knepley 
5299b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5300b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5301e5e52638SMatthew G. Knepley   }
5302e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5303e5e52638SMatthew G. Knepley }
5304e5e52638SMatthew G. Knepley 
5305e5e52638SMatthew G. Knepley /*@
5306e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5307e5e52638SMatthew G. Knepley 
5308d083f849SBarry Smith   Collective on dm
5309e5e52638SMatthew G. Knepley 
5310e5e52638SMatthew G. Knepley   Input Parameter:
5311e5e52638SMatthew G. Knepley . dm - The DM
5312e5e52638SMatthew G. Knepley 
5313e5e52638SMatthew G. Knepley   Output Parameter:
5314e5e52638SMatthew G. Knepley . newdm - The DM
5315e5e52638SMatthew G. Knepley 
5316e5e52638SMatthew G. Knepley   Level: advanced
5317e5e52638SMatthew G. Knepley 
5318e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5319e5e52638SMatthew G. Knepley @*/
5320e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5321e5e52638SMatthew G. Knepley {
5322e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5323e5e52638SMatthew G. Knepley 
5324e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5325e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5326e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5327e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5328e5e52638SMatthew G. Knepley }
5329e5e52638SMatthew G. Knepley 
5330b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5331b64e0483SPeter Brune {
5332b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5333b64e0483SPeter Brune   PetscErrorCode ierr;
5334b64e0483SPeter Brune   Vec coords,ccoords;
53356dbf9973SLawrence Mitchell   Mat inject;
5336b64e0483SPeter Brune   PetscFunctionBegin;
5337b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5338b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5339b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5340b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5341b64e0483SPeter Brune   if (coords && !ccoords) {
5342b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
53436668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
53446dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
53452adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
53466dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5347b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5348b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5349b64e0483SPeter Brune   }
5350b64e0483SPeter Brune   PetscFunctionReturn(0);
5351b64e0483SPeter Brune }
5352b64e0483SPeter Brune 
535303dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
535403dadc2fSPeter Brune {
535503dadc2fSPeter Brune   DM dm_coord,subdm_coord;
535603dadc2fSPeter Brune   PetscErrorCode ierr;
535703dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
535803dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
535903dadc2fSPeter Brune   PetscFunctionBegin;
536003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
536103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
536203dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
536303dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
536403dadc2fSPeter Brune   if (coords && !ccoords) {
536503dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
53666668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
536703dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
536824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
536903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
537003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
537103dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
53721ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
537303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
537403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
537503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
537603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
537703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
537803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
537903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
538003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
538103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
538203dadc2fSPeter Brune   }
538303dadc2fSPeter Brune   PetscFunctionReturn(0);
538403dadc2fSPeter Brune }
538503dadc2fSPeter Brune 
5386c73cfb54SMatthew G. Knepley /*@
5387c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5388c73cfb54SMatthew G. Knepley 
5389c73cfb54SMatthew G. Knepley   Not collective
5390c73cfb54SMatthew G. Knepley 
5391c73cfb54SMatthew G. Knepley   Input Parameter:
5392c73cfb54SMatthew G. Knepley . dm - The DM
5393c73cfb54SMatthew G. Knepley 
5394c73cfb54SMatthew G. Knepley   Output Parameter:
5395c73cfb54SMatthew G. Knepley . dim - The topological dimension
5396c73cfb54SMatthew G. Knepley 
5397c73cfb54SMatthew G. Knepley   Level: beginner
5398c73cfb54SMatthew G. Knepley 
5399c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5400c73cfb54SMatthew G. Knepley @*/
5401c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5402c73cfb54SMatthew G. Knepley {
5403c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5404c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5405534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5406c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5407c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5408c73cfb54SMatthew G. Knepley }
5409c73cfb54SMatthew G. Knepley 
5410c73cfb54SMatthew G. Knepley /*@
5411c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5412c73cfb54SMatthew G. Knepley 
5413c73cfb54SMatthew G. Knepley   Collective on dm
5414c73cfb54SMatthew G. Knepley 
5415c73cfb54SMatthew G. Knepley   Input Parameters:
5416c73cfb54SMatthew G. Knepley + dm - The DM
5417c73cfb54SMatthew G. Knepley - dim - The topological dimension
5418c73cfb54SMatthew G. Knepley 
5419c73cfb54SMatthew G. Knepley   Level: beginner
5420c73cfb54SMatthew G. Knepley 
5421c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5422c73cfb54SMatthew G. Knepley @*/
5423c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5424c73cfb54SMatthew G. Knepley {
5425e5e52638SMatthew G. Knepley   PetscDS        ds;
5426f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5427f17e8794SMatthew G. Knepley 
5428c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5429c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5430c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5431c73cfb54SMatthew G. Knepley   dm->dim = dim;
5432e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5433e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5434c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5435c73cfb54SMatthew G. Knepley }
5436c73cfb54SMatthew G. Knepley 
5437793f3fe5SMatthew G. Knepley /*@
5438793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5439793f3fe5SMatthew G. Knepley 
5440d083f849SBarry Smith   Collective on dm
5441793f3fe5SMatthew G. Knepley 
5442793f3fe5SMatthew G. Knepley   Input Parameters:
5443793f3fe5SMatthew G. Knepley + dm - the DM
5444793f3fe5SMatthew G. Knepley - dim - the dimension
5445793f3fe5SMatthew G. Knepley 
5446793f3fe5SMatthew G. Knepley   Output Parameters:
5447793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5448aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5449793f3fe5SMatthew G. Knepley 
5450793f3fe5SMatthew G. Knepley   Note:
5451793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5452a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5453793f3fe5SMatthew G. Knepley   then the interval is empty.
5454793f3fe5SMatthew G. Knepley 
5455793f3fe5SMatthew G. Knepley   Level: intermediate
5456793f3fe5SMatthew G. Knepley 
5457793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5458793f3fe5SMatthew G. Knepley @*/
5459793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5460793f3fe5SMatthew G. Knepley {
5461793f3fe5SMatthew G. Knepley   PetscInt       d;
5462793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5463793f3fe5SMatthew G. Knepley 
5464793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5465793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5466793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5467793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5468b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5469793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5470793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5471793f3fe5SMatthew G. Knepley }
5472793f3fe5SMatthew G. Knepley 
54736636e97aSMatthew G Knepley /*@
54746636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
54756636e97aSMatthew G Knepley 
5476d083f849SBarry Smith   Collective on dm
54776636e97aSMatthew G Knepley 
54786636e97aSMatthew G Knepley   Input Parameters:
54796636e97aSMatthew G Knepley + dm - the DM
54806636e97aSMatthew G Knepley - c - coordinate vector
54816636e97aSMatthew G Knepley 
54822dd40e9bSPatrick Sanan   Notes:
54832dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
54842dd40e9bSPatrick Sanan 
54852dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
54866636e97aSMatthew G Knepley 
54876636e97aSMatthew G Knepley   Level: intermediate
54886636e97aSMatthew G Knepley 
54892dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
54906636e97aSMatthew G Knepley @*/
54916636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
54926636e97aSMatthew G Knepley {
54936636e97aSMatthew G Knepley   PetscErrorCode ierr;
54946636e97aSMatthew G Knepley 
54956636e97aSMatthew G Knepley   PetscFunctionBegin;
54966636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54976636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
54986636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
54996636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55006636e97aSMatthew G Knepley   dm->coordinates = c;
55016636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5502b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
550303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
55046636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55056636e97aSMatthew G Knepley }
55066636e97aSMatthew G Knepley 
55076636e97aSMatthew G Knepley /*@
55086636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
55096636e97aSMatthew G Knepley 
55107058e716SVaclav Hapla   Not collective
55116636e97aSMatthew G Knepley 
55126636e97aSMatthew G Knepley    Input Parameters:
55136636e97aSMatthew G Knepley +  dm - the DM
55146636e97aSMatthew G Knepley -  c - coordinate vector
55156636e97aSMatthew G Knepley 
55162dd40e9bSPatrick Sanan   Notes:
55176636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
55186636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
55196636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
55206636e97aSMatthew G Knepley 
55212dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
55222dd40e9bSPatrick Sanan 
55236636e97aSMatthew G Knepley   Level: intermediate
55246636e97aSMatthew G Knepley 
55256636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
55266636e97aSMatthew G Knepley @*/
55276636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
55286636e97aSMatthew G Knepley {
55296636e97aSMatthew G Knepley   PetscErrorCode ierr;
55306636e97aSMatthew G Knepley 
55316636e97aSMatthew G Knepley   PetscFunctionBegin;
55326636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55346636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55356636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
55368865f1eaSKarl Rupp 
55376636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
55388865f1eaSKarl Rupp 
55396636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55406636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55416636e97aSMatthew G Knepley }
55426636e97aSMatthew G Knepley 
55436636e97aSMatthew G Knepley /*@
55446636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
55456636e97aSMatthew G Knepley 
5546d083f849SBarry Smith   Collective on dm
55476636e97aSMatthew G Knepley 
55486636e97aSMatthew G Knepley   Input Parameter:
55496636e97aSMatthew G Knepley . dm - the DM
55506636e97aSMatthew G Knepley 
55516636e97aSMatthew G Knepley   Output Parameter:
55526636e97aSMatthew G Knepley . c - global coordinate vector
55536636e97aSMatthew G Knepley 
55546636e97aSMatthew G Knepley   Note:
55556636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
55566636e97aSMatthew G Knepley 
55576636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
55586636e97aSMatthew G Knepley 
55596636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
55606636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
55616636e97aSMatthew G Knepley 
55626636e97aSMatthew G Knepley   Level: intermediate
55636636e97aSMatthew G Knepley 
55646636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
55656636e97aSMatthew G Knepley @*/
55666636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
55676636e97aSMatthew G Knepley {
55686636e97aSMatthew G Knepley   PetscErrorCode ierr;
55696636e97aSMatthew G Knepley 
55706636e97aSMatthew G Knepley   PetscFunctionBegin;
55716636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55726636e97aSMatthew G Knepley   PetscValidPointer(c,2);
55731f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
55740298fd71SBarry Smith     DM        cdm = NULL;
55751970a576SMatthew G. Knepley     PetscBool localized;
55766636e97aSMatthew G Knepley 
55776636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
55786636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
55791970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
55801970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
55811970a576SMatthew G. Knepley     if (localized) {
55821970a576SMatthew G. Knepley       PetscInt cdim;
55831970a576SMatthew G. Knepley 
55841970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
55851970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
55861970a576SMatthew G. Knepley     }
55876636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
55886636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
55896636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
55906636e97aSMatthew G Knepley   }
55916636e97aSMatthew G Knepley   *c = dm->coordinates;
55926636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55936636e97aSMatthew G Knepley }
55946636e97aSMatthew G Knepley 
55956636e97aSMatthew G Knepley /*@
559681e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
559781e9a530SVaclav Hapla 
5598d083f849SBarry Smith   Collective on dm
559981e9a530SVaclav Hapla 
560081e9a530SVaclav Hapla   Input Parameter:
560181e9a530SVaclav Hapla . dm - the DM
560281e9a530SVaclav Hapla 
560381e9a530SVaclav Hapla   Level: advanced
560481e9a530SVaclav Hapla 
560581e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
560681e9a530SVaclav Hapla @*/
560781e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
560881e9a530SVaclav Hapla {
560981e9a530SVaclav Hapla   PetscErrorCode ierr;
561081e9a530SVaclav Hapla 
561181e9a530SVaclav Hapla   PetscFunctionBegin;
561281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
561381e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
56141970a576SMatthew G. Knepley     DM        cdm = NULL;
56151970a576SMatthew G. Knepley     PetscBool localized;
56161970a576SMatthew G. Knepley 
561781e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
561881e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
56191970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
56201970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
56211970a576SMatthew G. Knepley     if (localized) {
56221970a576SMatthew G. Knepley       PetscInt cdim;
56231970a576SMatthew G. Knepley 
56241970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
56251970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
56261970a576SMatthew G. Knepley     }
562781e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
562881e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
562981e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
563081e9a530SVaclav Hapla   }
563181e9a530SVaclav Hapla   PetscFunctionReturn(0);
563281e9a530SVaclav Hapla }
563381e9a530SVaclav Hapla 
563481e9a530SVaclav Hapla /*@
56356636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
56366636e97aSMatthew G Knepley 
5637d083f849SBarry Smith   Collective on dm
56386636e97aSMatthew G Knepley 
56396636e97aSMatthew G Knepley   Input Parameter:
56406636e97aSMatthew G Knepley . dm - the DM
56416636e97aSMatthew G Knepley 
56426636e97aSMatthew G Knepley   Output Parameter:
56436636e97aSMatthew G Knepley . c - coordinate vector
56446636e97aSMatthew G Knepley 
56456636e97aSMatthew G Knepley   Note:
56466636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
56476636e97aSMatthew G Knepley 
56486636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
56496636e97aSMatthew G Knepley 
56506636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56516636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56526636e97aSMatthew G Knepley 
56536636e97aSMatthew G Knepley   Level: intermediate
56546636e97aSMatthew G Knepley 
565581e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
56566636e97aSMatthew G Knepley @*/
56576636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
56586636e97aSMatthew G Knepley {
56596636e97aSMatthew G Knepley   PetscErrorCode ierr;
56606636e97aSMatthew G Knepley 
56616636e97aSMatthew G Knepley   PetscFunctionBegin;
56626636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56636636e97aSMatthew G Knepley   PetscValidPointer(c,2);
566481e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
56656636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
56666636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56676636e97aSMatthew G Knepley }
56686636e97aSMatthew G Knepley 
566981e9a530SVaclav Hapla /*@
567081e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
567181e9a530SVaclav Hapla 
567281e9a530SVaclav Hapla   Not collective
567381e9a530SVaclav Hapla 
567481e9a530SVaclav Hapla   Input Parameter:
567581e9a530SVaclav Hapla . dm - the DM
567681e9a530SVaclav Hapla 
567781e9a530SVaclav Hapla   Output Parameter:
567881e9a530SVaclav Hapla . c - coordinate vector
567981e9a530SVaclav Hapla 
568081e9a530SVaclav Hapla   Level: advanced
568181e9a530SVaclav Hapla 
568281e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
568381e9a530SVaclav Hapla @*/
568481e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
568581e9a530SVaclav Hapla {
568681e9a530SVaclav Hapla   PetscFunctionBegin;
568781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
568881e9a530SVaclav Hapla   PetscValidPointer(c,2);
568981e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
56906636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
56916636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56926636e97aSMatthew G Knepley }
56936636e97aSMatthew G Knepley 
56942db98f8dSVaclav Hapla /*@
56952db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
56962db98f8dSVaclav Hapla 
56972db98f8dSVaclav Hapla   Not collective
56982db98f8dSVaclav Hapla 
56992db98f8dSVaclav Hapla   Input Parameter:
57002db98f8dSVaclav Hapla + dm - the DM
57012db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
57022db98f8dSVaclav Hapla 
57032db98f8dSVaclav Hapla   Output Parameter:
57042db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
57052db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
57062db98f8dSVaclav Hapla 
57072db98f8dSVaclav Hapla   Note:
57082db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
57092db98f8dSVaclav Hapla 
57102db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
57112db98f8dSVaclav Hapla 
57122db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
57132db98f8dSVaclav Hapla 
57142db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
57152db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
57162db98f8dSVaclav Hapla 
57172db98f8dSVaclav Hapla   Level: advanced
57182db98f8dSVaclav Hapla 
57192db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
57202db98f8dSVaclav Hapla @*/
57212db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
57222db98f8dSVaclav Hapla {
57232db98f8dSVaclav Hapla   PetscSection        cs, newcs;
57242db98f8dSVaclav Hapla   Vec                 coords;
57252db98f8dSVaclav Hapla   const PetscScalar   *arr;
57262db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
57272db98f8dSVaclav Hapla   PetscInt            n;
57282db98f8dSVaclav Hapla   PetscErrorCode      ierr;
57292db98f8dSVaclav Hapla 
57302db98f8dSVaclav Hapla   PetscFunctionBegin;
57312db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57322db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
57332db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
57342db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
57352db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
57361bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
57371bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
57382db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
57392db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
57402db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
57412db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
57422db98f8dSVaclav Hapla   if (pCoord) {
57432db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
57442db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
57452db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
57462db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5747ad9ac99dSVaclav Hapla   } else {
5748ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
57492db98f8dSVaclav Hapla   }
5750ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5751ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
57522db98f8dSVaclav Hapla   PetscFunctionReturn(0);
57532db98f8dSVaclav Hapla }
57542db98f8dSVaclav Hapla 
5755f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5756f19dbd58SToby Isaac {
5757f19dbd58SToby Isaac   PetscErrorCode ierr;
5758f19dbd58SToby Isaac 
5759f19dbd58SToby Isaac   PetscFunctionBegin;
5760f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5761f19dbd58SToby Isaac   PetscValidPointer(field,2);
5762f19dbd58SToby Isaac   if (!dm->coordinateField) {
5763f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5764f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5765f19dbd58SToby Isaac     }
5766f19dbd58SToby Isaac   }
5767f19dbd58SToby Isaac   *field = dm->coordinateField;
5768f19dbd58SToby Isaac   PetscFunctionReturn(0);
5769f19dbd58SToby Isaac }
5770f19dbd58SToby Isaac 
5771f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5772f19dbd58SToby Isaac {
5773f19dbd58SToby Isaac   PetscErrorCode ierr;
5774f19dbd58SToby Isaac 
5775f19dbd58SToby Isaac   PetscFunctionBegin;
5776f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5777f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5778c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5779f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5780f19dbd58SToby Isaac   dm->coordinateField = field;
5781f19dbd58SToby Isaac   PetscFunctionReturn(0);
5782f19dbd58SToby Isaac }
5783f19dbd58SToby Isaac 
57846636e97aSMatthew G Knepley /*@
57851cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
57866636e97aSMatthew G Knepley 
5787d083f849SBarry Smith   Collective on dm
57886636e97aSMatthew G Knepley 
57896636e97aSMatthew G Knepley   Input Parameter:
57906636e97aSMatthew G Knepley . dm - the DM
57916636e97aSMatthew G Knepley 
57926636e97aSMatthew G Knepley   Output Parameter:
57936636e97aSMatthew G Knepley . cdm - coordinate DM
57946636e97aSMatthew G Knepley 
57956636e97aSMatthew G Knepley   Level: intermediate
57966636e97aSMatthew G Knepley 
57971cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
57986636e97aSMatthew G Knepley @*/
57996636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
58006636e97aSMatthew G Knepley {
58016636e97aSMatthew G Knepley   PetscErrorCode ierr;
58026636e97aSMatthew G Knepley 
58036636e97aSMatthew G Knepley   PetscFunctionBegin;
58046636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58056636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
58066636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5807308f8a94SToby Isaac     DM cdm;
5808308f8a94SToby Isaac 
580982f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5810308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5811308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5812308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5813308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5814308f8a94SToby Isaac     dm->coordinateDM = cdm;
58156636e97aSMatthew G Knepley   }
58166636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
58176636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58186636e97aSMatthew G Knepley }
5819e87bb0d3SMatthew G Knepley 
58201cfe2091SMatthew G. Knepley /*@
58211cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
58221cfe2091SMatthew G. Knepley 
5823d083f849SBarry Smith   Logically Collective on dm
58241cfe2091SMatthew G. Knepley 
58251cfe2091SMatthew G. Knepley   Input Parameters:
58261cfe2091SMatthew G. Knepley + dm - the DM
58271cfe2091SMatthew G. Knepley - cdm - coordinate DM
58281cfe2091SMatthew G. Knepley 
58291cfe2091SMatthew G. Knepley   Level: intermediate
58301cfe2091SMatthew G. Knepley 
58311cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58321cfe2091SMatthew G. Knepley @*/
58331cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
58341cfe2091SMatthew G. Knepley {
58351cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
58361cfe2091SMatthew G. Knepley 
58371cfe2091SMatthew G. Knepley   PetscFunctionBegin;
58381cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58391cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5840f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
58411cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
58421cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
58431cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
58441cfe2091SMatthew G. Knepley }
58451cfe2091SMatthew G. Knepley 
584646e270d4SMatthew G. Knepley /*@
584746e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
584846e270d4SMatthew G. Knepley 
584946e270d4SMatthew G. Knepley   Not Collective
585046e270d4SMatthew G. Knepley 
585146e270d4SMatthew G. Knepley   Input Parameter:
585246e270d4SMatthew G. Knepley . dm - The DM object
585346e270d4SMatthew G. Knepley 
585446e270d4SMatthew G. Knepley   Output Parameter:
585546e270d4SMatthew G. Knepley . dim - The embedding dimension
585646e270d4SMatthew G. Knepley 
585746e270d4SMatthew G. Knepley   Level: intermediate
585846e270d4SMatthew G. Knepley 
585992fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
586046e270d4SMatthew G. Knepley @*/
586146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
586246e270d4SMatthew G. Knepley {
586346e270d4SMatthew G. Knepley   PetscFunctionBegin;
586446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5865534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
58669a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
58679a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
58689a9a41abSToby Isaac   }
586946e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
587046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
587146e270d4SMatthew G. Knepley }
587246e270d4SMatthew G. Knepley 
587346e270d4SMatthew G. Knepley /*@
587446e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
587546e270d4SMatthew G. Knepley 
587646e270d4SMatthew G. Knepley   Not Collective
587746e270d4SMatthew G. Knepley 
587846e270d4SMatthew G. Knepley   Input Parameters:
587946e270d4SMatthew G. Knepley + dm  - The DM object
588046e270d4SMatthew G. Knepley - dim - The embedding dimension
588146e270d4SMatthew G. Knepley 
588246e270d4SMatthew G. Knepley   Level: intermediate
588346e270d4SMatthew G. Knepley 
588492fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
588546e270d4SMatthew G. Knepley @*/
588646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
588746e270d4SMatthew G. Knepley {
5888e5e52638SMatthew G. Knepley   PetscDS        ds;
5889f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5890f17e8794SMatthew G. Knepley 
589146e270d4SMatthew G. Knepley   PetscFunctionBegin;
589246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
589346e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5894e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5895e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
589646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
589746e270d4SMatthew G. Knepley }
589846e270d4SMatthew G. Knepley 
5899e8abe2deSMatthew G. Knepley /*@
5900e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5901e8abe2deSMatthew G. Knepley 
5902d083f849SBarry Smith   Collective on dm
5903e8abe2deSMatthew G. Knepley 
5904e8abe2deSMatthew G. Knepley   Input Parameter:
5905e8abe2deSMatthew G. Knepley . dm - The DM object
5906e8abe2deSMatthew G. Knepley 
5907e8abe2deSMatthew G. Knepley   Output Parameter:
5908e8abe2deSMatthew G. Knepley . section - The PetscSection object
5909e8abe2deSMatthew G. Knepley 
5910e8abe2deSMatthew G. Knepley   Level: intermediate
5911e8abe2deSMatthew G. Knepley 
591292fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
5913e8abe2deSMatthew G. Knepley @*/
5914e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5915e8abe2deSMatthew G. Knepley {
5916e8abe2deSMatthew G. Knepley   DM             cdm;
5917e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5918e8abe2deSMatthew G. Knepley 
5919e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5920e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5921e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5922e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
592392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
5924e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5925e8abe2deSMatthew G. Knepley }
5926e8abe2deSMatthew G. Knepley 
5927e8abe2deSMatthew G. Knepley /*@
5928e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5929e8abe2deSMatthew G. Knepley 
5930e8abe2deSMatthew G. Knepley   Not Collective
5931e8abe2deSMatthew G. Knepley 
5932e8abe2deSMatthew G. Knepley   Input Parameters:
5933e8abe2deSMatthew G. Knepley + dm      - The DM object
593446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5935e8abe2deSMatthew G. Knepley - section - The PetscSection object
5936e8abe2deSMatthew G. Knepley 
5937e8abe2deSMatthew G. Knepley   Level: intermediate
5938e8abe2deSMatthew G. Knepley 
593992fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
5940e8abe2deSMatthew G. Knepley @*/
594146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5942e8abe2deSMatthew G. Knepley {
5943e8abe2deSMatthew G. Knepley   DM             cdm;
5944e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5945e8abe2deSMatthew G. Knepley 
5946e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5947e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
594846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5949e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
595092fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
595146e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
59524c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
595346e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
595446e270d4SMatthew G. Knepley 
595546e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
595646e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
595746e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
595846e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
595946e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
596046e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
596146e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
596246e270d4SMatthew G. Knepley     }
5963ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
596446e270d4SMatthew G. Knepley   }
5965e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5966e8abe2deSMatthew G. Knepley }
5967e8abe2deSMatthew G. Knepley 
59685dc8c3f7SMatthew G. Knepley /*@C
596990b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
59705dc8c3f7SMatthew G. Knepley 
59715dc8c3f7SMatthew G. Knepley   Input Parameters:
597290b157c4SStefano Zampini . dm      - The DM object
597390b157c4SStefano Zampini 
597490b157c4SStefano Zampini   Output Parameters:
597590b157c4SStefano Zampini + per     - Whether the DM is periodic or not
59765dc8c3f7SMatthew 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
59775dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
59785dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
59795dc8c3f7SMatthew G. Knepley 
59805dc8c3f7SMatthew G. Knepley   Level: developer
59815dc8c3f7SMatthew G. Knepley 
59825dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
59835dc8c3f7SMatthew G. Knepley @*/
598490b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5985c6b900c6SMatthew G. Knepley {
5986c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5987c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
598890b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5989c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5990c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
59915dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5992c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5993c6b900c6SMatthew G. Knepley }
5994c6b900c6SMatthew G. Knepley 
59955dc8c3f7SMatthew G. Knepley /*@C
59965dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
59975dc8c3f7SMatthew G. Knepley 
59985dc8c3f7SMatthew G. Knepley   Input Parameters:
59995dc8c3f7SMatthew G. Knepley + dm      - The DM object
600090b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
60015dc8c3f7SMatthew 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
60025dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
60035dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
60045dc8c3f7SMatthew G. Knepley 
60055dc8c3f7SMatthew G. Knepley   Level: developer
60065dc8c3f7SMatthew G. Knepley 
60075dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
60085dc8c3f7SMatthew G. Knepley @*/
600990b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6010c6b900c6SMatthew G. Knepley {
6011c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6012c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6013c6b900c6SMatthew G. Knepley 
6014c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6015c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
601690b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
601790b157c4SStefano Zampini   if (maxCell) {
6018534a8f05SLisandro Dalcin     PetscValidRealPointer(maxCell,3);
6019534a8f05SLisandro Dalcin     PetscValidRealPointer(L,4);
602090b157c4SStefano Zampini     PetscValidPointer(bd,5);
602190b157c4SStefano Zampini   }
60225dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
60235dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
602490b157c4SStefano Zampini   if (maxCell) {
60255dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
60265dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
602790b157c4SStefano Zampini   }
6028072d7d67SStefano Zampini   dm->periodic = per;
6029c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6030c6b900c6SMatthew G. Knepley }
6031c6b900c6SMatthew G. Knepley 
60322e17dfb7SMatthew G. Knepley /*@
60332e17dfb7SMatthew 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.
60342e17dfb7SMatthew G. Knepley 
60352e17dfb7SMatthew G. Knepley   Input Parameters:
60362e17dfb7SMatthew G. Knepley + dm     - The DM
603765da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
603865da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
60392e17dfb7SMatthew G. Knepley 
60402e17dfb7SMatthew G. Knepley   Output Parameter:
60412e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60422e17dfb7SMatthew G. Knepley 
60432e17dfb7SMatthew G. Knepley   Level: developer
60442e17dfb7SMatthew G. Knepley 
60452e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60462e17dfb7SMatthew G. Knepley @*/
604765da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
60482e17dfb7SMatthew G. Knepley {
60492e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
60502e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
60512e17dfb7SMatthew G. Knepley 
60522e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60532e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
60542e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60552e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60562e17dfb7SMatthew G. Knepley   } else {
605765da65dcSMatthew G. Knepley     if (endpoint) {
605865da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6059da3333bfSMatthew 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)) {
6060da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
606165da65dcSMatthew G. Knepley         } else {
6062da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
606365da65dcSMatthew G. Knepley         }
606465da65dcSMatthew G. Knepley       }
606565da65dcSMatthew G. Knepley     } else {
60662e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
60671118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
60682e17dfb7SMatthew G. Knepley       }
60692e17dfb7SMatthew G. Knepley     }
607065da65dcSMatthew G. Knepley   }
60712e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60722e17dfb7SMatthew G. Knepley }
60732e17dfb7SMatthew G. Knepley 
60742e17dfb7SMatthew G. Knepley /*
60752e17dfb7SMatthew 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.
60762e17dfb7SMatthew G. Knepley 
60772e17dfb7SMatthew G. Knepley   Input Parameters:
60782e17dfb7SMatthew G. Knepley + dm     - The DM
60792e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
60802e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
60812e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
60822e17dfb7SMatthew G. Knepley 
60832e17dfb7SMatthew G. Knepley   Output Parameter:
60842e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60852e17dfb7SMatthew G. Knepley 
60862e17dfb7SMatthew G. Knepley   Level: developer
60872e17dfb7SMatthew G. Knepley 
60882e17dfb7SMatthew 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
60892e17dfb7SMatthew G. Knepley 
60902e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60912e17dfb7SMatthew G. Knepley */
60922e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
60932e17dfb7SMatthew G. Knepley {
60942e17dfb7SMatthew G. Knepley   PetscInt d;
60952e17dfb7SMatthew G. Knepley 
60962e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60972e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60982e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60992e17dfb7SMatthew G. Knepley   } else {
61002e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6101908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
61022e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
61032e17dfb7SMatthew G. Knepley       } else {
61042e17dfb7SMatthew G. Knepley         out[d] = in[d];
61052e17dfb7SMatthew G. Knepley       }
61062e17dfb7SMatthew G. Knepley     }
61072e17dfb7SMatthew G. Knepley   }
61082e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61092e17dfb7SMatthew G. Knepley }
61102e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
61112e17dfb7SMatthew G. Knepley {
61122e17dfb7SMatthew G. Knepley   PetscInt d;
61132e17dfb7SMatthew G. Knepley 
61142e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61152e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61162e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61172e17dfb7SMatthew G. Knepley   } else {
61182e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6119908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
61202e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
61212e17dfb7SMatthew G. Knepley       } else {
61222e17dfb7SMatthew G. Knepley         out[d] = in[d];
61232e17dfb7SMatthew G. Knepley       }
61242e17dfb7SMatthew G. Knepley     }
61252e17dfb7SMatthew G. Knepley   }
61262e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61272e17dfb7SMatthew G. Knepley }
61282e17dfb7SMatthew G. Knepley 
61292e17dfb7SMatthew G. Knepley /*
61302e17dfb7SMatthew 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.
61312e17dfb7SMatthew G. Knepley 
61322e17dfb7SMatthew G. Knepley   Input Parameters:
61332e17dfb7SMatthew G. Knepley + dm     - The DM
61342e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
61352e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
61362e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
61372e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
61382e17dfb7SMatthew G. Knepley 
61392e17dfb7SMatthew G. Knepley   Output Parameter:
61402e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
61412e17dfb7SMatthew G. Knepley 
61422e17dfb7SMatthew G. Knepley   Level: developer
61432e17dfb7SMatthew G. Knepley 
61442e17dfb7SMatthew 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
61452e17dfb7SMatthew G. Knepley 
61462e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
61472e17dfb7SMatthew G. Knepley */
61482e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
61492e17dfb7SMatthew G. Knepley {
61502e17dfb7SMatthew G. Knepley   PetscInt d;
61512e17dfb7SMatthew G. Knepley 
61522e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61532e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61542e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
61552e17dfb7SMatthew G. Knepley   } else {
61562e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6157908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
61582e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
61592e17dfb7SMatthew G. Knepley       } else {
61602e17dfb7SMatthew G. Knepley         out[d] += in[d];
61612e17dfb7SMatthew G. Knepley       }
61622e17dfb7SMatthew G. Knepley     }
61632e17dfb7SMatthew G. Knepley   }
61642e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61652e17dfb7SMatthew G. Knepley }
61662e17dfb7SMatthew G. Knepley 
616736447a5eSToby Isaac /*@
61688f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
61698f700142SStefano Zampini 
61708f700142SStefano Zampini   Not collective
617136447a5eSToby Isaac 
617236447a5eSToby Isaac   Input Parameter:
617336447a5eSToby Isaac . dm - The DM
617436447a5eSToby Isaac 
617536447a5eSToby Isaac   Output Parameter:
617636447a5eSToby Isaac   areLocalized - True if localized
617736447a5eSToby Isaac 
617836447a5eSToby Isaac   Level: developer
617936447a5eSToby Isaac 
61808f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
618136447a5eSToby Isaac @*/
61828f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
618336447a5eSToby Isaac {
618436447a5eSToby Isaac   DM             cdm;
618536447a5eSToby Isaac   PetscSection   coordSection;
618646a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
618746a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
618836447a5eSToby Isaac   PetscErrorCode ierr;
618936447a5eSToby Isaac 
619036447a5eSToby Isaac   PetscFunctionBegin;
619136447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6192534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
61938b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
619446a3a80fSLisandro Dalcin 
619536447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
619636447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
619746a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
61989f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
619946a3a80fSLisandro Dalcin 
62009f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
620146a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
620236447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
620336447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
620446a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
620546a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
620636447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
620746a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
620836447a5eSToby Isaac   }
62098f700142SStefano Zampini   *areLocalized = alreadyLocalized;
621036447a5eSToby Isaac   PetscFunctionReturn(0);
621136447a5eSToby Isaac }
621236447a5eSToby Isaac 
62138f700142SStefano Zampini /*@
62148f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
62158f700142SStefano Zampini 
62168f700142SStefano Zampini   Collective on dm
62178f700142SStefano Zampini 
62188f700142SStefano Zampini   Input Parameter:
62198f700142SStefano Zampini . dm - The DM
62208f700142SStefano Zampini 
62218f700142SStefano Zampini   Output Parameter:
62228f700142SStefano Zampini   areLocalized - True if localized
62238f700142SStefano Zampini 
62248f700142SStefano Zampini   Level: developer
62258f700142SStefano Zampini 
62268f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
62278f700142SStefano Zampini @*/
62288f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
62298f700142SStefano Zampini {
62308f700142SStefano Zampini   PetscBool      localized;
62318f700142SStefano Zampini   PetscErrorCode ierr;
62328f700142SStefano Zampini 
62338f700142SStefano Zampini   PetscFunctionBegin;
62348f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6235534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62368f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
62378f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
62388f700142SStefano Zampini   PetscFunctionReturn(0);
62398f700142SStefano Zampini }
624036447a5eSToby Isaac 
62412e17dfb7SMatthew G. Knepley /*@
6242492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
62432e17dfb7SMatthew G. Knepley 
62448f700142SStefano Zampini   Collective on dm
62458f700142SStefano Zampini 
62462e17dfb7SMatthew G. Knepley   Input Parameter:
62472e17dfb7SMatthew G. Knepley . dm - The DM
62482e17dfb7SMatthew G. Knepley 
62492e17dfb7SMatthew G. Knepley   Level: developer
62502e17dfb7SMatthew G. Knepley 
62518f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
62522e17dfb7SMatthew G. Knepley @*/
62532e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
62542e17dfb7SMatthew G. Knepley {
62552e17dfb7SMatthew G. Knepley   DM             cdm;
62562e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
62572e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
62583e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
62593e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6260e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
62613e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
62623e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
62632e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
62642e17dfb7SMatthew G. Knepley 
62652e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
62662e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
626792c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6268f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6269f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6270f7cbd40bSStefano Zampini 
62712e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
62722e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62732e17dfb7SMatthew G. Knepley   {
62742e17dfb7SMatthew G. Knepley     PetscBool isplex;
62752e17dfb7SMatthew G. Knepley 
62762e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
62772e17dfb7SMatthew G. Knepley     if (isplex) {
62782e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
62793e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
628069291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
62813e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
62823e922f36SToby Isaac       newStart = vStart;
62833e922f36SToby Isaac       newEnd   = vEnd;
62843e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
62853e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
62863e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
62873e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
62883e922f36SToby Isaac       }
62892e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
62902e17dfb7SMatthew G. Knepley   }
62912e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
629243eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
62932e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
62943e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6295e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
62963e922f36SToby Isaac 
62972e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
62982e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
62992e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
63002e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
63013e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
63023e922f36SToby Isaac 
630369291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63043e922f36SToby Isaac   localized = &anchor[bs];
63053e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
63063e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63073e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63083e922f36SToby Isaac 
63093e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
63103e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
63113e922f36SToby Isaac       PetscInt     b;
63123e922f36SToby Isaac 
63133e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
63143e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63153e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63163e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
63173e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
63183e922f36SToby Isaac         for (b = 0; b < bs; b++) {
63193e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
63203e922f36SToby Isaac         }
63213e922f36SToby Isaac         if (b < bs) break;
63223e922f36SToby Isaac       }
63233e922f36SToby Isaac       if (d < dof/bs) {
63243e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
63253e922f36SToby Isaac           PetscInt cdof;
63263e922f36SToby Isaac 
63273e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
63283e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
63293e922f36SToby Isaac         }
63303e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
63313e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
63323e922f36SToby Isaac       }
63333e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63343e922f36SToby Isaac     }
63353e922f36SToby Isaac   }
63363e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
63373e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
633869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63393e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
634069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
63413e922f36SToby Isaac     PetscFunctionReturn(0);
63423e922f36SToby Isaac   }
63432e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63442e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63452e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
63462e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
63472e17dfb7SMatthew G. Knepley   }
63482e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
63492e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6350c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
63512e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
63522e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
63532e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
63542e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6355c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63562e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
63572e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63582e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63592e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
63602e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
63612e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
63622e17dfb7SMatthew G. Knepley   }
63633e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63643e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63653e922f36SToby Isaac 
63662e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
63672e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
63683e922f36SToby Isaac       PetscInt     b, cdof;
63692e17dfb7SMatthew G. Knepley 
63703e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
63713e922f36SToby Isaac       if (!cdof) continue;
63722e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63732e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
63742e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63752e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
63762e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63772e17dfb7SMatthew G. Knepley     }
63783e922f36SToby Isaac   }
637969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
638069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6381c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63822e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
63832e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
63842e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
63852e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
63862e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
63872e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63882e17dfb7SMatthew G. Knepley }
63892e17dfb7SMatthew G. Knepley 
6390e87bb0d3SMatthew G Knepley /*@
63913a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6392e87bb0d3SMatthew G Knepley 
6393d083f849SBarry Smith   Collective on v (see explanation below)
6394e87bb0d3SMatthew G Knepley 
6395e87bb0d3SMatthew G Knepley   Input Parameters:
6396e87bb0d3SMatthew G Knepley + dm - The DM
63973a93e3b7SToby Isaac . v - The Vec of points
639862a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
63993a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6400e87bb0d3SMatthew G Knepley 
640161e3bb9bSMatthew G Knepley   Output Parameter:
640262a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
640362a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
64043a93e3b7SToby Isaac 
6405e87bb0d3SMatthew G Knepley 
6406e87bb0d3SMatthew G Knepley   Level: developer
640761e3bb9bSMatthew G Knepley 
640862a38674SMatthew G. Knepley   Notes:
64093a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
641062a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
64113a93e3b7SToby Isaac 
64123a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
641362a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
64143a93e3b7SToby Isaac 
64153a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
64163a93e3b7SToby Isaac 
641762a38674SMatthew G. Knepley $    const PetscSFNode *cells;
641862a38674SMatthew G. Knepley $    PetscInt           nFound;
6419a6216909SToby Isaac $    const PetscInt    *found;
642062a38674SMatthew G. Knepley $
6421a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
64223a93e3b7SToby Isaac 
64233a93e3b7SToby 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
64243a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
64253a93e3b7SToby Isaac 
642662a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
642761e3bb9bSMatthew G Knepley @*/
642862a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6429e87bb0d3SMatthew G Knepley {
6430735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6431735aa83eSMatthew G Knepley 
6432e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6433e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6434e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6435e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
64363a93e3b7SToby Isaac   if (*cellSF) {
64373a93e3b7SToby Isaac     PetscMPIInt result;
64383a93e3b7SToby Isaac 
6439e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6440a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
64413a93e3b7SToby 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");
6442e0fc9d1bSMatthew G. Knepley   } else {
64433a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
64443a93e3b7SToby Isaac   }
6445b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
644647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
644762a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
644847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6449e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6450e87bb0d3SMatthew G Knepley }
645114f150ffSMatthew G. Knepley 
6452f4d763aaSMatthew G. Knepley /*@
6453f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6454f4d763aaSMatthew G. Knepley 
64558f700142SStefano Zampini   Collective on dm
64568f700142SStefano Zampini 
6457f4d763aaSMatthew G. Knepley   Input Parameter:
6458f4d763aaSMatthew G. Knepley . dm - The original DM
6459f4d763aaSMatthew G. Knepley 
6460f4d763aaSMatthew G. Knepley   Output Parameter:
6461f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6462f4d763aaSMatthew G. Knepley 
6463f4d763aaSMatthew G. Knepley   Level: intermediate
6464f4d763aaSMatthew G. Knepley 
6465e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6466f4d763aaSMatthew G. Knepley @*/
646714f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
646814f150ffSMatthew G. Knepley {
6469c26acbdeSMatthew G. Knepley   PetscSection   section;
64702d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
647114f150ffSMatthew G. Knepley   PetscErrorCode ierr;
647214f150ffSMatthew G. Knepley 
647314f150ffSMatthew G. Knepley   PetscFunctionBegin;
647414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
647514f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
647692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6477c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6478127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
64792d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6480c26acbdeSMatthew G. Knepley     *odm = dm;
6481c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6482c26acbdeSMatthew G. Knepley   }
648314f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6484c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
648514f150ffSMatthew G. Knepley     PetscSF      sf;
648614f150ffSMatthew G. Knepley 
648714f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6488e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
648914f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
649092fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
649114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
649214f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
649315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6494e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
649514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
649614f150ffSMatthew G. Knepley   }
649714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
649814f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
649914f150ffSMatthew G. Knepley }
6500f4d763aaSMatthew G. Knepley 
6501f4d763aaSMatthew G. Knepley /*@
6502cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6503f4d763aaSMatthew G. Knepley 
6504f4d763aaSMatthew G. Knepley   Input Parameter:
6505f4d763aaSMatthew G. Knepley . dm - The original DM
6506f4d763aaSMatthew G. Knepley 
6507cdb7a50dSMatthew G. Knepley   Output Parameters:
6508cdb7a50dSMatthew G. Knepley + num - The output sequence number
6509cdb7a50dSMatthew G. Knepley - val - The output sequence value
6510f4d763aaSMatthew G. Knepley 
6511f4d763aaSMatthew G. Knepley   Level: intermediate
6512f4d763aaSMatthew G. Knepley 
6513f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6514f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6515f4d763aaSMatthew G. Knepley 
6516f4d763aaSMatthew G. Knepley .seealso: VecView()
6517f4d763aaSMatthew G. Knepley @*/
6518cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6519f4d763aaSMatthew G. Knepley {
6520f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6521f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6522534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6523534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6524f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6525f4d763aaSMatthew G. Knepley }
6526f4d763aaSMatthew G. Knepley 
6527f4d763aaSMatthew G. Knepley /*@
6528cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6529f4d763aaSMatthew G. Knepley 
6530f4d763aaSMatthew G. Knepley   Input Parameters:
6531f4d763aaSMatthew G. Knepley + dm - The original DM
6532cdb7a50dSMatthew G. Knepley . num - The output sequence number
6533cdb7a50dSMatthew G. Knepley - val - The output sequence value
6534f4d763aaSMatthew G. Knepley 
6535f4d763aaSMatthew G. Knepley   Level: intermediate
6536f4d763aaSMatthew G. Knepley 
6537f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6538f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6539f4d763aaSMatthew G. Knepley 
6540f4d763aaSMatthew G. Knepley .seealso: VecView()
6541f4d763aaSMatthew G. Knepley @*/
6542cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6543f4d763aaSMatthew G. Knepley {
6544f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6545f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6546f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6547cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6548cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6549cdb7a50dSMatthew G. Knepley }
6550cdb7a50dSMatthew G. Knepley 
6551cdb7a50dSMatthew G. Knepley /*@C
6552cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6553cdb7a50dSMatthew G. Knepley 
6554cdb7a50dSMatthew G. Knepley   Input Parameters:
6555cdb7a50dSMatthew G. Knepley + dm   - The original DM
6556cdb7a50dSMatthew G. Knepley . name - The sequence name
6557cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6558cdb7a50dSMatthew G. Knepley 
6559cdb7a50dSMatthew G. Knepley   Output Parameter:
6560cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6561cdb7a50dSMatthew G. Knepley 
6562cdb7a50dSMatthew G. Knepley   Level: intermediate
6563cdb7a50dSMatthew G. Knepley 
6564cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6565cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6566cdb7a50dSMatthew G. Knepley 
6567cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6568cdb7a50dSMatthew G. Knepley @*/
6569cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6570cdb7a50dSMatthew G. Knepley {
6571cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6572cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6573cdb7a50dSMatthew G. Knepley 
6574cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6575cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6576cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6577534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6578cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6579cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6580cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6581cdb7a50dSMatthew G. Knepley     PetscScalar value;
6582cdb7a50dSMatthew G. Knepley 
658339d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
65844aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6585cdb7a50dSMatthew G. Knepley #endif
6586cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6587f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6588f4d763aaSMatthew G. Knepley }
65898e4ac7eaSMatthew G. Knepley 
65908e4ac7eaSMatthew G. Knepley /*@
65918e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
65928e4ac7eaSMatthew G. Knepley 
65938e4ac7eaSMatthew G. Knepley   Not collective
65948e4ac7eaSMatthew G. Knepley 
65958e4ac7eaSMatthew G. Knepley   Input Parameter:
65968e4ac7eaSMatthew G. Knepley . dm - The DM
65978e4ac7eaSMatthew G. Knepley 
65988e4ac7eaSMatthew G. Knepley   Output Parameter:
65998e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
66008e4ac7eaSMatthew G. Knepley 
66018e4ac7eaSMatthew G. Knepley   Level: beginner
66028e4ac7eaSMatthew G. Knepley 
66038e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
66048e4ac7eaSMatthew G. Knepley @*/
66058e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
66068e4ac7eaSMatthew G. Knepley {
66078e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66088e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6609534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
66108e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66118e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66128e4ac7eaSMatthew G. Knepley }
66138e4ac7eaSMatthew G. Knepley 
66148e4ac7eaSMatthew G. Knepley /*@
66155d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
66168e4ac7eaSMatthew G. Knepley 
66178e4ac7eaSMatthew G. Knepley   Collective on dm
66188e4ac7eaSMatthew G. Knepley 
66198e4ac7eaSMatthew G. Knepley   Input Parameters:
66208e4ac7eaSMatthew G. Knepley + dm - The DM
66218e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
66228e4ac7eaSMatthew G. Knepley 
66235d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
66245d3b26e6SMatthew G. Knepley 
66258e4ac7eaSMatthew G. Knepley   Level: beginner
66268e4ac7eaSMatthew G. Knepley 
66275d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
66288e4ac7eaSMatthew G. Knepley @*/
66298e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
66308e4ac7eaSMatthew G. Knepley {
66318e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66328e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66338833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66348e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66358e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66368e4ac7eaSMatthew G. Knepley }
6637c58f1c22SToby Isaac 
6638c58f1c22SToby Isaac 
6639c58f1c22SToby Isaac /*@C
6640c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6641c58f1c22SToby Isaac 
6642c58f1c22SToby Isaac   Not Collective
6643c58f1c22SToby Isaac 
6644c58f1c22SToby Isaac   Input Parameters:
6645c58f1c22SToby Isaac + dm   - The DM object
6646c58f1c22SToby Isaac - name - The label name
6647c58f1c22SToby Isaac 
6648c58f1c22SToby Isaac   Level: intermediate
6649c58f1c22SToby Isaac 
6650c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6651c58f1c22SToby Isaac @*/
6652c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6653c58f1c22SToby Isaac {
66545d80c0bfSVaclav Hapla   PetscBool      flg;
66555d80c0bfSVaclav Hapla   DMLabel        label;
6656c58f1c22SToby Isaac   PetscErrorCode ierr;
6657c58f1c22SToby Isaac 
6658c58f1c22SToby Isaac   PetscFunctionBegin;
6659c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6660c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66615d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
6662c58f1c22SToby Isaac   if (!flg) {
66635d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
66645d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
66655d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
6666c58f1c22SToby Isaac   }
6667c58f1c22SToby Isaac   PetscFunctionReturn(0);
6668c58f1c22SToby Isaac }
6669c58f1c22SToby Isaac 
6670c58f1c22SToby Isaac /*@C
6671c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6672c58f1c22SToby Isaac 
6673c58f1c22SToby Isaac   Not Collective
6674c58f1c22SToby Isaac 
6675c58f1c22SToby Isaac   Input Parameters:
6676c58f1c22SToby Isaac + dm   - The DM object
6677c58f1c22SToby Isaac . name - The label name
6678c58f1c22SToby Isaac - point - The mesh point
6679c58f1c22SToby Isaac 
6680c58f1c22SToby Isaac   Output Parameter:
6681c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6682c58f1c22SToby Isaac 
6683c58f1c22SToby Isaac   Level: beginner
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6686c58f1c22SToby Isaac @*/
6687c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6688c58f1c22SToby Isaac {
6689c58f1c22SToby Isaac   DMLabel        label;
6690c58f1c22SToby Isaac   PetscErrorCode ierr;
6691c58f1c22SToby Isaac 
6692c58f1c22SToby Isaac   PetscFunctionBegin;
6693c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6694c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6695c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
669613903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6697c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6698c58f1c22SToby Isaac   PetscFunctionReturn(0);
6699c58f1c22SToby Isaac }
6700c58f1c22SToby Isaac 
6701c58f1c22SToby Isaac /*@C
6702c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6703c58f1c22SToby Isaac 
6704c58f1c22SToby Isaac   Not Collective
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac   Input Parameters:
6707c58f1c22SToby Isaac + dm   - The DM object
6708c58f1c22SToby Isaac . name - The label name
6709c58f1c22SToby Isaac . point - The mesh point
6710c58f1c22SToby Isaac - value - The label value for this point
6711c58f1c22SToby Isaac 
6712c58f1c22SToby Isaac   Output Parameter:
6713c58f1c22SToby Isaac 
6714c58f1c22SToby Isaac   Level: beginner
6715c58f1c22SToby Isaac 
6716c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6717c58f1c22SToby Isaac @*/
6718c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6719c58f1c22SToby Isaac {
6720c58f1c22SToby Isaac   DMLabel        label;
6721c58f1c22SToby Isaac   PetscErrorCode ierr;
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac   PetscFunctionBegin;
6724c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6725c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6726c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6727c58f1c22SToby Isaac   if (!label) {
6728c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6729c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6730c58f1c22SToby Isaac   }
6731c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6732c58f1c22SToby Isaac   PetscFunctionReturn(0);
6733c58f1c22SToby Isaac }
6734c58f1c22SToby Isaac 
6735c58f1c22SToby Isaac /*@C
6736c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6737c58f1c22SToby Isaac 
6738c58f1c22SToby Isaac   Not Collective
6739c58f1c22SToby Isaac 
6740c58f1c22SToby Isaac   Input Parameters:
6741c58f1c22SToby Isaac + dm   - The DM object
6742c58f1c22SToby Isaac . name - The label name
6743c58f1c22SToby Isaac . point - The mesh point
6744c58f1c22SToby Isaac - value - The label value for this point
6745c58f1c22SToby Isaac 
6746c58f1c22SToby Isaac   Output Parameter:
6747c58f1c22SToby Isaac 
6748c58f1c22SToby Isaac   Level: beginner
6749c58f1c22SToby Isaac 
6750c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6751c58f1c22SToby Isaac @*/
6752c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6753c58f1c22SToby Isaac {
6754c58f1c22SToby Isaac   DMLabel        label;
6755c58f1c22SToby Isaac   PetscErrorCode ierr;
6756c58f1c22SToby Isaac 
6757c58f1c22SToby Isaac   PetscFunctionBegin;
6758c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6759c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6760c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6761c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6762c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6763c58f1c22SToby Isaac   PetscFunctionReturn(0);
6764c58f1c22SToby Isaac }
6765c58f1c22SToby Isaac 
6766c58f1c22SToby Isaac /*@C
6767c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6768c58f1c22SToby Isaac 
6769c58f1c22SToby Isaac   Not Collective
6770c58f1c22SToby Isaac 
6771c58f1c22SToby Isaac   Input Parameters:
6772c58f1c22SToby Isaac + dm   - The DM object
6773c58f1c22SToby Isaac - name - The label name
6774c58f1c22SToby Isaac 
6775c58f1c22SToby Isaac   Output Parameter:
6776c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6777c58f1c22SToby Isaac 
6778c58f1c22SToby Isaac   Level: beginner
6779c58f1c22SToby Isaac 
6780df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6781c58f1c22SToby Isaac @*/
6782c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6783c58f1c22SToby Isaac {
6784c58f1c22SToby Isaac   DMLabel        label;
6785c58f1c22SToby Isaac   PetscErrorCode ierr;
6786c58f1c22SToby Isaac 
6787c58f1c22SToby Isaac   PetscFunctionBegin;
6788c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6789c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6790534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
6791c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6792c58f1c22SToby Isaac   *size = 0;
6793c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6794c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6795c58f1c22SToby Isaac   PetscFunctionReturn(0);
6796c58f1c22SToby Isaac }
6797c58f1c22SToby Isaac 
6798c58f1c22SToby Isaac /*@C
6799c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6800c58f1c22SToby Isaac 
6801c58f1c22SToby Isaac   Not Collective
6802c58f1c22SToby Isaac 
6803c58f1c22SToby Isaac   Input Parameters:
6804c58f1c22SToby Isaac + mesh - The DM object
6805c58f1c22SToby Isaac - name - The label name
6806c58f1c22SToby Isaac 
6807c58f1c22SToby Isaac   Output Parameter:
6808c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6809c58f1c22SToby Isaac 
6810c58f1c22SToby Isaac   Level: beginner
6811c58f1c22SToby Isaac 
6812c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6813c58f1c22SToby Isaac @*/
6814c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6815c58f1c22SToby Isaac {
6816c58f1c22SToby Isaac   DMLabel        label;
6817c58f1c22SToby Isaac   PetscErrorCode ierr;
6818c58f1c22SToby Isaac 
6819c58f1c22SToby Isaac   PetscFunctionBegin;
6820c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6821c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6822c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6823c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6824c58f1c22SToby Isaac   *ids = NULL;
6825dab2e251SBlaise Bourdin  if (label) {
6826c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6827dab2e251SBlaise Bourdin   } else {
6828dab2e251SBlaise Bourdin     /* returning an empty IS */
6829dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6830dab2e251SBlaise Bourdin   }
6831c58f1c22SToby Isaac   PetscFunctionReturn(0);
6832c58f1c22SToby Isaac }
6833c58f1c22SToby Isaac 
6834c58f1c22SToby Isaac /*@C
6835c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6836c58f1c22SToby Isaac 
6837c58f1c22SToby Isaac   Not Collective
6838c58f1c22SToby Isaac 
6839c58f1c22SToby Isaac   Input Parameters:
6840c58f1c22SToby Isaac + dm - The DM object
6841c58f1c22SToby Isaac . name - The label name
6842c58f1c22SToby Isaac - value - The stratum value
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac   Output Parameter:
6845c58f1c22SToby Isaac . size - The stratum size
6846c58f1c22SToby Isaac 
6847c58f1c22SToby Isaac   Level: beginner
6848c58f1c22SToby Isaac 
6849c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6850c58f1c22SToby Isaac @*/
6851c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6852c58f1c22SToby Isaac {
6853c58f1c22SToby Isaac   DMLabel        label;
6854c58f1c22SToby Isaac   PetscErrorCode ierr;
6855c58f1c22SToby Isaac 
6856c58f1c22SToby Isaac   PetscFunctionBegin;
6857c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6858c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6859534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
6860c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6861c58f1c22SToby Isaac   *size = 0;
6862c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6863c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6864c58f1c22SToby Isaac   PetscFunctionReturn(0);
6865c58f1c22SToby Isaac }
6866c58f1c22SToby Isaac 
6867c58f1c22SToby Isaac /*@C
6868c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6869c58f1c22SToby Isaac 
6870c58f1c22SToby Isaac   Not Collective
6871c58f1c22SToby Isaac 
6872c58f1c22SToby Isaac   Input Parameters:
6873c58f1c22SToby Isaac + dm - The DM object
6874c58f1c22SToby Isaac . name - The label name
6875c58f1c22SToby Isaac - value - The stratum value
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac   Output Parameter:
6878c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6879c58f1c22SToby Isaac 
6880c58f1c22SToby Isaac   Level: beginner
6881c58f1c22SToby Isaac 
6882c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6883c58f1c22SToby Isaac @*/
6884c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6885c58f1c22SToby Isaac {
6886c58f1c22SToby Isaac   DMLabel        label;
6887c58f1c22SToby Isaac   PetscErrorCode ierr;
6888c58f1c22SToby Isaac 
6889c58f1c22SToby Isaac   PetscFunctionBegin;
6890c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6891c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6892c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6893c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6894c58f1c22SToby Isaac   *points = NULL;
6895c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6896c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6897c58f1c22SToby Isaac   PetscFunctionReturn(0);
6898c58f1c22SToby Isaac }
6899c58f1c22SToby Isaac 
69004de306b1SToby Isaac /*@C
69019044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69024de306b1SToby Isaac 
69034de306b1SToby Isaac   Not Collective
69044de306b1SToby Isaac 
69054de306b1SToby Isaac   Input Parameters:
69064de306b1SToby Isaac + dm - The DM object
69074de306b1SToby Isaac . name - The label name
69084de306b1SToby Isaac . value - The stratum value
69094de306b1SToby Isaac - points - The stratum points
69104de306b1SToby Isaac 
69114de306b1SToby Isaac   Level: beginner
69124de306b1SToby Isaac 
69134de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
69144de306b1SToby Isaac @*/
69154de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
69164de306b1SToby Isaac {
69174de306b1SToby Isaac   DMLabel        label;
69184de306b1SToby Isaac   PetscErrorCode ierr;
69194de306b1SToby Isaac 
69204de306b1SToby Isaac   PetscFunctionBegin;
69214de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69224de306b1SToby Isaac   PetscValidCharPointer(name, 2);
69234de306b1SToby Isaac   PetscValidPointer(points, 4);
69244de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
69254de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
69264de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
69274de306b1SToby Isaac   PetscFunctionReturn(0);
69284de306b1SToby Isaac }
69294de306b1SToby Isaac 
6930c58f1c22SToby Isaac /*@C
6931c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6932c58f1c22SToby Isaac 
6933c58f1c22SToby Isaac   Not Collective
6934c58f1c22SToby Isaac 
6935c58f1c22SToby Isaac   Input Parameters:
6936c58f1c22SToby Isaac + dm   - The DM object
6937c58f1c22SToby Isaac . name - The label name
6938c58f1c22SToby Isaac - value - The label value for this point
6939c58f1c22SToby Isaac 
6940c58f1c22SToby Isaac   Output Parameter:
6941c58f1c22SToby Isaac 
6942c58f1c22SToby Isaac   Level: beginner
6943c58f1c22SToby Isaac 
6944c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6945c58f1c22SToby Isaac @*/
6946c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6947c58f1c22SToby Isaac {
6948c58f1c22SToby Isaac   DMLabel        label;
6949c58f1c22SToby Isaac   PetscErrorCode ierr;
6950c58f1c22SToby Isaac 
6951c58f1c22SToby Isaac   PetscFunctionBegin;
6952c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6953c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6954c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6955c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6956c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6957c58f1c22SToby Isaac   PetscFunctionReturn(0);
6958c58f1c22SToby Isaac }
6959c58f1c22SToby Isaac 
6960c58f1c22SToby Isaac /*@
6961c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6962c58f1c22SToby Isaac 
6963c58f1c22SToby Isaac   Not Collective
6964c58f1c22SToby Isaac 
6965c58f1c22SToby Isaac   Input Parameter:
6966c58f1c22SToby Isaac . dm   - The DM object
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   Output Parameter:
6969c58f1c22SToby Isaac . numLabels - the number of Labels
6970c58f1c22SToby Isaac 
6971c58f1c22SToby Isaac   Level: intermediate
6972c58f1c22SToby Isaac 
6973c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6974c58f1c22SToby Isaac @*/
6975c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6976c58f1c22SToby Isaac {
69775d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6978c58f1c22SToby Isaac   PetscInt  n    = 0;
6979c58f1c22SToby Isaac 
6980c58f1c22SToby Isaac   PetscFunctionBegin;
6981c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6982534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
6983c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6984c58f1c22SToby Isaac   *numLabels = n;
6985c58f1c22SToby Isaac   PetscFunctionReturn(0);
6986c58f1c22SToby Isaac }
6987c58f1c22SToby Isaac 
6988c58f1c22SToby Isaac /*@C
6989c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6990c58f1c22SToby Isaac 
6991c58f1c22SToby Isaac   Not Collective
6992c58f1c22SToby Isaac 
6993c58f1c22SToby Isaac   Input Parameters:
6994c58f1c22SToby Isaac + dm - The DM object
6995c58f1c22SToby Isaac - n  - the label number
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac   Output Parameter:
6998c58f1c22SToby Isaac . name - the label name
6999c58f1c22SToby Isaac 
7000c58f1c22SToby Isaac   Level: intermediate
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7003c58f1c22SToby Isaac @*/
7004c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7005c58f1c22SToby Isaac {
70065d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7007c58f1c22SToby Isaac   PetscInt       l    = 0;
7008d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7009c58f1c22SToby Isaac 
7010c58f1c22SToby Isaac   PetscFunctionBegin;
7011c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7012c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7013c58f1c22SToby Isaac   while (next) {
7014c58f1c22SToby Isaac     if (l == n) {
7015d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7016c58f1c22SToby Isaac       PetscFunctionReturn(0);
7017c58f1c22SToby Isaac     }
7018c58f1c22SToby Isaac     ++l;
7019c58f1c22SToby Isaac     next = next->next;
7020c58f1c22SToby Isaac   }
7021c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7022c58f1c22SToby Isaac }
7023c58f1c22SToby Isaac 
7024c58f1c22SToby Isaac /*@C
7025c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7026c58f1c22SToby Isaac 
7027c58f1c22SToby Isaac   Not Collective
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac   Input Parameters:
7030c58f1c22SToby Isaac + dm   - The DM object
7031c58f1c22SToby Isaac - name - The label name
7032c58f1c22SToby Isaac 
7033c58f1c22SToby Isaac   Output Parameter:
7034c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7035c58f1c22SToby Isaac 
7036c58f1c22SToby Isaac   Level: intermediate
7037c58f1c22SToby Isaac 
7038c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7039c58f1c22SToby Isaac @*/
7040c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7041c58f1c22SToby Isaac {
70425d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7043d67d17b1SMatthew G. Knepley   const char    *lname;
7044c58f1c22SToby Isaac   PetscErrorCode ierr;
7045c58f1c22SToby Isaac 
7046c58f1c22SToby Isaac   PetscFunctionBegin;
7047c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7048c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7049534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7050c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7051c58f1c22SToby Isaac   while (next) {
7052d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7053d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7054c58f1c22SToby Isaac     if (*hasLabel) break;
7055c58f1c22SToby Isaac     next = next->next;
7056c58f1c22SToby Isaac   }
7057c58f1c22SToby Isaac   PetscFunctionReturn(0);
7058c58f1c22SToby Isaac }
7059c58f1c22SToby Isaac 
7060c58f1c22SToby Isaac /*@C
7061c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7062c58f1c22SToby Isaac 
7063c58f1c22SToby Isaac   Not Collective
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Input Parameters:
7066c58f1c22SToby Isaac + dm   - The DM object
7067c58f1c22SToby Isaac - name - The label name
7068c58f1c22SToby Isaac 
7069c58f1c22SToby Isaac   Output Parameter:
7070c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7071c58f1c22SToby Isaac 
7072c58f1c22SToby Isaac   Level: intermediate
7073c58f1c22SToby Isaac 
7074c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7075c58f1c22SToby Isaac @*/
7076c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7077c58f1c22SToby Isaac {
70785d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7079c58f1c22SToby Isaac   PetscBool      hasLabel;
7080d67d17b1SMatthew G. Knepley   const char    *lname;
7081c58f1c22SToby Isaac   PetscErrorCode ierr;
7082c58f1c22SToby Isaac 
7083c58f1c22SToby Isaac   PetscFunctionBegin;
7084c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7085c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7086c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7087c58f1c22SToby Isaac   *label = NULL;
7088c58f1c22SToby Isaac   while (next) {
7089d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7090d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7091c58f1c22SToby Isaac     if (hasLabel) {
7092c58f1c22SToby Isaac       *label = next->label;
7093c58f1c22SToby Isaac       break;
7094c58f1c22SToby Isaac     }
7095c58f1c22SToby Isaac     next = next->next;
7096c58f1c22SToby Isaac   }
7097c58f1c22SToby Isaac   PetscFunctionReturn(0);
7098c58f1c22SToby Isaac }
7099c58f1c22SToby Isaac 
7100c58f1c22SToby Isaac /*@C
7101c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7102c58f1c22SToby Isaac 
7103c58f1c22SToby Isaac   Not Collective
7104c58f1c22SToby Isaac 
7105c58f1c22SToby Isaac   Input Parameters:
7106c58f1c22SToby Isaac + dm - The DM object
7107c58f1c22SToby Isaac - n  - the label number
7108c58f1c22SToby Isaac 
7109c58f1c22SToby Isaac   Output Parameter:
7110c58f1c22SToby Isaac . label - the label
7111c58f1c22SToby Isaac 
7112c58f1c22SToby Isaac   Level: intermediate
7113c58f1c22SToby Isaac 
7114c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7115c58f1c22SToby Isaac @*/
7116c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7117c58f1c22SToby Isaac {
71185d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7119c58f1c22SToby Isaac   PetscInt    l    = 0;
7120c58f1c22SToby Isaac 
7121c58f1c22SToby Isaac   PetscFunctionBegin;
7122c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7123c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7124c58f1c22SToby Isaac   while (next) {
7125c58f1c22SToby Isaac     if (l == n) {
7126c58f1c22SToby Isaac       *label = next->label;
7127c58f1c22SToby Isaac       PetscFunctionReturn(0);
7128c58f1c22SToby Isaac     }
7129c58f1c22SToby Isaac     ++l;
7130c58f1c22SToby Isaac     next = next->next;
7131c58f1c22SToby Isaac   }
7132c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7133c58f1c22SToby Isaac }
7134c58f1c22SToby Isaac 
7135c58f1c22SToby Isaac /*@C
7136c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7137c58f1c22SToby Isaac 
7138c58f1c22SToby Isaac   Not Collective
7139c58f1c22SToby Isaac 
7140c58f1c22SToby Isaac   Input Parameters:
7141c58f1c22SToby Isaac + dm   - The DM object
7142c58f1c22SToby Isaac - label - The DMLabel
7143c58f1c22SToby Isaac 
7144c58f1c22SToby Isaac   Level: developer
7145c58f1c22SToby Isaac 
7146c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7147c58f1c22SToby Isaac @*/
7148c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7149c58f1c22SToby Isaac {
71505d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7151c58f1c22SToby Isaac   PetscBool      hasLabel;
7152d67d17b1SMatthew G. Knepley   const char    *lname;
71535d80c0bfSVaclav Hapla   PetscBool      flg;
7154c58f1c22SToby Isaac   PetscErrorCode ierr;
7155c58f1c22SToby Isaac 
7156c58f1c22SToby Isaac   PetscFunctionBegin;
7157c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7158d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7159d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7160d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7161c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7162c58f1c22SToby Isaac   tmpLabel->label  = label;
7163c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
71645d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
71655d80c0bfSVaclav Hapla   *p = tmpLabel;
716608f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
71675d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
71685d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7169ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7170ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7171c58f1c22SToby Isaac   PetscFunctionReturn(0);
7172c58f1c22SToby Isaac }
7173c58f1c22SToby Isaac 
7174c58f1c22SToby Isaac /*@C
7175e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7176c58f1c22SToby Isaac 
7177c58f1c22SToby Isaac   Not Collective
7178c58f1c22SToby Isaac 
7179c58f1c22SToby Isaac   Input Parameters:
7180c58f1c22SToby Isaac + dm   - The DM object
7181c58f1c22SToby Isaac - name - The label name
7182c58f1c22SToby Isaac 
7183c58f1c22SToby Isaac   Output Parameter:
7184c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7185c58f1c22SToby Isaac 
7186c58f1c22SToby Isaac   Level: developer
7187c58f1c22SToby Isaac 
7188e5472504SVaclav Hapla   Notes:
7189e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7190e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7191e5472504SVaclav Hapla 
7192e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7193e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7194e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7195e5472504SVaclav Hapla 
7196e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7197c58f1c22SToby Isaac @*/
7198c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7199c58f1c22SToby Isaac {
720095d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7201c58f1c22SToby Isaac   PetscBool      hasLabel;
7202d67d17b1SMatthew G. Knepley   const char    *lname;
7203c58f1c22SToby Isaac   PetscErrorCode ierr;
7204c58f1c22SToby Isaac 
7205c58f1c22SToby Isaac   PetscFunctionBegin;
7206c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7207e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7208e5472504SVaclav Hapla   if (label) {
7209e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7210c58f1c22SToby Isaac     *label = NULL;
7211e5472504SVaclav Hapla   }
72125d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
721395d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7214d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7215c58f1c22SToby Isaac     if (hasLabel) {
721695d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7217c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
721895d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7219ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7220ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
722195d578d6SVaclav Hapla       if (label) *label = link->label;
722295d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
722395d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7224c58f1c22SToby Isaac       break;
7225c58f1c22SToby Isaac     }
7226c58f1c22SToby Isaac   }
7227c58f1c22SToby Isaac   PetscFunctionReturn(0);
7228c58f1c22SToby Isaac }
7229c58f1c22SToby Isaac 
7230306894acSVaclav Hapla /*@
7231306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7232306894acSVaclav Hapla 
7233306894acSVaclav Hapla   Not Collective
7234306894acSVaclav Hapla 
7235306894acSVaclav Hapla   Input Parameters:
7236306894acSVaclav Hapla + dm   - The DM object
7237306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7238306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7239306894acSVaclav Hapla 
7240306894acSVaclav Hapla   Level: developer
7241306894acSVaclav Hapla 
7242306894acSVaclav Hapla   Notes:
7243306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7244306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7245306894acSVaclav Hapla   *label nullified.
7246306894acSVaclav Hapla 
7247306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7248306894acSVaclav Hapla @*/
7249306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7250306894acSVaclav Hapla {
725143e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7252306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7253306894acSVaclav Hapla   PetscErrorCode ierr;
7254306894acSVaclav Hapla 
7255306894acSVaclav Hapla   PetscFunctionBegin;
7256306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7257306894acSVaclav Hapla   PetscValidPointer(label, 2);
7258f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7259306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7260306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
72615d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
726243e45a93SVaclav Hapla     if (*label == link->label) {
7263306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
726443e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7265306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7266ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
726743e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
726843e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
726943e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7270306894acSVaclav Hapla       break;
7271306894acSVaclav Hapla     }
7272306894acSVaclav Hapla   }
7273306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7274306894acSVaclav Hapla   PetscFunctionReturn(0);
7275306894acSVaclav Hapla }
7276306894acSVaclav Hapla 
7277c58f1c22SToby Isaac /*@C
7278c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7279c58f1c22SToby Isaac 
7280c58f1c22SToby Isaac   Not Collective
7281c58f1c22SToby Isaac 
7282c58f1c22SToby Isaac   Input Parameters:
7283c58f1c22SToby Isaac + dm   - The DM object
7284c58f1c22SToby Isaac - name - The label name
7285c58f1c22SToby Isaac 
7286c58f1c22SToby Isaac   Output Parameter:
7287c58f1c22SToby Isaac . output - The flag for output
7288c58f1c22SToby Isaac 
7289c58f1c22SToby Isaac   Level: developer
7290c58f1c22SToby Isaac 
7291c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7292c58f1c22SToby Isaac @*/
7293c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7294c58f1c22SToby Isaac {
72955d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7296d67d17b1SMatthew G. Knepley   const char    *lname;
7297c58f1c22SToby Isaac   PetscErrorCode ierr;
7298c58f1c22SToby Isaac 
7299c58f1c22SToby Isaac   PetscFunctionBegin;
7300c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7301c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7302c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7303c58f1c22SToby Isaac   while (next) {
7304c58f1c22SToby Isaac     PetscBool flg;
7305c58f1c22SToby Isaac 
7306d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7307d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7308c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7309c58f1c22SToby Isaac     next = next->next;
7310c58f1c22SToby Isaac   }
7311c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7312c58f1c22SToby Isaac }
7313c58f1c22SToby Isaac 
7314c58f1c22SToby Isaac /*@C
7315c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7316c58f1c22SToby Isaac 
7317c58f1c22SToby Isaac   Not Collective
7318c58f1c22SToby Isaac 
7319c58f1c22SToby Isaac   Input Parameters:
7320c58f1c22SToby Isaac + dm     - The DM object
7321c58f1c22SToby Isaac . name   - The label name
7322c58f1c22SToby Isaac - output - The flag for output
7323c58f1c22SToby Isaac 
7324c58f1c22SToby Isaac   Level: developer
7325c58f1c22SToby Isaac 
7326c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7327c58f1c22SToby Isaac @*/
7328c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7329c58f1c22SToby Isaac {
73305d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7331d67d17b1SMatthew G. Knepley   const char    *lname;
7332c58f1c22SToby Isaac   PetscErrorCode ierr;
7333c58f1c22SToby Isaac 
7334c58f1c22SToby Isaac   PetscFunctionBegin;
7335c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7336534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7337c58f1c22SToby Isaac   while (next) {
7338c58f1c22SToby Isaac     PetscBool flg;
7339c58f1c22SToby Isaac 
7340d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7341d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7342c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7343c58f1c22SToby Isaac     next = next->next;
7344c58f1c22SToby Isaac   }
7345c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7346c58f1c22SToby Isaac }
7347c58f1c22SToby Isaac 
7348c58f1c22SToby Isaac /*@
7349c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7350c58f1c22SToby Isaac 
7351d083f849SBarry Smith   Collective on dmA
7352c58f1c22SToby Isaac 
7353c58f1c22SToby Isaac   Input Parameter:
73545d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
73552e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
73565d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7357ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7358c58f1c22SToby Isaac 
7359c58f1c22SToby Isaac   Level: intermediate
7360c58f1c22SToby Isaac 
7361c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7362c58f1c22SToby Isaac 
73635d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7364c58f1c22SToby Isaac @*/
73655d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7366c58f1c22SToby Isaac {
7367c58f1c22SToby Isaac   DMLabel        label, labelNew;
7368c58f1c22SToby Isaac   const char    *name;
7369c58f1c22SToby Isaac   PetscBool      flg;
73705d80c0bfSVaclav Hapla   DMLabelLink    link;
73715d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7372c58f1c22SToby Isaac 
73735d80c0bfSVaclav Hapla   PetscFunctionBegin;
73745d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
73755d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
73765d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
73775d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
73785d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
73795d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
73805d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
73815d80c0bfSVaclav Hapla     label=link->label;
73825d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
73835d80c0bfSVaclav Hapla     if (!all) {
7384c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7385c58f1c22SToby Isaac       if (flg) continue;
73867d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
73877d5acc75SStefano Zampini       if (flg) continue;
7388ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7389ba2698f1SMatthew G. Knepley       if (flg) continue;
73905d80c0bfSVaclav Hapla     } else {
73915d80c0bfSVaclav Hapla       dmB->depthLabel    = dmA->depthLabel;
7392ba2698f1SMatthew G. Knepley       dmB->celltypeLabel = dmA->celltypeLabel;
73935d80c0bfSVaclav Hapla     }
73945d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7395c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
73965d80c0bfSVaclav Hapla     } else {
73975d80c0bfSVaclav Hapla       labelNew = label;
73985d80c0bfSVaclav Hapla     }
7399c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
74005d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7401c58f1c22SToby Isaac   }
7402c58f1c22SToby Isaac   PetscFunctionReturn(0);
7403c58f1c22SToby Isaac }
7404a8fb8f29SToby Isaac 
7405a8fb8f29SToby Isaac /*@
7406a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7407a8fb8f29SToby Isaac 
7408a8fb8f29SToby Isaac   Input Parameter:
7409a8fb8f29SToby Isaac . dm - The DM object
7410a8fb8f29SToby Isaac 
7411a8fb8f29SToby Isaac   Output Parameter:
7412a8fb8f29SToby Isaac . cdm - The coarse DM
7413a8fb8f29SToby Isaac 
7414a8fb8f29SToby Isaac   Level: intermediate
7415a8fb8f29SToby Isaac 
7416a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7417a8fb8f29SToby Isaac @*/
7418a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7419a8fb8f29SToby Isaac {
7420a8fb8f29SToby Isaac   PetscFunctionBegin;
7421a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7422a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7423a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7424a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7425a8fb8f29SToby Isaac }
7426a8fb8f29SToby Isaac 
7427a8fb8f29SToby Isaac /*@
7428a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7429a8fb8f29SToby Isaac 
7430a8fb8f29SToby Isaac   Input Parameters:
7431a8fb8f29SToby Isaac + dm - The DM object
7432a8fb8f29SToby Isaac - cdm - The coarse DM
7433a8fb8f29SToby Isaac 
7434a8fb8f29SToby Isaac   Level: intermediate
7435a8fb8f29SToby Isaac 
7436a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7437a8fb8f29SToby Isaac @*/
7438a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7439a8fb8f29SToby Isaac {
7440a8fb8f29SToby Isaac   PetscErrorCode ierr;
7441a8fb8f29SToby Isaac 
7442a8fb8f29SToby Isaac   PetscFunctionBegin;
7443a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7444a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7445a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7446a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7447a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7448a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7449a8fb8f29SToby Isaac }
7450a8fb8f29SToby Isaac 
745188bdff64SToby Isaac /*@
745288bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
745388bdff64SToby Isaac 
745488bdff64SToby Isaac   Input Parameter:
745588bdff64SToby Isaac . dm - The DM object
745688bdff64SToby Isaac 
745788bdff64SToby Isaac   Output Parameter:
745888bdff64SToby Isaac . fdm - The fine DM
745988bdff64SToby Isaac 
746088bdff64SToby Isaac   Level: intermediate
746188bdff64SToby Isaac 
746288bdff64SToby Isaac .seealso: DMSetFineDM()
746388bdff64SToby Isaac @*/
746488bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
746588bdff64SToby Isaac {
746688bdff64SToby Isaac   PetscFunctionBegin;
746788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
746888bdff64SToby Isaac   PetscValidPointer(fdm, 2);
746988bdff64SToby Isaac   *fdm = dm->fineMesh;
747088bdff64SToby Isaac   PetscFunctionReturn(0);
747188bdff64SToby Isaac }
747288bdff64SToby Isaac 
747388bdff64SToby Isaac /*@
747488bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
747588bdff64SToby Isaac 
747688bdff64SToby Isaac   Input Parameters:
747788bdff64SToby Isaac + dm - The DM object
747888bdff64SToby Isaac - fdm - The fine DM
747988bdff64SToby Isaac 
748088bdff64SToby Isaac   Level: intermediate
748188bdff64SToby Isaac 
748288bdff64SToby Isaac .seealso: DMGetFineDM()
748388bdff64SToby Isaac @*/
748488bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
748588bdff64SToby Isaac {
748688bdff64SToby Isaac   PetscErrorCode ierr;
748788bdff64SToby Isaac 
748888bdff64SToby Isaac   PetscFunctionBegin;
748988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
749088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
749188bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
749288bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
749388bdff64SToby Isaac   dm->fineMesh = fdm;
749488bdff64SToby Isaac   PetscFunctionReturn(0);
749588bdff64SToby Isaac }
749688bdff64SToby Isaac 
7497a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7498a6ba4734SToby Isaac 
7499a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7500a6ba4734SToby Isaac {
7501e5e52638SMatthew G. Knepley   PetscInt       d;
7502a6ba4734SToby Isaac   PetscErrorCode ierr;
7503a6ba4734SToby Isaac 
7504a6ba4734SToby Isaac   PetscFunctionBegin;
7505e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7506e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7507e5e52638SMatthew G. Knepley   }
7508a6ba4734SToby Isaac   PetscFunctionReturn(0);
7509a6ba4734SToby Isaac }
7510a6ba4734SToby Isaac 
7511a6ba4734SToby Isaac /*@C
7512a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7513a6ba4734SToby Isaac 
7514a6ba4734SToby Isaac   Input Parameters:
75154c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7516f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7517a6ba4734SToby Isaac . name        - The BC name
7518a6ba4734SToby Isaac . labelname   - The label defining constrained points
7519a6ba4734SToby Isaac . field       - The field to constrain
7520e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7521a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7522a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7523a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7524a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7525a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7526a6ba4734SToby Isaac 
7527a6ba4734SToby Isaac   Options Database Keys:
7528a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7529a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7530a6ba4734SToby Isaac 
7531a6ba4734SToby Isaac   Level: developer
7532a6ba4734SToby Isaac 
7533a6ba4734SToby Isaac .seealso: DMGetBoundary()
7534a6ba4734SToby Isaac @*/
7535a30ec4eaSSatish 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)
7536a6ba4734SToby Isaac {
7537e5e52638SMatthew G. Knepley   PetscDS        ds;
7538a6ba4734SToby Isaac   PetscErrorCode ierr;
7539a6ba4734SToby Isaac 
7540a6ba4734SToby Isaac   PetscFunctionBegin;
7541a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7542e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7543e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7544a6ba4734SToby Isaac   PetscFunctionReturn(0);
7545a6ba4734SToby Isaac }
7546a6ba4734SToby Isaac 
7547a6ba4734SToby Isaac /*@
7548a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7549a6ba4734SToby Isaac 
7550a6ba4734SToby Isaac   Input Parameters:
7551a6ba4734SToby Isaac . dm - The mesh object
7552a6ba4734SToby Isaac 
7553a6ba4734SToby Isaac   Output Parameters:
7554a6ba4734SToby Isaac . numBd - The number of BC
7555a6ba4734SToby Isaac 
7556a6ba4734SToby Isaac   Level: intermediate
7557a6ba4734SToby Isaac 
7558a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7559a6ba4734SToby Isaac @*/
7560a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7561a6ba4734SToby Isaac {
7562e5e52638SMatthew G. Knepley   PetscDS        ds;
756358ebd649SToby Isaac   PetscErrorCode ierr;
7564a6ba4734SToby Isaac 
7565a6ba4734SToby Isaac   PetscFunctionBegin;
7566a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7567e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7568e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7569a6ba4734SToby Isaac   PetscFunctionReturn(0);
7570a6ba4734SToby Isaac }
7571a6ba4734SToby Isaac 
7572a6ba4734SToby Isaac /*@C
75731c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7574a6ba4734SToby Isaac 
7575a6ba4734SToby Isaac   Input Parameters:
7576a6ba4734SToby Isaac + dm          - The mesh object
7577a6ba4734SToby Isaac - bd          - The BC number
7578a6ba4734SToby Isaac 
7579a6ba4734SToby Isaac   Output Parameters:
7580f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7581a6ba4734SToby Isaac . name        - The BC name
7582a6ba4734SToby Isaac . labelname   - The label defining constrained points
7583a6ba4734SToby Isaac . field       - The field to constrain
7584a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7585a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7586a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7587a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7588a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7589a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7590a6ba4734SToby Isaac 
7591a6ba4734SToby Isaac   Options Database Keys:
7592a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7593a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7594a6ba4734SToby Isaac 
7595a6ba4734SToby Isaac   Level: developer
7596a6ba4734SToby Isaac 
7597a6ba4734SToby Isaac .seealso: DMAddBoundary()
7598a6ba4734SToby Isaac @*/
7599a30ec4eaSSatish 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)
7600a6ba4734SToby Isaac {
7601e5e52638SMatthew G. Knepley   PetscDS        ds;
760258ebd649SToby Isaac   PetscErrorCode ierr;
7603a6ba4734SToby Isaac 
7604a6ba4734SToby Isaac   PetscFunctionBegin;
7605a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7606e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7607e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7608a6ba4734SToby Isaac   PetscFunctionReturn(0);
7609a6ba4734SToby Isaac }
7610a6ba4734SToby Isaac 
7611e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7612e6f8dbb6SToby Isaac {
7613e5e52638SMatthew G. Knepley   PetscDS        ds;
7614dff059c6SToby Isaac   DMBoundary    *lastnext;
7615e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7616e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7617e6f8dbb6SToby Isaac 
7618e6f8dbb6SToby Isaac   PetscFunctionBegin;
7619e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7620e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
762147a1f5adSToby Isaac   if (dm->boundary) {
762247a1f5adSToby Isaac     DMBoundary next = dm->boundary;
762347a1f5adSToby Isaac 
762447a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
762547a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
762647a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
762747a1f5adSToby Isaac     while (next) {
762847a1f5adSToby Isaac       DMBoundary b = next;
762947a1f5adSToby Isaac 
763047a1f5adSToby Isaac       next = b->next;
763147a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7632a6ba4734SToby Isaac     }
763347a1f5adSToby Isaac     dm->boundary = NULL;
7634a6ba4734SToby Isaac   }
763547a1f5adSToby Isaac 
7636dff059c6SToby Isaac   lastnext = &(dm->boundary);
7637e6f8dbb6SToby Isaac   while (dsbound) {
7638e6f8dbb6SToby Isaac     DMBoundary dmbound;
7639e6f8dbb6SToby Isaac 
7640e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7641e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7642e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7643994fe344SLisandro 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);}
764447a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7645dff059c6SToby Isaac     *lastnext = dmbound;
7646dff059c6SToby Isaac     lastnext = &(dmbound->next);
7647dff059c6SToby Isaac     dsbound = dsbound->next;
7648a6ba4734SToby Isaac   }
7649a6ba4734SToby Isaac   PetscFunctionReturn(0);
7650a6ba4734SToby Isaac }
7651a6ba4734SToby Isaac 
7652a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7653a6ba4734SToby Isaac {
7654b95f2879SToby Isaac   DMBoundary     b;
7655a6ba4734SToby Isaac   PetscErrorCode ierr;
7656a6ba4734SToby Isaac 
7657a6ba4734SToby Isaac   PetscFunctionBegin;
7658a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7659534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7660a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7661e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7662b95f2879SToby Isaac   b = dm->boundary;
7663a6ba4734SToby Isaac   while (b && !(*isBd)) {
7664e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7665e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
76663424c85cSToby Isaac 
76673424c85cSToby Isaac     if (label) {
7668a6ba4734SToby Isaac       PetscInt i;
7669a6ba4734SToby Isaac 
7670e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7671e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7672a6ba4734SToby Isaac       }
7673a6ba4734SToby Isaac     }
7674a6ba4734SToby Isaac     b = b->next;
7675a6ba4734SToby Isaac   }
7676a6ba4734SToby Isaac   PetscFunctionReturn(0);
7677a6ba4734SToby Isaac }
76784d6f44ffSToby Isaac 
76794d6f44ffSToby Isaac /*@C
7680a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
7681a6e0b375SMatthew G. Knepley 
7682a6e0b375SMatthew G. Knepley   Collective on DM
76834d6f44ffSToby Isaac 
76844d6f44ffSToby Isaac   Input Parameters:
76854d6f44ffSToby Isaac + dm      - The DM
76860709b2feSToby Isaac . time    - The time
76874d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
76884d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
76894d6f44ffSToby Isaac - mode    - The insertion mode for values
76904d6f44ffSToby Isaac 
76914d6f44ffSToby Isaac   Output Parameter:
76924d6f44ffSToby Isaac . X - vector
76934d6f44ffSToby Isaac 
76944d6f44ffSToby Isaac    Calling sequence of func:
76950709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
76964d6f44ffSToby Isaac 
76974d6f44ffSToby Isaac +  dim - The spatial dimension
76984d6f44ffSToby Isaac .  x   - The coordinates
76994d6f44ffSToby Isaac .  Nf  - The number of fields
77004d6f44ffSToby Isaac .  u   - The output field values
77014d6f44ffSToby Isaac -  ctx - optional user-defined function context
77024d6f44ffSToby Isaac 
77034d6f44ffSToby Isaac   Level: developer
77044d6f44ffSToby Isaac 
7705a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
77064d6f44ffSToby Isaac @*/
77070709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
77084d6f44ffSToby Isaac {
77094d6f44ffSToby Isaac   Vec            localX;
77104d6f44ffSToby Isaac   PetscErrorCode ierr;
77114d6f44ffSToby Isaac 
77124d6f44ffSToby Isaac   PetscFunctionBegin;
77134d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77144d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
77150709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77164d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
77174d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
77184d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
77194d6f44ffSToby Isaac   PetscFunctionReturn(0);
77204d6f44ffSToby Isaac }
77214d6f44ffSToby Isaac 
7722a6e0b375SMatthew G. Knepley /*@C
7723a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
7724a6e0b375SMatthew G. Knepley 
7725a6e0b375SMatthew G. Knepley   Not collective
7726a6e0b375SMatthew G. Knepley 
7727a6e0b375SMatthew G. Knepley   Input Parameters:
7728a6e0b375SMatthew G. Knepley + dm      - The DM
7729a6e0b375SMatthew G. Knepley . time    - The time
7730a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7731a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7732a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7733a6e0b375SMatthew G. Knepley 
7734a6e0b375SMatthew G. Knepley   Output Parameter:
7735a6e0b375SMatthew G. Knepley . localX - vector
7736a6e0b375SMatthew G. Knepley 
7737a6e0b375SMatthew G. Knepley    Calling sequence of func:
7738a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7739a6e0b375SMatthew G. Knepley 
7740a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7741a6e0b375SMatthew G. Knepley .  x   - The coordinates
7742a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7743a6e0b375SMatthew G. Knepley .  u   - The output field values
7744a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7745a6e0b375SMatthew G. Knepley 
7746a6e0b375SMatthew G. Knepley   Level: developer
7747a6e0b375SMatthew G. Knepley 
7748a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
7749a6e0b375SMatthew G. Knepley @*/
77500709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
77514d6f44ffSToby Isaac {
77524d6f44ffSToby Isaac   PetscErrorCode ierr;
77534d6f44ffSToby Isaac 
77544d6f44ffSToby Isaac   PetscFunctionBegin;
77554d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77564d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
77570918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
77580709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77594d6f44ffSToby Isaac   PetscFunctionReturn(0);
77604d6f44ffSToby Isaac }
77614d6f44ffSToby Isaac 
7762a6e0b375SMatthew G. Knepley /*@C
7763a6e0b375SMatthew 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.
7764a6e0b375SMatthew G. Knepley 
7765a6e0b375SMatthew G. Knepley   Collective on DM
7766a6e0b375SMatthew G. Knepley 
7767a6e0b375SMatthew G. Knepley   Input Parameters:
7768a6e0b375SMatthew G. Knepley + dm      - The DM
7769a6e0b375SMatthew G. Knepley . time    - The time
7770a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7771a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7772a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7773a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7774a6e0b375SMatthew G. Knepley 
7775a6e0b375SMatthew G. Knepley   Output Parameter:
7776a6e0b375SMatthew G. Knepley . X - vector
7777a6e0b375SMatthew G. Knepley 
7778a6e0b375SMatthew G. Knepley    Calling sequence of func:
7779a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7780a6e0b375SMatthew G. Knepley 
7781a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7782a6e0b375SMatthew G. Knepley .  x   - The coordinates
7783a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7784a6e0b375SMatthew G. Knepley .  u   - The output field values
7785a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7786a6e0b375SMatthew G. Knepley 
7787a6e0b375SMatthew G. Knepley   Level: developer
7788a6e0b375SMatthew G. Knepley 
7789a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
7790a6e0b375SMatthew G. Knepley @*/
77912c53366bSMatthew 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)
77922c53366bSMatthew G. Knepley {
77932c53366bSMatthew G. Knepley   Vec            localX;
77942c53366bSMatthew G. Knepley   PetscErrorCode ierr;
77952c53366bSMatthew G. Knepley 
77962c53366bSMatthew G. Knepley   PetscFunctionBegin;
77972c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77982c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
77992c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
78002c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
78012c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
78022c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
78032c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
78042c53366bSMatthew G. Knepley }
78052c53366bSMatthew G. Knepley 
7806a6e0b375SMatthew G. Knepley /*@C
7807a6e0b375SMatthew 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.
7808a6e0b375SMatthew G. Knepley 
7809a6e0b375SMatthew G. Knepley   Not collective
7810a6e0b375SMatthew G. Knepley 
7811a6e0b375SMatthew G. Knepley   Input Parameters:
7812a6e0b375SMatthew G. Knepley + dm      - The DM
7813a6e0b375SMatthew G. Knepley . time    - The time
7814a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7815a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7816a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7817a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7818a6e0b375SMatthew G. Knepley 
7819a6e0b375SMatthew G. Knepley   Output Parameter:
7820a6e0b375SMatthew G. Knepley . localX - vector
7821a6e0b375SMatthew G. Knepley 
7822a6e0b375SMatthew G. Knepley    Calling sequence of func:
7823a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7824a6e0b375SMatthew G. Knepley 
7825a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7826a6e0b375SMatthew G. Knepley .  x   - The coordinates
7827a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7828a6e0b375SMatthew G. Knepley .  u   - The output field values
7829a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7830a6e0b375SMatthew G. Knepley 
7831a6e0b375SMatthew G. Knepley   Level: developer
7832a6e0b375SMatthew G. Knepley 
7833a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
7834a6e0b375SMatthew G. Knepley @*/
78351c531cf8SMatthew 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)
78364d6f44ffSToby Isaac {
78374d6f44ffSToby Isaac   PetscErrorCode ierr;
78384d6f44ffSToby Isaac 
78394d6f44ffSToby Isaac   PetscFunctionBegin;
78404d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78414d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
78420918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
78431c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
78444d6f44ffSToby Isaac   PetscFunctionReturn(0);
78454d6f44ffSToby Isaac }
78462716604bSToby Isaac 
7847a6e0b375SMatthew G. Knepley /*@C
7848a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
7849a6e0b375SMatthew G. Knepley 
7850a6e0b375SMatthew G. Knepley   Not collective
7851a6e0b375SMatthew G. Knepley 
7852a6e0b375SMatthew G. Knepley   Input Parameters:
7853a6e0b375SMatthew G. Knepley + dm      - The DM
7854a6e0b375SMatthew G. Knepley . time    - The time
7855a6e0b375SMatthew G. Knepley . localU  - The input field vector
7856a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7857a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7858a6e0b375SMatthew G. Knepley 
7859a6e0b375SMatthew G. Knepley   Output Parameter:
7860a6e0b375SMatthew G. Knepley . localX  - The output vector
7861a6e0b375SMatthew G. Knepley 
7862a6e0b375SMatthew G. Knepley    Calling sequence of func:
7863a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7864a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7865a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7866a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7867a6e0b375SMatthew G. Knepley 
7868a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7869a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7870a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7871a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7872a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7873a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7874a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7875a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7876a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7877a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7878a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7879a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7880a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7881a6e0b375SMatthew G. Knepley .  t            - The current time
7882a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7883a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7884a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7885a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7886a6e0b375SMatthew G. Knepley 
7887a6e0b375SMatthew 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.
7888a6e0b375SMatthew 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
7889a6e0b375SMatthew 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
7890a6e0b375SMatthew 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.
7891a6e0b375SMatthew G. Knepley 
7892a6e0b375SMatthew G. Knepley   Level: intermediate
7893a6e0b375SMatthew G. Knepley 
7894a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
7895a6e0b375SMatthew G. Knepley @*/
78968c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
78978c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
78988c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
78998c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7900191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
79018c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
79028c6c5593SMatthew G. Knepley {
79038c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
79048c6c5593SMatthew G. Knepley 
79058c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79068c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79078c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
79088c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
79090918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
79108c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
79118c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
79128c6c5593SMatthew G. Knepley }
79138c6c5593SMatthew G. Knepley 
7914a6e0b375SMatthew G. Knepley /*@C
7915a6e0b375SMatthew 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.
7916a6e0b375SMatthew G. Knepley 
7917a6e0b375SMatthew G. Knepley   Not collective
7918a6e0b375SMatthew G. Knepley 
7919a6e0b375SMatthew G. Knepley   Input Parameters:
7920a6e0b375SMatthew G. Knepley + dm      - The DM
7921a6e0b375SMatthew G. Knepley . time    - The time
7922a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
7923a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
7924a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
7925a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
7926a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
7927a6e0b375SMatthew G. Knepley . localU  - The input field vector
7928a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
7929a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7930a6e0b375SMatthew G. Knepley 
7931a6e0b375SMatthew G. Knepley   Output Parameter:
7932a6e0b375SMatthew G. Knepley . localX  - The output vector
7933a6e0b375SMatthew G. Knepley 
7934a6e0b375SMatthew G. Knepley    Calling sequence of func:
7935a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7936a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7937a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7938a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
7939a6e0b375SMatthew G. Knepley 
7940a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
7941a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
7942a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
7943a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
7944a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
7945a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
7946a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
7947a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
7948a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
7949a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
7950a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
7951a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
7952a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
7953a6e0b375SMatthew G. Knepley .  t            - The current time
7954a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
7955a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
7956a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
7957a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
7958a6e0b375SMatthew G. Knepley 
7959a6e0b375SMatthew 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.
7960a6e0b375SMatthew 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
7961a6e0b375SMatthew 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
7962a6e0b375SMatthew 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.
7963a6e0b375SMatthew G. Knepley 
7964a6e0b375SMatthew G. Knepley   Level: intermediate
7965a6e0b375SMatthew G. Knepley 
7966a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
7967a6e0b375SMatthew G. Knepley @*/
79681c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
79698c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
79708c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79718c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7972191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
79738c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
79748c6c5593SMatthew G. Knepley {
79758c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
79768c6c5593SMatthew G. Knepley 
79778c6c5593SMatthew G. Knepley   PetscFunctionBegin;
79788c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79798c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
79808c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
79810918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
79821c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
79838c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
79848c6c5593SMatthew G. Knepley }
79858c6c5593SMatthew G. Knepley 
79862716604bSToby Isaac /*@C
79872716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
79882716604bSToby Isaac 
79892716604bSToby Isaac   Input Parameters:
79902716604bSToby Isaac + dm    - The DM
79910709b2feSToby Isaac . time  - The time
79922716604bSToby Isaac . funcs - The functions to evaluate for each field component
79932716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7994574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
79952716604bSToby Isaac 
79962716604bSToby Isaac   Output Parameter:
79972716604bSToby Isaac . diff - The diff ||u - u_h||_2
79982716604bSToby Isaac 
79992716604bSToby Isaac   Level: developer
80002716604bSToby Isaac 
80011189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
80022716604bSToby Isaac @*/
80030709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
80042716604bSToby Isaac {
80052716604bSToby Isaac   PetscErrorCode ierr;
80062716604bSToby Isaac 
80072716604bSToby Isaac   PetscFunctionBegin;
80082716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8009b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80100918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
80110709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
80122716604bSToby Isaac   PetscFunctionReturn(0);
80132716604bSToby Isaac }
8014b698f381SToby Isaac 
8015b698f381SToby Isaac /*@C
8016b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8017b698f381SToby Isaac 
8018d083f849SBarry Smith   Collective on dm
8019d083f849SBarry Smith 
8020b698f381SToby Isaac   Input Parameters:
8021b698f381SToby Isaac + dm    - The DM
8022b698f381SToby Isaac , time  - The time
8023b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8024b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8025574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8026b698f381SToby Isaac - n     - The vector to project along
8027b698f381SToby Isaac 
8028b698f381SToby Isaac   Output Parameter:
8029b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8030b698f381SToby Isaac 
8031b698f381SToby Isaac   Level: developer
8032b698f381SToby Isaac 
8033b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8034b698f381SToby Isaac @*/
8035b698f381SToby 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)
8036b698f381SToby Isaac {
8037b698f381SToby Isaac   PetscErrorCode ierr;
8038b698f381SToby Isaac 
8039b698f381SToby Isaac   PetscFunctionBegin;
8040b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8041b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8042b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8043b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8044b698f381SToby Isaac   PetscFunctionReturn(0);
8045b698f381SToby Isaac }
8046b698f381SToby Isaac 
80472a16baeaSToby Isaac /*@C
80482a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
80492a16baeaSToby Isaac 
8050d083f849SBarry Smith   Collective on dm
8051d083f849SBarry Smith 
80522a16baeaSToby Isaac   Input Parameters:
80532a16baeaSToby Isaac + dm    - The DM
80542a16baeaSToby Isaac . time  - The time
80552a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
80562a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8057574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
80582a16baeaSToby Isaac 
80592a16baeaSToby Isaac   Output Parameter:
80602a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
80612a16baeaSToby Isaac 
80622a16baeaSToby Isaac   Level: developer
80632a16baeaSToby Isaac 
80641189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
80652a16baeaSToby Isaac @*/
80661189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
80672a16baeaSToby Isaac {
80682a16baeaSToby Isaac   PetscErrorCode ierr;
80692a16baeaSToby Isaac 
80702a16baeaSToby Isaac   PetscFunctionBegin;
80712a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80722a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
80730918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
80742a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
80752a16baeaSToby Isaac   PetscFunctionReturn(0);
80762a16baeaSToby Isaac }
80772a16baeaSToby Isaac 
8078df0b854cSToby Isaac /*@C
8079df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8080cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8081df0b854cSToby Isaac 
8082df0b854cSToby Isaac   Collective on dm
8083df0b854cSToby Isaac 
8084df0b854cSToby Isaac   Input parameters:
8085df0b854cSToby Isaac + dm - the pre-adaptation DM object
8086a1b0c543SToby Isaac - label - label with the flags
8087df0b854cSToby Isaac 
8088df0b854cSToby Isaac   Output parameters:
80890d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8090df0b854cSToby Isaac 
8091df0b854cSToby Isaac   Level: intermediate
80920d1cd5e0SMatthew G. Knepley 
80930d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8094df0b854cSToby Isaac @*/
80950d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8096df0b854cSToby Isaac {
8097df0b854cSToby Isaac   PetscErrorCode ierr;
8098df0b854cSToby Isaac 
8099df0b854cSToby Isaac   PetscFunctionBegin;
8100df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8101a1b0c543SToby Isaac   PetscValidPointer(label,2);
81020d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
81030d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
81046f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
81050d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
81060d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
81070d1cd5e0SMatthew G. Knepley }
81080d1cd5e0SMatthew G. Knepley 
81090d1cd5e0SMatthew G. Knepley /*@C
81100d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
81110d1cd5e0SMatthew G. Knepley 
81120d1cd5e0SMatthew G. Knepley   Input Parameters:
81130d1cd5e0SMatthew G. Knepley + dm - The DM object
81140d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
81156f25b0d8SLisandro 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_".
81160d1cd5e0SMatthew G. Knepley 
81170d1cd5e0SMatthew G. Knepley   Output Parameter:
81180d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
81190d1cd5e0SMatthew G. Knepley 
81200d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
81210d1cd5e0SMatthew G. Knepley 
81220d1cd5e0SMatthew G. Knepley   Level: advanced
81230d1cd5e0SMatthew G. Knepley 
81240d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
81250d1cd5e0SMatthew G. Knepley @*/
81260d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
81270d1cd5e0SMatthew G. Knepley {
81280d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
81290d1cd5e0SMatthew G. Knepley 
81300d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
81310d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81320d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
81330d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
81340d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
81356f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
81366f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
81370d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8138df0b854cSToby Isaac   PetscFunctionReturn(0);
8139df0b854cSToby Isaac }
8140c4088d22SMatthew G. Knepley 
8141502a2867SDave May /*@C
8142502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8143502a2867SDave May 
8144502a2867SDave May  Not Collective
8145502a2867SDave May 
8146502a2867SDave May  Input Parameter:
8147502a2867SDave May  . dm    - The DM
8148502a2867SDave May 
8149502a2867SDave May  Output Parameter:
8150502a2867SDave May  . nranks - the number of neighbours
8151502a2867SDave May  . ranks - the neighbors ranks
8152502a2867SDave May 
8153502a2867SDave May  Notes:
8154502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8155502a2867SDave May 
8156502a2867SDave May  Level: beginner
8157502a2867SDave May 
8158dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8159502a2867SDave May @*/
8160502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8161502a2867SDave May {
8162502a2867SDave May   PetscErrorCode ierr;
8163502a2867SDave May 
8164502a2867SDave May   PetscFunctionBegin;
8165502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81660918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8167502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8168502a2867SDave May   PetscFunctionReturn(0);
8169502a2867SDave May }
8170502a2867SDave May 
8171531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8172531c7667SBarry Smith 
8173531c7667SBarry Smith /*
8174531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8175531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8176531c7667SBarry Smith */
8177531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8178531c7667SBarry Smith {
8179531c7667SBarry Smith   PetscErrorCode ierr;
8180531c7667SBarry Smith 
8181531c7667SBarry Smith   PetscFunctionBegin;
8182531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8183531c7667SBarry Smith     Vec x1local;
8184531c7667SBarry Smith     DM  dm;
8185531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8186531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8187531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8188531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8189531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8190531c7667SBarry Smith     x1   = x1local;
8191531c7667SBarry Smith   }
8192531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8193531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8194531c7667SBarry Smith     DM  dm;
8195531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8196531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8197531c7667SBarry Smith   }
8198531c7667SBarry Smith   PetscFunctionReturn(0);
8199531c7667SBarry Smith }
8200531c7667SBarry Smith 
8201531c7667SBarry Smith /*@
8202531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8203531c7667SBarry Smith 
8204531c7667SBarry Smith     Input Parameter:
8205531c7667SBarry Smith .    coloring - the MatFDColoring object
8206531c7667SBarry Smith 
820795452b02SPatrick Sanan     Developer Notes:
820895452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8209531c7667SBarry Smith 
82101b266c99SBarry Smith     Level: advanced
82111b266c99SBarry Smith 
8212531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8213531c7667SBarry Smith @*/
8214531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8215531c7667SBarry Smith {
8216531c7667SBarry Smith   PetscFunctionBegin;
8217531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8218531c7667SBarry Smith   PetscFunctionReturn(0);
8219531c7667SBarry Smith }
82208320bc6fSPatrick Sanan 
82218320bc6fSPatrick Sanan /*@
82228320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
82238320bc6fSPatrick Sanan 
82248320bc6fSPatrick Sanan     Collective
82258320bc6fSPatrick Sanan 
82268320bc6fSPatrick Sanan     Input Parameters:
82278320bc6fSPatrick Sanan +    dm - the first DM
82288320bc6fSPatrick Sanan -    dm2 - the second DM
82298320bc6fSPatrick Sanan 
82308320bc6fSPatrick Sanan     Output Parameters:
82318320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
82328320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
82338320bc6fSPatrick Sanan 
82348320bc6fSPatrick Sanan     Notes:
82358320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
82363d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
82378320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
82383d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
82393d862458SPatrick Sanan     decomposition, but hold different data.
82408320bc6fSPatrick Sanan 
82418320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
82428320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
82438320bc6fSPatrick Sanan 
82448320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
82458320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
82468320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
82478320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
82488320bc6fSPatrick Sanan 
82498320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
82508320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
82518320bc6fSPatrick Sanan .vb
82528320bc6fSPatrick Sanan   ...
82538320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
82548320bc6fSPatrick Sanan   if (set && compatible)  {
82558320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
82568320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
82573d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
82588320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
82598320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
82608320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
82618320bc6fSPatrick Sanan       }
82628320bc6fSPatrick Sanan     }
82638320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
82648320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
82658320bc6fSPatrick Sanan   } else {
82668320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
82678320bc6fSPatrick Sanan   }
82688320bc6fSPatrick Sanan   ...
82698320bc6fSPatrick Sanan .ve
82708320bc6fSPatrick Sanan 
82718320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
82728320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
82738320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
82748320bc6fSPatrick Sanan     always check the "set" output parameter.
82758320bc6fSPatrick Sanan 
82768320bc6fSPatrick Sanan     A DM is always compatible with itself.
82778320bc6fSPatrick Sanan 
82788320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
82798320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
82808320bc6fSPatrick Sanan     incompatible.
82818320bc6fSPatrick Sanan 
82828320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
82838320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
82848320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
82858320bc6fSPatrick Sanan 
82868320bc6fSPatrick Sanan     Developer Notes:
82873d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
82883d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
82898320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
82908320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
82918320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
82923d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
82933d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
82948320bc6fSPatrick Sanan 
82958320bc6fSPatrick Sanan     Level: advanced
82968320bc6fSPatrick Sanan 
82973d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
82988320bc6fSPatrick Sanan @*/
82998320bc6fSPatrick Sanan 
83008320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
83018320bc6fSPatrick Sanan {
83028320bc6fSPatrick Sanan   PetscErrorCode ierr;
83038320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
83048320bc6fSPatrick Sanan   DMType         type,type2;
83058320bc6fSPatrick Sanan   PetscBool      sameType;
83068320bc6fSPatrick Sanan 
83078320bc6fSPatrick Sanan   PetscFunctionBegin;
83088320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83098320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83108320bc6fSPatrick Sanan 
83118320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
83128320bc6fSPatrick Sanan   if (dm == dm2) {
83138320bc6fSPatrick Sanan     *set = PETSC_TRUE;
83148320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
83158320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83168320bc6fSPatrick Sanan   }
83178320bc6fSPatrick Sanan 
83188320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
83198320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
83208320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
83218320bc6fSPatrick Sanan      determined by the implementation-specific logic */
83228320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
83238320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
83248320bc6fSPatrick Sanan     *set = PETSC_TRUE;
83258320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
83268320bc6fSPatrick Sanan     PetscFunctionReturn(0);
83278320bc6fSPatrick Sanan   }
83288320bc6fSPatrick Sanan 
83298320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
83308320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
83318320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
8332b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
83338320bc6fSPatrick Sanan   }
83348320bc6fSPatrick Sanan 
83358320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
83368320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
83378320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
83388320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
83398320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
83408320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
83418320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
83428320bc6fSPatrick Sanan   } else {
83438320bc6fSPatrick Sanan     *set = PETSC_FALSE;
83448320bc6fSPatrick Sanan   }
83458320bc6fSPatrick Sanan   PetscFunctionReturn(0);
83468320bc6fSPatrick Sanan }
8347c0f0dcc3SMatthew G. Knepley 
8348c0f0dcc3SMatthew G. Knepley /*@C
8349c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8350c0f0dcc3SMatthew G. Knepley 
8351c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8352c0f0dcc3SMatthew G. Knepley 
8353c0f0dcc3SMatthew G. Knepley   Input Parameters:
8354c0f0dcc3SMatthew G. Knepley + DM - the DM
8355c0f0dcc3SMatthew G. Knepley . f - the monitor function
8356c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8357c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8358c0f0dcc3SMatthew G. Knepley 
8359c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8360c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8361c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8362c0f0dcc3SMatthew G. Knepley 
8363c0f0dcc3SMatthew G. Knepley   Notes:
8364c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8365c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8366c0f0dcc3SMatthew G. Knepley   order in which they were set.
8367c0f0dcc3SMatthew G. Knepley 
8368c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8369c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8370c0f0dcc3SMatthew G. Knepley 
8371c0f0dcc3SMatthew G. Knepley   Level: intermediate
8372c0f0dcc3SMatthew G. Knepley 
8373c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8374c0f0dcc3SMatthew G. Knepley @*/
8375c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8376c0f0dcc3SMatthew G. Knepley {
8377c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8378c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8379c0f0dcc3SMatthew G. Knepley 
8380c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8381c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8382c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8383c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8384c0f0dcc3SMatthew G. Knepley 
8385c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8386c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8387c0f0dcc3SMatthew G. Knepley   }
8388c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8389c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8390c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8391c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8392c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8393c0f0dcc3SMatthew G. Knepley }
8394c0f0dcc3SMatthew G. Knepley 
8395c0f0dcc3SMatthew G. Knepley /*@
8396c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8397c0f0dcc3SMatthew G. Knepley 
8398c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8399c0f0dcc3SMatthew G. Knepley 
8400c0f0dcc3SMatthew G. Knepley   Input Parameter:
8401c0f0dcc3SMatthew G. Knepley . dm - the DM
8402c0f0dcc3SMatthew G. Knepley 
8403c0f0dcc3SMatthew G. Knepley   Options Database Key:
8404c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8405c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8406c0f0dcc3SMatthew G. Knepley   set via the options database
8407c0f0dcc3SMatthew G. Knepley 
8408c0f0dcc3SMatthew G. Knepley   Notes:
8409c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8410c0f0dcc3SMatthew G. Knepley 
8411c0f0dcc3SMatthew G. Knepley   Level: intermediate
8412c0f0dcc3SMatthew G. Knepley 
8413c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8414c0f0dcc3SMatthew G. Knepley @*/
8415c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8416c0f0dcc3SMatthew G. Knepley {
8417c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8418c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8419c0f0dcc3SMatthew G. Knepley 
8420c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8421c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8422c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8423c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
8424c0f0dcc3SMatthew G. Knepley   }
8425c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8426c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8427c0f0dcc3SMatthew G. Knepley }
8428c0f0dcc3SMatthew G. Knepley 
8429c0f0dcc3SMatthew G. Knepley /*@C
8430c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8431c0f0dcc3SMatthew G. Knepley 
8432c0f0dcc3SMatthew G. Knepley   Collective on DM
8433c0f0dcc3SMatthew G. Knepley 
8434c0f0dcc3SMatthew G. Knepley   Input Parameters:
8435c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8436c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8437c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8438c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8439c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8440c0f0dcc3SMatthew 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
8441c0f0dcc3SMatthew G. Knepley 
8442c0f0dcc3SMatthew G. Knepley   Output Parameter:
8443c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8444c0f0dcc3SMatthew G. Knepley 
8445c0f0dcc3SMatthew G. Knepley   Level: developer
8446c0f0dcc3SMatthew G. Knepley 
8447c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
8448c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
8449c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
8450c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
8451c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
8452c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
8453c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
8454c0f0dcc3SMatthew G. Knepley @*/
8455c0f0dcc3SMatthew 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)
8456c0f0dcc3SMatthew G. Knepley {
8457c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8458c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8459c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
8460c0f0dcc3SMatthew G. Knepley 
8461c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8462c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8463c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
8464c0f0dcc3SMatthew G. Knepley   if (*flg) {
8465c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8466c0f0dcc3SMatthew G. Knepley 
8467c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
8468c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
8469c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
8470c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
8471c0f0dcc3SMatthew G. Knepley   }
8472c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8473c0f0dcc3SMatthew G. Knepley }
8474c0f0dcc3SMatthew G. Knepley 
8475c0f0dcc3SMatthew G. Knepley /*@
8476c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8477c0f0dcc3SMatthew G. Knepley 
8478c0f0dcc3SMatthew G. Knepley    Collective on DM
8479c0f0dcc3SMatthew G. Knepley 
8480c0f0dcc3SMatthew G. Knepley    Input Parameters:
8481c0f0dcc3SMatthew G. Knepley .  dm - The DM
8482c0f0dcc3SMatthew G. Knepley 
8483c0f0dcc3SMatthew G. Knepley    Level: developer
8484c0f0dcc3SMatthew G. Knepley 
8485c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8486c0f0dcc3SMatthew G. Knepley @*/
8487c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8488c0f0dcc3SMatthew G. Knepley {
8489c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8490c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8491c0f0dcc3SMatthew G. Knepley 
8492c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8493c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8494c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8495c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8496c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
8497c0f0dcc3SMatthew G. Knepley   }
8498c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8499c0f0dcc3SMatthew G. Knepley }
8500