xref: /petsc/src/dm/interface/dm.c (revision 1bb6d2a8d27998275780769967d0e939765fcbef)
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;
115a84ad33SLisandro Dalcin PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix;
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};
15bff4a2f0SMatthew G. Knepley 
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);
53*1bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
54c58f1c22SToby Isaac   v->labels                   = NULL;
5534aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
5634aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
57c58f1c22SToby Isaac   v->depthLabel               = NULL;
58*1bb6d2a8SBarry Smith   v->localSection           = NULL;
59*1bb6d2a8SBarry Smith   v->globalSection     = NULL;
60fba222abSToby Isaac   v->defaultConstraintSection = NULL;
61fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
62c6b900c6SMatthew G. Knepley   v->L                        = NULL;
63c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
645dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
659a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
6696173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
67435a35e8SMatthew G Knepley   {
68435a35e8SMatthew G Knepley     PetscInt i;
69435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
700298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
71f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
72435a35e8SMatthew G Knepley     }
73435a35e8SMatthew G Knepley   }
74e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
75b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
76e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
7714f150ffSMatthew G. Knepley   v->dmBC = NULL;
78a8fb8f29SToby Isaac   v->coarseMesh = NULL;
79f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
80cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
81c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
82b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
83bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
84c58f1c22SToby Isaac   v->labels->refct = 1;
854a7a4c06SLawrence Mitchell 
861411c6eeSJed Brown   *dm = v;
87a4121054SBarry Smith   PetscFunctionReturn(0);
88a4121054SBarry Smith }
89a4121054SBarry Smith 
9038221697SMatthew G. Knepley /*@
9138221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9238221697SMatthew G. Knepley 
93d083f849SBarry Smith   Collective
9438221697SMatthew G. Knepley 
9538221697SMatthew G. Knepley   Input Parameter:
9638221697SMatthew G. Knepley . dm - The original DM object
9738221697SMatthew G. Knepley 
9838221697SMatthew G. Knepley   Output Parameter:
9938221697SMatthew G. Knepley . newdm  - The new DM object
10038221697SMatthew G. Knepley 
10138221697SMatthew G. Knepley   Level: beginner
10238221697SMatthew G. Knepley 
103*1bb6d2a8SBarry Smith   Notes: For some DM this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
104*1bb6d2a8SBarry Smith          DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does
105*1bb6d2a8SBarry Smith          share the PetscSection of the original DM
106*1bb6d2a8SBarry Smith 
107*1bb6d2a8SBarry Smith .seealso: DMDestry(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
108*1bb6d2a8SBarry Smith 
10938221697SMatthew G. Knepley @*/
11038221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11138221697SMatthew G. Knepley {
11238221697SMatthew G. Knepley   PetscSF        sf;
11338221697SMatthew G. Knepley   Vec            coords;
11438221697SMatthew G. Knepley   void          *ctx;
115a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11638221697SMatthew G. Knepley   PetscErrorCode ierr;
11738221697SMatthew G. Knepley 
11838221697SMatthew G. Knepley   PetscFunctionBegin;
11938221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12038221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12138221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
122c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
123c58f1c22SToby Isaac   dm->labels->refct++;
124c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
125c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
126ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
127ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1281de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1291de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13038221697SMatthew G. Knepley   if (dm->ops->clone) {
13138221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13238221697SMatthew G. Knepley   }
1333f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13438221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13638221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13738221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
138be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
139be4c1c3eSMatthew G. Knepley     DM           ncdm;
140be4c1c3eSMatthew G. Knepley     PetscSection cs;
1415a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
142be4c1c3eSMatthew G. Knepley 
14392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
144be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1455a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1465a0206caSToby Isaac     if (pEndMax >= 0) {
147be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
14883bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
14992fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
150a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
151be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
152be4c1c3eSMatthew G. Knepley     }
153be4c1c3eSMatthew G. Knepley   }
154a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
155a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15638221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15738221697SMatthew G. Knepley   if (coords) {
15838221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15938221697SMatthew G. Knepley   } else {
16038221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16138221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16238221697SMatthew G. Knepley   }
16390b157c4SStefano Zampini   {
16490b157c4SStefano Zampini     PetscBool             isper;
165c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1665dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16790b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16890b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
169c6b900c6SMatthew G. Knepley   }
17034aa8a36SMatthew G. Knepley   {
17134aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17234aa8a36SMatthew G. Knepley 
17334aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17534aa8a36SMatthew G. Knepley   }
17638221697SMatthew G. Knepley   PetscFunctionReturn(0);
17738221697SMatthew G. Knepley }
17838221697SMatthew G. Knepley 
1799a42bb27SBarry Smith /*@C
180564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1819a42bb27SBarry Smith 
182d083f849SBarry Smith    Logically Collective on da
1839a42bb27SBarry Smith 
1849a42bb27SBarry Smith    Input Parameter:
1859a42bb27SBarry Smith +  da - initial distributed array
186e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1879a42bb27SBarry Smith 
1889a42bb27SBarry Smith    Options Database:
189dd85299cSBarry Smith .   -dm_vec_type ctype
1909a42bb27SBarry Smith 
1919a42bb27SBarry Smith    Level: intermediate
1929a42bb27SBarry Smith 
193a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1949a42bb27SBarry Smith @*/
19519fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1969a42bb27SBarry Smith {
1979a42bb27SBarry Smith   PetscErrorCode ierr;
1989a42bb27SBarry Smith 
1999a42bb27SBarry Smith   PetscFunctionBegin;
2009a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2019a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2039a42bb27SBarry Smith   PetscFunctionReturn(0);
2049a42bb27SBarry Smith }
2059a42bb27SBarry Smith 
206c0dedaeaSBarry Smith /*@C
207c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
208c0dedaeaSBarry Smith 
209d083f849SBarry Smith    Logically Collective on da
210c0dedaeaSBarry Smith 
211c0dedaeaSBarry Smith    Input Parameter:
212c0dedaeaSBarry Smith .  da - initial distributed array
213c0dedaeaSBarry Smith 
214c0dedaeaSBarry Smith    Output Parameter:
215c0dedaeaSBarry Smith .  ctype - the vector type
216c0dedaeaSBarry Smith 
217c0dedaeaSBarry Smith    Level: intermediate
218c0dedaeaSBarry Smith 
219a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
220c0dedaeaSBarry Smith @*/
221c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
222c0dedaeaSBarry Smith {
223c0dedaeaSBarry Smith   PetscFunctionBegin;
224c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
225c0dedaeaSBarry Smith   *ctype = da->vectype;
226c0dedaeaSBarry Smith   PetscFunctionReturn(0);
227c0dedaeaSBarry Smith }
228c0dedaeaSBarry Smith 
2295f1ad066SMatthew G Knepley /*@
23034f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2315f1ad066SMatthew G Knepley 
2325f1ad066SMatthew G Knepley   Not collective
2335f1ad066SMatthew G Knepley 
2345f1ad066SMatthew G Knepley   Input Parameter:
2355f1ad066SMatthew G Knepley . v - The Vec
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley   Output Parameter:
2385f1ad066SMatthew G Knepley . dm - The DM
2395f1ad066SMatthew G Knepley 
2405f1ad066SMatthew G Knepley   Level: intermediate
2415f1ad066SMatthew G Knepley 
2425f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2435f1ad066SMatthew G Knepley @*/
2445f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2455f1ad066SMatthew G Knepley {
2465f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   PetscFunctionBegin;
2495f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2505f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2515f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2525f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2535f1ad066SMatthew G Knepley }
2545f1ad066SMatthew G Knepley 
2555f1ad066SMatthew G Knepley /*@
256d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley   Not collective
2595f1ad066SMatthew G Knepley 
2605f1ad066SMatthew G Knepley   Input Parameters:
2615f1ad066SMatthew G Knepley + v - The Vec
2625f1ad066SMatthew G Knepley - dm - The DM
2635f1ad066SMatthew G Knepley 
264d9805387SMatthew 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.
265d9805387SMatthew G. Knepley 
2665f1ad066SMatthew G Knepley   Level: intermediate
2675f1ad066SMatthew G Knepley 
2685f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2695f1ad066SMatthew G Knepley @*/
2705f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2715f1ad066SMatthew G Knepley {
2725f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2735f1ad066SMatthew G Knepley 
2745f1ad066SMatthew G Knepley   PetscFunctionBegin;
2755f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
276d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2775f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2785f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2795f1ad066SMatthew G Knepley }
2805f1ad066SMatthew G Knepley 
281521d9a4cSLisandro Dalcin /*@C
2828f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2838f1509bcSBarry Smith 
284d083f849SBarry Smith    Logically Collective on dm
2858f1509bcSBarry Smith 
2868f1509bcSBarry Smith    Input Parameters:
2878f1509bcSBarry Smith +  dm - the DM context
2888f1509bcSBarry Smith -  ctype - the matrix type
2898f1509bcSBarry Smith 
2908f1509bcSBarry Smith    Options Database:
2918f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2928f1509bcSBarry Smith 
2938f1509bcSBarry Smith    Level: intermediate
2948f1509bcSBarry Smith 
2958f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2968f1509bcSBarry Smith           DMGetISColoringType()
2978f1509bcSBarry Smith @*/
2988f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2998f1509bcSBarry Smith {
3008f1509bcSBarry Smith   PetscFunctionBegin;
3018f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3028f1509bcSBarry Smith   dm->coloringtype = ctype;
3038f1509bcSBarry Smith   PetscFunctionReturn(0);
3048f1509bcSBarry Smith }
3058f1509bcSBarry Smith 
3068f1509bcSBarry Smith /*@C
3078f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
308521d9a4cSLisandro Dalcin 
309d083f849SBarry Smith    Logically Collective on dm
310521d9a4cSLisandro Dalcin 
311521d9a4cSLisandro Dalcin    Input Parameter:
3128f1509bcSBarry Smith .  dm - the DM context
3138f1509bcSBarry Smith 
3148f1509bcSBarry Smith    Output Parameter:
3158f1509bcSBarry Smith .  ctype - the matrix type
3168f1509bcSBarry Smith 
3178f1509bcSBarry Smith    Options Database:
3188f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3198f1509bcSBarry Smith 
3208f1509bcSBarry Smith    Level: intermediate
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3238f1509bcSBarry Smith           DMGetISColoringType()
3248f1509bcSBarry Smith @*/
3258f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3268f1509bcSBarry Smith {
3278f1509bcSBarry Smith   PetscFunctionBegin;
3288f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3298f1509bcSBarry Smith   *ctype = dm->coloringtype;
3308f1509bcSBarry Smith   PetscFunctionReturn(0);
3318f1509bcSBarry Smith }
3328f1509bcSBarry Smith 
3338f1509bcSBarry Smith /*@C
3348f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3358f1509bcSBarry Smith 
336d083f849SBarry Smith    Logically Collective on dm
3378f1509bcSBarry Smith 
3388f1509bcSBarry Smith    Input Parameters:
339521d9a4cSLisandro Dalcin +  dm - the DM context
340a2b5a043SBarry Smith -  ctype - the matrix type
341521d9a4cSLisandro Dalcin 
342521d9a4cSLisandro Dalcin    Options Database:
343521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
344521d9a4cSLisandro Dalcin 
345521d9a4cSLisandro Dalcin    Level: intermediate
346521d9a4cSLisandro Dalcin 
347a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
348521d9a4cSLisandro Dalcin @*/
34919fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
350521d9a4cSLisandro Dalcin {
351521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35288f0584fSBarry Smith 
353521d9a4cSLisandro Dalcin   PetscFunctionBegin;
354521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
355521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
357521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
358521d9a4cSLisandro Dalcin }
359521d9a4cSLisandro Dalcin 
360c0dedaeaSBarry Smith /*@C
361c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
362c0dedaeaSBarry Smith 
363d083f849SBarry Smith    Logically Collective on dm
364c0dedaeaSBarry Smith 
365c0dedaeaSBarry Smith    Input Parameter:
366c0dedaeaSBarry Smith .  dm - the DM context
367c0dedaeaSBarry Smith 
368c0dedaeaSBarry Smith    Output Parameter:
369c0dedaeaSBarry Smith .  ctype - the matrix type
370c0dedaeaSBarry Smith 
371c0dedaeaSBarry Smith    Options Database:
372c0dedaeaSBarry Smith .   -dm_mat_type ctype
373c0dedaeaSBarry Smith 
374c0dedaeaSBarry Smith    Level: intermediate
375c0dedaeaSBarry Smith 
376a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
377c0dedaeaSBarry Smith @*/
378c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
379c0dedaeaSBarry Smith {
380c0dedaeaSBarry Smith   PetscFunctionBegin;
381c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
382c0dedaeaSBarry Smith   *ctype = dm->mattype;
383c0dedaeaSBarry Smith   PetscFunctionReturn(0);
384c0dedaeaSBarry Smith }
385c0dedaeaSBarry Smith 
386c688c046SMatthew G Knepley /*@
38734f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
388c688c046SMatthew G Knepley 
389c688c046SMatthew G Knepley   Not collective
390c688c046SMatthew G Knepley 
391c688c046SMatthew G Knepley   Input Parameter:
392c688c046SMatthew G Knepley . A - The Mat
393c688c046SMatthew G Knepley 
394c688c046SMatthew G Knepley   Output Parameter:
395c688c046SMatthew G Knepley . dm - The DM
396c688c046SMatthew G Knepley 
397c688c046SMatthew G Knepley   Level: intermediate
398c688c046SMatthew G Knepley 
3998f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4008f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4018f1509bcSBarry Smith 
402c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
403c688c046SMatthew G Knepley @*/
404c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
405c688c046SMatthew G Knepley {
406c688c046SMatthew G Knepley   PetscErrorCode ierr;
407c688c046SMatthew G Knepley 
408c688c046SMatthew G Knepley   PetscFunctionBegin;
409c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
410c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
411c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
412c688c046SMatthew G Knepley   PetscFunctionReturn(0);
413c688c046SMatthew G Knepley }
414c688c046SMatthew G Knepley 
415c688c046SMatthew G Knepley /*@
416c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
417c688c046SMatthew G Knepley 
418c688c046SMatthew G Knepley   Not collective
419c688c046SMatthew G Knepley 
420c688c046SMatthew G Knepley   Input Parameters:
421c688c046SMatthew G Knepley + A - The Mat
422c688c046SMatthew G Knepley - dm - The DM
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   Level: intermediate
425c688c046SMatthew G Knepley 
4268f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4278f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4288f1509bcSBarry Smith 
4298f1509bcSBarry Smith 
430c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
431c688c046SMatthew G Knepley @*/
432c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
433c688c046SMatthew G Knepley {
434c688c046SMatthew G Knepley   PetscErrorCode ierr;
435c688c046SMatthew G Knepley 
436c688c046SMatthew G Knepley   PetscFunctionBegin;
437c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4388865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
439c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
440c688c046SMatthew G Knepley   PetscFunctionReturn(0);
441c688c046SMatthew G Knepley }
442c688c046SMatthew G Knepley 
4439a42bb27SBarry Smith /*@C
4449a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4456757b960SDave May    DM options in the database.
4469a42bb27SBarry Smith 
447d083f849SBarry Smith    Logically Collective on dm
4489a42bb27SBarry Smith 
4499a42bb27SBarry Smith    Input Parameter:
4508353ddbbSDave May +  da - the DM context
4519a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4529a42bb27SBarry Smith 
4539a42bb27SBarry Smith    Notes:
4549a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4559a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4569a42bb27SBarry Smith 
4579a42bb27SBarry Smith    Level: advanced
4589a42bb27SBarry Smith 
4599a42bb27SBarry Smith .seealso: DMSetFromOptions()
4609a42bb27SBarry Smith @*/
4617087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4629a42bb27SBarry Smith {
4639a42bb27SBarry Smith   PetscErrorCode ierr;
4649a42bb27SBarry Smith 
4659a42bb27SBarry Smith   PetscFunctionBegin;
4669a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4679a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
468691be533SLawrence Mitchell   if (dm->sf) {
469691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
470691be533SLawrence Mitchell   }
471*1bb6d2a8SBarry Smith   if (dm->sectionSF) {
472*1bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
473691be533SLawrence Mitchell   }
4749a42bb27SBarry Smith   PetscFunctionReturn(0);
4759a42bb27SBarry Smith }
4769a42bb27SBarry Smith 
47731697293SDave May /*@C
47831697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47931697293SDave May    DM options in the database.
48031697293SDave May 
481d083f849SBarry Smith    Logically Collective on dm
48231697293SDave May 
48331697293SDave May    Input Parameters:
48431697293SDave May +  dm - the DM context
48531697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48631697293SDave May 
48731697293SDave May    Notes:
48831697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48931697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49031697293SDave May 
49131697293SDave May    Level: advanced
49231697293SDave May 
49331697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49431697293SDave May @*/
49531697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49631697293SDave May {
49731697293SDave May   PetscErrorCode ierr;
49831697293SDave May 
49931697293SDave May   PetscFunctionBegin;
50031697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50131697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50231697293SDave May   PetscFunctionReturn(0);
50331697293SDave May }
50431697293SDave May 
50531697293SDave May /*@C
50631697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50731697293SDave May    DM options in the database.
50831697293SDave May 
50931697293SDave May    Not Collective
51031697293SDave May 
51131697293SDave May    Input Parameters:
51231697293SDave May .  dm - the DM context
51331697293SDave May 
51431697293SDave May    Output Parameters:
51531697293SDave May .  prefix - pointer to the prefix string used is returned
51631697293SDave May 
51795452b02SPatrick Sanan    Notes:
51895452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51931697293SDave May    sufficient length to hold the prefix.
52031697293SDave May 
52131697293SDave May    Level: advanced
52231697293SDave May 
52331697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52431697293SDave May @*/
52531697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52631697293SDave May {
52731697293SDave May   PetscErrorCode ierr;
52831697293SDave May 
52931697293SDave May   PetscFunctionBegin;
53031697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53131697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53231697293SDave May   PetscFunctionReturn(0);
53331697293SDave May }
53431697293SDave May 
53588bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53688bdff64SToby Isaac {
53788bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
53888bdff64SToby Isaac   DMNamedVecLink nlink;
53988bdff64SToby Isaac   PetscErrorCode ierr;
54088bdff64SToby Isaac 
54188bdff64SToby Isaac   PetscFunctionBegin;
542aab5bcd8SJed Brown   *ncrefct = 0;
54388bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
54488bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
54588bdff64SToby Isaac     if (dm->localin[i])  refct--;
54688bdff64SToby Isaac     if (dm->globalin[i]) refct--;
54788bdff64SToby Isaac   }
54888bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
54988bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
55088bdff64SToby Isaac   if (dm->x) {
55188bdff64SToby Isaac     DM obj;
55288bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
55388bdff64SToby Isaac     if (obj == dm) refct--;
55488bdff64SToby Isaac   }
55588bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55688bdff64SToby Isaac     refct--;
55788bdff64SToby Isaac     if (recurseCoarse) {
55888bdff64SToby Isaac       PetscInt coarseCount;
55988bdff64SToby Isaac 
56088bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
56188bdff64SToby Isaac       refct += coarseCount;
56288bdff64SToby Isaac     }
56388bdff64SToby Isaac   }
56488bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56588bdff64SToby Isaac     refct--;
56688bdff64SToby Isaac     if (recurseFine) {
56788bdff64SToby Isaac       PetscInt fineCount;
56888bdff64SToby Isaac 
56988bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
57088bdff64SToby Isaac       refct += fineCount;
57188bdff64SToby Isaac     }
57288bdff64SToby Isaac   }
57388bdff64SToby Isaac   *ncrefct = refct;
57488bdff64SToby Isaac   PetscFunctionReturn(0);
57588bdff64SToby Isaac }
57688bdff64SToby Isaac 
577354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
578354557abSToby Isaac {
579354557abSToby Isaac   PetscErrorCode ierr;
580354557abSToby Isaac 
581354557abSToby Isaac   PetscFunctionBegin;
582354557abSToby Isaac   if (!--(dm->labels->refct)) {
583354557abSToby Isaac     DMLabelLink next = dm->labels->next;
584354557abSToby Isaac 
585354557abSToby Isaac     /* destroy the labels */
586354557abSToby Isaac     while (next) {
587354557abSToby Isaac       DMLabelLink tmp = next->next;
588354557abSToby Isaac 
589354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
590354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
591354557abSToby Isaac       next = tmp;
592354557abSToby Isaac     }
593354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
594354557abSToby Isaac   }
595354557abSToby Isaac   PetscFunctionReturn(0);
596354557abSToby Isaac }
597354557abSToby Isaac 
59847c6ae99SBarry Smith /*@
5998472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
60047c6ae99SBarry Smith 
601d083f849SBarry Smith     Collective on dm
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith     Input Parameter:
60447c6ae99SBarry Smith .   dm - the DM object to destroy
60547c6ae99SBarry Smith 
60647c6ae99SBarry Smith     Level: developer
60747c6ae99SBarry Smith 
608e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith @*/
611fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
61247c6ae99SBarry Smith {
61388bdff64SToby Isaac   PetscInt       i, cnt;
614dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61547c6ae99SBarry Smith   PetscErrorCode ierr;
61647c6ae99SBarry Smith 
61747c6ae99SBarry Smith   PetscFunctionBegin;
6186bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6196bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
62087e657c6SBarry Smith 
62188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
62288bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
62488bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
625732e2eb9SMatthew G Knepley   /*
626732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
627732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
628732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
629732e2eb9SMatthew G Knepley   */
6306bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6316bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
632732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6336bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6346bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
635732e2eb9SMatthew G Knepley   }
636f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6370298fd71SBarry Smith   (*dm)->namedglobal = NULL;
638f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6392348bcf4SPeter Brune     nnext = nlink->next;
6402348bcf4SPeter 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);
6412348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6422348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6432348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6442348bcf4SPeter Brune   }
645f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6460298fd71SBarry Smith   (*dm)->namedlocal = NULL;
647f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
648f490541aSPeter Brune     nnext = nlink->next;
649f490541aSPeter 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);
650f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
651f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
652f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
653f490541aSPeter Brune   }
6542348bcf4SPeter Brune 
655b17ce1afSJed Brown   /* Destroy the list of hooks */
656c833c3b5SJed Brown   {
657c833c3b5SJed Brown     DMCoarsenHookLink link,next;
658b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
659b17ce1afSJed Brown       next = link->next;
660b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
661b17ce1afSJed Brown     }
6620298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
663c833c3b5SJed Brown   }
664c833c3b5SJed Brown   {
665c833c3b5SJed Brown     DMRefineHookLink link,next;
666c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
667c833c3b5SJed Brown       next = link->next;
668c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
669c833c3b5SJed Brown     }
6700298fd71SBarry Smith     (*dm)->refinehook = NULL;
671c833c3b5SJed Brown   }
672be081cd6SPeter Brune   {
673be081cd6SPeter Brune     DMSubDomainHookLink link,next;
674be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
675be081cd6SPeter Brune       next = link->next;
676be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
677be081cd6SPeter Brune     }
6780298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
679be081cd6SPeter Brune   }
680baf369e7SPeter Brune   {
681baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
682baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
683baf369e7SPeter Brune       next = link->next;
684baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
685baf369e7SPeter Brune     }
6860298fd71SBarry Smith     (*dm)->gtolhook = NULL;
687baf369e7SPeter Brune   }
688d4d07f1eSToby Isaac   {
689d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
690d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
691d4d07f1eSToby Isaac       next = link->next;
692d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
693d4d07f1eSToby Isaac     }
694d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
695d4d07f1eSToby Isaac   }
696aa1993deSMatthew G Knepley   /* Destroy the work arrays */
697aa1993deSMatthew G Knepley   {
698aa1993deSMatthew G Knepley     DMWorkLink link,next;
699aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
700aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
701aa1993deSMatthew G Knepley       next = link->next;
702aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
703aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
704aa1993deSMatthew G Knepley     }
7050298fd71SBarry Smith     (*dm)->workin = NULL;
706aa1993deSMatthew G Knepley   }
707c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
708c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
709c58f1c22SToby Isaac 
710c58f1c22SToby Isaac     /* destroy the labels */
711c58f1c22SToby Isaac     while (next) {
712c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
713c58f1c22SToby Isaac 
714c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
715c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
716c58f1c22SToby Isaac       next = tmp;
717c58f1c22SToby Isaac     }
718c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
719c58f1c22SToby Isaac   }
720e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
721e6f8dbb6SToby Isaac   {
722e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
723e6f8dbb6SToby Isaac     while (next) {
724e6f8dbb6SToby Isaac       DMBoundary b = next;
725e6f8dbb6SToby Isaac 
726e6f8dbb6SToby Isaac       next = b->next;
727e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
728e6f8dbb6SToby Isaac     }
729e6f8dbb6SToby Isaac   }
730b17ce1afSJed Brown 
73152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
73252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
73352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
73452536dc3SBarry Smith 
7351a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7361a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7371a266240SBarry Smith   }
73887e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
73971cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7404dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7416bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7426bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
743073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
74488ed4aceSMatthew G Knepley 
745*1bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
746*1bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7478b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
748fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
749fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
75088ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
751*1bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
752736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
753736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7548e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
755736995cdSBlaise Bourdin     }
756736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
757736995cdSBlaise Bourdin   }
75888bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75988bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
76088bdff64SToby Isaac   }
761a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
76288bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
76388bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76488bdff64SToby Isaac   }
76588bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
766f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7676636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7686636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7696636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7705dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
771ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
772ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
773ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7746636e97aSMatthew G Knepley 
775e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
77614f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
777e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
778e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
779732e2eb9SMatthew G Knepley 
780ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7816bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
782ed3c66a1SDave May   }
783435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
784732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
78547c6ae99SBarry Smith   PetscFunctionReturn(0);
78647c6ae99SBarry Smith }
78747c6ae99SBarry Smith 
788d7bf68aeSBarry Smith /*@
789d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
790d7bf68aeSBarry Smith 
791d083f849SBarry Smith     Collective on dm
792d7bf68aeSBarry Smith 
793d7bf68aeSBarry Smith     Input Parameter:
794d7bf68aeSBarry Smith .   dm - the DM object to setup
795d7bf68aeSBarry Smith 
796d7bf68aeSBarry Smith     Level: developer
797d7bf68aeSBarry Smith 
798e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
799d7bf68aeSBarry Smith 
800d7bf68aeSBarry Smith @*/
8017087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
802d7bf68aeSBarry Smith {
803d7bf68aeSBarry Smith   PetscErrorCode ierr;
804d7bf68aeSBarry Smith 
805d7bf68aeSBarry Smith   PetscFunctionBegin;
806171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8078387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
808d7bf68aeSBarry Smith   if (dm->ops->setup) {
809d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
810d7bf68aeSBarry Smith   }
8118387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
812d7bf68aeSBarry Smith   PetscFunctionReturn(0);
813d7bf68aeSBarry Smith }
814d7bf68aeSBarry Smith 
815d7bf68aeSBarry Smith /*@
816d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
817d7bf68aeSBarry Smith 
818d083f849SBarry Smith     Collective on dm
819d7bf68aeSBarry Smith 
820d7bf68aeSBarry Smith     Input Parameter:
821d7bf68aeSBarry Smith .   dm - the DM object to set options for
822d7bf68aeSBarry Smith 
823732e2eb9SMatthew G Knepley     Options Database:
8244164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8254164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8264164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8278f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
828732e2eb9SMatthew G Knepley 
829d7bf68aeSBarry Smith     Level: developer
830d7bf68aeSBarry Smith 
831e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
832d7bf68aeSBarry Smith 
833d7bf68aeSBarry Smith @*/
8347087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
835d7bf68aeSBarry Smith {
8367781c08eSBarry Smith   char           typeName[256];
837ca266f36SBarry Smith   PetscBool      flg;
838d7bf68aeSBarry Smith   PetscErrorCode ierr;
839d7bf68aeSBarry Smith 
840d7bf68aeSBarry Smith   PetscFunctionBegin;
841171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84249be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
84349be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
844*1bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8453194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8460298fd71SBarry 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);
847a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
848f9ba7244SBarry Smith   if (flg) {
849f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
850f9ba7244SBarry Smith   }
851a264d7a6SBarry 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);
852073dac72SJed Brown   if (flg) {
853521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
854073dac72SJed Brown   }
8558f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
856f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
857e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
858f9ba7244SBarry Smith   }
859f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8600633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
86182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
862d7bf68aeSBarry Smith   PetscFunctionReturn(0);
863d7bf68aeSBarry Smith }
864d7bf68aeSBarry Smith 
865fc9bc008SSatish Balay /*@C
866224748a4SBarry Smith     DMView - Views a DM
86747c6ae99SBarry Smith 
868d083f849SBarry Smith     Collective on dm
86947c6ae99SBarry Smith 
87047c6ae99SBarry Smith     Input Parameter:
87147c6ae99SBarry Smith +   dm - the DM object to view
87247c6ae99SBarry Smith -   v - the viewer
87347c6ae99SBarry Smith 
874224748a4SBarry Smith     Level: beginner
87547c6ae99SBarry Smith 
876e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
87747c6ae99SBarry Smith 
87847c6ae99SBarry Smith @*/
8797087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
88047c6ae99SBarry Smith {
88147c6ae99SBarry Smith   PetscErrorCode    ierr;
88232c0f0efSBarry Smith   PetscBool         isbinary;
88376a8abe0SBarry Smith   PetscMPIInt       size;
88476a8abe0SBarry Smith   PetscViewerFormat format;
88547c6ae99SBarry Smith 
88647c6ae99SBarry Smith   PetscFunctionBegin;
887171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8883014e516SBarry Smith   if (!v) {
889ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8903014e516SBarry Smith   }
891b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
892b1b135c8SBarry Smith   PetscCheckSameComm(dm,1,v,2);
8938bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
894b1b135c8SBarry Smith 
89576a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
89676a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
89776a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
89898c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
89932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
90032c0f0efSBarry Smith   if (isbinary) {
90155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
90232c0f0efSBarry Smith     char     type[256];
90332c0f0efSBarry Smith 
90432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
90532c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
90632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
90732c0f0efSBarry Smith   }
9080c010503SBarry Smith   if (dm->ops->view) {
9090c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
91047c6ae99SBarry Smith   }
91147c6ae99SBarry Smith   PetscFunctionReturn(0);
91247c6ae99SBarry Smith }
91347c6ae99SBarry Smith 
91447c6ae99SBarry Smith /*@
9158472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
91647c6ae99SBarry Smith 
917d083f849SBarry Smith     Collective on dm
91847c6ae99SBarry Smith 
91947c6ae99SBarry Smith     Input Parameter:
92047c6ae99SBarry Smith .   dm - the DM object
92147c6ae99SBarry Smith 
92247c6ae99SBarry Smith     Output Parameter:
92347c6ae99SBarry Smith .   vec - the global vector
92447c6ae99SBarry Smith 
925073dac72SJed Brown     Level: beginner
92647c6ae99SBarry Smith 
927e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith @*/
9307087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
93147c6ae99SBarry Smith {
93247c6ae99SBarry Smith   PetscErrorCode ierr;
93347c6ae99SBarry Smith 
93447c6ae99SBarry Smith   PetscFunctionBegin;
935171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
936b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
937b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
93847c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
93947c6ae99SBarry Smith   PetscFunctionReturn(0);
94047c6ae99SBarry Smith }
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith /*@
9438472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith     Not Collective
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith     Input Parameter:
94847c6ae99SBarry Smith .   dm - the DM object
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith     Output Parameter:
95147c6ae99SBarry Smith .   vec - the local vector
95247c6ae99SBarry Smith 
953073dac72SJed Brown     Level: beginner
95447c6ae99SBarry Smith 
955e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith @*/
9587087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
95947c6ae99SBarry Smith {
96047c6ae99SBarry Smith   PetscErrorCode ierr;
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith   PetscFunctionBegin;
963171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
964b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
965b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
96647c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
96747c6ae99SBarry Smith   PetscFunctionReturn(0);
96847c6ae99SBarry Smith }
96947c6ae99SBarry Smith 
9701411c6eeSJed Brown /*@
9711411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9721411c6eeSJed Brown 
973d083f849SBarry Smith    Collective on dm
9741411c6eeSJed Brown 
9751411c6eeSJed Brown    Input Parameter:
9761411c6eeSJed Brown .  dm - the DM that provides the mapping
9771411c6eeSJed Brown 
9781411c6eeSJed Brown    Output Parameter:
9791411c6eeSJed Brown .  ltog - the mapping
9801411c6eeSJed Brown 
9811411c6eeSJed Brown    Level: intermediate
9821411c6eeSJed Brown 
9831411c6eeSJed Brown    Notes:
9841411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9851411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9861411c6eeSJed Brown 
987fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9881411c6eeSJed Brown @*/
9897087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9901411c6eeSJed Brown {
9910be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9921411c6eeSJed Brown   PetscErrorCode ierr;
9931411c6eeSJed Brown 
9941411c6eeSJed Brown   PetscFunctionBegin;
9951411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9961411c6eeSJed Brown   PetscValidPointer(ltog,2);
9971411c6eeSJed Brown   if (!dm->ltogmap) {
99837d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
99937d0c07bSMatthew G Knepley 
100092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
100137d0c07bSMatthew G Knepley     if (section) {
1002a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
100337d0c07bSMatthew G Knepley       PetscInt       *ltog;
1004ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
100537d0c07bSMatthew G Knepley 
1006e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
100737d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1008ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1009ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
101037d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1011a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
101237d0c07bSMatthew G Knepley 
101337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
101437d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1015a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1016a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
101737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10181a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10191a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10201a7dc684SMatthew G. Knepley         if (dof) {
1021b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1022b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10231a7dc684SMatthew G. Knepley         }
102437d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1025a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1026a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
102737d0c07bSMatthew G Knepley         }
102837d0c07bSMatthew G Knepley       }
1029bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10300be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10310be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10320be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10330be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10347591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10357591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1036ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1037ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1038ccf3bd66SMatthew G. Knepley         n /= bs;
1039ccf3bd66SMatthew G. Knepley       }
1040ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10413bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
104237d0c07bSMatthew G Knepley     } else {
1043b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1044184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10451411c6eeSJed Brown     }
104637d0c07bSMatthew G Knepley   }
10471411c6eeSJed Brown   *ltog = dm->ltogmap;
10481411c6eeSJed Brown   PetscFunctionReturn(0);
10491411c6eeSJed Brown }
10501411c6eeSJed Brown 
10511411c6eeSJed Brown /*@
10521411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10531411c6eeSJed Brown 
10541411c6eeSJed Brown    Not Collective
10551411c6eeSJed Brown 
10561411c6eeSJed Brown    Input Parameter:
10571411c6eeSJed Brown .  dm - the DM with block structure
10581411c6eeSJed Brown 
10591411c6eeSJed Brown    Output Parameter:
10601411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10611411c6eeSJed Brown 
10621411c6eeSJed Brown    Level: intermediate
10631411c6eeSJed Brown 
1064fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10651411c6eeSJed Brown @*/
10667087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10671411c6eeSJed Brown {
10681411c6eeSJed Brown   PetscFunctionBegin;
10691411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1070534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
10711411c6eeSJed 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");
10721411c6eeSJed Brown   *bs = dm->bs;
10731411c6eeSJed Brown   PetscFunctionReturn(0);
10741411c6eeSJed Brown }
10751411c6eeSJed Brown 
107647c6ae99SBarry Smith /*@
10778472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
107847c6ae99SBarry Smith 
1079d083f849SBarry Smith     Collective on dm1
108047c6ae99SBarry Smith 
108147c6ae99SBarry Smith     Input Parameter:
108247c6ae99SBarry Smith +   dm1 - the DM object
108347c6ae99SBarry Smith -   dm2 - the second, finer DM object
108447c6ae99SBarry Smith 
108547c6ae99SBarry Smith     Output Parameter:
108647c6ae99SBarry Smith +  mat - the interpolation
108747c6ae99SBarry Smith -  vec - the scaling (optional)
108847c6ae99SBarry Smith 
108947c6ae99SBarry Smith     Level: developer
109047c6ae99SBarry Smith 
109195452b02SPatrick Sanan     Notes:
109295452b02SPatrick 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
109385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1094d52bd9f3SBarry Smith 
10951f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1096d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
109785afcc9aSBarry Smith 
109885afcc9aSBarry Smith 
10993ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
110047c6ae99SBarry Smith 
110147c6ae99SBarry Smith @*/
1102e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
110347c6ae99SBarry Smith {
110447c6ae99SBarry Smith   PetscErrorCode ierr;
110547c6ae99SBarry Smith 
110647c6ae99SBarry Smith   PetscFunctionBegin;
1107171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1108171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1109c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1110b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dm1)->type_name);
1111cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111225296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1113cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111447c6ae99SBarry Smith   PetscFunctionReturn(0);
111547c6ae99SBarry Smith }
111647c6ae99SBarry Smith 
11173ad4599aSBarry Smith /*@
11183ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11193ad4599aSBarry Smith 
1120d083f849SBarry Smith     Collective on dm1
11213ad4599aSBarry Smith 
11223ad4599aSBarry Smith     Input Parameter:
11233ad4599aSBarry Smith +   dm1 - the DM object
11243ad4599aSBarry Smith -   dm2 - the second, finer DM object
11253ad4599aSBarry Smith 
11263ad4599aSBarry Smith     Output Parameter:
11273ad4599aSBarry Smith .  mat - the restriction
11283ad4599aSBarry Smith 
11293ad4599aSBarry Smith 
11303ad4599aSBarry Smith     Level: developer
11313ad4599aSBarry Smith 
113295452b02SPatrick Sanan     Notes:
113395452b02SPatrick 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
11343ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11353ad4599aSBarry Smith 
11363ad4599aSBarry Smith 
11373ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11383ad4599aSBarry Smith 
11393ad4599aSBarry Smith @*/
11403ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11413ad4599aSBarry Smith {
11423ad4599aSBarry Smith   PetscErrorCode ierr;
11433ad4599aSBarry Smith 
11443ad4599aSBarry Smith   PetscFunctionBegin;
11453ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11463ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11475a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1148b9d85ea2SLisandro Dalcin   if (!dm1->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dm1)->type_name);
11493ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11503ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11513ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11523ad4599aSBarry Smith   PetscFunctionReturn(0);
11533ad4599aSBarry Smith }
11543ad4599aSBarry Smith 
115547c6ae99SBarry Smith /*@
11568472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
115747c6ae99SBarry Smith 
1158d083f849SBarry Smith     Collective on dm1
115947c6ae99SBarry Smith 
116047c6ae99SBarry Smith     Input Parameter:
116147c6ae99SBarry Smith +   dm1 - the DM object
116247c6ae99SBarry Smith -   dm2 - the second, finer DM object
116347c6ae99SBarry Smith 
116447c6ae99SBarry Smith     Output Parameter:
11656dbf9973SLawrence Mitchell .   mat - the injection
116647c6ae99SBarry Smith 
116747c6ae99SBarry Smith     Level: developer
116847c6ae99SBarry Smith 
116995452b02SPatrick Sanan    Notes:
117095452b02SPatrick 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
117185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
117285afcc9aSBarry Smith 
1173e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
117447c6ae99SBarry Smith 
117547c6ae99SBarry Smith @*/
11766dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
117747c6ae99SBarry Smith {
117847c6ae99SBarry Smith   PetscErrorCode ierr;
117947c6ae99SBarry Smith 
118047c6ae99SBarry Smith   PetscFunctionBegin;
1181171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1182171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11835a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1184b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dm1)->type_name);
11855a84ad33SLisandro Dalcin   ierr = PetscLogEventBegin(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
11865a84ad33SLisandro Dalcin   ierr = (*dm1->ops->createinjection)(dm1,dm2,mat);CHKERRQ(ierr);
11875a84ad33SLisandro Dalcin   ierr = PetscLogEventEnd(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
118847c6ae99SBarry Smith   PetscFunctionReturn(0);
118947c6ae99SBarry Smith }
119047c6ae99SBarry Smith 
1191b412c318SBarry Smith /*@
1192bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1193bd041c0cSMatthew G. Knepley 
1194d083f849SBarry Smith   Collective on dm1
1195bd041c0cSMatthew G. Knepley 
1196bd041c0cSMatthew G. Knepley   Input Parameter:
1197bd041c0cSMatthew G. Knepley + dm1 - the DM object
1198bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1199bd041c0cSMatthew G. Knepley 
1200bd041c0cSMatthew G. Knepley   Output Parameter:
1201bd041c0cSMatthew G. Knepley . mat - the interpolation
1202bd041c0cSMatthew G. Knepley 
1203bd041c0cSMatthew G. Knepley   Level: developer
1204bd041c0cSMatthew G. Knepley 
1205bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1206bd041c0cSMatthew G. Knepley @*/
1207bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1208bd041c0cSMatthew G. Knepley {
1209bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1210bd041c0cSMatthew G. Knepley 
1211bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1212bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1213bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
12145a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1215b9d85ea2SLisandro Dalcin   if (!dm1->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dm1)->type_name);
1216bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1217bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1218bd041c0cSMatthew G. Knepley }
1219bd041c0cSMatthew G. Knepley 
1220bd041c0cSMatthew G. Knepley /*@
1221b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
122247c6ae99SBarry Smith 
1223d083f849SBarry Smith     Collective on dm
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith     Input Parameter:
122647c6ae99SBarry Smith +   dm - the DM object
12275bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
122847c6ae99SBarry Smith 
122947c6ae99SBarry Smith     Output Parameter:
123047c6ae99SBarry Smith .   coloring - the coloring
123147c6ae99SBarry Smith 
123247c6ae99SBarry Smith     Level: developer
123347c6ae99SBarry Smith 
1234b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
123547c6ae99SBarry Smith 
1236aab9d709SJed Brown @*/
1237b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
123847c6ae99SBarry Smith {
123947c6ae99SBarry Smith   PetscErrorCode ierr;
124047c6ae99SBarry Smith 
124147c6ae99SBarry Smith   PetscFunctionBegin;
1242171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12435a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1244b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1245b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
124647c6ae99SBarry Smith   PetscFunctionReturn(0);
124747c6ae99SBarry Smith }
124847c6ae99SBarry Smith 
1249b412c318SBarry Smith /*@
12508472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
125147c6ae99SBarry Smith 
1252d083f849SBarry Smith     Collective on dm
125347c6ae99SBarry Smith 
125447c6ae99SBarry Smith     Input Parameter:
1255b412c318SBarry Smith .   dm - the DM object
125647c6ae99SBarry Smith 
125747c6ae99SBarry Smith     Output Parameter:
125847c6ae99SBarry Smith .   mat - the empty Jacobian
125947c6ae99SBarry Smith 
1260073dac72SJed Brown     Level: beginner
126147c6ae99SBarry Smith 
126295452b02SPatrick Sanan     Notes:
126395452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
126494013140SBarry Smith        do not need to do it yourself.
126594013140SBarry Smith 
126694013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12677889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
126894013140SBarry Smith 
126994013140SBarry 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
127094013140SBarry Smith        internally by PETSc.
127194013140SBarry Smith 
127294013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1273aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
127494013140SBarry Smith 
1275b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
127647c6ae99SBarry Smith 
1277aab9d709SJed Brown @*/
1278b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
127947c6ae99SBarry Smith {
128047c6ae99SBarry Smith   PetscErrorCode ierr;
128147c6ae99SBarry Smith 
128247c6ae99SBarry Smith   PetscFunctionBegin;
1283171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1284c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1285b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
12865a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1287fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1288b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1289e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1290e5e52638SMatthew G. Knepley   if (dm->Nf) {
1291e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1292e571a35bSMatthew G. Knepley     PetscInt     Nf;
1293e571a35bSMatthew G. Knepley 
1294e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1295e571a35bSMatthew G. Knepley     if (Nf == 1) {
1296e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1297e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1298e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1299e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1300e571a35bSMatthew G. Knepley       }
1301e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1302e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1303e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1304e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1305e571a35bSMatthew G. Knepley       }
1306e571a35bSMatthew G. Knepley     }
1307e571a35bSMatthew G. Knepley   }
1308fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
130947c6ae99SBarry Smith   PetscFunctionReturn(0);
131047c6ae99SBarry Smith }
131147c6ae99SBarry Smith 
1312732e2eb9SMatthew G Knepley /*@
1313950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1314732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1315732e2eb9SMatthew G Knepley 
1316d083f849SBarry Smith   Logically Collective on dm
1317732e2eb9SMatthew G Knepley 
1318732e2eb9SMatthew G Knepley   Input Parameter:
1319732e2eb9SMatthew G Knepley + dm - the DM
1320732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1321732e2eb9SMatthew G Knepley 
1322732e2eb9SMatthew G Knepley   Level: developer
1323b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1324732e2eb9SMatthew G Knepley @*/
1325732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1326732e2eb9SMatthew G Knepley {
1327732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1328732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1329732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1330732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1331732e2eb9SMatthew G Knepley }
1332732e2eb9SMatthew G Knepley 
1333b06ff27eSHong Zhang /*@
1334b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1335b06ff27eSHong Zhang     but the array for values will not be allocated.
1336b06ff27eSHong Zhang 
1337d083f849SBarry Smith   Logically Collective on dm
1338b06ff27eSHong Zhang 
1339b06ff27eSHong Zhang   Input Parameter:
1340b06ff27eSHong Zhang + dm - the DM
1341b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1342b06ff27eSHong Zhang 
1343b06ff27eSHong Zhang   Level: developer
1344b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1345b06ff27eSHong Zhang @*/
1346b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1347b06ff27eSHong Zhang {
1348b06ff27eSHong Zhang   PetscFunctionBegin;
1349b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1350b06ff27eSHong Zhang   dm->structure_only = only;
1351b06ff27eSHong Zhang   PetscFunctionReturn(0);
1352b06ff27eSHong Zhang }
1353b06ff27eSHong Zhang 
1354a89ea682SMatthew G Knepley /*@C
1355aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1356a89ea682SMatthew G Knepley 
1357a89ea682SMatthew G Knepley   Not Collective
1358a89ea682SMatthew G Knepley 
1359a89ea682SMatthew G Knepley   Input Parameters:
1360a89ea682SMatthew G Knepley + dm - the DM object
1361aa1993deSMatthew G Knepley . count - The minium size
136269291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1363a89ea682SMatthew G Knepley 
1364a89ea682SMatthew G Knepley   Output Parameter:
1365a89ea682SMatthew G Knepley . array - the work array
1366a89ea682SMatthew G Knepley 
1367a89ea682SMatthew G Knepley   Level: developer
1368a89ea682SMatthew G Knepley 
1369a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1370a89ea682SMatthew G Knepley @*/
137169291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1372a89ea682SMatthew G Knepley {
1373a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1374aa1993deSMatthew G Knepley   DMWorkLink     link;
137569291d52SBarry Smith   PetscMPIInt    dsize;
1376a89ea682SMatthew G Knepley 
1377a89ea682SMatthew G Knepley   PetscFunctionBegin;
1378a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1379aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1380aa1993deSMatthew G Knepley   if (dm->workin) {
1381aa1993deSMatthew G Knepley     link       = dm->workin;
1382aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1383aa1993deSMatthew G Knepley   } else {
1384b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1385a89ea682SMatthew G Knepley   }
138669291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13875056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1388aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1389854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1390abe1f95cSSatish Balay     ierr        = PetscMemzero(link->mem,dsize*count);CHKERRQ(ierr);
1391854ce69bSBarry Smith     link->bytes = dsize*count;
1392aa1993deSMatthew G Knepley   }
1393aa1993deSMatthew G Knepley   link->next   = dm->workout;
1394aa1993deSMatthew G Knepley   dm->workout  = link;
1395aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1396a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1397a89ea682SMatthew G Knepley }
1398a89ea682SMatthew G Knepley 
1399aa1993deSMatthew G Knepley /*@C
1400aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1401aa1993deSMatthew G Knepley 
1402aa1993deSMatthew G Knepley   Not Collective
1403aa1993deSMatthew G Knepley 
1404aa1993deSMatthew G Knepley   Input Parameters:
1405aa1993deSMatthew G Knepley + dm - the DM object
1406aa1993deSMatthew G Knepley . count - The minium size
140769291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1408aa1993deSMatthew G Knepley 
1409aa1993deSMatthew G Knepley   Output Parameter:
1410aa1993deSMatthew G Knepley . array - the work array
1411aa1993deSMatthew G Knepley 
1412aa1993deSMatthew G Knepley   Level: developer
1413aa1993deSMatthew G Knepley 
141495452b02SPatrick Sanan   Developer Notes:
141595452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1416aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1417aa1993deSMatthew G Knepley @*/
141869291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1419aa1993deSMatthew G Knepley {
1420aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1421aa1993deSMatthew G Knepley 
1422aa1993deSMatthew G Knepley   PetscFunctionBegin;
1423aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1424aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1425aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1426aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1427aa1993deSMatthew G Knepley       *p           = link->next;
1428aa1993deSMatthew G Knepley       link->next   = dm->workin;
1429aa1993deSMatthew G Knepley       dm->workin   = link;
14300298fd71SBarry Smith       *(void**)mem = NULL;
1431aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1432aa1993deSMatthew G Knepley     }
1433aa1993deSMatthew G Knepley   }
1434aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1435aa1993deSMatthew G Knepley }
1436e7c4fc90SDmitry Karpeev 
1437435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1438435a35e8SMatthew G Knepley {
1439435a35e8SMatthew G Knepley   PetscFunctionBegin;
1440435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
144182f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1442435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1443435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1444435a35e8SMatthew G Knepley }
1445435a35e8SMatthew G Knepley 
14460a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14470a50eb56SMatthew G. Knepley {
14480a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14490a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1450f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14510a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14520a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14530a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14540a50eb56SMatthew G. Knepley }
14550a50eb56SMatthew G. Knepley 
1456f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1457f9d4088aSMatthew G. Knepley {
1458f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1459f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1460f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1461f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1462f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1463f9d4088aSMatthew G. Knepley }
1464f9d4088aSMatthew G. Knepley 
1465f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1466f9d4088aSMatthew G. Knepley {
1467f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1468f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1469f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1470f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1471f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1472f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1473f9d4088aSMatthew G. Knepley }
1474f9d4088aSMatthew G. Knepley 
14754f3b5142SJed Brown /*@C
14764d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14774d343eeaSMatthew G Knepley 
14784d343eeaSMatthew G Knepley   Not collective
14794d343eeaSMatthew G Knepley 
14804d343eeaSMatthew G Knepley   Input Parameter:
14814d343eeaSMatthew G Knepley . dm - the DM object
14824d343eeaSMatthew G Knepley 
14834d343eeaSMatthew G Knepley   Output Parameters:
14840298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14850298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14860298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14874d343eeaSMatthew G Knepley 
14884d343eeaSMatthew G Knepley   Level: intermediate
14894d343eeaSMatthew G Knepley 
149021c9b008SJed Brown   Notes:
149121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
149221c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
149321c9b008SJed Brown   PetscFree().
149421c9b008SJed Brown 
14954d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14964d343eeaSMatthew G Knepley @*/
149737d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14984d343eeaSMatthew G Knepley {
149937d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
15004d343eeaSMatthew G Knepley   PetscErrorCode ierr;
15014d343eeaSMatthew G Knepley 
15024d343eeaSMatthew G Knepley   PetscFunctionBegin;
15034d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
150469ca1f37SDmitry Karpeev   if (numFields) {
1505534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
150669ca1f37SDmitry Karpeev     *numFields = 0;
150769ca1f37SDmitry Karpeev   }
150837d0c07bSMatthew G Knepley   if (fieldNames) {
150937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15100298fd71SBarry Smith     *fieldNames = NULL;
151169ca1f37SDmitry Karpeev   }
151269ca1f37SDmitry Karpeev   if (fields) {
151369ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15140298fd71SBarry Smith     *fields = NULL;
151569ca1f37SDmitry Karpeev   }
151692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
151737d0c07bSMatthew G Knepley   if (section) {
15183a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
151937d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
152037d0c07bSMatthew G Knepley 
1521e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
152237d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15233a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
152437d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
152537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
152637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15273a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
152837d0c07bSMatthew G Knepley     }
152937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
153037d0c07bSMatthew G Knepley       PetscInt gdof;
153137d0c07bSMatthew G Knepley 
153237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
153337d0c07bSMatthew G Knepley       if (gdof > 0) {
153437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15353a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
153637d0c07bSMatthew G Knepley 
153737d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
153837d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15393a544194SStefano Zampini           fpdof = fdof-fcdof;
15403a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15413a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15423a544194SStefano Zampini             fieldNc[f] = 1;
15433a544194SStefano Zampini           }
15443a544194SStefano Zampini           fieldSizes[f] += fpdof;
154537d0c07bSMatthew G Knepley         }
154637d0c07bSMatthew G Knepley       }
154737d0c07bSMatthew G Knepley     }
154837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1549785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
155037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
155137d0c07bSMatthew G Knepley     }
155237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
155337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
155437d0c07bSMatthew G Knepley 
155537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
155637d0c07bSMatthew G Knepley       if (gdof > 0) {
155737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
155837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
155937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
156037d0c07bSMatthew G Knepley 
156137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
156237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
156337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
156437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
156537d0c07bSMatthew G Knepley           }
156637d0c07bSMatthew G Knepley         }
156737d0c07bSMatthew G Knepley       }
156837d0c07bSMatthew G Knepley     }
15698865f1eaSKarl Rupp     if (numFields) *numFields = nF;
157037d0c07bSMatthew G Knepley     if (fieldNames) {
1571785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
157237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
157337d0c07bSMatthew G Knepley         const char *fieldName;
157437d0c07bSMatthew G Knepley 
157537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
157637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
157737d0c07bSMatthew G Knepley       }
157837d0c07bSMatthew G Knepley     }
157937d0c07bSMatthew G Knepley     if (fields) {
1580785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
158137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15823a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15833a544194SStefano Zampini 
158482f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15853a544194SStefano Zampini         in[0] = -fieldNc[f];
15863a544194SStefano Zampini         in[1] = fieldNc[f];
15873a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15883a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15893a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
159037d0c07bSMatthew G Knepley       }
159137d0c07bSMatthew G Knepley     }
15923a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15938865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15948865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
159569ca1f37SDmitry Karpeev   }
15964d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15974d343eeaSMatthew G Knepley }
15984d343eeaSMatthew G Knepley 
159916621825SDmitry Karpeev 
160016621825SDmitry Karpeev /*@C
160116621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
160216621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
160316621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1604e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1605e7c4fc90SDmitry Karpeev 
1606e7c4fc90SDmitry Karpeev   Not collective
1607e7c4fc90SDmitry Karpeev 
1608e7c4fc90SDmitry Karpeev   Input Parameter:
1609e7c4fc90SDmitry Karpeev . dm - the DM object
1610e7c4fc90SDmitry Karpeev 
1611e7c4fc90SDmitry Karpeev   Output Parameters:
16120298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16130298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16140298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16150298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1616e7c4fc90SDmitry Karpeev 
1617e7c4fc90SDmitry Karpeev   Level: intermediate
1618e7c4fc90SDmitry Karpeev 
1619e7c4fc90SDmitry Karpeev   Notes:
1620e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1621e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1622e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1623e7c4fc90SDmitry Karpeev 
1624e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1625e7c4fc90SDmitry Karpeev @*/
162616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1627e7c4fc90SDmitry Karpeev {
1628e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1629e7c4fc90SDmitry Karpeev 
1630e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1631e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16328865f1eaSKarl Rupp   if (len) {
1633534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
16348865f1eaSKarl Rupp     *len = 0;
16358865f1eaSKarl Rupp   }
16368865f1eaSKarl Rupp   if (namelist) {
16378865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16388865f1eaSKarl Rupp     *namelist = 0;
16398865f1eaSKarl Rupp   }
16408865f1eaSKarl Rupp   if (islist) {
16418865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16428865f1eaSKarl Rupp     *islist = 0;
16438865f1eaSKarl Rupp   }
16448865f1eaSKarl Rupp   if (dmlist) {
16458865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16468865f1eaSKarl Rupp     *dmlist = 0;
16478865f1eaSKarl Rupp   }
1648f3f0edfdSDmitry Karpeev   /*
1649f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1650f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1651f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1652f3f0edfdSDmitry Karpeev    */
1653ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
165416621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1655435a35e8SMatthew G Knepley     PetscSection section;
1656435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1657435a35e8SMatthew G Knepley 
165892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1659435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1660435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1661f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
166203dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
166303dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
166403dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1665435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1666435a35e8SMatthew G Knepley         const char *fieldName;
1667435a35e8SMatthew G Knepley 
166803dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
166903dc3394SMatthew G. Knepley         if (namelist) {
1670435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1671435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1672435a35e8SMatthew G Knepley         }
167303dc3394SMatthew G. Knepley       }
1674435a35e8SMatthew G Knepley     } else {
167569ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1676e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16770298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1678e7c4fc90SDmitry Karpeev     }
16798865f1eaSKarl Rupp   } else {
168016621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
168116621825SDmitry Karpeev   }
168216621825SDmitry Karpeev   PetscFunctionReturn(0);
168316621825SDmitry Karpeev }
168416621825SDmitry Karpeev 
1685564cec59SMatthew G. Knepley /*@
1686435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1687435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1688435a35e8SMatthew G Knepley 
1689435a35e8SMatthew G Knepley   Not collective
1690435a35e8SMatthew G Knepley 
1691435a35e8SMatthew G Knepley   Input Parameters:
16922adcc780SMatthew G. Knepley + dm        - The DM object
16932adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16942adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1695435a35e8SMatthew G Knepley 
1696435a35e8SMatthew G Knepley   Output Parameters:
16972adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16982adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1699435a35e8SMatthew G Knepley 
17005d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17015d3b26e6SMatthew G. Knepley 
1702435a35e8SMatthew G Knepley   Level: intermediate
1703435a35e8SMatthew G Knepley 
17045d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1705435a35e8SMatthew G Knepley @*/
170637bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1707435a35e8SMatthew G Knepley {
1708435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1709435a35e8SMatthew G Knepley 
1710435a35e8SMatthew G Knepley   PetscFunctionBegin;
1711435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1712435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17138865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17148865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1715b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1716435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1717435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1718435a35e8SMatthew G Knepley }
1719435a35e8SMatthew G Knepley 
17202adcc780SMatthew G. Knepley /*@C
17212adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17222adcc780SMatthew G. Knepley 
17232adcc780SMatthew G. Knepley   Not collective
17242adcc780SMatthew G. Knepley 
17252adcc780SMatthew G. Knepley   Input Parameter:
17262adcc780SMatthew G. Knepley + dms - The DM objects
17272adcc780SMatthew G. Knepley - len - The number of DMs
17282adcc780SMatthew G. Knepley 
17292adcc780SMatthew G. Knepley   Output Parameters:
1730a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17312adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17322adcc780SMatthew G. Knepley 
17335d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17345d3b26e6SMatthew G. Knepley 
17352adcc780SMatthew G. Knepley   Level: intermediate
17362adcc780SMatthew G. Knepley 
17375d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17382adcc780SMatthew G. Knepley @*/
17392adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17402adcc780SMatthew G. Knepley {
17412adcc780SMatthew G. Knepley   PetscInt       i;
17422adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17432adcc780SMatthew G. Knepley 
17442adcc780SMatthew G. Knepley   PetscFunctionBegin;
17452adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17462adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17472adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1748a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17492adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17502adcc780SMatthew G. Knepley   if (len) {
1751b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1752b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1753b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
17542adcc780SMatthew G. Knepley   }
17552adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17562adcc780SMatthew G. Knepley }
17572adcc780SMatthew G. Knepley 
175816621825SDmitry Karpeev 
175916621825SDmitry Karpeev /*@C
17608d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17618d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17628d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17638d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17648d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
176516621825SDmitry Karpeev 
176616621825SDmitry Karpeev   Not collective
176716621825SDmitry Karpeev 
176816621825SDmitry Karpeev   Input Parameter:
176916621825SDmitry Karpeev . dm - the DM object
177016621825SDmitry Karpeev 
177116621825SDmitry Karpeev   Output Parameters:
17720298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17730298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17740298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17750298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17760298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
177716621825SDmitry Karpeev 
177816621825SDmitry Karpeev   Level: intermediate
177916621825SDmitry Karpeev 
178016621825SDmitry Karpeev   Notes:
178116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
178216621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
178316621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
178416621825SDmitry Karpeev 
17858d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
178616621825SDmitry Karpeev @*/
17878d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
178816621825SDmitry Karpeev {
178916621825SDmitry Karpeev   PetscErrorCode      ierr;
1790be081cd6SPeter Brune   DMSubDomainHookLink link;
1791be081cd6SPeter Brune   PetscInt            i,l;
179216621825SDmitry Karpeev 
179316621825SDmitry Karpeev   PetscFunctionBegin;
179416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
179514a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17960298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17970298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17980298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17990298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1800f3f0edfdSDmitry Karpeev   /*
1801f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1802f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1803f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1804f3f0edfdSDmitry Karpeev    */
1805ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
180616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1807be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
180814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1809f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1810be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1811be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1812be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1813be081cd6SPeter Brune         }
1814648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1815e7c4fc90SDmitry Karpeev       }
181614a18fd3SPeter Brune     }
181714a18fd3SPeter Brune     if (len) *len = l;
181814a18fd3SPeter Brune   }
1819e30e807fSPeter Brune   PetscFunctionReturn(0);
1820e30e807fSPeter Brune }
1821e30e807fSPeter Brune 
1822e30e807fSPeter Brune 
1823e30e807fSPeter Brune /*@C
1824e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1825e30e807fSPeter Brune 
1826e30e807fSPeter Brune   Not collective
1827e30e807fSPeter Brune 
1828e30e807fSPeter Brune   Input Parameters:
1829e30e807fSPeter Brune + dm - the DM object
1830e30e807fSPeter Brune . n  - the number of subdomain scatters
1831e30e807fSPeter Brune - subdms - the local subdomains
1832e30e807fSPeter Brune 
1833e30e807fSPeter Brune   Output Parameters:
1834e30e807fSPeter Brune + n     - the number of scatters returned
1835e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1836e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1837e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1838e30e807fSPeter Brune 
183995452b02SPatrick Sanan   Notes:
184095452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1841e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1842e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1843e30e807fSPeter Brune   solution and residual data.
1844e30e807fSPeter Brune 
1845e30e807fSPeter Brune   Level: developer
1846e30e807fSPeter Brune 
1847e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1848e30e807fSPeter Brune @*/
1849e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1850e30e807fSPeter Brune {
1851e30e807fSPeter Brune   PetscErrorCode ierr;
1852e30e807fSPeter Brune 
1853e30e807fSPeter Brune   PetscFunctionBegin;
1854e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1855e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1856b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1857e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1858e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1859e7c4fc90SDmitry Karpeev }
1860e7c4fc90SDmitry Karpeev 
186147c6ae99SBarry Smith /*@
186247c6ae99SBarry Smith   DMRefine - Refines a DM object
186347c6ae99SBarry Smith 
1864d083f849SBarry Smith   Collective on dm
186547c6ae99SBarry Smith 
186647c6ae99SBarry Smith   Input Parameter:
186747c6ae99SBarry Smith + dm   - the DM object
186891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
186947c6ae99SBarry Smith 
187047c6ae99SBarry Smith   Output Parameter:
18710298fd71SBarry Smith . dmf - the refined DM, or NULL
1872ae0a1c52SMatthew G Knepley 
18730298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
187447c6ae99SBarry Smith 
187547c6ae99SBarry Smith   Level: developer
187647c6ae99SBarry Smith 
1877e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
187847c6ae99SBarry Smith @*/
18797087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
188047c6ae99SBarry Smith {
188147c6ae99SBarry Smith   PetscErrorCode   ierr;
1882c833c3b5SJed Brown   DMRefineHookLink link;
188347c6ae99SBarry Smith 
188447c6ae99SBarry Smith   PetscFunctionBegin;
1885732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1886b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
18871ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
188847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18894057135bSMatthew G Knepley   if (*dmf) {
189043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18918865f1eaSKarl Rupp 
18928cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18938865f1eaSKarl Rupp 
1894644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18950598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1896656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18978865f1eaSKarl Rupp 
1898e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1899c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
19008865f1eaSKarl Rupp       if (link->refinehook) {
19018865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19028865f1eaSKarl Rupp       }
1903c833c3b5SJed Brown     }
1904c833c3b5SJed Brown   }
19051ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1906c833c3b5SJed Brown   PetscFunctionReturn(0);
1907c833c3b5SJed Brown }
1908c833c3b5SJed Brown 
1909bb9467b5SJed Brown /*@C
1910c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1911c833c3b5SJed Brown 
1912c833c3b5SJed Brown    Logically Collective
1913c833c3b5SJed Brown 
1914c833c3b5SJed Brown    Input Arguments:
1915c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1916c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1917c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19180298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1919c833c3b5SJed Brown 
1920c833c3b5SJed Brown    Calling sequence of refinehook:
1921c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1922c833c3b5SJed Brown 
1923c833c3b5SJed Brown +  coarse - coarse level DM
1924c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1925c833c3b5SJed Brown -  ctx - optional user-defined function context
1926c833c3b5SJed Brown 
1927c833c3b5SJed Brown    Calling sequence for interphook:
1928c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1929c833c3b5SJed Brown 
1930c833c3b5SJed Brown +  coarse - coarse level DM
1931c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1932c833c3b5SJed Brown .  fine - fine level DM to update
1933c833c3b5SJed Brown -  ctx - optional user-defined function context
1934c833c3b5SJed Brown 
1935c833c3b5SJed Brown    Level: advanced
1936c833c3b5SJed Brown 
1937c833c3b5SJed Brown    Notes:
1938c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1939c833c3b5SJed Brown 
1940c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1941c833c3b5SJed Brown 
1942bb9467b5SJed Brown    This function is currently not available from Fortran.
1943bb9467b5SJed Brown 
1944c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1945c833c3b5SJed Brown @*/
1946c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1947c833c3b5SJed Brown {
1948c833c3b5SJed Brown   PetscErrorCode   ierr;
1949c833c3b5SJed Brown   DMRefineHookLink link,*p;
1950c833c3b5SJed Brown 
1951c833c3b5SJed Brown   PetscFunctionBegin;
1952c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19533d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19543d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19553d8e3701SJed Brown   }
195695dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1957c833c3b5SJed Brown   link->refinehook = refinehook;
1958c833c3b5SJed Brown   link->interphook = interphook;
1959c833c3b5SJed Brown   link->ctx        = ctx;
19600298fd71SBarry Smith   link->next       = NULL;
1961c833c3b5SJed Brown   *p               = link;
1962c833c3b5SJed Brown   PetscFunctionReturn(0);
1963c833c3b5SJed Brown }
1964c833c3b5SJed Brown 
19653d8e3701SJed Brown /*@C
19663d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19673d8e3701SJed Brown 
19683d8e3701SJed Brown    Logically Collective
19693d8e3701SJed Brown 
19703d8e3701SJed Brown    Input Arguments:
19713d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19723d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19733d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19743d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19753d8e3701SJed Brown 
19763d8e3701SJed Brown    Level: advanced
19773d8e3701SJed Brown 
19783d8e3701SJed Brown    Notes:
19793d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19803d8e3701SJed Brown 
19813d8e3701SJed Brown    This function is currently not available from Fortran.
19823d8e3701SJed Brown 
19833d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19843d8e3701SJed Brown @*/
19853d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19863d8e3701SJed Brown {
19873d8e3701SJed Brown   PetscErrorCode   ierr;
19883d8e3701SJed Brown   DMRefineHookLink link,*p;
19893d8e3701SJed Brown 
19903d8e3701SJed Brown   PetscFunctionBegin;
19913d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19923d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19933d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19943d8e3701SJed Brown       link = *p;
19953d8e3701SJed Brown       *p = link->next;
19963d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19973d8e3701SJed Brown       break;
19983d8e3701SJed Brown     }
19993d8e3701SJed Brown   }
20003d8e3701SJed Brown   PetscFunctionReturn(0);
20013d8e3701SJed Brown }
20023d8e3701SJed Brown 
2003c833c3b5SJed Brown /*@
2004c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2005c833c3b5SJed Brown 
2006c833c3b5SJed Brown    Collective if any hooks are
2007c833c3b5SJed Brown 
2008c833c3b5SJed Brown    Input Arguments:
2009c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2010e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2011c833c3b5SJed Brown -  fine - finer DM to update
2012c833c3b5SJed Brown 
2013c833c3b5SJed Brown    Level: developer
2014c833c3b5SJed Brown 
2015c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2016c833c3b5SJed Brown @*/
2017c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2018c833c3b5SJed Brown {
2019c833c3b5SJed Brown   PetscErrorCode   ierr;
2020c833c3b5SJed Brown   DMRefineHookLink link;
2021c833c3b5SJed Brown 
2022c833c3b5SJed Brown   PetscFunctionBegin;
2023c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20248865f1eaSKarl Rupp     if (link->interphook) {
20258865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20268865f1eaSKarl Rupp     }
20274057135bSMatthew G Knepley   }
202847c6ae99SBarry Smith   PetscFunctionReturn(0);
202947c6ae99SBarry Smith }
203047c6ae99SBarry Smith 
2031eb3f98d2SBarry Smith /*@
2032eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2033eb3f98d2SBarry Smith 
2034eb3f98d2SBarry Smith     Not Collective
2035eb3f98d2SBarry Smith 
2036eb3f98d2SBarry Smith     Input Parameter:
2037eb3f98d2SBarry Smith .   dm - the DM object
2038eb3f98d2SBarry Smith 
2039eb3f98d2SBarry Smith     Output Parameter:
2040eb3f98d2SBarry Smith .   level - number of refinements
2041eb3f98d2SBarry Smith 
2042eb3f98d2SBarry Smith     Level: developer
2043eb3f98d2SBarry Smith 
20446a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2045eb3f98d2SBarry Smith 
2046eb3f98d2SBarry Smith @*/
2047eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2048eb3f98d2SBarry Smith {
2049eb3f98d2SBarry Smith   PetscFunctionBegin;
2050eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2051eb3f98d2SBarry Smith   *level = dm->levelup;
2052eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2053eb3f98d2SBarry Smith }
2054eb3f98d2SBarry Smith 
2055fef3a512SBarry Smith /*@
2056fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2057fef3a512SBarry Smith 
2058fef3a512SBarry Smith     Not Collective
2059fef3a512SBarry Smith 
2060fef3a512SBarry Smith     Input Parameter:
2061fef3a512SBarry Smith +   dm - the DM object
2062fef3a512SBarry Smith -   level - number of refinements
2063fef3a512SBarry Smith 
2064fef3a512SBarry Smith     Level: advanced
2065fef3a512SBarry Smith 
206695452b02SPatrick Sanan     Notes:
206795452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2068fef3a512SBarry Smith 
2069fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2070fef3a512SBarry Smith 
2071fef3a512SBarry Smith @*/
2072fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2073fef3a512SBarry Smith {
2074fef3a512SBarry Smith   PetscFunctionBegin;
2075fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2076fef3a512SBarry Smith   dm->levelup = level;
2077fef3a512SBarry Smith   PetscFunctionReturn(0);
2078fef3a512SBarry Smith }
2079fef3a512SBarry Smith 
2080ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2081ca3d3a14SMatthew G. Knepley {
2082ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2083ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2084ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2085ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2086ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2087ca3d3a14SMatthew G. Knepley }
2088ca3d3a14SMatthew G. Knepley 
2089ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2090ca3d3a14SMatthew G. Knepley {
2091ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2092ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2093ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2094ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2095ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2096ca3d3a14SMatthew G. Knepley }
2097ca3d3a14SMatthew G. Knepley 
2098ca3d3a14SMatthew G. Knepley /*@
2099c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2100ca3d3a14SMatthew G. Knepley 
2101ca3d3a14SMatthew G. Knepley   Input Parameter:
2102ca3d3a14SMatthew G. Knepley . dm - The DM
2103ca3d3a14SMatthew G. Knepley 
2104ca3d3a14SMatthew G. Knepley   Output Parameter:
2105ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2106ca3d3a14SMatthew G. Knepley 
2107ca3d3a14SMatthew G. Knepley   Level: developer
2108ca3d3a14SMatthew G. Knepley 
2109ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2110ca3d3a14SMatthew G. Knepley @*/
2111ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2112ca3d3a14SMatthew G. Knepley {
2113ca3d3a14SMatthew G. Knepley   Vec            tv;
2114ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2115ca3d3a14SMatthew G. Knepley 
2116ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2117ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2118534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2119ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2120ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2121ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2122ca3d3a14SMatthew G. Knepley }
2123ca3d3a14SMatthew G. Knepley 
2124ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2125ca3d3a14SMatthew G. Knepley {
2126ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2127ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2128ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2129ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2130ca3d3a14SMatthew G. Knepley 
2131ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2132ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
213392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2134ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2135ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2136ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
213792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2138ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2139ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2140ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2141ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2142ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2143ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2144ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2145ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2146ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2147ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2148ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2149ca3d3a14SMatthew G. Knepley     }
2150ca3d3a14SMatthew G. Knepley   }
2151ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2152ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2153ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2154ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2155ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2156ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2157ca3d3a14SMatthew G. Knepley       if (dof) {
2158ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2159ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2160ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2161ca3d3a14SMatthew G. Knepley 
2162ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2163ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2164ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2165580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2166ca3d3a14SMatthew G. Knepley       }
2167ca3d3a14SMatthew G. Knepley     }
2168ca3d3a14SMatthew G. Knepley   }
2169ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2170ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2171ca3d3a14SMatthew G. Knepley }
2172ca3d3a14SMatthew G. Knepley 
2173ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2174ca3d3a14SMatthew G. Knepley {
2175ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2176ca3d3a14SMatthew G. Knepley 
2177ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2178ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2179ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2180ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2181ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2182ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2183ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2184ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2185ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2186ca3d3a14SMatthew G. Knepley }
2187ca3d3a14SMatthew G. Knepley 
2188bb9467b5SJed Brown /*@C
2189baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2190baf369e7SPeter Brune 
2191baf369e7SPeter Brune    Logically Collective
2192baf369e7SPeter Brune 
2193baf369e7SPeter Brune    Input Arguments:
2194baf369e7SPeter Brune +  dm - the DM
2195baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2196baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
21970298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2198baf369e7SPeter Brune 
2199baf369e7SPeter Brune    Calling sequence for beginhook:
2200baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2201baf369e7SPeter Brune 
2202baf369e7SPeter Brune +  dm - global DM
2203baf369e7SPeter Brune .  g - global vector
2204baf369e7SPeter Brune .  mode - mode
2205baf369e7SPeter Brune .  l - local vector
2206baf369e7SPeter Brune -  ctx - optional user-defined function context
2207baf369e7SPeter Brune 
2208baf369e7SPeter Brune 
2209baf369e7SPeter Brune    Calling sequence for endhook:
2210ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2211baf369e7SPeter Brune 
2212baf369e7SPeter Brune +  global - global DM
2213baf369e7SPeter Brune -  ctx - optional user-defined function context
2214baf369e7SPeter Brune 
2215baf369e7SPeter Brune    Level: advanced
2216baf369e7SPeter Brune 
2217baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2218baf369e7SPeter Brune @*/
2219baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2220baf369e7SPeter Brune {
2221baf369e7SPeter Brune   PetscErrorCode          ierr;
2222baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2223baf369e7SPeter Brune 
2224baf369e7SPeter Brune   PetscFunctionBegin;
2225baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2226baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
222795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2228baf369e7SPeter Brune   link->beginhook = beginhook;
2229baf369e7SPeter Brune   link->endhook   = endhook;
2230baf369e7SPeter Brune   link->ctx       = ctx;
22310298fd71SBarry Smith   link->next      = NULL;
2232baf369e7SPeter Brune   *p              = link;
2233baf369e7SPeter Brune   PetscFunctionReturn(0);
2234baf369e7SPeter Brune }
2235baf369e7SPeter Brune 
22364c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
22374c274da1SToby Isaac {
22384c274da1SToby Isaac   Mat cMat;
22394c274da1SToby Isaac   Vec cVec;
22404c274da1SToby Isaac   PetscSection section, cSec;
22414c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22424c274da1SToby Isaac   PetscErrorCode ierr;
22434c274da1SToby Isaac 
22444c274da1SToby Isaac   PetscFunctionBegin;
22454c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22464c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22474c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
22485db9a05bSToby Isaac     PetscInt nRows;
22495db9a05bSToby Isaac 
22505db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22515db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
225292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
22537711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22544c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
22554c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22564c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22574c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22584c274da1SToby Isaac       if (dof) {
22594c274da1SToby Isaac         PetscScalar *vals;
22604c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
22614c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
22624c274da1SToby Isaac       }
22634c274da1SToby Isaac     }
22644c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22654c274da1SToby Isaac   }
22664c274da1SToby Isaac   PetscFunctionReturn(0);
22674c274da1SToby Isaac }
22684c274da1SToby Isaac 
226947c6ae99SBarry Smith /*@
227001729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
227101729b5cSPatrick Sanan 
2272d083f849SBarry Smith     Neighbor-wise Collective on dm
227301729b5cSPatrick Sanan 
227401729b5cSPatrick Sanan     Input Parameters:
227501729b5cSPatrick Sanan +   dm - the DM object
227601729b5cSPatrick Sanan .   g - the global vector
227701729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
227801729b5cSPatrick Sanan -   l - the local vector
227901729b5cSPatrick Sanan 
228001729b5cSPatrick Sanan     Notes:
228101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
228201729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
228301729b5cSPatrick Sanan 
228401729b5cSPatrick Sanan     Level: beginner
228501729b5cSPatrick Sanan 
228601729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
228701729b5cSPatrick Sanan 
228801729b5cSPatrick Sanan @*/
228901729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
229001729b5cSPatrick Sanan {
229101729b5cSPatrick Sanan   PetscErrorCode ierr;
229201729b5cSPatrick Sanan 
229301729b5cSPatrick Sanan   PetscFunctionBegin;
229401729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
229501729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
229601729b5cSPatrick Sanan   PetscFunctionReturn(0);
229701729b5cSPatrick Sanan }
229801729b5cSPatrick Sanan 
229901729b5cSPatrick Sanan /*@
230047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
230147c6ae99SBarry Smith 
2302d083f849SBarry Smith     Neighbor-wise Collective on dm
230347c6ae99SBarry Smith 
230447c6ae99SBarry Smith     Input Parameters:
230547c6ae99SBarry Smith +   dm - the DM object
230647c6ae99SBarry Smith .   g - the global vector
230747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
230847c6ae99SBarry Smith -   l - the local vector
230947c6ae99SBarry Smith 
231001729b5cSPatrick Sanan     Level: intermediate
231147c6ae99SBarry Smith 
231201729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
231347c6ae99SBarry Smith 
231447c6ae99SBarry Smith @*/
23157087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
231647c6ae99SBarry Smith {
23177128ae9fSMatthew G Knepley   PetscSF                 sf;
231847c6ae99SBarry Smith   PetscErrorCode          ierr;
2319baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
232047c6ae99SBarry Smith 
232147c6ae99SBarry Smith   PetscFunctionBegin;
2322171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2323baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23248865f1eaSKarl Rupp     if (link->beginhook) {
23258865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23268865f1eaSKarl Rupp     }
2327baf369e7SPeter Brune   }
2328*1bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
23297128ae9fSMatthew G Knepley   if (sf) {
2330ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2331ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
23327128ae9fSMatthew G Knepley 
233382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23347128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2335ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23367128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23377128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2338ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
23397128ae9fSMatthew G Knepley   } else {
234033907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2341843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23427128ae9fSMatthew G Knepley   }
234347c6ae99SBarry Smith   PetscFunctionReturn(0);
234447c6ae99SBarry Smith }
234547c6ae99SBarry Smith 
234647c6ae99SBarry Smith /*@
234747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
234847c6ae99SBarry Smith 
2349d083f849SBarry Smith     Neighbor-wise Collective on dm
235047c6ae99SBarry Smith 
235147c6ae99SBarry Smith     Input Parameters:
235247c6ae99SBarry Smith +   dm - the DM object
235347c6ae99SBarry Smith .   g - the global vector
235447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
235547c6ae99SBarry Smith -   l - the local vector
235647c6ae99SBarry Smith 
235701729b5cSPatrick Sanan     Level: intermediate
235847c6ae99SBarry Smith 
235901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
236047c6ae99SBarry Smith 
236147c6ae99SBarry Smith @*/
23627087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
236347c6ae99SBarry Smith {
23647128ae9fSMatthew G Knepley   PetscSF                 sf;
236547c6ae99SBarry Smith   PetscErrorCode          ierr;
2366ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2367ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2368ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2369baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
237047c6ae99SBarry Smith 
237147c6ae99SBarry Smith   PetscFunctionBegin;
2372171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2373*1bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2374ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
23757128ae9fSMatthew G Knepley   if (sf) {
237682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23777128ae9fSMatthew G Knepley 
23787128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2379ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23807128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23817128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2382ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2383ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
23847128ae9fSMatthew G Knepley   } else {
238533907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2386843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23877128ae9fSMatthew G Knepley   }
23884c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2389baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2390baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2391baf369e7SPeter Brune   }
239247c6ae99SBarry Smith   PetscFunctionReturn(0);
239347c6ae99SBarry Smith }
239447c6ae99SBarry Smith 
2395d4d07f1eSToby Isaac /*@C
2396d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2397d4d07f1eSToby Isaac 
2398d4d07f1eSToby Isaac    Logically Collective
2399d4d07f1eSToby Isaac 
2400d4d07f1eSToby Isaac    Input Arguments:
2401d4d07f1eSToby Isaac +  dm - the DM
2402d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2403d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2404d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2405d4d07f1eSToby Isaac 
2406d4d07f1eSToby Isaac    Calling sequence for beginhook:
2407d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2408d4d07f1eSToby Isaac 
2409d4d07f1eSToby Isaac +  dm - global DM
2410d4d07f1eSToby Isaac .  l - local vector
2411d4d07f1eSToby Isaac .  mode - mode
2412d4d07f1eSToby Isaac .  g - global vector
2413d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2414d4d07f1eSToby Isaac 
2415d4d07f1eSToby Isaac 
2416d4d07f1eSToby Isaac    Calling sequence for endhook:
2417d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2418d4d07f1eSToby Isaac 
2419d4d07f1eSToby Isaac +  global - global DM
2420d4d07f1eSToby Isaac .  l - local vector
2421d4d07f1eSToby Isaac .  mode - mode
2422d4d07f1eSToby Isaac .  g - global vector
2423d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2424d4d07f1eSToby Isaac 
2425d4d07f1eSToby Isaac    Level: advanced
2426d4d07f1eSToby Isaac 
2427d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2428d4d07f1eSToby Isaac @*/
2429d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2430d4d07f1eSToby Isaac {
2431d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2432d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2433d4d07f1eSToby Isaac 
2434d4d07f1eSToby Isaac   PetscFunctionBegin;
2435d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2436d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
243795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2438d4d07f1eSToby Isaac   link->beginhook = beginhook;
2439d4d07f1eSToby Isaac   link->endhook   = endhook;
2440d4d07f1eSToby Isaac   link->ctx       = ctx;
2441d4d07f1eSToby Isaac   link->next      = NULL;
2442d4d07f1eSToby Isaac   *p              = link;
2443d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2444d4d07f1eSToby Isaac }
2445d4d07f1eSToby Isaac 
24464c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
24474c274da1SToby Isaac {
24484c274da1SToby Isaac   Mat cMat;
24494c274da1SToby Isaac   Vec cVec;
24504c274da1SToby Isaac   PetscSection section, cSec;
24514c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24524c274da1SToby Isaac   PetscErrorCode ierr;
24534c274da1SToby Isaac 
24544c274da1SToby Isaac   PetscFunctionBegin;
24554c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24564c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24574c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
24585db9a05bSToby Isaac     PetscInt nRows;
24595db9a05bSToby Isaac 
24605db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24615db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
246292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
24637711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24644c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24654c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24664c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24674c274da1SToby Isaac       if (dof) {
24684c274da1SToby Isaac         PetscInt d;
24694c274da1SToby Isaac         PetscScalar *vals;
24704c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
24714c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
24724c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
24734c274da1SToby Isaac          * we just extracted */
24744c274da1SToby Isaac         for (d = 0; d < dof; d++) {
24754c274da1SToby Isaac           vals[d] = 0.;
24764c274da1SToby Isaac         }
24774c274da1SToby Isaac       }
24784c274da1SToby Isaac     }
24794c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
24804c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24814c274da1SToby Isaac   }
24824c274da1SToby Isaac   PetscFunctionReturn(0);
24834c274da1SToby Isaac }
248401729b5cSPatrick Sanan /*@
248501729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
248601729b5cSPatrick Sanan 
2487d083f849SBarry Smith     Neighbor-wise Collective on dm
248801729b5cSPatrick Sanan 
248901729b5cSPatrick Sanan     Input Parameters:
249001729b5cSPatrick Sanan +   dm - the DM object
249101729b5cSPatrick Sanan .   l - the local vector
249201729b5cSPatrick 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.
249301729b5cSPatrick Sanan -   g - the global vector
249401729b5cSPatrick Sanan 
249501729b5cSPatrick Sanan     Notes:
249601729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
249701729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
249801729b5cSPatrick Sanan 
249901729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
250001729b5cSPatrick 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.
250101729b5cSPatrick Sanan 
250201729b5cSPatrick Sanan     Level: beginner
250301729b5cSPatrick Sanan 
250401729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
250501729b5cSPatrick Sanan 
250601729b5cSPatrick Sanan @*/
250701729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
250801729b5cSPatrick Sanan {
250901729b5cSPatrick Sanan   PetscErrorCode ierr;
251001729b5cSPatrick Sanan 
251101729b5cSPatrick Sanan   PetscFunctionBegin;
251201729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
251301729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
251401729b5cSPatrick Sanan   PetscFunctionReturn(0);
251501729b5cSPatrick Sanan }
25164c274da1SToby Isaac 
251747c6ae99SBarry Smith /*@
251801729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25199a42bb27SBarry Smith 
2520d083f849SBarry Smith     Neighbor-wise Collective on dm
25219a42bb27SBarry Smith 
25229a42bb27SBarry Smith     Input Parameters:
25239a42bb27SBarry Smith +   dm - the DM object
2524f6813fd5SJed Brown .   l - the local vector
25251eb28f2eSBarry 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.
25261eb28f2eSBarry Smith -   g - the global vector
25279a42bb27SBarry Smith 
252895452b02SPatrick Sanan     Notes:
252995452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
253084330215SMatthew 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.
25319a42bb27SBarry Smith 
253201729b5cSPatrick Sanan     Level: intermediate
25339a42bb27SBarry Smith 
253401729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
25359a42bb27SBarry Smith 
25369a42bb27SBarry Smith @*/
25377087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
25389a42bb27SBarry Smith {
25397128ae9fSMatthew G Knepley   PetscSF                 sf;
254084330215SMatthew G. Knepley   PetscSection            s, gs;
2541d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2542ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2543ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2544ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2545ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
254684330215SMatthew G. Knepley   PetscErrorCode          ierr;
25479a42bb27SBarry Smith 
25489a42bb27SBarry Smith   PetscFunctionBegin;
2549171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2550d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2551d4d07f1eSToby Isaac     if (link->beginhook) {
2552d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2553d4d07f1eSToby Isaac     }
2554d4d07f1eSToby Isaac   }
25554c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
2556*1bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
255792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
25587128ae9fSMatthew G Knepley   switch (mode) {
25597128ae9fSMatthew G Knepley   case INSERT_VALUES:
25607128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2561304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
256284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25637128ae9fSMatthew G Knepley   case ADD_VALUES:
25647128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2565304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
256684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25677128ae9fSMatthew G Knepley   default:
256882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25697128ae9fSMatthew G Knepley   }
2570ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2571ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2572ca3d3a14SMatthew G. Knepley     if (transform) {
2573ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2574ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2575ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2576ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2577ca3d3a14SMatthew G. Knepley     } else {
2578ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2579ca3d3a14SMatthew G. Knepley     }
25807128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2581ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2582a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
258384330215SMatthew G. Knepley     } else if (s && isInsert) {
258484330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
258584330215SMatthew G. Knepley 
2586e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
258784330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
258884330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
258984330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2590b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
259184330215SMatthew G. Knepley 
259284330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
259303442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
259484330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2595b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
259684330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
259784330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2598b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
259903442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2600b3b16f48SMatthew 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);
2601b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2602b3b16f48SMatthew G. Knepley         if (!gcdof) {
260384330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2604b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2605b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
260684330215SMatthew G. Knepley           const PetscInt *cdofs;
260784330215SMatthew G. Knepley           PetscInt        cind = 0;
260884330215SMatthew G. Knepley 
260984330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2610b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
261184330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2612b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
261384330215SMatthew G. Knepley           }
2614b3b16f48SMatthew 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);
261584330215SMatthew G. Knepley       }
2616ca3d3a14SMatthew G. Knepley     }
26177128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2618ca3d3a14SMatthew G. Knepley     if (transform) {
2619ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2620ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2621ca3d3a14SMatthew G. Knepley     } else {
2622ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2623ca3d3a14SMatthew G. Knepley     }
26247128ae9fSMatthew G Knepley   } else {
2625b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2626843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26277128ae9fSMatthew G Knepley   }
26289a42bb27SBarry Smith   PetscFunctionReturn(0);
26299a42bb27SBarry Smith }
26309a42bb27SBarry Smith 
26319a42bb27SBarry Smith /*@
26329a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
263347c6ae99SBarry Smith 
2634d083f849SBarry Smith     Neighbor-wise Collective on dm
263547c6ae99SBarry Smith 
263647c6ae99SBarry Smith     Input Parameters:
263747c6ae99SBarry Smith +   dm - the DM object
2638f6813fd5SJed Brown .   l - the local vector
263947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2640f6813fd5SJed Brown -   g - the global vector
264147c6ae99SBarry Smith 
264201729b5cSPatrick Sanan     Level: intermediate
264347c6ae99SBarry Smith 
2644e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
264547c6ae99SBarry Smith 
264647c6ae99SBarry Smith @*/
26477087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
264847c6ae99SBarry Smith {
26497128ae9fSMatthew G Knepley   PetscSF                 sf;
265084330215SMatthew G. Knepley   PetscSection            s;
2651d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2652ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
265384330215SMatthew G. Knepley   PetscErrorCode          ierr;
265447c6ae99SBarry Smith 
265547c6ae99SBarry Smith   PetscFunctionBegin;
2656171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2657*1bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
265892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26597128ae9fSMatthew G Knepley   switch (mode) {
26607128ae9fSMatthew G Knepley   case INSERT_VALUES:
26617128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
266284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26637128ae9fSMatthew G Knepley   case ADD_VALUES:
26647128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
266584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26667128ae9fSMatthew G Knepley   default:
266782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26687128ae9fSMatthew G Knepley   }
266984330215SMatthew G. Knepley   if (sf && !isInsert) {
2670ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2671ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2672ca3d3a14SMatthew G. Knepley     Vec                tmpl;
267384330215SMatthew G. Knepley 
2674ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2675ca3d3a14SMatthew G. Knepley     if (transform) {
2676ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2677ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2678ca3d3a14SMatthew G. Knepley     } else {
2679ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2680ca3d3a14SMatthew G. Knepley     }
26817128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2682a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2683ca3d3a14SMatthew G. Knepley     if (transform) {
2684ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2685ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2686ca3d3a14SMatthew G. Knepley     } else {
2687ae5cfb4aSMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2688ca3d3a14SMatthew G. Knepley     }
26897128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
269084330215SMatthew G. Knepley   } else if (s && isInsert) {
26917128ae9fSMatthew G Knepley   } else {
2692b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2693843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26947128ae9fSMatthew G Knepley   }
2695d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2696d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2697d4d07f1eSToby Isaac   }
269847c6ae99SBarry Smith   PetscFunctionReturn(0);
269947c6ae99SBarry Smith }
270047c6ae99SBarry Smith 
2701f089877aSRichard Tran Mills /*@
2702bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2703bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2704d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2705f089877aSRichard Tran Mills 
2706d083f849SBarry Smith    Neighbor-wise Collective on dm
2707f089877aSRichard Tran Mills 
2708f089877aSRichard Tran Mills    Input Parameters:
2709f089877aSRichard Tran Mills +  dm - the DM object
2710bc0a1609SRichard Tran Mills .  g - the original local vector
2711bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2712f089877aSRichard Tran Mills 
2713bc0a1609SRichard Tran Mills    Output Parameter:
2714bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2715f089877aSRichard Tran Mills 
2716f089877aSRichard Tran Mills    Level: intermediate
2717f089877aSRichard Tran Mills 
2718bc0a1609SRichard Tran Mills    Notes:
2719bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2720bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2721bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2722bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2723bc0a1609SRichard Tran Mills 
2724bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2725f089877aSRichard Tran Mills 
2726f089877aSRichard Tran Mills @*/
2727f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2728f089877aSRichard Tran Mills {
2729f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2730f089877aSRichard Tran Mills 
2731f089877aSRichard Tran Mills   PetscFunctionBegin;
2732f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2733bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2734f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2735f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2736f089877aSRichard Tran Mills }
2737f089877aSRichard Tran Mills 
2738f089877aSRichard Tran Mills /*@
2739bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2740bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2741d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2742f089877aSRichard Tran Mills 
2743d083f849SBarry Smith    Neighbor-wise Collective on dm
2744f089877aSRichard Tran Mills 
2745f089877aSRichard Tran Mills    Input Parameters:
2746bc0a1609SRichard Tran Mills +  da - the DM object
2747bc0a1609SRichard Tran Mills .  g - the original local vector
2748bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2749f089877aSRichard Tran Mills 
2750bc0a1609SRichard Tran Mills    Output Parameter:
2751bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2752f089877aSRichard Tran Mills 
2753f089877aSRichard Tran Mills    Level: intermediate
2754f089877aSRichard Tran Mills 
2755bc0a1609SRichard Tran Mills    Notes:
2756bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2757bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2758bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2759bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2760bc0a1609SRichard Tran Mills 
2761bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2762f089877aSRichard Tran Mills 
2763f089877aSRichard Tran Mills @*/
2764f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2765f089877aSRichard Tran Mills {
2766f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2767f089877aSRichard Tran Mills 
2768f089877aSRichard Tran Mills   PetscFunctionBegin;
2769f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2770bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2771f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2772f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2773f089877aSRichard Tran Mills }
2774f089877aSRichard Tran Mills 
2775f089877aSRichard Tran Mills 
277647c6ae99SBarry Smith /*@
277747c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
277847c6ae99SBarry Smith 
2779d083f849SBarry Smith     Collective on dm
278047c6ae99SBarry Smith 
278147c6ae99SBarry Smith     Input Parameter:
278247c6ae99SBarry Smith +   dm - the DM object
278391d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
278447c6ae99SBarry Smith 
278547c6ae99SBarry Smith     Output Parameter:
278647c6ae99SBarry Smith .   dmc - the coarsened DM
278747c6ae99SBarry Smith 
278847c6ae99SBarry Smith     Level: developer
278947c6ae99SBarry Smith 
2790e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
279147c6ae99SBarry Smith 
279247c6ae99SBarry Smith @*/
27937087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
279447c6ae99SBarry Smith {
279547c6ae99SBarry Smith   PetscErrorCode    ierr;
2796b17ce1afSJed Brown   DMCoarsenHookLink link;
279747c6ae99SBarry Smith 
279847c6ae99SBarry Smith   PetscFunctionBegin;
2799171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2800b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
280147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
280247c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2803b9d85ea2SLisandro Dalcin   if (*dmc) {
2804a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
280543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28068cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2807644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
28080598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2809656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2810e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2811b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2812b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2813b17ce1afSJed Brown     }
2814b9d85ea2SLisandro Dalcin   }
281547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2816b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2817b17ce1afSJed Brown   PetscFunctionReturn(0);
2818b17ce1afSJed Brown }
2819b17ce1afSJed Brown 
2820bb9467b5SJed Brown /*@C
2821b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2822b17ce1afSJed Brown 
2823b17ce1afSJed Brown    Logically Collective
2824b17ce1afSJed Brown 
2825b17ce1afSJed Brown    Input Arguments:
2826b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2827b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2828b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
28290298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2830b17ce1afSJed Brown 
2831b17ce1afSJed Brown    Calling sequence of coarsenhook:
2832b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2833b17ce1afSJed Brown 
2834b17ce1afSJed Brown +  fine - fine level DM
2835b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2836b17ce1afSJed Brown -  ctx - optional user-defined function context
2837b17ce1afSJed Brown 
2838b17ce1afSJed Brown    Calling sequence for restricthook:
2839c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2840b17ce1afSJed Brown 
2841b17ce1afSJed Brown +  fine - fine level DM
2842b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2843c833c3b5SJed Brown .  rscale - scaling vector for restriction
2844c833c3b5SJed Brown .  inject - matrix restricting by injection
2845b17ce1afSJed Brown .  coarse - coarse level DM to update
2846b17ce1afSJed Brown -  ctx - optional user-defined function context
2847b17ce1afSJed Brown 
2848b17ce1afSJed Brown    Level: advanced
2849b17ce1afSJed Brown 
2850b17ce1afSJed Brown    Notes:
2851b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2852b17ce1afSJed Brown 
2853b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2854b17ce1afSJed Brown 
2855b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2856b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2857b17ce1afSJed Brown 
2858bb9467b5SJed Brown    This function is currently not available from Fortran.
2859bb9467b5SJed Brown 
2860dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2861b17ce1afSJed Brown @*/
2862b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2863b17ce1afSJed Brown {
2864b17ce1afSJed Brown   PetscErrorCode    ierr;
2865b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2866b17ce1afSJed Brown 
2867b17ce1afSJed Brown   PetscFunctionBegin;
2868b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
28691e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
28701e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
28711e3d8eccSJed Brown   }
287295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2873b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2874b17ce1afSJed Brown   link->restricthook = restricthook;
2875b17ce1afSJed Brown   link->ctx          = ctx;
28760298fd71SBarry Smith   link->next         = NULL;
2877b17ce1afSJed Brown   *p                 = link;
2878b17ce1afSJed Brown   PetscFunctionReturn(0);
2879b17ce1afSJed Brown }
2880b17ce1afSJed Brown 
2881dc822a44SJed Brown /*@C
2882dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2883dc822a44SJed Brown 
2884dc822a44SJed Brown    Logically Collective
2885dc822a44SJed Brown 
2886dc822a44SJed Brown    Input Arguments:
2887dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2888dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2889dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2890dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2891dc822a44SJed Brown 
2892dc822a44SJed Brown    Level: advanced
2893dc822a44SJed Brown 
2894dc822a44SJed Brown    Notes:
2895dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2896dc822a44SJed Brown 
2897dc822a44SJed Brown    This function is currently not available from Fortran.
2898dc822a44SJed Brown 
2899dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2900dc822a44SJed Brown @*/
2901dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2902dc822a44SJed Brown {
2903dc822a44SJed Brown   PetscErrorCode    ierr;
2904dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2905dc822a44SJed Brown 
2906dc822a44SJed Brown   PetscFunctionBegin;
2907dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2908dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2909dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2910dc822a44SJed Brown       link = *p;
2911dc822a44SJed Brown       *p = link->next;
2912dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2913dc822a44SJed Brown       break;
2914dc822a44SJed Brown     }
2915dc822a44SJed Brown   }
2916dc822a44SJed Brown   PetscFunctionReturn(0);
2917dc822a44SJed Brown }
2918dc822a44SJed Brown 
2919dc822a44SJed Brown 
2920b17ce1afSJed Brown /*@
2921b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2922b17ce1afSJed Brown 
2923b17ce1afSJed Brown    Collective if any hooks are
2924b17ce1afSJed Brown 
2925b17ce1afSJed Brown    Input Arguments:
2926b17ce1afSJed Brown +  fine - finer DM to use as a base
2927b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2928e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2929b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2930e91eccc2SStefano Zampini -  coarse - coarser DM to update
2931b17ce1afSJed Brown 
2932b17ce1afSJed Brown    Level: developer
2933b17ce1afSJed Brown 
2934b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2935b17ce1afSJed Brown @*/
2936b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2937b17ce1afSJed Brown {
2938b17ce1afSJed Brown   PetscErrorCode    ierr;
2939b17ce1afSJed Brown   DMCoarsenHookLink link;
2940b17ce1afSJed Brown 
2941b17ce1afSJed Brown   PetscFunctionBegin;
2942b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
29438865f1eaSKarl Rupp     if (link->restricthook) {
29448865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
29458865f1eaSKarl Rupp     }
2946b17ce1afSJed Brown   }
294747c6ae99SBarry Smith   PetscFunctionReturn(0);
294847c6ae99SBarry Smith }
294947c6ae99SBarry Smith 
2950bb9467b5SJed Brown /*@C
2951be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
29525dbd56e3SPeter Brune 
2953d083f849SBarry Smith    Logically Collective on global
29545dbd56e3SPeter Brune 
29555dbd56e3SPeter Brune    Input Arguments:
29565dbd56e3SPeter Brune +  global - global DM
2957ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
29585dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
29590298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
29605dbd56e3SPeter Brune 
2961ec4806b8SPeter Brune 
2962ec4806b8SPeter Brune    Calling sequence for ddhook:
2963ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2964ec4806b8SPeter Brune 
2965ec4806b8SPeter Brune +  global - global DM
2966ec4806b8SPeter Brune .  block  - block DM
2967ec4806b8SPeter Brune -  ctx - optional user-defined function context
2968ec4806b8SPeter Brune 
29695dbd56e3SPeter Brune    Calling sequence for restricthook:
2970ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
29715dbd56e3SPeter Brune 
29725dbd56e3SPeter Brune +  global - global DM
29735dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
29745dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2975ec4806b8SPeter Brune .  block  - block DM
29765dbd56e3SPeter Brune -  ctx - optional user-defined function context
29775dbd56e3SPeter Brune 
29785dbd56e3SPeter Brune    Level: advanced
29795dbd56e3SPeter Brune 
29805dbd56e3SPeter Brune    Notes:
2981ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
29825dbd56e3SPeter Brune 
29835dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
29845dbd56e3SPeter Brune 
29855dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2986ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
29875dbd56e3SPeter Brune 
2988bb9467b5SJed Brown    This function is currently not available from Fortran.
2989bb9467b5SJed Brown 
29905dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
29915dbd56e3SPeter Brune @*/
2992be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
29935dbd56e3SPeter Brune {
29945dbd56e3SPeter Brune   PetscErrorCode      ierr;
2995be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
29965dbd56e3SPeter Brune 
29975dbd56e3SPeter Brune   PetscFunctionBegin;
29985dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2999b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3000b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3001b3a6b972SJed Brown   }
300295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30035dbd56e3SPeter Brune   link->restricthook = restricthook;
3004be081cd6SPeter Brune   link->ddhook       = ddhook;
30055dbd56e3SPeter Brune   link->ctx          = ctx;
30060298fd71SBarry Smith   link->next         = NULL;
30075dbd56e3SPeter Brune   *p                 = link;
30085dbd56e3SPeter Brune   PetscFunctionReturn(0);
30095dbd56e3SPeter Brune }
30105dbd56e3SPeter Brune 
3011b3a6b972SJed Brown /*@C
3012b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3013b3a6b972SJed Brown 
3014b3a6b972SJed Brown    Logically Collective
3015b3a6b972SJed Brown 
3016b3a6b972SJed Brown    Input Arguments:
3017b3a6b972SJed Brown +  global - global DM
3018b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3019b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3020b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3021b3a6b972SJed Brown 
3022b3a6b972SJed Brown    Level: advanced
3023b3a6b972SJed Brown 
3024b3a6b972SJed Brown    Notes:
3025b3a6b972SJed Brown 
3026b3a6b972SJed Brown    This function is currently not available from Fortran.
3027b3a6b972SJed Brown 
3028b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3029b3a6b972SJed Brown @*/
3030b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3031b3a6b972SJed Brown {
3032b3a6b972SJed Brown   PetscErrorCode      ierr;
3033b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3034b3a6b972SJed Brown 
3035b3a6b972SJed Brown   PetscFunctionBegin;
3036b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3037b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3038b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3039b3a6b972SJed Brown       link = *p;
3040b3a6b972SJed Brown       *p = link->next;
3041b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3042b3a6b972SJed Brown       break;
3043b3a6b972SJed Brown     }
3044b3a6b972SJed Brown   }
3045b3a6b972SJed Brown   PetscFunctionReturn(0);
3046b3a6b972SJed Brown }
3047b3a6b972SJed Brown 
30485dbd56e3SPeter Brune /*@
3049be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
30505dbd56e3SPeter Brune 
30515dbd56e3SPeter Brune    Collective if any hooks are
30525dbd56e3SPeter Brune 
30535dbd56e3SPeter Brune    Input Arguments:
30545dbd56e3SPeter Brune +  fine - finer DM to use as a base
3055be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3056be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
30575dbd56e3SPeter Brune -  coarse - coarer DM to update
30585dbd56e3SPeter Brune 
30595dbd56e3SPeter Brune    Level: developer
30605dbd56e3SPeter Brune 
30615dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
30625dbd56e3SPeter Brune @*/
3063be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
30645dbd56e3SPeter Brune {
30655dbd56e3SPeter Brune   PetscErrorCode      ierr;
3066be081cd6SPeter Brune   DMSubDomainHookLink link;
30675dbd56e3SPeter Brune 
30685dbd56e3SPeter Brune   PetscFunctionBegin;
3069be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
30708865f1eaSKarl Rupp     if (link->restricthook) {
30718865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
30728865f1eaSKarl Rupp     }
30735dbd56e3SPeter Brune   }
30745dbd56e3SPeter Brune   PetscFunctionReturn(0);
30755dbd56e3SPeter Brune }
30765dbd56e3SPeter Brune 
30775fe1f584SPeter Brune /*@
30786a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
30795fe1f584SPeter Brune 
30805fe1f584SPeter Brune     Not Collective
30815fe1f584SPeter Brune 
30825fe1f584SPeter Brune     Input Parameter:
30835fe1f584SPeter Brune .   dm - the DM object
30845fe1f584SPeter Brune 
30855fe1f584SPeter Brune     Output Parameter:
30866a7d9d85SPeter Brune .   level - number of coarsenings
30875fe1f584SPeter Brune 
30885fe1f584SPeter Brune     Level: developer
30895fe1f584SPeter Brune 
30906a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
30915fe1f584SPeter Brune 
30925fe1f584SPeter Brune @*/
30935fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
30945fe1f584SPeter Brune {
30955fe1f584SPeter Brune   PetscFunctionBegin;
30965fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3097b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
30985fe1f584SPeter Brune   *level = dm->leveldown;
30995fe1f584SPeter Brune   PetscFunctionReturn(0);
31005fe1f584SPeter Brune }
31015fe1f584SPeter Brune 
31029a64c4a8SMatthew G. Knepley /*@
31039a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31049a64c4a8SMatthew G. Knepley 
31059a64c4a8SMatthew G. Knepley     Not Collective
31069a64c4a8SMatthew G. Knepley 
31079a64c4a8SMatthew G. Knepley     Input Parameters:
31089a64c4a8SMatthew G. Knepley +   dm - the DM object
31099a64c4a8SMatthew G. Knepley -   level - number of coarsenings
31109a64c4a8SMatthew G. Knepley 
31119a64c4a8SMatthew G. Knepley     Level: developer
31129a64c4a8SMatthew G. Knepley 
31139a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31149a64c4a8SMatthew G. Knepley @*/
31159a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
31169a64c4a8SMatthew G. Knepley {
31179a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
31189a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31199a64c4a8SMatthew G. Knepley   dm->leveldown = level;
31209a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
31219a64c4a8SMatthew G. Knepley }
31229a64c4a8SMatthew G. Knepley 
31235fe1f584SPeter Brune 
31245fe1f584SPeter Brune 
312547c6ae99SBarry Smith /*@C
312647c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
312747c6ae99SBarry Smith 
3128d083f849SBarry Smith     Collective on dm
312947c6ae99SBarry Smith 
313047c6ae99SBarry Smith     Input Parameter:
313147c6ae99SBarry Smith +   dm - the DM object
313247c6ae99SBarry Smith -   nlevels - the number of levels of refinement
313347c6ae99SBarry Smith 
313447c6ae99SBarry Smith     Output Parameter:
313547c6ae99SBarry Smith .   dmf - the refined DM hierarchy
313647c6ae99SBarry Smith 
313747c6ae99SBarry Smith     Level: developer
313847c6ae99SBarry Smith 
3139e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
314047c6ae99SBarry Smith 
314147c6ae99SBarry Smith @*/
31427087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
314347c6ae99SBarry Smith {
314447c6ae99SBarry Smith   PetscErrorCode ierr;
314547c6ae99SBarry Smith 
314647c6ae99SBarry Smith   PetscFunctionBegin;
3147171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3148ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
314947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3150b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
315147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
315247c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
315347c6ae99SBarry Smith   } else if (dm->ops->refine) {
315447c6ae99SBarry Smith     PetscInt i;
315547c6ae99SBarry Smith 
3156ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
315747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3158ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
315947c6ae99SBarry Smith     }
3160ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
316147c6ae99SBarry Smith   PetscFunctionReturn(0);
316247c6ae99SBarry Smith }
316347c6ae99SBarry Smith 
316447c6ae99SBarry Smith /*@C
316547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
316647c6ae99SBarry Smith 
3167d083f849SBarry Smith     Collective on dm
316847c6ae99SBarry Smith 
316947c6ae99SBarry Smith     Input Parameter:
317047c6ae99SBarry Smith +   dm - the DM object
317147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
317247c6ae99SBarry Smith 
317347c6ae99SBarry Smith     Output Parameter:
317447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
317547c6ae99SBarry Smith 
317647c6ae99SBarry Smith     Level: developer
317747c6ae99SBarry Smith 
3178e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
317947c6ae99SBarry Smith 
318047c6ae99SBarry Smith @*/
31817087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
318247c6ae99SBarry Smith {
318347c6ae99SBarry Smith   PetscErrorCode ierr;
318447c6ae99SBarry Smith 
318547c6ae99SBarry Smith   PetscFunctionBegin;
3186171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3187ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
318847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
318947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
319047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
319147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
319247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
319347c6ae99SBarry Smith     PetscInt i;
319447c6ae99SBarry Smith 
3195ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
319647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3197ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
319847c6ae99SBarry Smith     }
3199ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
320047c6ae99SBarry Smith   PetscFunctionReturn(0);
320147c6ae99SBarry Smith }
320247c6ae99SBarry Smith 
32031a266240SBarry Smith /*@C
32041a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32051a266240SBarry Smith 
32061a266240SBarry Smith     Not Collective
32071a266240SBarry Smith 
32081a266240SBarry Smith     Input Parameters:
32091a266240SBarry Smith +   dm - the DM object
32101a266240SBarry Smith -   destroy - the destroy function
32111a266240SBarry Smith 
32121a266240SBarry Smith     Level: intermediate
32131a266240SBarry Smith 
3214e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
32151a266240SBarry Smith 
3216f07f9ceaSJed Brown @*/
32171a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
32181a266240SBarry Smith {
32191a266240SBarry Smith   PetscFunctionBegin;
3220171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32211a266240SBarry Smith   dm->ctxdestroy = destroy;
32221a266240SBarry Smith   PetscFunctionReturn(0);
32231a266240SBarry Smith }
32241a266240SBarry Smith 
3225b07ff414SBarry Smith /*@
32261b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
322747c6ae99SBarry Smith 
322847c6ae99SBarry Smith     Not Collective
322947c6ae99SBarry Smith 
323047c6ae99SBarry Smith     Input Parameters:
323147c6ae99SBarry Smith +   dm - the DM object
323247c6ae99SBarry Smith -   ctx - the user context
323347c6ae99SBarry Smith 
323447c6ae99SBarry Smith     Level: intermediate
323547c6ae99SBarry Smith 
3236e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
323747c6ae99SBarry Smith 
323847c6ae99SBarry Smith @*/
32391b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
324047c6ae99SBarry Smith {
324147c6ae99SBarry Smith   PetscFunctionBegin;
3242171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
324347c6ae99SBarry Smith   dm->ctx = ctx;
324447c6ae99SBarry Smith   PetscFunctionReturn(0);
324547c6ae99SBarry Smith }
324647c6ae99SBarry Smith 
324747c6ae99SBarry Smith /*@
32481b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
324947c6ae99SBarry Smith 
325047c6ae99SBarry Smith     Not Collective
325147c6ae99SBarry Smith 
325247c6ae99SBarry Smith     Input Parameter:
325347c6ae99SBarry Smith .   dm - the DM object
325447c6ae99SBarry Smith 
325547c6ae99SBarry Smith     Output Parameter:
325647c6ae99SBarry Smith .   ctx - the user context
325747c6ae99SBarry Smith 
325847c6ae99SBarry Smith     Level: intermediate
325947c6ae99SBarry Smith 
3260e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
326147c6ae99SBarry Smith 
326247c6ae99SBarry Smith @*/
32631b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
326447c6ae99SBarry Smith {
326547c6ae99SBarry Smith   PetscFunctionBegin;
3266171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32671b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
326847c6ae99SBarry Smith   PetscFunctionReturn(0);
326947c6ae99SBarry Smith }
327047c6ae99SBarry Smith 
327108da532bSDmitry Karpeev /*@C
3272df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
327308da532bSDmitry Karpeev 
3274d083f849SBarry Smith     Logically Collective on dm
327508da532bSDmitry Karpeev 
327608da532bSDmitry Karpeev     Input Parameter:
327708da532bSDmitry Karpeev +   dm - the DM object
32780298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
327908da532bSDmitry Karpeev 
328008da532bSDmitry Karpeev     Level: intermediate
328108da532bSDmitry Karpeev 
3282835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
328308da532bSDmitry Karpeev          DMSetJacobian()
328408da532bSDmitry Karpeev 
328508da532bSDmitry Karpeev @*/
328608da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
328708da532bSDmitry Karpeev {
328808da532bSDmitry Karpeev   PetscFunctionBegin;
32895a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
329008da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
329108da532bSDmitry Karpeev   PetscFunctionReturn(0);
329208da532bSDmitry Karpeev }
329308da532bSDmitry Karpeev 
329408da532bSDmitry Karpeev /*@
329508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
329608da532bSDmitry Karpeev 
329708da532bSDmitry Karpeev     Not Collective
329808da532bSDmitry Karpeev 
329908da532bSDmitry Karpeev     Input Parameter:
330008da532bSDmitry Karpeev .   dm - the DM object to destroy
330108da532bSDmitry Karpeev 
330208da532bSDmitry Karpeev     Output Parameter:
330308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
330408da532bSDmitry Karpeev 
330508da532bSDmitry Karpeev     Level: developer
330608da532bSDmitry Karpeev 
330774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
330808da532bSDmitry Karpeev 
330908da532bSDmitry Karpeev @*/
331008da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
331108da532bSDmitry Karpeev {
331208da532bSDmitry Karpeev   PetscFunctionBegin;
33135a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3314534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
331508da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
331608da532bSDmitry Karpeev   PetscFunctionReturn(0);
331708da532bSDmitry Karpeev }
331808da532bSDmitry Karpeev 
331908da532bSDmitry Karpeev /*@C
332008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
332108da532bSDmitry Karpeev 
3322d083f849SBarry Smith     Logically Collective on dm
332308da532bSDmitry Karpeev 
332408da532bSDmitry Karpeev     Input Parameters:
3325907376e6SBarry Smith .   dm - the DM object
332608da532bSDmitry Karpeev 
332708da532bSDmitry Karpeev     Output parameters:
332808da532bSDmitry Karpeev +   xl - lower bound
332908da532bSDmitry Karpeev -   xu - upper bound
333008da532bSDmitry Karpeev 
3331907376e6SBarry Smith     Level: advanced
3332907376e6SBarry Smith 
333395452b02SPatrick Sanan     Notes:
333495452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
333508da532bSDmitry Karpeev 
333674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
333708da532bSDmitry Karpeev 
333808da532bSDmitry Karpeev @*/
333908da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
334008da532bSDmitry Karpeev {
334108da532bSDmitry Karpeev   PetscErrorCode ierr;
33425fd66863SKarl Rupp 
334308da532bSDmitry Karpeev   PetscFunctionBegin;
33445a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
334508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
33465a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3347b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
334808da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
334908da532bSDmitry Karpeev   PetscFunctionReturn(0);
335008da532bSDmitry Karpeev }
335108da532bSDmitry Karpeev 
3352b0ae01b7SPeter Brune /*@
3353b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3354b0ae01b7SPeter Brune 
3355b0ae01b7SPeter Brune     Not Collective
3356b0ae01b7SPeter Brune 
3357b0ae01b7SPeter Brune     Input Parameter:
3358b0ae01b7SPeter Brune .   dm - the DM object
3359b0ae01b7SPeter Brune 
3360b0ae01b7SPeter Brune     Output Parameter:
3361b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3362b0ae01b7SPeter Brune 
3363b0ae01b7SPeter Brune     Level: developer
3364b0ae01b7SPeter Brune 
33651565f0a7SPatrick Sanan .seealso DMCreateColoring()
3366b0ae01b7SPeter Brune 
3367b0ae01b7SPeter Brune @*/
3368b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3369b0ae01b7SPeter Brune {
3370b0ae01b7SPeter Brune   PetscFunctionBegin;
33715a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3372534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3373b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3374b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3375b0ae01b7SPeter Brune }
3376b0ae01b7SPeter Brune 
33773ad4599aSBarry Smith /*@
33783ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
33793ad4599aSBarry Smith 
33803ad4599aSBarry Smith     Not Collective
33813ad4599aSBarry Smith 
33823ad4599aSBarry Smith     Input Parameter:
33833ad4599aSBarry Smith .   dm - the DM object
33843ad4599aSBarry Smith 
33853ad4599aSBarry Smith     Output Parameter:
33863ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
33873ad4599aSBarry Smith 
33883ad4599aSBarry Smith     Level: developer
33893ad4599aSBarry Smith 
33901565f0a7SPatrick Sanan .seealso DMCreateRestriction()
33913ad4599aSBarry Smith 
33923ad4599aSBarry Smith @*/
33933ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
33943ad4599aSBarry Smith {
33953ad4599aSBarry Smith   PetscFunctionBegin;
33965a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3397534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
33983ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
33993ad4599aSBarry Smith   PetscFunctionReturn(0);
34003ad4599aSBarry Smith }
34013ad4599aSBarry Smith 
3402a7058e45SLawrence Mitchell 
3403a7058e45SLawrence Mitchell /*@
3404a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3405a7058e45SLawrence Mitchell 
3406a7058e45SLawrence Mitchell     Not Collective
3407a7058e45SLawrence Mitchell 
3408a7058e45SLawrence Mitchell     Input Parameter:
3409a7058e45SLawrence Mitchell .   dm - the DM object
3410a7058e45SLawrence Mitchell 
3411a7058e45SLawrence Mitchell     Output Parameter:
3412a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3413a7058e45SLawrence Mitchell 
3414a7058e45SLawrence Mitchell     Level: developer
3415a7058e45SLawrence Mitchell 
34161565f0a7SPatrick Sanan .seealso DMCreateInjection()
3417a7058e45SLawrence Mitchell 
3418a7058e45SLawrence Mitchell @*/
3419a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3420a7058e45SLawrence Mitchell {
34214a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
34225a84ad33SLisandro Dalcin 
3423a7058e45SLawrence Mitchell   PetscFunctionBegin;
34245a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3425534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34265a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
34274a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
34285a84ad33SLisandro Dalcin   } else {
34295a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
34305a84ad33SLisandro Dalcin   }
3431a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3432a7058e45SLawrence Mitchell }
3433a7058e45SLawrence Mitchell 
34345a84ad33SLisandro Dalcin 
3435748fac09SDmitry Karpeev /*@C
343608da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
343708da532bSDmitry Karpeev 
3438d083f849SBarry Smith     Collective on dm
343908da532bSDmitry Karpeev 
344008da532bSDmitry Karpeev     Input Parameter:
344108da532bSDmitry Karpeev +   dm - the DM object
34420298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
344308da532bSDmitry Karpeev 
344408da532bSDmitry Karpeev     Level: developer
344508da532bSDmitry Karpeev 
344674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
344708da532bSDmitry Karpeev 
344808da532bSDmitry Karpeev @*/
344908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
345008da532bSDmitry Karpeev {
345108da532bSDmitry Karpeev   PetscErrorCode ierr;
34525fd66863SKarl Rupp 
345308da532bSDmitry Karpeev   PetscFunctionBegin;
3454b9d85ea2SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
345508da532bSDmitry Karpeev   if (x) {
345608da532bSDmitry Karpeev     if (!dm->x) {
345708da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
345808da532bSDmitry Karpeev     }
345908da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
34608865f1eaSKarl Rupp   } else if (dm->x) {
346108da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
346208da532bSDmitry Karpeev   }
346308da532bSDmitry Karpeev   PetscFunctionReturn(0);
346408da532bSDmitry Karpeev }
346508da532bSDmitry Karpeev 
34660298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3467264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3468264ace61SBarry Smith 
3469264ace61SBarry Smith /*@C
3470264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3471264ace61SBarry Smith 
3472d083f849SBarry Smith   Collective on dm
3473264ace61SBarry Smith 
3474264ace61SBarry Smith   Input Parameters:
3475264ace61SBarry Smith + dm     - The DM object
3476264ace61SBarry Smith - method - The name of the DM type
3477264ace61SBarry Smith 
3478264ace61SBarry Smith   Options Database Key:
3479264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3480264ace61SBarry Smith 
3481264ace61SBarry Smith   Notes:
3482e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3483264ace61SBarry Smith 
3484264ace61SBarry Smith   Level: intermediate
3485264ace61SBarry Smith 
3486264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3487264ace61SBarry Smith @*/
348819fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3489264ace61SBarry Smith {
3490264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3491264ace61SBarry Smith   PetscBool      match;
3492264ace61SBarry Smith   PetscErrorCode ierr;
3493264ace61SBarry Smith 
3494264ace61SBarry Smith   PetscFunctionBegin;
3495264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3496251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3497264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3498264ace61SBarry Smith 
34990f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35001c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3501ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3502264ace61SBarry Smith 
3503264ace61SBarry Smith   if (dm->ops->destroy) {
3504264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3505264ace61SBarry Smith   }
3506d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3507264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3508d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3509264ace61SBarry Smith   PetscFunctionReturn(0);
3510264ace61SBarry Smith }
3511264ace61SBarry Smith 
3512264ace61SBarry Smith /*@C
3513264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3514264ace61SBarry Smith 
3515264ace61SBarry Smith   Not Collective
3516264ace61SBarry Smith 
3517264ace61SBarry Smith   Input Parameter:
3518264ace61SBarry Smith . dm  - The DM
3519264ace61SBarry Smith 
3520264ace61SBarry Smith   Output Parameter:
3521264ace61SBarry Smith . type - The DM type name
3522264ace61SBarry Smith 
3523264ace61SBarry Smith   Level: intermediate
3524264ace61SBarry Smith 
3525264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3526264ace61SBarry Smith @*/
352719fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3528264ace61SBarry Smith {
3529264ace61SBarry Smith   PetscErrorCode ierr;
3530264ace61SBarry Smith 
3531264ace61SBarry Smith   PetscFunctionBegin;
3532264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3533c959eef4SJed Brown   PetscValidPointer(type,2);
3534607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3535264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3536264ace61SBarry Smith   PetscFunctionReturn(0);
3537264ace61SBarry Smith }
3538264ace61SBarry Smith 
353967a56275SMatthew G Knepley /*@C
354067a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
354167a56275SMatthew G Knepley 
3542d083f849SBarry Smith   Collective on dm
354367a56275SMatthew G Knepley 
354467a56275SMatthew G Knepley   Input Parameters:
354567a56275SMatthew G Knepley + dm - the DM
354667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
354767a56275SMatthew G Knepley 
354867a56275SMatthew G Knepley   Output Parameter:
354967a56275SMatthew G Knepley . M - pointer to new DM
355067a56275SMatthew G Knepley 
355167a56275SMatthew G Knepley   Notes:
355267a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
355367a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
355467a56275SMatthew G Knepley   of the input DM.
355567a56275SMatthew G Knepley 
355667a56275SMatthew G Knepley   Level: intermediate
355767a56275SMatthew G Knepley 
355867a56275SMatthew G Knepley .seealso: DMCreate()
355967a56275SMatthew G Knepley @*/
356019fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
356167a56275SMatthew G Knepley {
356267a56275SMatthew G Knepley   DM             B;
356367a56275SMatthew G Knepley   char           convname[256];
3564c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
356567a56275SMatthew G Knepley   PetscErrorCode ierr;
356667a56275SMatthew G Knepley 
356767a56275SMatthew G Knepley   PetscFunctionBegin;
356867a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
356967a56275SMatthew G Knepley   PetscValidType(dm,1);
357067a56275SMatthew G Knepley   PetscValidPointer(M,3);
3571251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3572c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3573c067b6caSMatthew G. Knepley   if (sametype) {
3574c067b6caSMatthew G. Knepley     *M   = dm;
3575c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3576c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3577c067b6caSMatthew G. Knepley   } else {
35780298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
357967a56275SMatthew G Knepley 
358067a56275SMatthew G Knepley     /*
358167a56275SMatthew G Knepley        Order of precedence:
358267a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
358367a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
358467a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
358567a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
358667a56275SMatthew G Knepley        5) Use a really basic converter.
358767a56275SMatthew G Knepley     */
358867a56275SMatthew G Knepley 
358967a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3590a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3591a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3592a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3593a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3594a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
35950005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
359667a56275SMatthew G Knepley     if (conv) goto foundconv;
359767a56275SMatthew G Knepley 
359867a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
359982f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
360067a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3601a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3602a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3603a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3604a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3605a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36060005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
360767a56275SMatthew G Knepley     if (conv) {
3608fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
360967a56275SMatthew G Knepley       goto foundconv;
361067a56275SMatthew G Knepley     }
361167a56275SMatthew G Knepley 
361267a56275SMatthew G Knepley #if 0
361367a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
361467a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3615fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
361667a56275SMatthew G Knepley     if (conv) goto foundconv;
361767a56275SMatthew G Knepley 
361867a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
361967a56275SMatthew G Knepley     if (dm->ops->convert) {
362067a56275SMatthew G Knepley       conv = dm->ops->convert;
362167a56275SMatthew G Knepley     }
362267a56275SMatthew G Knepley     if (conv) goto foundconv;
362367a56275SMatthew G Knepley #endif
362467a56275SMatthew G Knepley 
362567a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
362682f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
362767a56275SMatthew G Knepley 
362867a56275SMatthew G Knepley foundconv:
362967a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
363067a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
363112fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
363290b157c4SStefano Zampini     {
363390b157c4SStefano Zampini       PetscBool             isper;
363412fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
363512fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
363690b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
363790b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
363812fa691eSMatthew G. Knepley     }
363967a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
364067a56275SMatthew G Knepley   }
364167a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
364267a56275SMatthew G Knepley   PetscFunctionReturn(0);
364367a56275SMatthew G Knepley }
3644264ace61SBarry Smith 
3645264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3646264ace61SBarry Smith 
3647264ace61SBarry Smith /*@C
36481c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
36491c84c290SBarry Smith 
36501c84c290SBarry Smith   Not Collective
36511c84c290SBarry Smith 
36521c84c290SBarry Smith   Input Parameters:
36531c84c290SBarry Smith + name        - The name of a new user-defined creation routine
36541c84c290SBarry Smith - create_func - The creation routine itself
36551c84c290SBarry Smith 
36561c84c290SBarry Smith   Notes:
36571c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
36581c84c290SBarry Smith 
36591c84c290SBarry Smith 
36601c84c290SBarry Smith   Sample usage:
36611c84c290SBarry Smith .vb
3662bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
36631c84c290SBarry Smith .ve
36641c84c290SBarry Smith 
36651c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
36661c84c290SBarry Smith .vb
36671c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
36681c84c290SBarry Smith     DMSetType(DM,"my_da");
36691c84c290SBarry Smith .ve
36701c84c290SBarry Smith    or at runtime via the option
36711c84c290SBarry Smith .vb
36721c84c290SBarry Smith     -da_type my_da
36731c84c290SBarry Smith .ve
3674264ace61SBarry Smith 
3675264ace61SBarry Smith   Level: advanced
36761c84c290SBarry Smith 
3677bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
36781c84c290SBarry Smith 
3679264ace61SBarry Smith @*/
3680bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3681264ace61SBarry Smith {
3682264ace61SBarry Smith   PetscErrorCode ierr;
3683264ace61SBarry Smith 
3684264ace61SBarry Smith   PetscFunctionBegin;
36851d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3686a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3687264ace61SBarry Smith   PetscFunctionReturn(0);
3688264ace61SBarry Smith }
3689264ace61SBarry Smith 
3690b859378eSBarry Smith /*@C
369155849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3692b859378eSBarry Smith 
3693d083f849SBarry Smith   Collective on viewer
3694b859378eSBarry Smith 
3695b859378eSBarry Smith   Input Parameters:
3696b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3697b859378eSBarry Smith            some related function before a call to DMLoad().
3698b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3699b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3700b859378eSBarry Smith 
3701b859378eSBarry Smith    Level: intermediate
3702b859378eSBarry Smith 
3703b859378eSBarry Smith   Notes:
370455849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3705b859378eSBarry Smith 
3706b859378eSBarry Smith   Notes for advanced users:
3707b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3708b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3709b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3710b859378eSBarry Smith   format is
3711b859378eSBarry Smith .vb
3712b859378eSBarry Smith      has not yet been determined
3713b859378eSBarry Smith .ve
3714b859378eSBarry Smith 
3715b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3716b859378eSBarry Smith @*/
3717b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3718b859378eSBarry Smith {
37199331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3720b859378eSBarry Smith   PetscErrorCode ierr;
3721b859378eSBarry Smith 
3722b859378eSBarry Smith   PetscFunctionBegin;
3723b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3724b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3725fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
372632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37279331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
37289331c7a4SMatthew G. Knepley   if (isbinary) {
37299331c7a4SMatthew G. Knepley     PetscInt classid;
37309331c7a4SMatthew G. Knepley     char     type[256];
3731b859378eSBarry Smith 
3732060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37339200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3734060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
373532c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37369331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37379331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37389331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37399331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3740b859378eSBarry Smith   PetscFunctionReturn(0);
3741b859378eSBarry Smith }
3742b859378eSBarry Smith 
37437da65231SMatthew G Knepley /******************************** FEM Support **********************************/
37447da65231SMatthew G Knepley 
3745a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3746a6dfd86eSKarl Rupp {
37471d47ebbbSSatish Balay   PetscInt       f;
37481b30c384SMatthew G Knepley   PetscErrorCode ierr;
37491b30c384SMatthew G Knepley 
37507da65231SMatthew G Knepley   PetscFunctionBegin;
375174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
37521d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
375357622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
37547da65231SMatthew G Knepley   }
37557da65231SMatthew G Knepley   PetscFunctionReturn(0);
37567da65231SMatthew G Knepley }
37577da65231SMatthew G Knepley 
3758a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3759a6dfd86eSKarl Rupp {
37601b30c384SMatthew G Knepley   PetscInt       f, g;
37617da65231SMatthew G Knepley   PetscErrorCode ierr;
37627da65231SMatthew G Knepley 
37637da65231SMatthew G Knepley   PetscFunctionBegin;
376474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
37651d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
376674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
37671d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3768e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
37697da65231SMatthew G Knepley     }
377074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
37717da65231SMatthew G Knepley   }
37727da65231SMatthew G Knepley   PetscFunctionReturn(0);
37737da65231SMatthew G Knepley }
3774e7c4fc90SDmitry Karpeev 
37756113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3776e759306cSMatthew G. Knepley {
37770c5b8624SToby Isaac   PetscInt          localSize, bs;
37780c5b8624SToby Isaac   PetscMPIInt       size;
37790c5b8624SToby Isaac   Vec               x, xglob;
37800c5b8624SToby Isaac   const PetscScalar *xarray;
3781e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3782e759306cSMatthew G. Knepley 
3783e759306cSMatthew G. Knepley   PetscFunctionBegin;
37849852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3785e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3786e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
37876113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
37880c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
37890c5b8624SToby Isaac   if (size > 1) {
37900c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
37910c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
37920c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
37930c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
37940c5b8624SToby Isaac   } else {
37950c5b8624SToby Isaac     xglob = x;
37960c5b8624SToby Isaac   }
37970c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
37980c5b8624SToby Isaac   if (size > 1) {
37990c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
38000c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
38010c5b8624SToby Isaac   }
3802e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3803e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3804e759306cSMatthew G. Knepley }
3805e759306cSMatthew G. Knepley 
380688ed4aceSMatthew G Knepley /*@
3807*1bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3808061576a5SJed Brown 
3809061576a5SJed Brown   Input Parameter:
3810061576a5SJed Brown . dm - The DM
3811061576a5SJed Brown 
3812061576a5SJed Brown   Output Parameter:
3813061576a5SJed Brown . section - The PetscSection
3814061576a5SJed Brown 
3815061576a5SJed Brown   Options Database Keys:
3816061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3817061576a5SJed Brown 
3818061576a5SJed Brown   Level: advanced
3819061576a5SJed Brown 
3820061576a5SJed Brown   Notes:
3821061576a5SJed Brown   Use DMGetLocalSection() in new code.
3822061576a5SJed Brown 
3823061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3824061576a5SJed Brown 
3825061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3826061576a5SJed Brown @*/
3827061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3828061576a5SJed Brown {
3829061576a5SJed Brown   PetscErrorCode ierr;
3830061576a5SJed Brown 
3831061576a5SJed Brown   PetscFunctionBegin;
3832061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3833061576a5SJed Brown   PetscFunctionReturn(0);
3834061576a5SJed Brown }
3835061576a5SJed Brown 
3836061576a5SJed Brown /*@
3837061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
383888ed4aceSMatthew G Knepley 
383988ed4aceSMatthew G Knepley   Input Parameter:
384088ed4aceSMatthew G Knepley . dm - The DM
384188ed4aceSMatthew G Knepley 
384288ed4aceSMatthew G Knepley   Output Parameter:
384388ed4aceSMatthew G Knepley . section - The PetscSection
384488ed4aceSMatthew G Knepley 
3845e5893cccSMatthew G. Knepley   Options Database Keys:
3846e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3847e5893cccSMatthew G. Knepley 
384888ed4aceSMatthew G Knepley   Level: intermediate
384988ed4aceSMatthew G Knepley 
385088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
385188ed4aceSMatthew G Knepley 
3852061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
385388ed4aceSMatthew G Knepley @*/
3854061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
38550adebc6cSBarry Smith {
3856fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3857fd59a867SMatthew G. Knepley 
385888ed4aceSMatthew G Knepley   PetscFunctionBegin;
385988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
386088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
3861*1bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
3862e5e52638SMatthew G. Knepley     PetscInt d;
3863e5e52638SMatthew G. Knepley 
3864e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
3865*1bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
3866*1bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
38672f0f8703SMatthew G. Knepley   }
3868*1bb6d2a8SBarry Smith   *section = dm->localSection;
386988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
387088ed4aceSMatthew G Knepley }
387188ed4aceSMatthew G Knepley 
387288ed4aceSMatthew G Knepley /*@
3873*1bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
3874061576a5SJed Brown 
3875061576a5SJed Brown   Input Parameters:
3876061576a5SJed Brown + dm - The DM
3877061576a5SJed Brown - section - The PetscSection
3878061576a5SJed Brown 
3879061576a5SJed Brown   Level: advanced
3880061576a5SJed Brown 
3881061576a5SJed Brown   Notes:
3882061576a5SJed Brown   Use DMSetLocalSection() in new code.
3883061576a5SJed Brown 
3884061576a5SJed Brown   Any existing Section will be destroyed
3885061576a5SJed Brown 
3886061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
3887061576a5SJed Brown @*/
3888061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
3889061576a5SJed Brown {
3890061576a5SJed Brown   PetscErrorCode ierr;
3891061576a5SJed Brown 
3892061576a5SJed Brown   PetscFunctionBegin;
3893061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
3894061576a5SJed Brown   PetscFunctionReturn(0);
3895061576a5SJed Brown }
3896061576a5SJed Brown 
3897061576a5SJed Brown /*@
3898061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
389988ed4aceSMatthew G Knepley 
390088ed4aceSMatthew G Knepley   Input Parameters:
390188ed4aceSMatthew G Knepley + dm - The DM
390288ed4aceSMatthew G Knepley - section - The PetscSection
390388ed4aceSMatthew G Knepley 
390488ed4aceSMatthew G Knepley   Level: intermediate
390588ed4aceSMatthew G Knepley 
390688ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
390788ed4aceSMatthew G Knepley 
3908061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
390988ed4aceSMatthew G Knepley @*/
3910061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
39110adebc6cSBarry Smith {
3912c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3913af122d2aSMatthew G Knepley   PetscInt       f;
391488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
391588ed4aceSMatthew G Knepley 
391688ed4aceSMatthew G Knepley   PetscFunctionBegin;
391788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3918b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
39191d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3920*1bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
3921*1bb6d2a8SBarry Smith   dm->localSection = section;
3922*1bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
3923af122d2aSMatthew G Knepley   if (numFields) {
3924af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3925af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
39260f21e855SMatthew G. Knepley       PetscObject disc;
3927af122d2aSMatthew G Knepley       const char *name;
3928af122d2aSMatthew G Knepley 
3929*1bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
393044a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
39310f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3932af122d2aSMatthew G Knepley     }
3933af122d2aSMatthew G Knepley   }
3934e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
3935*1bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
393688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
393788ed4aceSMatthew G Knepley }
393888ed4aceSMatthew G Knepley 
39399435951eSToby Isaac /*@
39409435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
39419435951eSToby Isaac 
3942e228b242SToby Isaac   not collective
3943e228b242SToby Isaac 
39449435951eSToby Isaac   Input Parameter:
39459435951eSToby Isaac . dm - The DM
39469435951eSToby Isaac 
39479435951eSToby Isaac   Output Parameter:
39489435951eSToby 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.
39499435951eSToby 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.
39509435951eSToby Isaac 
39519435951eSToby Isaac   Level: advanced
39529435951eSToby Isaac 
39539435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
39549435951eSToby Isaac 
39559435951eSToby Isaac .seealso: DMSetDefaultConstraints()
39569435951eSToby Isaac @*/
39579435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
39589435951eSToby Isaac {
39599435951eSToby Isaac   PetscErrorCode ierr;
39609435951eSToby Isaac 
39619435951eSToby Isaac   PetscFunctionBegin;
39629435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39639435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
396445a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
396545a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
39669435951eSToby Isaac   PetscFunctionReturn(0);
39679435951eSToby Isaac }
39689435951eSToby Isaac 
39699435951eSToby Isaac /*@
39709435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
39719435951eSToby Isaac 
39729435951eSToby 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().
39739435951eSToby Isaac 
39749435951eSToby 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.
39759435951eSToby Isaac 
3976e228b242SToby Isaac   collective on dm
3977e228b242SToby Isaac 
39789435951eSToby Isaac   Input Parameters:
39799435951eSToby Isaac + dm - The DM
3980e228b242SToby 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).
3981e228b242SToby 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).
39829435951eSToby Isaac 
39839435951eSToby Isaac   Level: advanced
39849435951eSToby Isaac 
39859435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
39869435951eSToby Isaac 
39879435951eSToby Isaac .seealso: DMGetDefaultConstraints()
39889435951eSToby Isaac @*/
39899435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
39909435951eSToby Isaac {
3991e228b242SToby Isaac   PetscMPIInt result;
39929435951eSToby Isaac   PetscErrorCode ierr;
39939435951eSToby Isaac 
39949435951eSToby Isaac   PetscFunctionBegin;
39959435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3996e228b242SToby Isaac   if (section) {
3997e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3998e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3999f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4000e228b242SToby Isaac   }
4001e228b242SToby Isaac   if (mat) {
4002e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4003e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4004f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4005e228b242SToby Isaac   }
40069435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40079435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
40089435951eSToby Isaac   dm->defaultConstraintSection = section;
40099435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
40109435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
40119435951eSToby Isaac   dm->defaultConstraintMat = mat;
40129435951eSToby Isaac   PetscFunctionReturn(0);
40139435951eSToby Isaac }
40149435951eSToby Isaac 
4015497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4016507e4973SMatthew G. Knepley /*
4017507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4018507e4973SMatthew G. Knepley 
4019507e4973SMatthew G. Knepley   Input Parameters:
4020507e4973SMatthew G. Knepley + dm - The DM
4021507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4022507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4023507e4973SMatthew G. Knepley 
4024507e4973SMatthew G. Knepley   Level: intermediate
4025507e4973SMatthew G. Knepley 
4026*1bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4027507e4973SMatthew G. Knepley */
4028f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4029507e4973SMatthew G. Knepley {
4030507e4973SMatthew G. Knepley   MPI_Comm        comm;
4031507e4973SMatthew G. Knepley   PetscLayout     layout;
4032507e4973SMatthew G. Knepley   const PetscInt *ranges;
4033507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4034507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4035507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4036507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4037507e4973SMatthew G. Knepley 
4038507e4973SMatthew G. Knepley   PetscFunctionBegin;
4039507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4040507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4041507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4042507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4043507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4044507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4045507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4046507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4047507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4048507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4049507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4050507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4051f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4052507e4973SMatthew G. Knepley 
4053507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4054507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4055507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4056507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4057507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4058507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4059507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4060507e4973SMatthew 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;}
4061507e4973SMatthew 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;}
4062507e4973SMatthew G. Knepley     if (gdof < 0) {
4063507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4064507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4065507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4066507e4973SMatthew G. Knepley 
4067507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4068507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4069507e4973SMatthew 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;}
4070507e4973SMatthew G. Knepley       }
4071507e4973SMatthew G. Knepley     }
4072507e4973SMatthew G. Knepley   }
4073507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4074507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4075b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4076507e4973SMatthew G. Knepley   if (!gvalid) {
4077507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4078507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4079507e4973SMatthew G. Knepley   }
4080507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4081507e4973SMatthew G. Knepley }
4082f741bcd2SMatthew G. Knepley #endif
4083507e4973SMatthew G. Knepley 
408488ed4aceSMatthew G Knepley /*@
4085e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
408688ed4aceSMatthew G Knepley 
4087d083f849SBarry Smith   Collective on dm
40888b1ab98fSJed Brown 
408988ed4aceSMatthew G Knepley   Input Parameter:
409088ed4aceSMatthew G Knepley . dm - The DM
409188ed4aceSMatthew G Knepley 
409288ed4aceSMatthew G Knepley   Output Parameter:
409388ed4aceSMatthew G Knepley . section - The PetscSection
409488ed4aceSMatthew G Knepley 
409588ed4aceSMatthew G Knepley   Level: intermediate
409688ed4aceSMatthew G Knepley 
409788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
409888ed4aceSMatthew G Knepley 
409992fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
410088ed4aceSMatthew G Knepley @*/
4101e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
41020adebc6cSBarry Smith {
410388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
410488ed4aceSMatthew G Knepley 
410588ed4aceSMatthew G Knepley   PetscFunctionBegin;
410688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
410788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
4108*1bb6d2a8SBarry Smith   if (!dm->globalSection) {
4109fd59a867SMatthew G. Knepley     PetscSection s;
4110fd59a867SMatthew G. Knepley 
411192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4112fd59a867SMatthew 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");
411333907cc2SStefano 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");
4114*1bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4115cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
4116*1bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
4117*1bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
411888ed4aceSMatthew G Knepley   }
4119*1bb6d2a8SBarry Smith   *section = dm->globalSection;
412088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
412188ed4aceSMatthew G Knepley }
412288ed4aceSMatthew G Knepley 
4123b21d0597SMatthew G Knepley /*@
4124e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4125b21d0597SMatthew G Knepley 
4126b21d0597SMatthew G Knepley   Input Parameters:
4127b21d0597SMatthew G Knepley + dm - The DM
41285080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4129b21d0597SMatthew G Knepley 
4130b21d0597SMatthew G Knepley   Level: intermediate
4131b21d0597SMatthew G Knepley 
4132b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4133b21d0597SMatthew G Knepley 
413492fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4135b21d0597SMatthew G Knepley @*/
4136e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
41370adebc6cSBarry Smith {
4138b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4139b21d0597SMatthew G Knepley 
4140b21d0597SMatthew G Knepley   PetscFunctionBegin;
4141b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41425080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
41431d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
4144*1bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
4145*1bb6d2a8SBarry Smith   dm->globalSection = section;
4146497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4147*1bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4148507e4973SMatthew G. Knepley #endif
4149b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4150b21d0597SMatthew G Knepley }
4151b21d0597SMatthew G Knepley 
415288ed4aceSMatthew G Knepley /*@
4153*1bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
415488ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
415588ed4aceSMatthew G Knepley 
415688ed4aceSMatthew G Knepley   Input Parameter:
415788ed4aceSMatthew G Knepley . dm - The DM
415888ed4aceSMatthew G Knepley 
415988ed4aceSMatthew G Knepley   Output Parameter:
416088ed4aceSMatthew G Knepley . sf - The PetscSF
416188ed4aceSMatthew G Knepley 
416288ed4aceSMatthew G Knepley   Level: intermediate
416388ed4aceSMatthew G Knepley 
416488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
416588ed4aceSMatthew G Knepley 
4166*1bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
416788ed4aceSMatthew G Knepley @*/
4168*1bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
41690adebc6cSBarry Smith {
417088ed4aceSMatthew G Knepley   PetscInt       nroots;
417188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
417288ed4aceSMatthew G Knepley 
417388ed4aceSMatthew G Knepley   PetscFunctionBegin;
417488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
417588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
4176*1bb6d2a8SBarry Smith   if (!dm->sectionSF) {
4177*1bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
417833907cc2SStefano Zampini   }
4179*1bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
418088ed4aceSMatthew G Knepley   if (nroots < 0) {
418188ed4aceSMatthew G Knepley     PetscSection section, gSection;
418288ed4aceSMatthew G Knepley 
418392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
418431ea6d37SMatthew G Knepley     if (section) {
4185e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
4186*1bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
418731ea6d37SMatthew G Knepley     } else {
41880298fd71SBarry Smith       *sf = NULL;
418931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
419031ea6d37SMatthew G Knepley     }
419188ed4aceSMatthew G Knepley   }
4192*1bb6d2a8SBarry Smith   *sf = dm->sectionSF;
419388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
419488ed4aceSMatthew G Knepley }
419588ed4aceSMatthew G Knepley 
419688ed4aceSMatthew G Knepley /*@
4197*1bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
419888ed4aceSMatthew G Knepley 
419988ed4aceSMatthew G Knepley   Input Parameters:
420088ed4aceSMatthew G Knepley + dm - The DM
420188ed4aceSMatthew G Knepley - sf - The PetscSF
420288ed4aceSMatthew G Knepley 
420388ed4aceSMatthew G Knepley   Level: intermediate
420488ed4aceSMatthew G Knepley 
420588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
420688ed4aceSMatthew G Knepley 
4207*1bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
420888ed4aceSMatthew G Knepley @*/
4209*1bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
42100adebc6cSBarry Smith {
421188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
421288ed4aceSMatthew G Knepley 
421388ed4aceSMatthew G Knepley   PetscFunctionBegin;
421488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4215b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
421633907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
4217*1bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
4218*1bb6d2a8SBarry Smith   dm->sectionSF = sf;
421988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
422088ed4aceSMatthew G Knepley }
422188ed4aceSMatthew G Knepley 
422288ed4aceSMatthew G Knepley /*@C
4223*1bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
422488ed4aceSMatthew G Knepley   describing the data layout.
422588ed4aceSMatthew G Knepley 
422688ed4aceSMatthew G Knepley   Input Parameters:
422788ed4aceSMatthew G Knepley + dm - The DM
422888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
422988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
423088ed4aceSMatthew G Knepley 
4231*1bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
423288ed4aceSMatthew G Knepley 
4233*1bb6d2a8SBarry Smith   Level: developer
4234*1bb6d2a8SBarry Smith 
4235*1bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
4236*1bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
4237*1bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
4238*1bb6d2a8SBarry Smith 
4239*1bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
424088ed4aceSMatthew G Knepley @*/
4241*1bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
424288ed4aceSMatthew G Knepley {
424382f516ccSBarry Smith   MPI_Comm       comm;
424488ed4aceSMatthew G Knepley   PetscLayout    layout;
424588ed4aceSMatthew G Knepley   const PetscInt *ranges;
424688ed4aceSMatthew G Knepley   PetscInt       *local;
424788ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4248ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
424988ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
425088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
425188ed4aceSMatthew G Knepley 
425288ed4aceSMatthew G Knepley   PetscFunctionBegin;
425388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4254367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
425588ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
425688ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
425788ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
425888ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
425988ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
426088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
426188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
426288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
426388ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4264ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
42656636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
426688ed4aceSMatthew G Knepley 
42676636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
42686636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4269235fbf56SMatthew 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));
42706636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
427188ed4aceSMatthew G Knepley   }
4272785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4273785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
427488ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
42751f588964SMatthew G Knepley     const PetscInt *cind;
42766636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
427788ed4aceSMatthew G Knepley 
427888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
427988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
428088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
428188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
428288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
42836636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
428488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
42856636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
42866636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
42876636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4288057b4bcdSMatthew 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);
42896636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
42906636e97aSMatthew G Knepley     }
429188ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
429288ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
429388ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
429488ed4aceSMatthew G Knepley     }
429588ed4aceSMatthew G Knepley     if (gdof < 0) {
42966636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
429788ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
429888ed4aceSMatthew G Knepley 
429905376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
430031d3f06eSJed Brown         if (r < 0) r = -(r+2);
430105376888SMatthew 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);
430288ed4aceSMatthew G Knepley         remote[l].rank  = r;
430388ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
430488ed4aceSMatthew G Knepley       }
430588ed4aceSMatthew G Knepley     } else {
43066636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
430788ed4aceSMatthew G Knepley         remote[l].rank  = rank;
430888ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
430988ed4aceSMatthew G Knepley       }
431088ed4aceSMatthew G Knepley     }
431188ed4aceSMatthew G Knepley   }
43126636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
431388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4314*1bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
431588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
431688ed4aceSMatthew G Knepley }
4317af122d2aSMatthew G Knepley 
4318b21d0597SMatthew G Knepley /*@
4319b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4320b21d0597SMatthew G Knepley 
4321b21d0597SMatthew G Knepley   Input Parameter:
4322b21d0597SMatthew G Knepley . dm - The DM
4323b21d0597SMatthew G Knepley 
4324b21d0597SMatthew G Knepley   Output Parameter:
4325b21d0597SMatthew G Knepley . sf - The PetscSF
4326b21d0597SMatthew G Knepley 
4327b21d0597SMatthew G Knepley   Level: intermediate
4328b21d0597SMatthew G Knepley 
4329b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4330b21d0597SMatthew G Knepley 
4331*1bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4332b21d0597SMatthew G Knepley @*/
43330adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
43340adebc6cSBarry Smith {
4335b21d0597SMatthew G Knepley   PetscFunctionBegin;
4336b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4337b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4338b21d0597SMatthew G Knepley   *sf = dm->sf;
4339b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4340b21d0597SMatthew G Knepley }
4341b21d0597SMatthew G Knepley 
4342057b4bcdSMatthew G Knepley /*@
4343057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4344057b4bcdSMatthew G Knepley 
4345057b4bcdSMatthew G Knepley   Input Parameters:
4346057b4bcdSMatthew G Knepley + dm - The DM
4347057b4bcdSMatthew G Knepley - sf - The PetscSF
4348057b4bcdSMatthew G Knepley 
4349057b4bcdSMatthew G Knepley   Level: intermediate
4350057b4bcdSMatthew G Knepley 
4351*1bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4352057b4bcdSMatthew G Knepley @*/
43530adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
43540adebc6cSBarry Smith {
4355057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4356057b4bcdSMatthew G Knepley 
4357057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4358057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4359b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4360057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
436133907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4362057b4bcdSMatthew G Knepley   dm->sf = sf;
4363057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4364057b4bcdSMatthew G Knepley }
4365057b4bcdSMatthew G Knepley 
436634aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
436734aa8a36SMatthew G. Knepley {
436834aa8a36SMatthew G. Knepley   PetscClassId   id;
436934aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
437034aa8a36SMatthew G. Knepley 
437134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
437234aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
437334aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
437434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
437534aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
437634aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
437717c1d62eSMatthew G. Knepley   } else {
437817c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
437934aa8a36SMatthew G. Knepley   }
438034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
438134aa8a36SMatthew G. Knepley }
438234aa8a36SMatthew G. Knepley 
438344a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
438444a7f3ddSMatthew G. Knepley {
438544a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
438644a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
438744a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
438844a7f3ddSMatthew G. Knepley 
438944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
439044a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
439144a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
439244a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
439344a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
439444a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
439544a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
439644a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
439744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
439844a7f3ddSMatthew G. Knepley }
439944a7f3ddSMatthew G. Knepley 
440044a7f3ddSMatthew G. Knepley /*@
440144a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
440244a7f3ddSMatthew G. Knepley 
4403d083f849SBarry Smith   Logically collective on dm
440444a7f3ddSMatthew G. Knepley 
440544a7f3ddSMatthew G. Knepley   Input Parameter:
440644a7f3ddSMatthew G. Knepley . dm - The DM
440744a7f3ddSMatthew G. Knepley 
440844a7f3ddSMatthew G. Knepley   Level: intermediate
440944a7f3ddSMatthew G. Knepley 
441044a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
441144a7f3ddSMatthew G. Knepley @*/
441244a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
441344a7f3ddSMatthew G. Knepley {
441444a7f3ddSMatthew G. Knepley   PetscInt       f;
441544a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
441644a7f3ddSMatthew G. Knepley 
441744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
441844a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
441944a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
442044a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
442144a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
442244a7f3ddSMatthew G. Knepley   }
442344a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
442444a7f3ddSMatthew G. Knepley   dm->fields = NULL;
442544a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
442644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
442744a7f3ddSMatthew G. Knepley }
442844a7f3ddSMatthew G. Knepley 
4429689b5837SMatthew G. Knepley /*@
4430689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4431689b5837SMatthew G. Knepley 
4432689b5837SMatthew G. Knepley   Not collective
4433689b5837SMatthew G. Knepley 
4434689b5837SMatthew G. Knepley   Input Parameter:
4435689b5837SMatthew G. Knepley . dm - The DM
4436689b5837SMatthew G. Knepley 
4437689b5837SMatthew G. Knepley   Output Parameter:
4438689b5837SMatthew G. Knepley . Nf - The number of fields
4439689b5837SMatthew G. Knepley 
4440689b5837SMatthew G. Knepley   Level: intermediate
4441689b5837SMatthew G. Knepley 
4442689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4443689b5837SMatthew G. Knepley @*/
44440f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
44450f21e855SMatthew G. Knepley {
44460f21e855SMatthew G. Knepley   PetscFunctionBegin;
44470f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4448534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
444944a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4450af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4451af122d2aSMatthew G Knepley }
4452af122d2aSMatthew G Knepley 
4453689b5837SMatthew G. Knepley /*@
4454689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4455689b5837SMatthew G. Knepley 
4456d083f849SBarry Smith   Logically collective on dm
4457689b5837SMatthew G. Knepley 
4458689b5837SMatthew G. Knepley   Input Parameters:
4459689b5837SMatthew G. Knepley + dm - The DM
4460689b5837SMatthew G. Knepley - Nf - The number of fields
4461689b5837SMatthew G. Knepley 
4462689b5837SMatthew G. Knepley   Level: intermediate
4463689b5837SMatthew G. Knepley 
4464689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4465689b5837SMatthew G. Knepley @*/
4466af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4467af122d2aSMatthew G Knepley {
44680f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4469af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4470af122d2aSMatthew G Knepley 
4471af122d2aSMatthew G Knepley   PetscFunctionBegin;
4472af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
447344a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
44740f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
44750f21e855SMatthew G. Knepley     PetscContainer obj;
44760f21e855SMatthew G. Knepley 
44770f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
447844a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
44790f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4480af122d2aSMatthew G Knepley   }
4481af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4482af122d2aSMatthew G Knepley }
4483af122d2aSMatthew G Knepley 
4484c1929be8SMatthew G. Knepley /*@
4485c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4486c1929be8SMatthew G. Knepley 
4487c1929be8SMatthew G. Knepley   Not collective
4488c1929be8SMatthew G. Knepley 
4489c1929be8SMatthew G. Knepley   Input Parameters:
4490c1929be8SMatthew G. Knepley + dm - The DM
4491c1929be8SMatthew G. Knepley - f  - The field number
4492c1929be8SMatthew G. Knepley 
449344a7f3ddSMatthew G. Knepley   Output Parameters:
449444a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
449544a7f3ddSMatthew G. Knepley - field - The discretization object
4496c1929be8SMatthew G. Knepley 
449744a7f3ddSMatthew G. Knepley   Level: intermediate
4498c1929be8SMatthew G. Knepley 
449944a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4500c1929be8SMatthew G. Knepley @*/
450144a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4502af122d2aSMatthew G Knepley {
4503af122d2aSMatthew G Knepley   PetscFunctionBegin;
4504af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
450544a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
450644a7f3ddSMatthew 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);
450744a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
450844a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4509decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4510decb47aaSMatthew G. Knepley }
4511decb47aaSMatthew G. Knepley 
4512c1929be8SMatthew G. Knepley /*@
4513c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4514c1929be8SMatthew G. Knepley 
4515d083f849SBarry Smith   Logically collective on dm
4516c1929be8SMatthew G. Knepley 
4517c1929be8SMatthew G. Knepley   Input Parameters:
4518c1929be8SMatthew G. Knepley + dm    - The DM
4519c1929be8SMatthew G. Knepley . f     - The field number
452044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4521c1929be8SMatthew G. Knepley - field - The discretization object
4522c1929be8SMatthew G. Knepley 
452344a7f3ddSMatthew G. Knepley   Level: intermediate
4524c1929be8SMatthew G. Knepley 
452544a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4526c1929be8SMatthew G. Knepley @*/
452744a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4528decb47aaSMatthew G. Knepley {
4529decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4530decb47aaSMatthew G. Knepley 
4531decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4532decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4533e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
453444a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4535e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
453644a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
453744a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
453844a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
453944a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
454044a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
454144a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
454244a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
454334aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
45442df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
454544a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
454644a7f3ddSMatthew G. Knepley }
454744a7f3ddSMatthew G. Knepley 
454844a7f3ddSMatthew G. Knepley /*@
454944a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
455044a7f3ddSMatthew G. Knepley 
4551d083f849SBarry Smith   Logically collective on dm
455244a7f3ddSMatthew G. Knepley 
455344a7f3ddSMatthew G. Knepley   Input Parameters:
455444a7f3ddSMatthew G. Knepley + dm    - The DM
455544a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
455644a7f3ddSMatthew G. Knepley - field - The discretization object
455744a7f3ddSMatthew G. Knepley 
455844a7f3ddSMatthew G. Knepley   Level: intermediate
455944a7f3ddSMatthew G. Knepley 
456044a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
456144a7f3ddSMatthew G. Knepley @*/
456244a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
456344a7f3ddSMatthew G. Knepley {
456444a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
456544a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
456644a7f3ddSMatthew G. Knepley 
456744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
456844a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
456944a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
457044a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
457144a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
457244a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
457344a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
457444a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
457544a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
457634aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
45772df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4578af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4579af122d2aSMatthew G Knepley }
45806636e97aSMatthew G Knepley 
4581e5e52638SMatthew G. Knepley /*@
4582e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4583e5e52638SMatthew G. Knepley 
4584d083f849SBarry Smith   Collective on dm
4585e5e52638SMatthew G. Knepley 
4586e5e52638SMatthew G. Knepley   Input Parameter:
4587e5e52638SMatthew G. Knepley . dm - The DM
4588e5e52638SMatthew G. Knepley 
4589e5e52638SMatthew G. Knepley   Output Parameter:
4590e5e52638SMatthew G. Knepley . newdm - The DM
4591e5e52638SMatthew G. Knepley 
4592e5e52638SMatthew G. Knepley   Level: advanced
4593e5e52638SMatthew G. Knepley 
4594e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4595e5e52638SMatthew G. Knepley @*/
4596e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4597e5e52638SMatthew G. Knepley {
4598e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4599e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4600e5e52638SMatthew G. Knepley 
4601e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4602e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4603e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4604e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4605e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4606e5e52638SMatthew G. Knepley     DMLabel     label;
4607e5e52638SMatthew G. Knepley     PetscObject field;
460834aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4609e5e52638SMatthew G. Knepley 
4610e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4611e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
461234aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
461334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
461434aa8a36SMatthew G. Knepley   }
461534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
461634aa8a36SMatthew G. Knepley }
461734aa8a36SMatthew G. Knepley 
461834aa8a36SMatthew G. Knepley /*@
461934aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
462034aa8a36SMatthew G. Knepley 
462134aa8a36SMatthew G. Knepley   Not collective
462234aa8a36SMatthew G. Knepley 
462334aa8a36SMatthew G. Knepley   Input Parameters:
462434aa8a36SMatthew G. Knepley + dm - The DM object
462534aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
462634aa8a36SMatthew G. Knepley 
462734aa8a36SMatthew G. Knepley   Output Parameter:
462834aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
462934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
463034aa8a36SMatthew G. Knepley 
463134aa8a36SMatthew G. Knepley   Notes:
463234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
463334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
463434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4635979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
463634aa8a36SMatthew G. Knepley 
463734aa8a36SMatthew G. Knepley   Level: developer
463834aa8a36SMatthew G. Knepley 
463934aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
464034aa8a36SMatthew G. Knepley @*/
464134aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
464234aa8a36SMatthew G. Knepley {
464334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
464434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4645534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4646534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
464734aa8a36SMatthew G. Knepley   if (f < 0) {
464834aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
464934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
465034aa8a36SMatthew G. Knepley   } else {
465134aa8a36SMatthew G. Knepley     PetscInt       Nf;
465234aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
465334aa8a36SMatthew G. Knepley 
465434aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
465534aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
465634aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
465734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
465834aa8a36SMatthew G. Knepley   }
465934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
466034aa8a36SMatthew G. Knepley }
466134aa8a36SMatthew G. Knepley 
466234aa8a36SMatthew G. Knepley /*@
466334aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
466434aa8a36SMatthew G. Knepley 
466534aa8a36SMatthew G. Knepley   Not collective
466634aa8a36SMatthew G. Knepley 
466734aa8a36SMatthew G. Knepley   Input Parameters:
466834aa8a36SMatthew G. Knepley + dm         - The DM object
466934aa8a36SMatthew G. Knepley . f          - The field number
467034aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
467134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
467234aa8a36SMatthew G. Knepley 
467334aa8a36SMatthew G. Knepley   Notes:
467434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
467534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
467634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4677979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
467834aa8a36SMatthew G. Knepley 
467934aa8a36SMatthew G. Knepley   Level: developer
468034aa8a36SMatthew G. Knepley 
468134aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
468234aa8a36SMatthew G. Knepley @*/
468334aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
468434aa8a36SMatthew G. Knepley {
468534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
468634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
468734aa8a36SMatthew G. Knepley   if (f < 0) {
468834aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
468934aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
469034aa8a36SMatthew G. Knepley   } else {
469134aa8a36SMatthew G. Knepley     PetscInt       Nf;
469234aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
469334aa8a36SMatthew G. Knepley 
469434aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
469534aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
469634aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
469734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4698e5e52638SMatthew G. Knepley   }
4699e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4700e5e52638SMatthew G. Knepley }
4701e5e52638SMatthew G. Knepley 
4702b0441da4SMatthew G. Knepley /*@
4703b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4704b0441da4SMatthew G. Knepley 
4705b0441da4SMatthew G. Knepley   Not collective
4706b0441da4SMatthew G. Knepley 
4707b0441da4SMatthew G. Knepley   Input Parameters:
4708b0441da4SMatthew G. Knepley . dm - The DM object
4709b0441da4SMatthew G. Knepley 
4710b0441da4SMatthew G. Knepley   Output Parameter:
4711b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4712b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4713b0441da4SMatthew G. Knepley 
4714b0441da4SMatthew G. Knepley   Notes:
4715b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4716b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4717b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4718b0441da4SMatthew G. Knepley 
4719b0441da4SMatthew G. Knepley   Level: developer
4720b0441da4SMatthew G. Knepley 
4721b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4722b0441da4SMatthew G. Knepley @*/
4723b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4724b0441da4SMatthew G. Knepley {
4725b0441da4SMatthew G. Knepley   PetscInt       Nf;
4726b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4727b0441da4SMatthew G. Knepley 
4728b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4729b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4730534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4731534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4732b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4733b0441da4SMatthew G. Knepley   if (!Nf) {
4734b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4735b0441da4SMatthew G. Knepley   } else {
4736b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4737b0441da4SMatthew G. Knepley   }
4738b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4739b0441da4SMatthew G. Knepley }
4740b0441da4SMatthew G. Knepley 
4741b0441da4SMatthew G. Knepley /*@
4742b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4743b0441da4SMatthew G. Knepley 
4744b0441da4SMatthew G. Knepley   Not collective
4745b0441da4SMatthew G. Knepley 
4746b0441da4SMatthew G. Knepley   Input Parameters:
4747b0441da4SMatthew G. Knepley + dm         - The DM object
4748b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4749b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4750b0441da4SMatthew G. Knepley 
4751b0441da4SMatthew G. Knepley   Notes:
4752b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4753b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4754b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4755b0441da4SMatthew G. Knepley 
4756b0441da4SMatthew G. Knepley   Level: developer
4757b0441da4SMatthew G. Knepley 
4758b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4759b0441da4SMatthew G. Knepley @*/
4760b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4761b0441da4SMatthew G. Knepley {
4762b0441da4SMatthew G. Knepley   PetscInt       Nf;
4763b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4764b0441da4SMatthew G. Knepley 
4765b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4766b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4767b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4768b0441da4SMatthew G. Knepley   if (!Nf) {
4769b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4770b0441da4SMatthew G. Knepley   } else {
4771b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4772e5e52638SMatthew G. Knepley   }
4773e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4774e5e52638SMatthew G. Knepley }
4775e5e52638SMatthew G. Knepley 
4776e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4777e5e52638SMatthew G. Knepley {
4778e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4779e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4780e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4781e5e52638SMatthew G. Knepley 
4782e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4783e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4784e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4785e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4786b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4787e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4788e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4789e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4790e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4791e5e52638SMatthew G. Knepley }
4792e5e52638SMatthew G. Knepley 
4793e5e52638SMatthew G. Knepley /*@
4794e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4795e5e52638SMatthew G. Knepley 
4796e5e52638SMatthew G. Knepley   Not collective
4797e5e52638SMatthew G. Knepley 
4798e5e52638SMatthew G. Knepley   Input Parameter:
4799e5e52638SMatthew G. Knepley . dm - The DM
4800e5e52638SMatthew G. Knepley 
4801e5e52638SMatthew G. Knepley   Output Parameter:
4802e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4803e5e52638SMatthew G. Knepley 
4804e5e52638SMatthew G. Knepley   Level: intermediate
4805e5e52638SMatthew G. Knepley 
4806e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4807e5e52638SMatthew G. Knepley @*/
4808e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4809e5e52638SMatthew G. Knepley {
4810e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4811e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4812534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
4813e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4814e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4815e5e52638SMatthew G. Knepley }
4816e5e52638SMatthew G. Knepley 
4817e5e52638SMatthew G. Knepley /*@
4818e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4819e5e52638SMatthew G. Knepley 
4820d083f849SBarry Smith   Logically collective on dm
4821e5e52638SMatthew G. Knepley 
4822e5e52638SMatthew G. Knepley   Input Parameter:
4823e5e52638SMatthew G. Knepley . dm - The DM
4824e5e52638SMatthew G. Knepley 
4825e5e52638SMatthew G. Knepley   Level: intermediate
4826e5e52638SMatthew G. Knepley 
4827e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4828e5e52638SMatthew G. Knepley @*/
4829e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4830e5e52638SMatthew G. Knepley {
4831e5e52638SMatthew G. Knepley   PetscInt       s;
4832e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4833e5e52638SMatthew G. Knepley 
4834e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4835e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4836e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4837e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4838e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4839b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4840e5e52638SMatthew G. Knepley   }
4841e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4842e5e52638SMatthew G. Knepley   dm->probs = NULL;
4843e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4844e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4845e5e52638SMatthew G. Knepley }
4846e5e52638SMatthew G. Knepley 
4847e5e52638SMatthew G. Knepley /*@
4848e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4849e5e52638SMatthew G. Knepley 
4850e5e52638SMatthew G. Knepley   Not collective
4851e5e52638SMatthew G. Knepley 
4852e5e52638SMatthew G. Knepley   Input Parameter:
4853e5e52638SMatthew G. Knepley . dm    - The DM
4854e5e52638SMatthew G. Knepley 
4855e5e52638SMatthew G. Knepley   Output Parameter:
4856e5e52638SMatthew G. Knepley . prob - The default PetscDS
4857e5e52638SMatthew G. Knepley 
4858e5e52638SMatthew G. Knepley   Level: intermediate
4859e5e52638SMatthew G. Knepley 
4860e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4861e5e52638SMatthew G. Knepley @*/
4862e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4863e5e52638SMatthew G. Knepley {
4864b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4865b0143b4dSMatthew G. Knepley 
4866e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4867e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4868e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4869b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4870b0143b4dSMatthew G. Knepley     PetscDS ds;
4871b0143b4dSMatthew G. Knepley 
4872b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4873b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
4874b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4875b0143b4dSMatthew G. Knepley   }
4876b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4877e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4878e5e52638SMatthew G. Knepley }
4879e5e52638SMatthew G. Knepley 
4880e5e52638SMatthew G. Knepley /*@
4881e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
4882e5e52638SMatthew G. Knepley 
4883e5e52638SMatthew G. Knepley   Not collective
4884e5e52638SMatthew G. Knepley 
4885e5e52638SMatthew G. Knepley   Input Parameters:
4886e5e52638SMatthew G. Knepley + dm    - The DM
4887e5e52638SMatthew G. Knepley - point - Cell for the DS
4888e5e52638SMatthew G. Knepley 
4889e5e52638SMatthew G. Knepley   Output Parameter:
4890e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
4891e5e52638SMatthew G. Knepley 
4892e5e52638SMatthew G. Knepley   Level: developer
4893e5e52638SMatthew G. Knepley 
4894b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
4895e5e52638SMatthew G. Knepley @*/
4896e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
4897e5e52638SMatthew G. Knepley {
4898e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
4899e5e52638SMatthew G. Knepley   PetscInt       s;
4900e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4901e5e52638SMatthew G. Knepley 
4902e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4903e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4904e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
4905e5e52638SMatthew G. Knepley   *prob = NULL;
4906e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4907e5e52638SMatthew G. Knepley     PetscInt val;
4908e5e52638SMatthew G. Knepley 
4909e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
4910e5e52638SMatthew G. Knepley     else {
4911e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
4912e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
4913e5e52638SMatthew G. Knepley     }
4914e5e52638SMatthew G. Knepley   }
4915e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
4916e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4917e5e52638SMatthew G. Knepley }
4918e5e52638SMatthew G. Knepley 
4919e5e52638SMatthew G. Knepley /*@
4920e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
4921e5e52638SMatthew G. Knepley 
4922e5e52638SMatthew G. Knepley   Not collective
4923e5e52638SMatthew G. Knepley 
4924e5e52638SMatthew G. Knepley   Input Parameters:
4925e5e52638SMatthew G. Knepley + dm    - The DM
4926e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
4927e5e52638SMatthew G. Knepley 
4928b3cf3223SMatthew G. Knepley   Output Parameters:
4929b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
4930b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
4931e5e52638SMatthew G. Knepley 
4932e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
4933e5e52638SMatthew G. Knepley 
4934e5e52638SMatthew G. Knepley   Level: advanced
4935e5e52638SMatthew G. Knepley 
4936e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4937e5e52638SMatthew G. Knepley @*/
4938b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
4939e5e52638SMatthew G. Knepley {
4940e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
4941e5e52638SMatthew G. Knepley 
4942e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4943e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4944e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4945b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
4946b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
4947e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
4948b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
4949b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
4950b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
4951b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
4952b3cf3223SMatthew G. Knepley     }
4953e5e52638SMatthew G. Knepley   }
49542df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
4955e5e52638SMatthew G. Knepley }
4956e5e52638SMatthew G. Knepley 
4957e5e52638SMatthew G. Knepley /*@
4958e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
4959e5e52638SMatthew G. Knepley 
4960e5e52638SMatthew G. Knepley   Not collective
4961e5e52638SMatthew G. Knepley 
4962e5e52638SMatthew G. Knepley   Input Parameters:
4963e5e52638SMatthew G. Knepley + dm  - The DM
4964e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
4965e5e52638SMatthew G. Knepley 
4966e5e52638SMatthew G. Knepley   Output Parameters:
4967b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
4968b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
4969b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
4970e5e52638SMatthew G. Knepley 
4971e5e52638SMatthew G. Knepley   Level: advanced
4972e5e52638SMatthew G. Knepley 
4973e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4974e5e52638SMatthew G. Knepley @*/
4975b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
4976e5e52638SMatthew G. Knepley {
4977e5e52638SMatthew G. Knepley   PetscInt       Nds;
4978e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4979e5e52638SMatthew G. Knepley 
4980e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4981e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4982e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
4983e5e52638SMatthew 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);
4984e5e52638SMatthew G. Knepley   if (label) {
4985e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
4986e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
4987e5e52638SMatthew G. Knepley   }
4988b3cf3223SMatthew G. Knepley   if (fields) {
4989b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
4990b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
4991b3cf3223SMatthew G. Knepley   }
4992e5e52638SMatthew G. Knepley   if (ds) {
4993b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
4994e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
4995e5e52638SMatthew G. Knepley   }
4996e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4997e5e52638SMatthew G. Knepley }
4998e5e52638SMatthew G. Knepley 
4999e5e52638SMatthew G. Knepley /*@
5000e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5001e5e52638SMatthew G. Knepley 
5002d083f849SBarry Smith   Collective on dm
5003e5e52638SMatthew G. Knepley 
5004e5e52638SMatthew G. Knepley   Input Parameters:
5005e5e52638SMatthew G. Knepley + dm     - The DM
5006e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5007b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5008e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5009e5e52638SMatthew G. Knepley 
5010b3cf3223SMatthew 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,
5011b3cf3223SMatthew G. Knepley   the fields argument is ignored.
5012e5e52638SMatthew G. Knepley 
5013e5e52638SMatthew G. Knepley   Level: advanced
5014e5e52638SMatthew G. Knepley 
5015e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
5016e5e52638SMatthew G. Knepley @*/
5017b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5018e5e52638SMatthew G. Knepley {
5019e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5020e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5021e5e52638SMatthew G. Knepley 
5022e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5023e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5024e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5025e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5026e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5027e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
5028b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5029e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
5030e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
5031e5e52638SMatthew G. Knepley     }
5032e5e52638SMatthew G. Knepley   }
50331b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5034e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5035b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5036e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5037e5e52638SMatthew G. Knepley   if (!label) {
5038e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5039e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5040e5e52638SMatthew G. Knepley     Nds = 0;
5041e5e52638SMatthew G. Knepley   }
5042e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
5043b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5044e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5045e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5046e5e52638SMatthew G. Knepley }
5047e5e52638SMatthew G. Knepley 
5048e5e52638SMatthew G. Knepley /*@
5049e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5050e5e52638SMatthew G. Knepley 
5051d083f849SBarry Smith   Collective on dm
5052e5e52638SMatthew G. Knepley 
5053e5e52638SMatthew G. Knepley   Input Parameter:
5054e5e52638SMatthew G. Knepley . dm - The DM
5055e5e52638SMatthew G. Knepley 
5056e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5057e5e52638SMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley   Level: intermediate
5059e5e52638SMatthew G. Knepley 
5060e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5061e5e52638SMatthew G. Knepley @*/
5062e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5063e5e52638SMatthew G. Knepley {
5064e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5065e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5066b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5067e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5068e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5069e5e52638SMatthew G. Knepley 
5070e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5071e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5072e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5073e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5074e5e52638SMatthew G. Knepley    1) NULL
5075e5e52638SMatthew G. Knepley    2) Hybrid cells
5076e5e52638SMatthew G. Knepley   */
5077e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5078e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5079e5e52638SMatthew G. Knepley   /* Create default DS */
5080b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
50812df9ee95SMatthew G. Knepley   if (!prob) {
5082b3cf3223SMatthew G. Knepley     IS        fields;
5083b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5084b3cf3223SMatthew G. Knepley 
5085b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5086b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5087b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
508888f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
508988f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
509088f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
509188f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
509288f0c812SMatthew G. Knepley 
50932df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5094b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
50952df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5096b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5097b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
50982df9ee95SMatthew G. Knepley   }
5099e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5100e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5101b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5102e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5103e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5104e5e52638SMatthew G. Knepley 
5105e5e52638SMatthew G. Knepley     if (label) {
51060122748bSMatthew G. Knepley       DM        plex;
5107a2d47024SMatthew G. Knepley       IS        fields;
5108a2d47024SMatthew G. Knepley       PetscInt *fld;
51090122748bSMatthew G. Knepley       PetscInt  depth, pMax[4];
51100122748bSMatthew G. Knepley 
51110122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
51120122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
51130122748bSMatthew 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);
51140122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
51150122748bSMatthew G. Knepley 
5116e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5117e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
5118e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5119a2d47024SMatthew G. Knepley       ierr = PetscMalloc1(1, &fld);CHKERRQ(ierr);
5120a2d47024SMatthew G. Knepley       fld[0] = f;
5121a2d47024SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5122a2d47024SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5123a2d47024SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5124a2d47024SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, 1, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5125a2d47024SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, fields, probh);CHKERRQ(ierr);
5126a2d47024SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
5127e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5128e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5129e5e52638SMatthew G. Knepley       break;
5130e5e52638SMatthew G. Knepley     }
5131e5e52638SMatthew G. Knepley   }
5132e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5133b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5134e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5135e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5136e5e52638SMatthew G. Knepley 
5137e5e52638SMatthew G. Knepley     if (!label) {
5138e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5139e5e52638SMatthew G. Knepley       if (probh) {
5140e5e52638SMatthew G. Knepley         PetscFE subfe;
5141e5e52638SMatthew G. Knepley 
5142e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5143e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5144e5e52638SMatthew G. Knepley       }
5145e5e52638SMatthew G. Knepley     } else {
5146e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5147e5e52638SMatthew G. Knepley     }
5148e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5149e5e52638SMatthew G. Knepley     {
5150e5e52638SMatthew G. Knepley       PetscClassId id;
5151e5e52638SMatthew G. Knepley 
5152e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5153e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5154e5e52638SMatthew G. Knepley     }
5155e5e52638SMatthew G. Knepley   }
5156e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5157e5e52638SMatthew G. Knepley   /* Setup DSes */
5158e5e52638SMatthew G. Knepley   if (doSetup) {
5159e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5160e5e52638SMatthew G. Knepley   }
5161e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5162e5e52638SMatthew G. Knepley }
5163e5e52638SMatthew G. Knepley 
5164e5e52638SMatthew G. Knepley /*@
5165e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5166e5e52638SMatthew G. Knepley 
5167d083f849SBarry Smith   Collective on dm
5168e5e52638SMatthew G. Knepley 
5169e5e52638SMatthew G. Knepley   Input Parameter:
5170e5e52638SMatthew G. Knepley . dm - The DM
5171e5e52638SMatthew G. Knepley 
5172e5e52638SMatthew G. Knepley   Output Parameter:
5173e5e52638SMatthew G. Knepley . newdm - The DM
5174e5e52638SMatthew G. Knepley 
5175e5e52638SMatthew G. Knepley   Level: advanced
5176e5e52638SMatthew G. Knepley 
5177e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5178e5e52638SMatthew G. Knepley @*/
5179e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5180e5e52638SMatthew G. Knepley {
5181e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5182e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5183e5e52638SMatthew G. Knepley 
5184e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5185e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5186e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5187e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5188e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5189e5e52638SMatthew G. Knepley     DMLabel label;
5190b3cf3223SMatthew G. Knepley     IS      fields;
5191e5e52638SMatthew G. Knepley     PetscDS ds;
5192e5e52638SMatthew G. Knepley 
5193b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5194b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5195e5e52638SMatthew G. Knepley   }
5196e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5197e5e52638SMatthew G. Knepley }
5198e5e52638SMatthew G. Knepley 
5199e5e52638SMatthew G. Knepley /*@
5200e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5201e5e52638SMatthew G. Knepley 
5202d083f849SBarry Smith   Collective on dm
5203e5e52638SMatthew G. Knepley 
5204e5e52638SMatthew G. Knepley   Input Parameter:
5205e5e52638SMatthew G. Knepley . dm - The DM
5206e5e52638SMatthew G. Knepley 
5207e5e52638SMatthew G. Knepley   Output Parameter:
5208e5e52638SMatthew G. Knepley . newdm - The DM
5209e5e52638SMatthew G. Knepley 
5210e5e52638SMatthew G. Knepley   Level: advanced
5211e5e52638SMatthew G. Knepley 
5212e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5213e5e52638SMatthew G. Knepley @*/
5214e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5215e5e52638SMatthew G. Knepley {
5216e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5217e5e52638SMatthew G. Knepley 
5218e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5219e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5220e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5221e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5222e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5223e5e52638SMatthew G. Knepley }
5224e5e52638SMatthew G. Knepley 
5225b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5226b64e0483SPeter Brune {
5227b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5228b64e0483SPeter Brune   PetscErrorCode ierr;
5229b64e0483SPeter Brune   Vec coords,ccoords;
52306dbf9973SLawrence Mitchell   Mat inject;
5231b64e0483SPeter Brune   PetscFunctionBegin;
5232b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5233b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5234b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5235b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5236b64e0483SPeter Brune   if (coords && !ccoords) {
5237b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
52386668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
52396dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
52402adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
52416dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5242b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5243b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5244b64e0483SPeter Brune   }
5245b64e0483SPeter Brune   PetscFunctionReturn(0);
5246b64e0483SPeter Brune }
5247b64e0483SPeter Brune 
524803dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
524903dadc2fSPeter Brune {
525003dadc2fSPeter Brune   DM dm_coord,subdm_coord;
525103dadc2fSPeter Brune   PetscErrorCode ierr;
525203dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
525303dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
525403dadc2fSPeter Brune   PetscFunctionBegin;
525503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
525603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
525703dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
525803dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
525903dadc2fSPeter Brune   if (coords && !ccoords) {
526003dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
52616668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
526203dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
526324640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
526403dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
526503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
526603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
52671ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
526803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
526903dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
527003dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
527103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
527203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
527303dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
527403dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
527503dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
527603dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
527703dadc2fSPeter Brune   }
527803dadc2fSPeter Brune   PetscFunctionReturn(0);
527903dadc2fSPeter Brune }
528003dadc2fSPeter Brune 
5281c73cfb54SMatthew G. Knepley /*@
5282c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5283c73cfb54SMatthew G. Knepley 
5284c73cfb54SMatthew G. Knepley   Not collective
5285c73cfb54SMatthew G. Knepley 
5286c73cfb54SMatthew G. Knepley   Input Parameter:
5287c73cfb54SMatthew G. Knepley . dm - The DM
5288c73cfb54SMatthew G. Knepley 
5289c73cfb54SMatthew G. Knepley   Output Parameter:
5290c73cfb54SMatthew G. Knepley . dim - The topological dimension
5291c73cfb54SMatthew G. Knepley 
5292c73cfb54SMatthew G. Knepley   Level: beginner
5293c73cfb54SMatthew G. Knepley 
5294c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5295c73cfb54SMatthew G. Knepley @*/
5296c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5297c73cfb54SMatthew G. Knepley {
5298c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5299c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5300534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5301c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5302c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5303c73cfb54SMatthew G. Knepley }
5304c73cfb54SMatthew G. Knepley 
5305c73cfb54SMatthew G. Knepley /*@
5306c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5307c73cfb54SMatthew G. Knepley 
5308c73cfb54SMatthew G. Knepley   Collective on dm
5309c73cfb54SMatthew G. Knepley 
5310c73cfb54SMatthew G. Knepley   Input Parameters:
5311c73cfb54SMatthew G. Knepley + dm - The DM
5312c73cfb54SMatthew G. Knepley - dim - The topological dimension
5313c73cfb54SMatthew G. Knepley 
5314c73cfb54SMatthew G. Knepley   Level: beginner
5315c73cfb54SMatthew G. Knepley 
5316c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5317c73cfb54SMatthew G. Knepley @*/
5318c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5319c73cfb54SMatthew G. Knepley {
5320e5e52638SMatthew G. Knepley   PetscDS        ds;
5321f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5322f17e8794SMatthew G. Knepley 
5323c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5324c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5325c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5326c73cfb54SMatthew G. Knepley   dm->dim = dim;
5327e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5328e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5329c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5330c73cfb54SMatthew G. Knepley }
5331c73cfb54SMatthew G. Knepley 
5332793f3fe5SMatthew G. Knepley /*@
5333793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5334793f3fe5SMatthew G. Knepley 
5335d083f849SBarry Smith   Collective on dm
5336793f3fe5SMatthew G. Knepley 
5337793f3fe5SMatthew G. Knepley   Input Parameters:
5338793f3fe5SMatthew G. Knepley + dm - the DM
5339793f3fe5SMatthew G. Knepley - dim - the dimension
5340793f3fe5SMatthew G. Knepley 
5341793f3fe5SMatthew G. Knepley   Output Parameters:
5342793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5343aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5344793f3fe5SMatthew G. Knepley 
5345793f3fe5SMatthew G. Knepley   Note:
5346793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5347a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5348793f3fe5SMatthew G. Knepley   then the interval is empty.
5349793f3fe5SMatthew G. Knepley 
5350793f3fe5SMatthew G. Knepley   Level: intermediate
5351793f3fe5SMatthew G. Knepley 
5352793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5353793f3fe5SMatthew G. Knepley @*/
5354793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5355793f3fe5SMatthew G. Knepley {
5356793f3fe5SMatthew G. Knepley   PetscInt       d;
5357793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5358793f3fe5SMatthew G. Knepley 
5359793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5360793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5361793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5362793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5363b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5364793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5365793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5366793f3fe5SMatthew G. Knepley }
5367793f3fe5SMatthew G. Knepley 
53686636e97aSMatthew G Knepley /*@
53696636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
53706636e97aSMatthew G Knepley 
5371d083f849SBarry Smith   Collective on dm
53726636e97aSMatthew G Knepley 
53736636e97aSMatthew G Knepley   Input Parameters:
53746636e97aSMatthew G Knepley + dm - the DM
53756636e97aSMatthew G Knepley - c - coordinate vector
53766636e97aSMatthew G Knepley 
53772dd40e9bSPatrick Sanan   Notes:
53782dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
53792dd40e9bSPatrick Sanan 
53802dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
53816636e97aSMatthew G Knepley 
53826636e97aSMatthew G Knepley   Level: intermediate
53836636e97aSMatthew G Knepley 
53842dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
53856636e97aSMatthew G Knepley @*/
53866636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
53876636e97aSMatthew G Knepley {
53886636e97aSMatthew G Knepley   PetscErrorCode ierr;
53896636e97aSMatthew G Knepley 
53906636e97aSMatthew G Knepley   PetscFunctionBegin;
53916636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53926636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
53936636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
53946636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
53956636e97aSMatthew G Knepley   dm->coordinates = c;
53966636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5397b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
539803dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
53996636e97aSMatthew G Knepley   PetscFunctionReturn(0);
54006636e97aSMatthew G Knepley }
54016636e97aSMatthew G Knepley 
54026636e97aSMatthew G Knepley /*@
54036636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
54046636e97aSMatthew G Knepley 
54057058e716SVaclav Hapla   Not collective
54066636e97aSMatthew G Knepley 
54076636e97aSMatthew G Knepley    Input Parameters:
54086636e97aSMatthew G Knepley +  dm - the DM
54096636e97aSMatthew G Knepley -  c - coordinate vector
54106636e97aSMatthew G Knepley 
54112dd40e9bSPatrick Sanan   Notes:
54126636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
54136636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
54146636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
54156636e97aSMatthew G Knepley 
54162dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
54172dd40e9bSPatrick Sanan 
54186636e97aSMatthew G Knepley   Level: intermediate
54196636e97aSMatthew G Knepley 
54206636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
54216636e97aSMatthew G Knepley @*/
54226636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
54236636e97aSMatthew G Knepley {
54246636e97aSMatthew G Knepley   PetscErrorCode ierr;
54256636e97aSMatthew G Knepley 
54266636e97aSMatthew G Knepley   PetscFunctionBegin;
54276636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
54296636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
54306636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
54318865f1eaSKarl Rupp 
54326636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
54338865f1eaSKarl Rupp 
54346636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
54356636e97aSMatthew G Knepley   PetscFunctionReturn(0);
54366636e97aSMatthew G Knepley }
54376636e97aSMatthew G Knepley 
54386636e97aSMatthew G Knepley /*@
54396636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
54406636e97aSMatthew G Knepley 
5441d083f849SBarry Smith   Collective on dm
54426636e97aSMatthew G Knepley 
54436636e97aSMatthew G Knepley   Input Parameter:
54446636e97aSMatthew G Knepley . dm - the DM
54456636e97aSMatthew G Knepley 
54466636e97aSMatthew G Knepley   Output Parameter:
54476636e97aSMatthew G Knepley . c - global coordinate vector
54486636e97aSMatthew G Knepley 
54496636e97aSMatthew G Knepley   Note:
54506636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
54516636e97aSMatthew G Knepley 
54526636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
54536636e97aSMatthew G Knepley 
54546636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
54556636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
54566636e97aSMatthew G Knepley 
54576636e97aSMatthew G Knepley   Level: intermediate
54586636e97aSMatthew G Knepley 
54596636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
54606636e97aSMatthew G Knepley @*/
54616636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
54626636e97aSMatthew G Knepley {
54636636e97aSMatthew G Knepley   PetscErrorCode ierr;
54646636e97aSMatthew G Knepley 
54656636e97aSMatthew G Knepley   PetscFunctionBegin;
54666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54676636e97aSMatthew G Knepley   PetscValidPointer(c,2);
54681f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
54690298fd71SBarry Smith     DM        cdm = NULL;
54701970a576SMatthew G. Knepley     PetscBool localized;
54716636e97aSMatthew G Knepley 
54726636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
54736636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
54741970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
54751970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
54761970a576SMatthew G. Knepley     if (localized) {
54771970a576SMatthew G. Knepley       PetscInt cdim;
54781970a576SMatthew G. Knepley 
54791970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
54801970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
54811970a576SMatthew G. Knepley     }
54826636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
54836636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
54846636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
54856636e97aSMatthew G Knepley   }
54866636e97aSMatthew G Knepley   *c = dm->coordinates;
54876636e97aSMatthew G Knepley   PetscFunctionReturn(0);
54886636e97aSMatthew G Knepley }
54896636e97aSMatthew G Knepley 
54906636e97aSMatthew G Knepley /*@
549181e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
549281e9a530SVaclav Hapla 
5493d083f849SBarry Smith   Collective on dm
549481e9a530SVaclav Hapla 
549581e9a530SVaclav Hapla   Input Parameter:
549681e9a530SVaclav Hapla . dm - the DM
549781e9a530SVaclav Hapla 
549881e9a530SVaclav Hapla   Level: advanced
549981e9a530SVaclav Hapla 
550081e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
550181e9a530SVaclav Hapla @*/
550281e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
550381e9a530SVaclav Hapla {
550481e9a530SVaclav Hapla   PetscErrorCode ierr;
550581e9a530SVaclav Hapla 
550681e9a530SVaclav Hapla   PetscFunctionBegin;
550781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
550881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
55091970a576SMatthew G. Knepley     DM        cdm = NULL;
55101970a576SMatthew G. Knepley     PetscBool localized;
55111970a576SMatthew G. Knepley 
551281e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
551381e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
55141970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
55151970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
55161970a576SMatthew G. Knepley     if (localized) {
55171970a576SMatthew G. Knepley       PetscInt cdim;
55181970a576SMatthew G. Knepley 
55191970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
55201970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
55211970a576SMatthew G. Knepley     }
552281e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
552381e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
552481e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
552581e9a530SVaclav Hapla   }
552681e9a530SVaclav Hapla   PetscFunctionReturn(0);
552781e9a530SVaclav Hapla }
552881e9a530SVaclav Hapla 
552981e9a530SVaclav Hapla /*@
55306636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
55316636e97aSMatthew G Knepley 
5532d083f849SBarry Smith   Collective on dm
55336636e97aSMatthew G Knepley 
55346636e97aSMatthew G Knepley   Input Parameter:
55356636e97aSMatthew G Knepley . dm - the DM
55366636e97aSMatthew G Knepley 
55376636e97aSMatthew G Knepley   Output Parameter:
55386636e97aSMatthew G Knepley . c - coordinate vector
55396636e97aSMatthew G Knepley 
55406636e97aSMatthew G Knepley   Note:
55416636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
55426636e97aSMatthew G Knepley 
55436636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
55446636e97aSMatthew G Knepley 
55456636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
55466636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
55476636e97aSMatthew G Knepley 
55486636e97aSMatthew G Knepley   Level: intermediate
55496636e97aSMatthew G Knepley 
555081e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
55516636e97aSMatthew G Knepley @*/
55526636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
55536636e97aSMatthew G Knepley {
55546636e97aSMatthew G Knepley   PetscErrorCode ierr;
55556636e97aSMatthew G Knepley 
55566636e97aSMatthew G Knepley   PetscFunctionBegin;
55576636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55586636e97aSMatthew G Knepley   PetscValidPointer(c,2);
555981e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
55606636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
55616636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55626636e97aSMatthew G Knepley }
55636636e97aSMatthew G Knepley 
556481e9a530SVaclav Hapla /*@
556581e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
556681e9a530SVaclav Hapla 
556781e9a530SVaclav Hapla   Not collective
556881e9a530SVaclav Hapla 
556981e9a530SVaclav Hapla   Input Parameter:
557081e9a530SVaclav Hapla . dm - the DM
557181e9a530SVaclav Hapla 
557281e9a530SVaclav Hapla   Output Parameter:
557381e9a530SVaclav Hapla . c - coordinate vector
557481e9a530SVaclav Hapla 
557581e9a530SVaclav Hapla   Level: advanced
557681e9a530SVaclav Hapla 
557781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
557881e9a530SVaclav Hapla @*/
557981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
558081e9a530SVaclav Hapla {
558181e9a530SVaclav Hapla   PetscFunctionBegin;
558281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
558381e9a530SVaclav Hapla   PetscValidPointer(c,2);
558481e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
55856636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
55866636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55876636e97aSMatthew G Knepley }
55886636e97aSMatthew G Knepley 
55892db98f8dSVaclav Hapla /*@
55902db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
55912db98f8dSVaclav Hapla 
55922db98f8dSVaclav Hapla   Not collective
55932db98f8dSVaclav Hapla 
55942db98f8dSVaclav Hapla   Input Parameter:
55952db98f8dSVaclav Hapla + dm - the DM
55962db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
55972db98f8dSVaclav Hapla 
55982db98f8dSVaclav Hapla   Output Parameter:
55992db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
56002db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
56012db98f8dSVaclav Hapla 
56022db98f8dSVaclav Hapla   Note:
56032db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
56042db98f8dSVaclav Hapla 
56052db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
56062db98f8dSVaclav Hapla 
56072db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
56082db98f8dSVaclav Hapla 
56092db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56102db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56112db98f8dSVaclav Hapla 
56122db98f8dSVaclav Hapla   Level: advanced
56132db98f8dSVaclav Hapla 
56142db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
56152db98f8dSVaclav Hapla @*/
56162db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
56172db98f8dSVaclav Hapla {
56182db98f8dSVaclav Hapla   PetscSection        cs, newcs;
56192db98f8dSVaclav Hapla   Vec                 coords;
56202db98f8dSVaclav Hapla   const PetscScalar   *arr;
56212db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
56222db98f8dSVaclav Hapla   PetscInt            n;
56232db98f8dSVaclav Hapla   PetscErrorCode      ierr;
56242db98f8dSVaclav Hapla 
56252db98f8dSVaclav Hapla   PetscFunctionBegin;
56262db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56272db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
56282db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
56292db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
56302db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
5631*1bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
5632*1bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
56332db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
56342db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
56352db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
56362db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
56372db98f8dSVaclav Hapla   if (pCoord) {
56382db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
56392db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
56402db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
56412db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5642ad9ac99dSVaclav Hapla   } else {
5643ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
56442db98f8dSVaclav Hapla   }
5645ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5646ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
56472db98f8dSVaclav Hapla   PetscFunctionReturn(0);
56482db98f8dSVaclav Hapla }
56492db98f8dSVaclav Hapla 
5650f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5651f19dbd58SToby Isaac {
5652f19dbd58SToby Isaac   PetscErrorCode ierr;
5653f19dbd58SToby Isaac 
5654f19dbd58SToby Isaac   PetscFunctionBegin;
5655f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5656f19dbd58SToby Isaac   PetscValidPointer(field,2);
5657f19dbd58SToby Isaac   if (!dm->coordinateField) {
5658f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5659f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5660f19dbd58SToby Isaac     }
5661f19dbd58SToby Isaac   }
5662f19dbd58SToby Isaac   *field = dm->coordinateField;
5663f19dbd58SToby Isaac   PetscFunctionReturn(0);
5664f19dbd58SToby Isaac }
5665f19dbd58SToby Isaac 
5666f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5667f19dbd58SToby Isaac {
5668f19dbd58SToby Isaac   PetscErrorCode ierr;
5669f19dbd58SToby Isaac 
5670f19dbd58SToby Isaac   PetscFunctionBegin;
5671f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5672f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5673c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5674f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5675f19dbd58SToby Isaac   dm->coordinateField = field;
5676f19dbd58SToby Isaac   PetscFunctionReturn(0);
5677f19dbd58SToby Isaac }
5678f19dbd58SToby Isaac 
56796636e97aSMatthew G Knepley /*@
56801cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
56816636e97aSMatthew G Knepley 
5682d083f849SBarry Smith   Collective on dm
56836636e97aSMatthew G Knepley 
56846636e97aSMatthew G Knepley   Input Parameter:
56856636e97aSMatthew G Knepley . dm - the DM
56866636e97aSMatthew G Knepley 
56876636e97aSMatthew G Knepley   Output Parameter:
56886636e97aSMatthew G Knepley . cdm - coordinate DM
56896636e97aSMatthew G Knepley 
56906636e97aSMatthew G Knepley   Level: intermediate
56916636e97aSMatthew G Knepley 
56921cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
56936636e97aSMatthew G Knepley @*/
56946636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
56956636e97aSMatthew G Knepley {
56966636e97aSMatthew G Knepley   PetscErrorCode ierr;
56976636e97aSMatthew G Knepley 
56986636e97aSMatthew G Knepley   PetscFunctionBegin;
56996636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57006636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
57016636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5702308f8a94SToby Isaac     DM cdm;
5703308f8a94SToby Isaac 
570482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5705308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5706308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5707308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5708308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5709308f8a94SToby Isaac     dm->coordinateDM = cdm;
57106636e97aSMatthew G Knepley   }
57116636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
57126636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57136636e97aSMatthew G Knepley }
5714e87bb0d3SMatthew G Knepley 
57151cfe2091SMatthew G. Knepley /*@
57161cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
57171cfe2091SMatthew G. Knepley 
5718d083f849SBarry Smith   Logically Collective on dm
57191cfe2091SMatthew G. Knepley 
57201cfe2091SMatthew G. Knepley   Input Parameters:
57211cfe2091SMatthew G. Knepley + dm - the DM
57221cfe2091SMatthew G. Knepley - cdm - coordinate DM
57231cfe2091SMatthew G. Knepley 
57241cfe2091SMatthew G. Knepley   Level: intermediate
57251cfe2091SMatthew G. Knepley 
57261cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
57271cfe2091SMatthew G. Knepley @*/
57281cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
57291cfe2091SMatthew G. Knepley {
57301cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
57311cfe2091SMatthew G. Knepley 
57321cfe2091SMatthew G. Knepley   PetscFunctionBegin;
57331cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57341cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5735f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
57361cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
57371cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
57381cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
57391cfe2091SMatthew G. Knepley }
57401cfe2091SMatthew G. Knepley 
574146e270d4SMatthew G. Knepley /*@
574246e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
574346e270d4SMatthew G. Knepley 
574446e270d4SMatthew G. Knepley   Not Collective
574546e270d4SMatthew G. Knepley 
574646e270d4SMatthew G. Knepley   Input Parameter:
574746e270d4SMatthew G. Knepley . dm - The DM object
574846e270d4SMatthew G. Knepley 
574946e270d4SMatthew G. Knepley   Output Parameter:
575046e270d4SMatthew G. Knepley . dim - The embedding dimension
575146e270d4SMatthew G. Knepley 
575246e270d4SMatthew G. Knepley   Level: intermediate
575346e270d4SMatthew G. Knepley 
575492fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
575546e270d4SMatthew G. Knepley @*/
575646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
575746e270d4SMatthew G. Knepley {
575846e270d4SMatthew G. Knepley   PetscFunctionBegin;
575946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5760534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
57619a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
57629a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
57639a9a41abSToby Isaac   }
576446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
576546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
576646e270d4SMatthew G. Knepley }
576746e270d4SMatthew G. Knepley 
576846e270d4SMatthew G. Knepley /*@
576946e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
577046e270d4SMatthew G. Knepley 
577146e270d4SMatthew G. Knepley   Not Collective
577246e270d4SMatthew G. Knepley 
577346e270d4SMatthew G. Knepley   Input Parameters:
577446e270d4SMatthew G. Knepley + dm  - The DM object
577546e270d4SMatthew G. Knepley - dim - The embedding dimension
577646e270d4SMatthew G. Knepley 
577746e270d4SMatthew G. Knepley   Level: intermediate
577846e270d4SMatthew G. Knepley 
577992fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
578046e270d4SMatthew G. Knepley @*/
578146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
578246e270d4SMatthew G. Knepley {
5783e5e52638SMatthew G. Knepley   PetscDS        ds;
5784f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5785f17e8794SMatthew G. Knepley 
578646e270d4SMatthew G. Knepley   PetscFunctionBegin;
578746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
578846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5789e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5790e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
579146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
579246e270d4SMatthew G. Knepley }
579346e270d4SMatthew G. Knepley 
5794e8abe2deSMatthew G. Knepley /*@
5795e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5796e8abe2deSMatthew G. Knepley 
5797d083f849SBarry Smith   Collective on dm
5798e8abe2deSMatthew G. Knepley 
5799e8abe2deSMatthew G. Knepley   Input Parameter:
5800e8abe2deSMatthew G. Knepley . dm - The DM object
5801e8abe2deSMatthew G. Knepley 
5802e8abe2deSMatthew G. Knepley   Output Parameter:
5803e8abe2deSMatthew G. Knepley . section - The PetscSection object
5804e8abe2deSMatthew G. Knepley 
5805e8abe2deSMatthew G. Knepley   Level: intermediate
5806e8abe2deSMatthew G. Knepley 
580792fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
5808e8abe2deSMatthew G. Knepley @*/
5809e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5810e8abe2deSMatthew G. Knepley {
5811e8abe2deSMatthew G. Knepley   DM             cdm;
5812e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5813e8abe2deSMatthew G. Knepley 
5814e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5815e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5816e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5817e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
581892fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
5819e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5820e8abe2deSMatthew G. Knepley }
5821e8abe2deSMatthew G. Knepley 
5822e8abe2deSMatthew G. Knepley /*@
5823e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5824e8abe2deSMatthew G. Knepley 
5825e8abe2deSMatthew G. Knepley   Not Collective
5826e8abe2deSMatthew G. Knepley 
5827e8abe2deSMatthew G. Knepley   Input Parameters:
5828e8abe2deSMatthew G. Knepley + dm      - The DM object
582946e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5830e8abe2deSMatthew G. Knepley - section - The PetscSection object
5831e8abe2deSMatthew G. Knepley 
5832e8abe2deSMatthew G. Knepley   Level: intermediate
5833e8abe2deSMatthew G. Knepley 
583492fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
5835e8abe2deSMatthew G. Knepley @*/
583646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5837e8abe2deSMatthew G. Knepley {
5838e8abe2deSMatthew G. Knepley   DM             cdm;
5839e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5840e8abe2deSMatthew G. Knepley 
5841e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5842e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
584346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5844e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
584592fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
584646e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
58474c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
584846e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
584946e270d4SMatthew G. Knepley 
585046e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
585146e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
585246e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
585346e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
585446e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
585546e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
585646e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
585746e270d4SMatthew G. Knepley     }
5858ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
585946e270d4SMatthew G. Knepley   }
5860e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5861e8abe2deSMatthew G. Knepley }
5862e8abe2deSMatthew G. Knepley 
58635dc8c3f7SMatthew G. Knepley /*@C
586490b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
58655dc8c3f7SMatthew G. Knepley 
58665dc8c3f7SMatthew G. Knepley   Input Parameters:
586790b157c4SStefano Zampini . dm      - The DM object
586890b157c4SStefano Zampini 
586990b157c4SStefano Zampini   Output Parameters:
587090b157c4SStefano Zampini + per     - Whether the DM is periodic or not
58715dc8c3f7SMatthew 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
58725dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
58735dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
58745dc8c3f7SMatthew G. Knepley 
58755dc8c3f7SMatthew G. Knepley   Level: developer
58765dc8c3f7SMatthew G. Knepley 
58775dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
58785dc8c3f7SMatthew G. Knepley @*/
587990b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5880c6b900c6SMatthew G. Knepley {
5881c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5882c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
588390b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5884c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5885c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
58865dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5887c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5888c6b900c6SMatthew G. Knepley }
5889c6b900c6SMatthew G. Knepley 
58905dc8c3f7SMatthew G. Knepley /*@C
58915dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
58925dc8c3f7SMatthew G. Knepley 
58935dc8c3f7SMatthew G. Knepley   Input Parameters:
58945dc8c3f7SMatthew G. Knepley + dm      - The DM object
589590b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
58965dc8c3f7SMatthew 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
58975dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
58985dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
58995dc8c3f7SMatthew G. Knepley 
59005dc8c3f7SMatthew G. Knepley   Level: developer
59015dc8c3f7SMatthew G. Knepley 
59025dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
59035dc8c3f7SMatthew G. Knepley @*/
590490b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
5905c6b900c6SMatthew G. Knepley {
5906c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
5907c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
5908c6b900c6SMatthew G. Knepley 
5909c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5910c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
591190b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
591290b157c4SStefano Zampini   if (maxCell) {
5913534a8f05SLisandro Dalcin     PetscValidRealPointer(maxCell,3);
5914534a8f05SLisandro Dalcin     PetscValidRealPointer(L,4);
591590b157c4SStefano Zampini     PetscValidPointer(bd,5);
591690b157c4SStefano Zampini   }
59175dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
59185dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
591990b157c4SStefano Zampini   if (maxCell) {
59205dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
59215dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
592290b157c4SStefano Zampini   }
5923072d7d67SStefano Zampini   dm->periodic = per;
5924c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5925c6b900c6SMatthew G. Knepley }
5926c6b900c6SMatthew G. Knepley 
59272e17dfb7SMatthew G. Knepley /*@
59282e17dfb7SMatthew 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.
59292e17dfb7SMatthew G. Knepley 
59302e17dfb7SMatthew G. Knepley   Input Parameters:
59312e17dfb7SMatthew G. Knepley + dm     - The DM
593265da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
593365da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
59342e17dfb7SMatthew G. Knepley 
59352e17dfb7SMatthew G. Knepley   Output Parameter:
59362e17dfb7SMatthew G. Knepley . out - The localized coordinate point
59372e17dfb7SMatthew G. Knepley 
59382e17dfb7SMatthew G. Knepley   Level: developer
59392e17dfb7SMatthew G. Knepley 
59402e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
59412e17dfb7SMatthew G. Knepley @*/
594265da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
59432e17dfb7SMatthew G. Knepley {
59442e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
59452e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
59462e17dfb7SMatthew G. Knepley 
59472e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
59482e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
59492e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
59502e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
59512e17dfb7SMatthew G. Knepley   } else {
595265da65dcSMatthew G. Knepley     if (endpoint) {
595365da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
5954da3333bfSMatthew 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)) {
5955da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
595665da65dcSMatthew G. Knepley         } else {
5957da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
595865da65dcSMatthew G. Knepley         }
595965da65dcSMatthew G. Knepley       }
596065da65dcSMatthew G. Knepley     } else {
59612e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
59621118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
59632e17dfb7SMatthew G. Knepley       }
59642e17dfb7SMatthew G. Knepley     }
596565da65dcSMatthew G. Knepley   }
59662e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
59672e17dfb7SMatthew G. Knepley }
59682e17dfb7SMatthew G. Knepley 
59692e17dfb7SMatthew G. Knepley /*
59702e17dfb7SMatthew 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.
59712e17dfb7SMatthew G. Knepley 
59722e17dfb7SMatthew G. Knepley   Input Parameters:
59732e17dfb7SMatthew G. Knepley + dm     - The DM
59742e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
59752e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
59762e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
59772e17dfb7SMatthew G. Knepley 
59782e17dfb7SMatthew G. Knepley   Output Parameter:
59792e17dfb7SMatthew G. Knepley . out - The localized coordinate point
59802e17dfb7SMatthew G. Knepley 
59812e17dfb7SMatthew G. Knepley   Level: developer
59822e17dfb7SMatthew G. Knepley 
59832e17dfb7SMatthew 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
59842e17dfb7SMatthew G. Knepley 
59852e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
59862e17dfb7SMatthew G. Knepley */
59872e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
59882e17dfb7SMatthew G. Knepley {
59892e17dfb7SMatthew G. Knepley   PetscInt d;
59902e17dfb7SMatthew G. Knepley 
59912e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
59922e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
59932e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
59942e17dfb7SMatthew G. Knepley   } else {
59952e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5996908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
59972e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
59982e17dfb7SMatthew G. Knepley       } else {
59992e17dfb7SMatthew G. Knepley         out[d] = in[d];
60002e17dfb7SMatthew G. Knepley       }
60012e17dfb7SMatthew G. Knepley     }
60022e17dfb7SMatthew G. Knepley   }
60032e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60042e17dfb7SMatthew G. Knepley }
60052e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
60062e17dfb7SMatthew G. Knepley {
60072e17dfb7SMatthew G. Knepley   PetscInt d;
60082e17dfb7SMatthew G. Knepley 
60092e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60102e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60112e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60122e17dfb7SMatthew G. Knepley   } else {
60132e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6014908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
60152e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
60162e17dfb7SMatthew G. Knepley       } else {
60172e17dfb7SMatthew G. Knepley         out[d] = in[d];
60182e17dfb7SMatthew G. Knepley       }
60192e17dfb7SMatthew G. Knepley     }
60202e17dfb7SMatthew G. Knepley   }
60212e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60222e17dfb7SMatthew G. Knepley }
60232e17dfb7SMatthew G. Knepley 
60242e17dfb7SMatthew G. Knepley /*
60252e17dfb7SMatthew 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.
60262e17dfb7SMatthew G. Knepley 
60272e17dfb7SMatthew G. Knepley   Input Parameters:
60282e17dfb7SMatthew G. Knepley + dm     - The DM
60292e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
60302e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
60312e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
60322e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
60332e17dfb7SMatthew G. Knepley 
60342e17dfb7SMatthew G. Knepley   Output Parameter:
60352e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
60362e17dfb7SMatthew G. Knepley 
60372e17dfb7SMatthew G. Knepley   Level: developer
60382e17dfb7SMatthew G. Knepley 
60392e17dfb7SMatthew 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
60402e17dfb7SMatthew G. Knepley 
60412e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
60422e17dfb7SMatthew G. Knepley */
60432e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
60442e17dfb7SMatthew G. Knepley {
60452e17dfb7SMatthew G. Knepley   PetscInt d;
60462e17dfb7SMatthew G. Knepley 
60472e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60482e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60492e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
60502e17dfb7SMatthew G. Knepley   } else {
60512e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6052908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
60532e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
60542e17dfb7SMatthew G. Knepley       } else {
60552e17dfb7SMatthew G. Knepley         out[d] += in[d];
60562e17dfb7SMatthew G. Knepley       }
60572e17dfb7SMatthew G. Knepley     }
60582e17dfb7SMatthew G. Knepley   }
60592e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60602e17dfb7SMatthew G. Knepley }
60612e17dfb7SMatthew G. Knepley 
606236447a5eSToby Isaac /*@
60638f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
60648f700142SStefano Zampini 
60658f700142SStefano Zampini   Not collective
606636447a5eSToby Isaac 
606736447a5eSToby Isaac   Input Parameter:
606836447a5eSToby Isaac . dm - The DM
606936447a5eSToby Isaac 
607036447a5eSToby Isaac   Output Parameter:
607136447a5eSToby Isaac   areLocalized - True if localized
607236447a5eSToby Isaac 
607336447a5eSToby Isaac   Level: developer
607436447a5eSToby Isaac 
60758f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
607636447a5eSToby Isaac @*/
60778f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
607836447a5eSToby Isaac {
607936447a5eSToby Isaac   DM             cdm;
608036447a5eSToby Isaac   PetscSection   coordSection;
608146a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
608246a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
608336447a5eSToby Isaac   PetscErrorCode ierr;
608436447a5eSToby Isaac 
608536447a5eSToby Isaac   PetscFunctionBegin;
608636447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6087534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
60888b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
608946a3a80fSLisandro Dalcin 
609036447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
609136447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
609246a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
60939f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
609446a3a80fSLisandro Dalcin 
60959f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
609646a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
609736447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
609836447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
609946a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
610046a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
610136447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
610246a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
610336447a5eSToby Isaac   }
61048f700142SStefano Zampini   *areLocalized = alreadyLocalized;
610536447a5eSToby Isaac   PetscFunctionReturn(0);
610636447a5eSToby Isaac }
610736447a5eSToby Isaac 
61088f700142SStefano Zampini /*@
61098f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
61108f700142SStefano Zampini 
61118f700142SStefano Zampini   Collective on dm
61128f700142SStefano Zampini 
61138f700142SStefano Zampini   Input Parameter:
61148f700142SStefano Zampini . dm - The DM
61158f700142SStefano Zampini 
61168f700142SStefano Zampini   Output Parameter:
61178f700142SStefano Zampini   areLocalized - True if localized
61188f700142SStefano Zampini 
61198f700142SStefano Zampini   Level: developer
61208f700142SStefano Zampini 
61218f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
61228f700142SStefano Zampini @*/
61238f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
61248f700142SStefano Zampini {
61258f700142SStefano Zampini   PetscBool      localized;
61268f700142SStefano Zampini   PetscErrorCode ierr;
61278f700142SStefano Zampini 
61288f700142SStefano Zampini   PetscFunctionBegin;
61298f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6130534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
61318f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
61328f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
61338f700142SStefano Zampini   PetscFunctionReturn(0);
61348f700142SStefano Zampini }
613536447a5eSToby Isaac 
61362e17dfb7SMatthew G. Knepley /*@
6137492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
61382e17dfb7SMatthew G. Knepley 
61398f700142SStefano Zampini   Collective on dm
61408f700142SStefano Zampini 
61412e17dfb7SMatthew G. Knepley   Input Parameter:
61422e17dfb7SMatthew G. Knepley . dm - The DM
61432e17dfb7SMatthew G. Knepley 
61442e17dfb7SMatthew G. Knepley   Level: developer
61452e17dfb7SMatthew G. Knepley 
61468f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
61472e17dfb7SMatthew G. Knepley @*/
61482e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
61492e17dfb7SMatthew G. Knepley {
61502e17dfb7SMatthew G. Knepley   DM             cdm;
61512e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
61522e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
61533e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
61543e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6155e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
61563e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
61573e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
61582e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
61592e17dfb7SMatthew G. Knepley 
61602e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61612e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
616292c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6163f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6164f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6165f7cbd40bSStefano Zampini 
61662e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
61672e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
61682e17dfb7SMatthew G. Knepley   {
61692e17dfb7SMatthew G. Knepley     PetscBool isplex;
61702e17dfb7SMatthew G. Knepley 
61712e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
61722e17dfb7SMatthew G. Knepley     if (isplex) {
61732e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
61743e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
617569291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
61763e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
61773e922f36SToby Isaac       newStart = vStart;
61783e922f36SToby Isaac       newEnd   = vEnd;
61793e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
61803e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
61813e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
61823e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
61833e922f36SToby Isaac       }
61842e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
61852e17dfb7SMatthew G. Knepley   }
61862e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
618743eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
61882e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
61893e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6190e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
61913e922f36SToby Isaac 
61922e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
61932e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
61942e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
61952e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
61963e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
61973e922f36SToby Isaac 
619869291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
61993e922f36SToby Isaac   localized = &anchor[bs];
62003e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
62013e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
62023e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
62033e922f36SToby Isaac 
62043e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
62053e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
62063e922f36SToby Isaac       PetscInt     b;
62073e922f36SToby Isaac 
62083e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
62093e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62103e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
62113e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
62123e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
62133e922f36SToby Isaac         for (b = 0; b < bs; b++) {
62143e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
62153e922f36SToby Isaac         }
62163e922f36SToby Isaac         if (b < bs) break;
62173e922f36SToby Isaac       }
62183e922f36SToby Isaac       if (d < dof/bs) {
62193e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
62203e922f36SToby Isaac           PetscInt cdof;
62213e922f36SToby Isaac 
62223e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
62233e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
62243e922f36SToby Isaac         }
62253e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
62263e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
62273e922f36SToby Isaac       }
62283e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62293e922f36SToby Isaac     }
62303e922f36SToby Isaac   }
62313e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
62323e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
623369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
62343e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
623569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
62363e922f36SToby Isaac     PetscFunctionReturn(0);
62373e922f36SToby Isaac   }
62382e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
62392e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
62402e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
62412e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
62422e17dfb7SMatthew G. Knepley   }
62432e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
62442e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6245c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
62462e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
62472e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
62482e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
62492e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6250c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
62512e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
62522e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
62532e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
62542e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
62552e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
62562e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
62572e17dfb7SMatthew G. Knepley   }
62583e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
62593e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
62603e922f36SToby Isaac 
62612e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
62622e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
62633e922f36SToby Isaac       PetscInt     b, cdof;
62642e17dfb7SMatthew G. Knepley 
62653e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
62663e922f36SToby Isaac       if (!cdof) continue;
62672e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62682e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
62692e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
62702e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
62712e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62722e17dfb7SMatthew G. Knepley     }
62733e922f36SToby Isaac   }
627469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
627569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6276c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
62772e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
62782e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
62792e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
62802e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
62812e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
62822e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
62832e17dfb7SMatthew G. Knepley }
62842e17dfb7SMatthew G. Knepley 
6285e87bb0d3SMatthew G Knepley /*@
62863a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6287e87bb0d3SMatthew G Knepley 
6288d083f849SBarry Smith   Collective on v (see explanation below)
6289e87bb0d3SMatthew G Knepley 
6290e87bb0d3SMatthew G Knepley   Input Parameters:
6291e87bb0d3SMatthew G Knepley + dm - The DM
62923a93e3b7SToby Isaac . v - The Vec of points
629362a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
62943a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6295e87bb0d3SMatthew G Knepley 
629661e3bb9bSMatthew G Knepley   Output Parameter:
629762a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
629862a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
62993a93e3b7SToby Isaac 
6300e87bb0d3SMatthew G Knepley 
6301e87bb0d3SMatthew G Knepley   Level: developer
630261e3bb9bSMatthew G Knepley 
630362a38674SMatthew G. Knepley   Notes:
63043a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
630562a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
63063a93e3b7SToby Isaac 
63073a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
630862a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
63093a93e3b7SToby Isaac 
63103a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
63113a93e3b7SToby Isaac 
631262a38674SMatthew G. Knepley $    const PetscSFNode *cells;
631362a38674SMatthew G. Knepley $    PetscInt           nFound;
6314a6216909SToby Isaac $    const PetscInt    *found;
631562a38674SMatthew G. Knepley $
6316a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
63173a93e3b7SToby Isaac 
63183a93e3b7SToby 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
63193a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
63203a93e3b7SToby Isaac 
632162a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
632261e3bb9bSMatthew G Knepley @*/
632362a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6324e87bb0d3SMatthew G Knepley {
6325735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6326735aa83eSMatthew G Knepley 
6327e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6328e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6329e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6330e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
63313a93e3b7SToby Isaac   if (*cellSF) {
63323a93e3b7SToby Isaac     PetscMPIInt result;
63333a93e3b7SToby Isaac 
6334e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6335a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
63363a93e3b7SToby 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");
6337e0fc9d1bSMatthew G. Knepley   } else {
63383a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
63393a93e3b7SToby Isaac   }
6340b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
634147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
634262a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
634347a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6344e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6345e87bb0d3SMatthew G Knepley }
634614f150ffSMatthew G. Knepley 
6347f4d763aaSMatthew G. Knepley /*@
6348f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6349f4d763aaSMatthew G. Knepley 
63508f700142SStefano Zampini   Collective on dm
63518f700142SStefano Zampini 
6352f4d763aaSMatthew G. Knepley   Input Parameter:
6353f4d763aaSMatthew G. Knepley . dm - The original DM
6354f4d763aaSMatthew G. Knepley 
6355f4d763aaSMatthew G. Knepley   Output Parameter:
6356f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6357f4d763aaSMatthew G. Knepley 
6358f4d763aaSMatthew G. Knepley   Level: intermediate
6359f4d763aaSMatthew G. Knepley 
6360e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6361f4d763aaSMatthew G. Knepley @*/
636214f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
636314f150ffSMatthew G. Knepley {
6364c26acbdeSMatthew G. Knepley   PetscSection   section;
63652d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
636614f150ffSMatthew G. Knepley   PetscErrorCode ierr;
636714f150ffSMatthew G. Knepley 
636814f150ffSMatthew G. Knepley   PetscFunctionBegin;
636914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
637014f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
637192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6372c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6373127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
63742d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6375c26acbdeSMatthew G. Knepley     *odm = dm;
6376c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6377c26acbdeSMatthew G. Knepley   }
637814f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6379c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
638014f150ffSMatthew G. Knepley     PetscSF      sf;
638114f150ffSMatthew G. Knepley 
638214f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6383e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
638414f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
638592fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
638614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
638714f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
638815b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6389e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
639014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
639114f150ffSMatthew G. Knepley   }
639214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
639314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
639414f150ffSMatthew G. Knepley }
6395f4d763aaSMatthew G. Knepley 
6396f4d763aaSMatthew G. Knepley /*@
6397cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6398f4d763aaSMatthew G. Knepley 
6399f4d763aaSMatthew G. Knepley   Input Parameter:
6400f4d763aaSMatthew G. Knepley . dm - The original DM
6401f4d763aaSMatthew G. Knepley 
6402cdb7a50dSMatthew G. Knepley   Output Parameters:
6403cdb7a50dSMatthew G. Knepley + num - The output sequence number
6404cdb7a50dSMatthew G. Knepley - val - The output sequence value
6405f4d763aaSMatthew G. Knepley 
6406f4d763aaSMatthew G. Knepley   Level: intermediate
6407f4d763aaSMatthew G. Knepley 
6408f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6409f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6410f4d763aaSMatthew G. Knepley 
6411f4d763aaSMatthew G. Knepley .seealso: VecView()
6412f4d763aaSMatthew G. Knepley @*/
6413cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6414f4d763aaSMatthew G. Knepley {
6415f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6416f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6417534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6418534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6419f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6420f4d763aaSMatthew G. Knepley }
6421f4d763aaSMatthew G. Knepley 
6422f4d763aaSMatthew G. Knepley /*@
6423cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6424f4d763aaSMatthew G. Knepley 
6425f4d763aaSMatthew G. Knepley   Input Parameters:
6426f4d763aaSMatthew G. Knepley + dm - The original DM
6427cdb7a50dSMatthew G. Knepley . num - The output sequence number
6428cdb7a50dSMatthew G. Knepley - val - The output sequence value
6429f4d763aaSMatthew G. Knepley 
6430f4d763aaSMatthew G. Knepley   Level: intermediate
6431f4d763aaSMatthew G. Knepley 
6432f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6433f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6434f4d763aaSMatthew G. Knepley 
6435f4d763aaSMatthew G. Knepley .seealso: VecView()
6436f4d763aaSMatthew G. Knepley @*/
6437cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6438f4d763aaSMatthew G. Knepley {
6439f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6440f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6441f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6442cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6443cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6444cdb7a50dSMatthew G. Knepley }
6445cdb7a50dSMatthew G. Knepley 
6446cdb7a50dSMatthew G. Knepley /*@C
6447cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6448cdb7a50dSMatthew G. Knepley 
6449cdb7a50dSMatthew G. Knepley   Input Parameters:
6450cdb7a50dSMatthew G. Knepley + dm   - The original DM
6451cdb7a50dSMatthew G. Knepley . name - The sequence name
6452cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6453cdb7a50dSMatthew G. Knepley 
6454cdb7a50dSMatthew G. Knepley   Output Parameter:
6455cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6456cdb7a50dSMatthew G. Knepley 
6457cdb7a50dSMatthew G. Knepley   Level: intermediate
6458cdb7a50dSMatthew G. Knepley 
6459cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6460cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6461cdb7a50dSMatthew G. Knepley 
6462cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6463cdb7a50dSMatthew G. Knepley @*/
6464cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6465cdb7a50dSMatthew G. Knepley {
6466cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6467cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6468cdb7a50dSMatthew G. Knepley 
6469cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6470cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6471cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6472534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6473cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6474cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6475cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6476cdb7a50dSMatthew G. Knepley     PetscScalar value;
6477cdb7a50dSMatthew G. Knepley 
647839d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
64794aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6480cdb7a50dSMatthew G. Knepley #endif
6481cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6482f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6483f4d763aaSMatthew G. Knepley }
64848e4ac7eaSMatthew G. Knepley 
64858e4ac7eaSMatthew G. Knepley /*@
64868e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
64878e4ac7eaSMatthew G. Knepley 
64888e4ac7eaSMatthew G. Knepley   Not collective
64898e4ac7eaSMatthew G. Knepley 
64908e4ac7eaSMatthew G. Knepley   Input Parameter:
64918e4ac7eaSMatthew G. Knepley . dm - The DM
64928e4ac7eaSMatthew G. Knepley 
64938e4ac7eaSMatthew G. Knepley   Output Parameter:
64948e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
64958e4ac7eaSMatthew G. Knepley 
64968e4ac7eaSMatthew G. Knepley   Level: beginner
64978e4ac7eaSMatthew G. Knepley 
64988e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
64998e4ac7eaSMatthew G. Knepley @*/
65008e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
65018e4ac7eaSMatthew G. Knepley {
65028e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65038e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6504534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
65058e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
65068e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
65078e4ac7eaSMatthew G. Knepley }
65088e4ac7eaSMatthew G. Knepley 
65098e4ac7eaSMatthew G. Knepley /*@
65105d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
65118e4ac7eaSMatthew G. Knepley 
65128e4ac7eaSMatthew G. Knepley   Collective on dm
65138e4ac7eaSMatthew G. Knepley 
65148e4ac7eaSMatthew G. Knepley   Input Parameters:
65158e4ac7eaSMatthew G. Knepley + dm - The DM
65168e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
65178e4ac7eaSMatthew G. Knepley 
65185d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
65195d3b26e6SMatthew G. Knepley 
65208e4ac7eaSMatthew G. Knepley   Level: beginner
65218e4ac7eaSMatthew G. Knepley 
65225d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
65238e4ac7eaSMatthew G. Knepley @*/
65248e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
65258e4ac7eaSMatthew G. Knepley {
65268e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65278e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65288833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
65298e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
65308e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
65318e4ac7eaSMatthew G. Knepley }
6532c58f1c22SToby Isaac 
6533c58f1c22SToby Isaac 
6534c58f1c22SToby Isaac /*@C
6535c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6536c58f1c22SToby Isaac 
6537c58f1c22SToby Isaac   Not Collective
6538c58f1c22SToby Isaac 
6539c58f1c22SToby Isaac   Input Parameters:
6540c58f1c22SToby Isaac + dm   - The DM object
6541c58f1c22SToby Isaac - name - The label name
6542c58f1c22SToby Isaac 
6543c58f1c22SToby Isaac   Level: intermediate
6544c58f1c22SToby Isaac 
6545c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6546c58f1c22SToby Isaac @*/
6547c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6548c58f1c22SToby Isaac {
6549c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
6550c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
6551d67d17b1SMatthew G. Knepley   const char    *lname;
6552c58f1c22SToby Isaac   PetscErrorCode ierr;
6553c58f1c22SToby Isaac 
6554c58f1c22SToby Isaac   PetscFunctionBegin;
6555c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6556c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6557c58f1c22SToby Isaac   while (next) {
6558d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6559d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6560c58f1c22SToby Isaac     if (flg) break;
6561c58f1c22SToby Isaac     next = next->next;
6562c58f1c22SToby Isaac   }
6563c58f1c22SToby Isaac   if (!flg) {
6564c58f1c22SToby Isaac     DMLabelLink tmpLabel;
6565c58f1c22SToby Isaac 
6566c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6567d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &tmpLabel->label);CHKERRQ(ierr);
6568c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
6569c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
6570c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
6571c58f1c22SToby Isaac   }
6572c58f1c22SToby Isaac   PetscFunctionReturn(0);
6573c58f1c22SToby Isaac }
6574c58f1c22SToby Isaac 
6575c58f1c22SToby Isaac /*@C
6576c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6577c58f1c22SToby Isaac 
6578c58f1c22SToby Isaac   Not Collective
6579c58f1c22SToby Isaac 
6580c58f1c22SToby Isaac   Input Parameters:
6581c58f1c22SToby Isaac + dm   - The DM object
6582c58f1c22SToby Isaac . name - The label name
6583c58f1c22SToby Isaac - point - The mesh point
6584c58f1c22SToby Isaac 
6585c58f1c22SToby Isaac   Output Parameter:
6586c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6587c58f1c22SToby Isaac 
6588c58f1c22SToby Isaac   Level: beginner
6589c58f1c22SToby Isaac 
6590c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6591c58f1c22SToby Isaac @*/
6592c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6593c58f1c22SToby Isaac {
6594c58f1c22SToby Isaac   DMLabel        label;
6595c58f1c22SToby Isaac   PetscErrorCode ierr;
6596c58f1c22SToby Isaac 
6597c58f1c22SToby Isaac   PetscFunctionBegin;
6598c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6599c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6600c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
660113903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6602c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6603c58f1c22SToby Isaac   PetscFunctionReturn(0);
6604c58f1c22SToby Isaac }
6605c58f1c22SToby Isaac 
6606c58f1c22SToby Isaac /*@C
6607c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6608c58f1c22SToby Isaac 
6609c58f1c22SToby Isaac   Not Collective
6610c58f1c22SToby Isaac 
6611c58f1c22SToby Isaac   Input Parameters:
6612c58f1c22SToby Isaac + dm   - The DM object
6613c58f1c22SToby Isaac . name - The label name
6614c58f1c22SToby Isaac . point - The mesh point
6615c58f1c22SToby Isaac - value - The label value for this point
6616c58f1c22SToby Isaac 
6617c58f1c22SToby Isaac   Output Parameter:
6618c58f1c22SToby Isaac 
6619c58f1c22SToby Isaac   Level: beginner
6620c58f1c22SToby Isaac 
6621c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6622c58f1c22SToby Isaac @*/
6623c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6624c58f1c22SToby Isaac {
6625c58f1c22SToby Isaac   DMLabel        label;
6626c58f1c22SToby Isaac   PetscErrorCode ierr;
6627c58f1c22SToby Isaac 
6628c58f1c22SToby Isaac   PetscFunctionBegin;
6629c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6630c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6631c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6632c58f1c22SToby Isaac   if (!label) {
6633c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6634c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6635c58f1c22SToby Isaac   }
6636c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6637c58f1c22SToby Isaac   PetscFunctionReturn(0);
6638c58f1c22SToby Isaac }
6639c58f1c22SToby Isaac 
6640c58f1c22SToby Isaac /*@C
6641c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6642c58f1c22SToby Isaac 
6643c58f1c22SToby Isaac   Not Collective
6644c58f1c22SToby Isaac 
6645c58f1c22SToby Isaac   Input Parameters:
6646c58f1c22SToby Isaac + dm   - The DM object
6647c58f1c22SToby Isaac . name - The label name
6648c58f1c22SToby Isaac . point - The mesh point
6649c58f1c22SToby Isaac - value - The label value for this point
6650c58f1c22SToby Isaac 
6651c58f1c22SToby Isaac   Output Parameter:
6652c58f1c22SToby Isaac 
6653c58f1c22SToby Isaac   Level: beginner
6654c58f1c22SToby Isaac 
6655c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6656c58f1c22SToby Isaac @*/
6657c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6658c58f1c22SToby Isaac {
6659c58f1c22SToby Isaac   DMLabel        label;
6660c58f1c22SToby Isaac   PetscErrorCode ierr;
6661c58f1c22SToby Isaac 
6662c58f1c22SToby Isaac   PetscFunctionBegin;
6663c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6664c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6665c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6666c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6667c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6668c58f1c22SToby Isaac   PetscFunctionReturn(0);
6669c58f1c22SToby Isaac }
6670c58f1c22SToby Isaac 
6671c58f1c22SToby Isaac /*@C
6672c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6673c58f1c22SToby Isaac 
6674c58f1c22SToby Isaac   Not Collective
6675c58f1c22SToby Isaac 
6676c58f1c22SToby Isaac   Input Parameters:
6677c58f1c22SToby Isaac + dm   - The DM object
6678c58f1c22SToby Isaac - name - The label name
6679c58f1c22SToby Isaac 
6680c58f1c22SToby Isaac   Output Parameter:
6681c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6682c58f1c22SToby Isaac 
6683c58f1c22SToby Isaac   Level: beginner
6684c58f1c22SToby Isaac 
6685df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6686c58f1c22SToby Isaac @*/
6687c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
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);
6695534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
6696c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6697c58f1c22SToby Isaac   *size = 0;
6698c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6699c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6700c58f1c22SToby Isaac   PetscFunctionReturn(0);
6701c58f1c22SToby Isaac }
6702c58f1c22SToby Isaac 
6703c58f1c22SToby Isaac /*@C
6704c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac   Not Collective
6707c58f1c22SToby Isaac 
6708c58f1c22SToby Isaac   Input Parameters:
6709c58f1c22SToby Isaac + mesh - The DM object
6710c58f1c22SToby Isaac - name - The label name
6711c58f1c22SToby Isaac 
6712c58f1c22SToby Isaac   Output Parameter:
6713c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6714c58f1c22SToby Isaac 
6715c58f1c22SToby Isaac   Level: beginner
6716c58f1c22SToby Isaac 
6717c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6718c58f1c22SToby Isaac @*/
6719c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6720c58f1c22SToby Isaac {
6721c58f1c22SToby Isaac   DMLabel        label;
6722c58f1c22SToby Isaac   PetscErrorCode ierr;
6723c58f1c22SToby Isaac 
6724c58f1c22SToby Isaac   PetscFunctionBegin;
6725c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6726c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6727c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6728c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6729c58f1c22SToby Isaac   *ids = NULL;
6730dab2e251SBlaise Bourdin  if (label) {
6731c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6732dab2e251SBlaise Bourdin   } else {
6733dab2e251SBlaise Bourdin     /* returning an empty IS */
6734dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6735dab2e251SBlaise Bourdin   }
6736c58f1c22SToby Isaac   PetscFunctionReturn(0);
6737c58f1c22SToby Isaac }
6738c58f1c22SToby Isaac 
6739c58f1c22SToby Isaac /*@C
6740c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6741c58f1c22SToby Isaac 
6742c58f1c22SToby Isaac   Not Collective
6743c58f1c22SToby Isaac 
6744c58f1c22SToby Isaac   Input Parameters:
6745c58f1c22SToby Isaac + dm - The DM object
6746c58f1c22SToby Isaac . name - The label name
6747c58f1c22SToby Isaac - value - The stratum value
6748c58f1c22SToby Isaac 
6749c58f1c22SToby Isaac   Output Parameter:
6750c58f1c22SToby Isaac . size - The stratum size
6751c58f1c22SToby Isaac 
6752c58f1c22SToby Isaac   Level: beginner
6753c58f1c22SToby Isaac 
6754c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6755c58f1c22SToby Isaac @*/
6756c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6757c58f1c22SToby Isaac {
6758c58f1c22SToby Isaac   DMLabel        label;
6759c58f1c22SToby Isaac   PetscErrorCode ierr;
6760c58f1c22SToby Isaac 
6761c58f1c22SToby Isaac   PetscFunctionBegin;
6762c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6763c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6764534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
6765c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6766c58f1c22SToby Isaac   *size = 0;
6767c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6768c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6769c58f1c22SToby Isaac   PetscFunctionReturn(0);
6770c58f1c22SToby Isaac }
6771c58f1c22SToby Isaac 
6772c58f1c22SToby Isaac /*@C
6773c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6774c58f1c22SToby Isaac 
6775c58f1c22SToby Isaac   Not Collective
6776c58f1c22SToby Isaac 
6777c58f1c22SToby Isaac   Input Parameters:
6778c58f1c22SToby Isaac + dm - The DM object
6779c58f1c22SToby Isaac . name - The label name
6780c58f1c22SToby Isaac - value - The stratum value
6781c58f1c22SToby Isaac 
6782c58f1c22SToby Isaac   Output Parameter:
6783c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6784c58f1c22SToby Isaac 
6785c58f1c22SToby Isaac   Level: beginner
6786c58f1c22SToby Isaac 
6787c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6788c58f1c22SToby Isaac @*/
6789c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6790c58f1c22SToby Isaac {
6791c58f1c22SToby Isaac   DMLabel        label;
6792c58f1c22SToby Isaac   PetscErrorCode ierr;
6793c58f1c22SToby Isaac 
6794c58f1c22SToby Isaac   PetscFunctionBegin;
6795c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6796c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6797c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6798c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6799c58f1c22SToby Isaac   *points = NULL;
6800c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6801c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6802c58f1c22SToby Isaac   PetscFunctionReturn(0);
6803c58f1c22SToby Isaac }
6804c58f1c22SToby Isaac 
68054de306b1SToby Isaac /*@C
68069044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68074de306b1SToby Isaac 
68084de306b1SToby Isaac   Not Collective
68094de306b1SToby Isaac 
68104de306b1SToby Isaac   Input Parameters:
68114de306b1SToby Isaac + dm - The DM object
68124de306b1SToby Isaac . name - The label name
68134de306b1SToby Isaac . value - The stratum value
68144de306b1SToby Isaac - points - The stratum points
68154de306b1SToby Isaac 
68164de306b1SToby Isaac   Level: beginner
68174de306b1SToby Isaac 
68184de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
68194de306b1SToby Isaac @*/
68204de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
68214de306b1SToby Isaac {
68224de306b1SToby Isaac   DMLabel        label;
68234de306b1SToby Isaac   PetscErrorCode ierr;
68244de306b1SToby Isaac 
68254de306b1SToby Isaac   PetscFunctionBegin;
68264de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68274de306b1SToby Isaac   PetscValidCharPointer(name, 2);
68284de306b1SToby Isaac   PetscValidPointer(points, 4);
68294de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
68304de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
68314de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
68324de306b1SToby Isaac   PetscFunctionReturn(0);
68334de306b1SToby Isaac }
68344de306b1SToby Isaac 
6835c58f1c22SToby Isaac /*@C
6836c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6837c58f1c22SToby Isaac 
6838c58f1c22SToby Isaac   Not Collective
6839c58f1c22SToby Isaac 
6840c58f1c22SToby Isaac   Input Parameters:
6841c58f1c22SToby Isaac + dm   - The DM object
6842c58f1c22SToby Isaac . name - The label name
6843c58f1c22SToby Isaac - value - The label value for this point
6844c58f1c22SToby Isaac 
6845c58f1c22SToby Isaac   Output Parameter:
6846c58f1c22SToby Isaac 
6847c58f1c22SToby Isaac   Level: beginner
6848c58f1c22SToby Isaac 
6849c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6850c58f1c22SToby Isaac @*/
6851c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
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);
6859c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6860c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6861c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6862c58f1c22SToby Isaac   PetscFunctionReturn(0);
6863c58f1c22SToby Isaac }
6864c58f1c22SToby Isaac 
6865c58f1c22SToby Isaac /*@
6866c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Not Collective
6869c58f1c22SToby Isaac 
6870c58f1c22SToby Isaac   Input Parameter:
6871c58f1c22SToby Isaac . dm   - The DM object
6872c58f1c22SToby Isaac 
6873c58f1c22SToby Isaac   Output Parameter:
6874c58f1c22SToby Isaac . numLabels - the number of Labels
6875c58f1c22SToby Isaac 
6876c58f1c22SToby Isaac   Level: intermediate
6877c58f1c22SToby Isaac 
6878c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6879c58f1c22SToby Isaac @*/
6880c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6881c58f1c22SToby Isaac {
6882c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6883c58f1c22SToby Isaac   PetscInt  n    = 0;
6884c58f1c22SToby Isaac 
6885c58f1c22SToby Isaac   PetscFunctionBegin;
6886c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6887534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
6888c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6889c58f1c22SToby Isaac   *numLabels = n;
6890c58f1c22SToby Isaac   PetscFunctionReturn(0);
6891c58f1c22SToby Isaac }
6892c58f1c22SToby Isaac 
6893c58f1c22SToby Isaac /*@C
6894c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6895c58f1c22SToby Isaac 
6896c58f1c22SToby Isaac   Not Collective
6897c58f1c22SToby Isaac 
6898c58f1c22SToby Isaac   Input Parameters:
6899c58f1c22SToby Isaac + dm - The DM object
6900c58f1c22SToby Isaac - n  - the label number
6901c58f1c22SToby Isaac 
6902c58f1c22SToby Isaac   Output Parameter:
6903c58f1c22SToby Isaac . name - the label name
6904c58f1c22SToby Isaac 
6905c58f1c22SToby Isaac   Level: intermediate
6906c58f1c22SToby Isaac 
6907c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6908c58f1c22SToby Isaac @*/
6909c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6910c58f1c22SToby Isaac {
6911c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6912c58f1c22SToby Isaac   PetscInt       l    = 0;
6913d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
6914c58f1c22SToby Isaac 
6915c58f1c22SToby Isaac   PetscFunctionBegin;
6916c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6917c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6918c58f1c22SToby Isaac   while (next) {
6919c58f1c22SToby Isaac     if (l == n) {
6920d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
6921c58f1c22SToby Isaac       PetscFunctionReturn(0);
6922c58f1c22SToby Isaac     }
6923c58f1c22SToby Isaac     ++l;
6924c58f1c22SToby Isaac     next = next->next;
6925c58f1c22SToby Isaac   }
6926c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6927c58f1c22SToby Isaac }
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac /*@C
6930c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6931c58f1c22SToby Isaac 
6932c58f1c22SToby Isaac   Not Collective
6933c58f1c22SToby Isaac 
6934c58f1c22SToby Isaac   Input Parameters:
6935c58f1c22SToby Isaac + dm   - The DM object
6936c58f1c22SToby Isaac - name - The label name
6937c58f1c22SToby Isaac 
6938c58f1c22SToby Isaac   Output Parameter:
6939c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
6940c58f1c22SToby Isaac 
6941c58f1c22SToby Isaac   Level: intermediate
6942c58f1c22SToby Isaac 
6943c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6944c58f1c22SToby Isaac @*/
6945c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6946c58f1c22SToby Isaac {
6947c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6948d67d17b1SMatthew G. Knepley   const char    *lname;
6949c58f1c22SToby Isaac   PetscErrorCode ierr;
6950c58f1c22SToby Isaac 
6951c58f1c22SToby Isaac   PetscFunctionBegin;
6952c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6953c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6954534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6955c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6956c58f1c22SToby Isaac   while (next) {
6957d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6958d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
6959c58f1c22SToby Isaac     if (*hasLabel) break;
6960c58f1c22SToby Isaac     next = next->next;
6961c58f1c22SToby Isaac   }
6962c58f1c22SToby Isaac   PetscFunctionReturn(0);
6963c58f1c22SToby Isaac }
6964c58f1c22SToby Isaac 
6965c58f1c22SToby Isaac /*@C
6966c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   Not Collective
6969c58f1c22SToby Isaac 
6970c58f1c22SToby Isaac   Input Parameters:
6971c58f1c22SToby Isaac + dm   - The DM object
6972c58f1c22SToby Isaac - name - The label name
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   Output Parameter:
6975c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6976c58f1c22SToby Isaac 
6977c58f1c22SToby Isaac   Level: intermediate
6978c58f1c22SToby Isaac 
6979c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6980c58f1c22SToby Isaac @*/
6981c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6982c58f1c22SToby Isaac {
6983c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6984c58f1c22SToby Isaac   PetscBool      hasLabel;
6985d67d17b1SMatthew G. Knepley   const char    *lname;
6986c58f1c22SToby Isaac   PetscErrorCode ierr;
6987c58f1c22SToby Isaac 
6988c58f1c22SToby Isaac   PetscFunctionBegin;
6989c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6990c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6991c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6992c58f1c22SToby Isaac   *label = NULL;
6993c58f1c22SToby Isaac   while (next) {
6994d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6995d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6996c58f1c22SToby Isaac     if (hasLabel) {
6997c58f1c22SToby Isaac       *label = next->label;
6998c58f1c22SToby Isaac       break;
6999c58f1c22SToby Isaac     }
7000c58f1c22SToby Isaac     next = next->next;
7001c58f1c22SToby Isaac   }
7002c58f1c22SToby Isaac   PetscFunctionReturn(0);
7003c58f1c22SToby Isaac }
7004c58f1c22SToby Isaac 
7005c58f1c22SToby Isaac /*@C
7006c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7007c58f1c22SToby Isaac 
7008c58f1c22SToby Isaac   Not Collective
7009c58f1c22SToby Isaac 
7010c58f1c22SToby Isaac   Input Parameters:
7011c58f1c22SToby Isaac + dm - The DM object
7012c58f1c22SToby Isaac - n  - the label number
7013c58f1c22SToby Isaac 
7014c58f1c22SToby Isaac   Output Parameter:
7015c58f1c22SToby Isaac . label - the label
7016c58f1c22SToby Isaac 
7017c58f1c22SToby Isaac   Level: intermediate
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7020c58f1c22SToby Isaac @*/
7021c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7022c58f1c22SToby Isaac {
7023c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
7024c58f1c22SToby Isaac   PetscInt    l    = 0;
7025c58f1c22SToby Isaac 
7026c58f1c22SToby Isaac   PetscFunctionBegin;
7027c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7028c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7029c58f1c22SToby Isaac   while (next) {
7030c58f1c22SToby Isaac     if (l == n) {
7031c58f1c22SToby Isaac       *label = next->label;
7032c58f1c22SToby Isaac       PetscFunctionReturn(0);
7033c58f1c22SToby Isaac     }
7034c58f1c22SToby Isaac     ++l;
7035c58f1c22SToby Isaac     next = next->next;
7036c58f1c22SToby Isaac   }
7037c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7038c58f1c22SToby Isaac }
7039c58f1c22SToby Isaac 
7040c58f1c22SToby Isaac /*@C
7041c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7042c58f1c22SToby Isaac 
7043c58f1c22SToby Isaac   Not Collective
7044c58f1c22SToby Isaac 
7045c58f1c22SToby Isaac   Input Parameters:
7046c58f1c22SToby Isaac + dm   - The DM object
7047c58f1c22SToby Isaac - label - The DMLabel
7048c58f1c22SToby Isaac 
7049c58f1c22SToby Isaac   Level: developer
7050c58f1c22SToby Isaac 
7051c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7052c58f1c22SToby Isaac @*/
7053c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7054c58f1c22SToby Isaac {
7055c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
7056c58f1c22SToby Isaac   PetscBool      hasLabel;
7057d67d17b1SMatthew G. Knepley   const char    *lname;
7058c58f1c22SToby Isaac   PetscErrorCode ierr;
7059c58f1c22SToby Isaac 
7060c58f1c22SToby Isaac   PetscFunctionBegin;
7061c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7062d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7063d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7064d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7065c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7066c58f1c22SToby Isaac   tmpLabel->label  = label;
7067c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
7068c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
7069c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
7070c58f1c22SToby Isaac   PetscFunctionReturn(0);
7071c58f1c22SToby Isaac }
7072c58f1c22SToby Isaac 
7073c58f1c22SToby Isaac /*@C
7074c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
7075c58f1c22SToby Isaac 
7076c58f1c22SToby Isaac   Not Collective
7077c58f1c22SToby Isaac 
7078c58f1c22SToby Isaac   Input Parameters:
7079c58f1c22SToby Isaac + dm   - The DM object
7080c58f1c22SToby Isaac - name - The label name
7081c58f1c22SToby Isaac 
7082c58f1c22SToby Isaac   Output Parameter:
7083c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7084c58f1c22SToby Isaac 
7085c58f1c22SToby Isaac   Level: developer
7086c58f1c22SToby Isaac 
7087c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7088c58f1c22SToby Isaac @*/
7089c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7090c58f1c22SToby Isaac {
7091c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7092c58f1c22SToby Isaac   DMLabelLink    last = NULL;
7093c58f1c22SToby Isaac   PetscBool      hasLabel;
7094d67d17b1SMatthew G. Knepley   const char    *lname;
7095c58f1c22SToby Isaac   PetscErrorCode ierr;
7096c58f1c22SToby Isaac 
7097c58f1c22SToby Isaac   PetscFunctionBegin;
7098c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7099c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
7100c58f1c22SToby Isaac   *label = NULL;
7101c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
7102c58f1c22SToby Isaac   while (next) {
7103d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7104d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7105c58f1c22SToby Isaac     if (hasLabel) {
7106c58f1c22SToby Isaac       if (last) last->next       = next->next;
7107c58f1c22SToby Isaac       else      dm->labels->next = next->next;
7108c58f1c22SToby Isaac       next->next = NULL;
7109c58f1c22SToby Isaac       *label     = next->label;
7110c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
7111c58f1c22SToby Isaac       if (hasLabel) {
7112c58f1c22SToby Isaac         dm->depthLabel = NULL;
7113c58f1c22SToby Isaac       }
7114c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
7115c58f1c22SToby Isaac       break;
7116c58f1c22SToby Isaac     }
7117c58f1c22SToby Isaac     last = next;
7118c58f1c22SToby Isaac     next = next->next;
7119c58f1c22SToby Isaac   }
7120c58f1c22SToby Isaac   PetscFunctionReturn(0);
7121c58f1c22SToby Isaac }
7122c58f1c22SToby Isaac 
7123c58f1c22SToby Isaac /*@C
7124c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7125c58f1c22SToby Isaac 
7126c58f1c22SToby Isaac   Not Collective
7127c58f1c22SToby Isaac 
7128c58f1c22SToby Isaac   Input Parameters:
7129c58f1c22SToby Isaac + dm   - The DM object
7130c58f1c22SToby Isaac - name - The label name
7131c58f1c22SToby Isaac 
7132c58f1c22SToby Isaac   Output Parameter:
7133c58f1c22SToby Isaac . output - The flag for output
7134c58f1c22SToby Isaac 
7135c58f1c22SToby Isaac   Level: developer
7136c58f1c22SToby Isaac 
7137c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7138c58f1c22SToby Isaac @*/
7139c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7140c58f1c22SToby Isaac {
7141c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7142d67d17b1SMatthew G. Knepley   const char    *lname;
7143c58f1c22SToby Isaac   PetscErrorCode ierr;
7144c58f1c22SToby Isaac 
7145c58f1c22SToby Isaac   PetscFunctionBegin;
7146c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7147c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7148c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7149c58f1c22SToby Isaac   while (next) {
7150c58f1c22SToby Isaac     PetscBool flg;
7151c58f1c22SToby Isaac 
7152d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7153d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7154c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7155c58f1c22SToby Isaac     next = next->next;
7156c58f1c22SToby Isaac   }
7157c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7158c58f1c22SToby Isaac }
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac /*@C
7161c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7162c58f1c22SToby Isaac 
7163c58f1c22SToby Isaac   Not Collective
7164c58f1c22SToby Isaac 
7165c58f1c22SToby Isaac   Input Parameters:
7166c58f1c22SToby Isaac + dm     - The DM object
7167c58f1c22SToby Isaac . name   - The label name
7168c58f1c22SToby Isaac - output - The flag for output
7169c58f1c22SToby Isaac 
7170c58f1c22SToby Isaac   Level: developer
7171c58f1c22SToby Isaac 
7172c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7173c58f1c22SToby Isaac @*/
7174c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7175c58f1c22SToby Isaac {
7176c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7177d67d17b1SMatthew G. Knepley   const char    *lname;
7178c58f1c22SToby Isaac   PetscErrorCode ierr;
7179c58f1c22SToby Isaac 
7180c58f1c22SToby Isaac   PetscFunctionBegin;
7181c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7182534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7183c58f1c22SToby Isaac   while (next) {
7184c58f1c22SToby Isaac     PetscBool flg;
7185c58f1c22SToby Isaac 
7186d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7187d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7188c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7189c58f1c22SToby Isaac     next = next->next;
7190c58f1c22SToby Isaac   }
7191c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7192c58f1c22SToby Isaac }
7193c58f1c22SToby Isaac 
7194c58f1c22SToby Isaac 
7195c58f1c22SToby Isaac /*@
7196c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7197c58f1c22SToby Isaac 
7198d083f849SBarry Smith   Collective on dmA
7199c58f1c22SToby Isaac 
7200c58f1c22SToby Isaac   Input Parameter:
72012e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
7202c58f1c22SToby Isaac 
7203c58f1c22SToby Isaac   Output Parameter:
72042e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
7205c58f1c22SToby Isaac 
7206c58f1c22SToby Isaac   Level: intermediate
7207c58f1c22SToby Isaac 
7208c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7209c58f1c22SToby Isaac 
7210367003a6SStefano Zampini .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
7211c58f1c22SToby Isaac @*/
7212c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
7213c58f1c22SToby Isaac {
7214c58f1c22SToby Isaac   PetscInt       numLabels, l;
7215c58f1c22SToby Isaac   PetscErrorCode ierr;
7216c58f1c22SToby Isaac 
7217c58f1c22SToby Isaac   PetscFunctionBegin;
7218c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
7219c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
7220c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
7221c58f1c22SToby Isaac     DMLabel     label, labelNew;
7222c58f1c22SToby Isaac     const char *name;
7223c58f1c22SToby Isaac     PetscBool   flg;
7224c58f1c22SToby Isaac 
7225c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
7226c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7227c58f1c22SToby Isaac     if (flg) continue;
72287d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
72297d5acc75SStefano Zampini     if (flg) continue;
7230c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
7231c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
7232c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
7233c58f1c22SToby Isaac   }
7234c58f1c22SToby Isaac   PetscFunctionReturn(0);
7235c58f1c22SToby Isaac }
7236a8fb8f29SToby Isaac 
7237a8fb8f29SToby Isaac /*@
7238a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7239a8fb8f29SToby Isaac 
7240a8fb8f29SToby Isaac   Input Parameter:
7241a8fb8f29SToby Isaac . dm - The DM object
7242a8fb8f29SToby Isaac 
7243a8fb8f29SToby Isaac   Output Parameter:
7244a8fb8f29SToby Isaac . cdm - The coarse DM
7245a8fb8f29SToby Isaac 
7246a8fb8f29SToby Isaac   Level: intermediate
7247a8fb8f29SToby Isaac 
7248a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7249a8fb8f29SToby Isaac @*/
7250a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7251a8fb8f29SToby Isaac {
7252a8fb8f29SToby Isaac   PetscFunctionBegin;
7253a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7254a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7255a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7256a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7257a8fb8f29SToby Isaac }
7258a8fb8f29SToby Isaac 
7259a8fb8f29SToby Isaac /*@
7260a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7261a8fb8f29SToby Isaac 
7262a8fb8f29SToby Isaac   Input Parameters:
7263a8fb8f29SToby Isaac + dm - The DM object
7264a8fb8f29SToby Isaac - cdm - The coarse DM
7265a8fb8f29SToby Isaac 
7266a8fb8f29SToby Isaac   Level: intermediate
7267a8fb8f29SToby Isaac 
7268a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7269a8fb8f29SToby Isaac @*/
7270a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7271a8fb8f29SToby Isaac {
7272a8fb8f29SToby Isaac   PetscErrorCode ierr;
7273a8fb8f29SToby Isaac 
7274a8fb8f29SToby Isaac   PetscFunctionBegin;
7275a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7276a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7277a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7278a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7279a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7280a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7281a8fb8f29SToby Isaac }
7282a8fb8f29SToby Isaac 
728388bdff64SToby Isaac /*@
728488bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
728588bdff64SToby Isaac 
728688bdff64SToby Isaac   Input Parameter:
728788bdff64SToby Isaac . dm - The DM object
728888bdff64SToby Isaac 
728988bdff64SToby Isaac   Output Parameter:
729088bdff64SToby Isaac . fdm - The fine DM
729188bdff64SToby Isaac 
729288bdff64SToby Isaac   Level: intermediate
729388bdff64SToby Isaac 
729488bdff64SToby Isaac .seealso: DMSetFineDM()
729588bdff64SToby Isaac @*/
729688bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
729788bdff64SToby Isaac {
729888bdff64SToby Isaac   PetscFunctionBegin;
729988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
730088bdff64SToby Isaac   PetscValidPointer(fdm, 2);
730188bdff64SToby Isaac   *fdm = dm->fineMesh;
730288bdff64SToby Isaac   PetscFunctionReturn(0);
730388bdff64SToby Isaac }
730488bdff64SToby Isaac 
730588bdff64SToby Isaac /*@
730688bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
730788bdff64SToby Isaac 
730888bdff64SToby Isaac   Input Parameters:
730988bdff64SToby Isaac + dm - The DM object
731088bdff64SToby Isaac - fdm - The fine DM
731188bdff64SToby Isaac 
731288bdff64SToby Isaac   Level: intermediate
731388bdff64SToby Isaac 
731488bdff64SToby Isaac .seealso: DMGetFineDM()
731588bdff64SToby Isaac @*/
731688bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
731788bdff64SToby Isaac {
731888bdff64SToby Isaac   PetscErrorCode ierr;
731988bdff64SToby Isaac 
732088bdff64SToby Isaac   PetscFunctionBegin;
732188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
732288bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
732388bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
732488bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
732588bdff64SToby Isaac   dm->fineMesh = fdm;
732688bdff64SToby Isaac   PetscFunctionReturn(0);
732788bdff64SToby Isaac }
732888bdff64SToby Isaac 
7329a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7330a6ba4734SToby Isaac 
7331a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7332a6ba4734SToby Isaac {
7333e5e52638SMatthew G. Knepley   PetscInt       d;
7334a6ba4734SToby Isaac   PetscErrorCode ierr;
7335a6ba4734SToby Isaac 
7336a6ba4734SToby Isaac   PetscFunctionBegin;
7337e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7338e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7339e5e52638SMatthew G. Knepley   }
7340a6ba4734SToby Isaac   PetscFunctionReturn(0);
7341a6ba4734SToby Isaac }
7342a6ba4734SToby Isaac 
7343a6ba4734SToby Isaac /*@C
7344a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7345a6ba4734SToby Isaac 
7346a6ba4734SToby Isaac   Input Parameters:
73474c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7348f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7349a6ba4734SToby Isaac . name        - The BC name
7350a6ba4734SToby Isaac . labelname   - The label defining constrained points
7351a6ba4734SToby Isaac . field       - The field to constrain
7352e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7353a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7354a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7355a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7356a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7357a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7358a6ba4734SToby Isaac 
7359a6ba4734SToby Isaac   Options Database Keys:
7360a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7361a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7362a6ba4734SToby Isaac 
7363a6ba4734SToby Isaac   Level: developer
7364a6ba4734SToby Isaac 
7365a6ba4734SToby Isaac .seealso: DMGetBoundary()
7366a6ba4734SToby Isaac @*/
7367a30ec4eaSSatish 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)
7368a6ba4734SToby Isaac {
7369e5e52638SMatthew G. Knepley   PetscDS        ds;
7370a6ba4734SToby Isaac   PetscErrorCode ierr;
7371a6ba4734SToby Isaac 
7372a6ba4734SToby Isaac   PetscFunctionBegin;
7373a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7374e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7375e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7376a6ba4734SToby Isaac   PetscFunctionReturn(0);
7377a6ba4734SToby Isaac }
7378a6ba4734SToby Isaac 
7379a6ba4734SToby Isaac /*@
7380a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7381a6ba4734SToby Isaac 
7382a6ba4734SToby Isaac   Input Parameters:
7383a6ba4734SToby Isaac . dm - The mesh object
7384a6ba4734SToby Isaac 
7385a6ba4734SToby Isaac   Output Parameters:
7386a6ba4734SToby Isaac . numBd - The number of BC
7387a6ba4734SToby Isaac 
7388a6ba4734SToby Isaac   Level: intermediate
7389a6ba4734SToby Isaac 
7390a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7391a6ba4734SToby Isaac @*/
7392a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7393a6ba4734SToby Isaac {
7394e5e52638SMatthew G. Knepley   PetscDS        ds;
739558ebd649SToby Isaac   PetscErrorCode ierr;
7396a6ba4734SToby Isaac 
7397a6ba4734SToby Isaac   PetscFunctionBegin;
7398a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7399e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7400e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7401a6ba4734SToby Isaac   PetscFunctionReturn(0);
7402a6ba4734SToby Isaac }
7403a6ba4734SToby Isaac 
7404a6ba4734SToby Isaac /*@C
74051c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7406a6ba4734SToby Isaac 
7407a6ba4734SToby Isaac   Input Parameters:
7408a6ba4734SToby Isaac + dm          - The mesh object
7409a6ba4734SToby Isaac - bd          - The BC number
7410a6ba4734SToby Isaac 
7411a6ba4734SToby Isaac   Output Parameters:
7412f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7413a6ba4734SToby Isaac . name        - The BC name
7414a6ba4734SToby Isaac . labelname   - The label defining constrained points
7415a6ba4734SToby Isaac . field       - The field to constrain
7416a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7417a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7418a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7419a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7420a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7421a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7422a6ba4734SToby Isaac 
7423a6ba4734SToby Isaac   Options Database Keys:
7424a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7425a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7426a6ba4734SToby Isaac 
7427a6ba4734SToby Isaac   Level: developer
7428a6ba4734SToby Isaac 
7429a6ba4734SToby Isaac .seealso: DMAddBoundary()
7430a6ba4734SToby Isaac @*/
7431a30ec4eaSSatish 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)
7432a6ba4734SToby Isaac {
7433e5e52638SMatthew G. Knepley   PetscDS        ds;
743458ebd649SToby Isaac   PetscErrorCode ierr;
7435a6ba4734SToby Isaac 
7436a6ba4734SToby Isaac   PetscFunctionBegin;
7437a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7438e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7439e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7440a6ba4734SToby Isaac   PetscFunctionReturn(0);
7441a6ba4734SToby Isaac }
7442a6ba4734SToby Isaac 
7443e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7444e6f8dbb6SToby Isaac {
7445e5e52638SMatthew G. Knepley   PetscDS        ds;
7446dff059c6SToby Isaac   DMBoundary    *lastnext;
7447e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7448e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7449e6f8dbb6SToby Isaac 
7450e6f8dbb6SToby Isaac   PetscFunctionBegin;
7451e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7452e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
745347a1f5adSToby Isaac   if (dm->boundary) {
745447a1f5adSToby Isaac     DMBoundary next = dm->boundary;
745547a1f5adSToby Isaac 
745647a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
745747a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
745847a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
745947a1f5adSToby Isaac     while (next) {
746047a1f5adSToby Isaac       DMBoundary b = next;
746147a1f5adSToby Isaac 
746247a1f5adSToby Isaac       next = b->next;
746347a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7464a6ba4734SToby Isaac     }
746547a1f5adSToby Isaac     dm->boundary = NULL;
7466a6ba4734SToby Isaac   }
746747a1f5adSToby Isaac 
7468dff059c6SToby Isaac   lastnext = &(dm->boundary);
7469e6f8dbb6SToby Isaac   while (dsbound) {
7470e6f8dbb6SToby Isaac     DMBoundary dmbound;
7471e6f8dbb6SToby Isaac 
7472e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7473e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7474e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7475994fe344SLisandro 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);}
747647a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7477dff059c6SToby Isaac     *lastnext = dmbound;
7478dff059c6SToby Isaac     lastnext = &(dmbound->next);
7479dff059c6SToby Isaac     dsbound = dsbound->next;
7480a6ba4734SToby Isaac   }
7481a6ba4734SToby Isaac   PetscFunctionReturn(0);
7482a6ba4734SToby Isaac }
7483a6ba4734SToby Isaac 
7484a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7485a6ba4734SToby Isaac {
7486b95f2879SToby Isaac   DMBoundary     b;
7487a6ba4734SToby Isaac   PetscErrorCode ierr;
7488a6ba4734SToby Isaac 
7489a6ba4734SToby Isaac   PetscFunctionBegin;
7490a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7491534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7492a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7493e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7494b95f2879SToby Isaac   b = dm->boundary;
7495a6ba4734SToby Isaac   while (b && !(*isBd)) {
7496e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7497e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
74983424c85cSToby Isaac 
74993424c85cSToby Isaac     if (label) {
7500a6ba4734SToby Isaac       PetscInt i;
7501a6ba4734SToby Isaac 
7502e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7503e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7504a6ba4734SToby Isaac       }
7505a6ba4734SToby Isaac     }
7506a6ba4734SToby Isaac     b = b->next;
7507a6ba4734SToby Isaac   }
7508a6ba4734SToby Isaac   PetscFunctionReturn(0);
7509a6ba4734SToby Isaac }
75104d6f44ffSToby Isaac 
75114d6f44ffSToby Isaac /*@C
75124d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
75134d6f44ffSToby Isaac 
75144d6f44ffSToby Isaac   Input Parameters:
75154d6f44ffSToby Isaac + dm      - The DM
75160709b2feSToby Isaac . time    - The time
75174d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
75184d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
75194d6f44ffSToby Isaac - mode    - The insertion mode for values
75204d6f44ffSToby Isaac 
75214d6f44ffSToby Isaac   Output Parameter:
75224d6f44ffSToby Isaac . X - vector
75234d6f44ffSToby Isaac 
75244d6f44ffSToby Isaac    Calling sequence of func:
75250709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
75264d6f44ffSToby Isaac 
75274d6f44ffSToby Isaac +  dim - The spatial dimension
75284d6f44ffSToby Isaac .  x   - The coordinates
75294d6f44ffSToby Isaac .  Nf  - The number of fields
75304d6f44ffSToby Isaac .  u   - The output field values
75314d6f44ffSToby Isaac -  ctx - optional user-defined function context
75324d6f44ffSToby Isaac 
75334d6f44ffSToby Isaac   Level: developer
75344d6f44ffSToby Isaac 
75352716604bSToby Isaac .seealso: DMComputeL2Diff()
75364d6f44ffSToby Isaac @*/
75370709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
75384d6f44ffSToby Isaac {
75394d6f44ffSToby Isaac   Vec            localX;
75404d6f44ffSToby Isaac   PetscErrorCode ierr;
75414d6f44ffSToby Isaac 
75424d6f44ffSToby Isaac   PetscFunctionBegin;
75434d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75444d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
75450709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75464d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
75474d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
75484d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
75494d6f44ffSToby Isaac   PetscFunctionReturn(0);
75504d6f44ffSToby Isaac }
75514d6f44ffSToby Isaac 
75520709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
75534d6f44ffSToby Isaac {
75544d6f44ffSToby Isaac   PetscErrorCode ierr;
75554d6f44ffSToby Isaac 
75564d6f44ffSToby Isaac   PetscFunctionBegin;
75574d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75584d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
75590918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
75600709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75614d6f44ffSToby Isaac   PetscFunctionReturn(0);
75624d6f44ffSToby Isaac }
75634d6f44ffSToby Isaac 
75642c53366bSMatthew 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)
75652c53366bSMatthew G. Knepley {
75662c53366bSMatthew G. Knepley   Vec            localX;
75672c53366bSMatthew G. Knepley   PetscErrorCode ierr;
75682c53366bSMatthew G. Knepley 
75692c53366bSMatthew G. Knepley   PetscFunctionBegin;
75702c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75712c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
75722c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75732c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
75742c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
75752c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
75762c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
75772c53366bSMatthew G. Knepley }
75782c53366bSMatthew G. Knepley 
75791c531cf8SMatthew 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)
75804d6f44ffSToby Isaac {
75814d6f44ffSToby Isaac   PetscErrorCode ierr;
75824d6f44ffSToby Isaac 
75834d6f44ffSToby Isaac   PetscFunctionBegin;
75844d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
75854d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
75860918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
75871c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
75884d6f44ffSToby Isaac   PetscFunctionReturn(0);
75894d6f44ffSToby Isaac }
75902716604bSToby Isaac 
75918c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
75928c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
75938c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
75948c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7595191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
75968c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
75978c6c5593SMatthew G. Knepley {
75988c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
75998c6c5593SMatthew G. Knepley 
76008c6c5593SMatthew G. Knepley   PetscFunctionBegin;
76018c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
76028c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
76038c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
76040918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
76058c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
76068c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
76078c6c5593SMatthew G. Knepley }
76088c6c5593SMatthew G. Knepley 
76091c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
76108c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
76118c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
76128c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7613191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
76148c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
76158c6c5593SMatthew G. Knepley {
76168c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
76178c6c5593SMatthew G. Knepley 
76188c6c5593SMatthew G. Knepley   PetscFunctionBegin;
76198c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
76208c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
76218c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
76220918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
76231c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
76248c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
76258c6c5593SMatthew G. Knepley }
76268c6c5593SMatthew G. Knepley 
76272716604bSToby Isaac /*@C
76282716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
76292716604bSToby Isaac 
76302716604bSToby Isaac   Input Parameters:
76312716604bSToby Isaac + dm    - The DM
76320709b2feSToby Isaac . time  - The time
76332716604bSToby Isaac . funcs - The functions to evaluate for each field component
76342716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7635574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
76362716604bSToby Isaac 
76372716604bSToby Isaac   Output Parameter:
76382716604bSToby Isaac . diff - The diff ||u - u_h||_2
76392716604bSToby Isaac 
76402716604bSToby Isaac   Level: developer
76412716604bSToby Isaac 
76421189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
76432716604bSToby Isaac @*/
76440709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
76452716604bSToby Isaac {
76462716604bSToby Isaac   PetscErrorCode ierr;
76472716604bSToby Isaac 
76482716604bSToby Isaac   PetscFunctionBegin;
76492716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7650b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
76510918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
76520709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
76532716604bSToby Isaac   PetscFunctionReturn(0);
76542716604bSToby Isaac }
7655b698f381SToby Isaac 
7656b698f381SToby Isaac /*@C
7657b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
7658b698f381SToby Isaac 
7659d083f849SBarry Smith   Collective on dm
7660d083f849SBarry Smith 
7661b698f381SToby Isaac   Input Parameters:
7662b698f381SToby Isaac + dm    - The DM
7663b698f381SToby Isaac , time  - The time
7664b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
7665b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7666574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
7667b698f381SToby Isaac - n     - The vector to project along
7668b698f381SToby Isaac 
7669b698f381SToby Isaac   Output Parameter:
7670b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
7671b698f381SToby Isaac 
7672b698f381SToby Isaac   Level: developer
7673b698f381SToby Isaac 
7674b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
7675b698f381SToby Isaac @*/
7676b698f381SToby 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)
7677b698f381SToby Isaac {
7678b698f381SToby Isaac   PetscErrorCode ierr;
7679b698f381SToby Isaac 
7680b698f381SToby Isaac   PetscFunctionBegin;
7681b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7682b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7683b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
7684b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
7685b698f381SToby Isaac   PetscFunctionReturn(0);
7686b698f381SToby Isaac }
7687b698f381SToby Isaac 
76882a16baeaSToby Isaac /*@C
76892a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
76902a16baeaSToby Isaac 
7691d083f849SBarry Smith   Collective on dm
7692d083f849SBarry Smith 
76932a16baeaSToby Isaac   Input Parameters:
76942a16baeaSToby Isaac + dm    - The DM
76952a16baeaSToby Isaac . time  - The time
76962a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
76972a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7698574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
76992a16baeaSToby Isaac 
77002a16baeaSToby Isaac   Output Parameter:
77012a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
77022a16baeaSToby Isaac 
77032a16baeaSToby Isaac   Level: developer
77042a16baeaSToby Isaac 
77051189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
77062a16baeaSToby Isaac @*/
77071189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
77082a16baeaSToby Isaac {
77092a16baeaSToby Isaac   PetscErrorCode ierr;
77102a16baeaSToby Isaac 
77112a16baeaSToby Isaac   PetscFunctionBegin;
77122a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77132a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
77140918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
77152a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
77162a16baeaSToby Isaac   PetscFunctionReturn(0);
77172a16baeaSToby Isaac }
77182a16baeaSToby Isaac 
7719df0b854cSToby Isaac /*@C
7720df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
7721cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
7722df0b854cSToby Isaac 
7723df0b854cSToby Isaac   Collective on dm
7724df0b854cSToby Isaac 
7725df0b854cSToby Isaac   Input parameters:
7726df0b854cSToby Isaac + dm - the pre-adaptation DM object
7727a1b0c543SToby Isaac - label - label with the flags
7728df0b854cSToby Isaac 
7729df0b854cSToby Isaac   Output parameters:
77300d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
7731df0b854cSToby Isaac 
7732df0b854cSToby Isaac   Level: intermediate
77330d1cd5e0SMatthew G. Knepley 
77340d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
7735df0b854cSToby Isaac @*/
77360d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
7737df0b854cSToby Isaac {
7738df0b854cSToby Isaac   PetscErrorCode ierr;
7739df0b854cSToby Isaac 
7740df0b854cSToby Isaac   PetscFunctionBegin;
7741df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7742a1b0c543SToby Isaac   PetscValidPointer(label,2);
77430d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
77440d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
77456f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
77460d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
77470d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
77480d1cd5e0SMatthew G. Knepley }
77490d1cd5e0SMatthew G. Knepley 
77500d1cd5e0SMatthew G. Knepley /*@C
77510d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
77520d1cd5e0SMatthew G. Knepley 
77530d1cd5e0SMatthew G. Knepley   Input Parameters:
77540d1cd5e0SMatthew G. Knepley + dm - The DM object
77550d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
77566f25b0d8SLisandro 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_".
77570d1cd5e0SMatthew G. Knepley 
77580d1cd5e0SMatthew G. Knepley   Output Parameter:
77590d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
77600d1cd5e0SMatthew G. Knepley 
77610d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
77620d1cd5e0SMatthew G. Knepley 
77630d1cd5e0SMatthew G. Knepley   Level: advanced
77640d1cd5e0SMatthew G. Knepley 
77650d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
77660d1cd5e0SMatthew G. Knepley @*/
77670d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
77680d1cd5e0SMatthew G. Knepley {
77690d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
77700d1cd5e0SMatthew G. Knepley 
77710d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
77720d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77730d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
77740d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
77750d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
77766f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
77776f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
77780d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
7779df0b854cSToby Isaac   PetscFunctionReturn(0);
7780df0b854cSToby Isaac }
7781c4088d22SMatthew G. Knepley 
7782502a2867SDave May /*@C
7783502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
7784502a2867SDave May 
7785502a2867SDave May  Not Collective
7786502a2867SDave May 
7787502a2867SDave May  Input Parameter:
7788502a2867SDave May  . dm    - The DM
7789502a2867SDave May 
7790502a2867SDave May  Output Parameter:
7791502a2867SDave May  . nranks - the number of neighbours
7792502a2867SDave May  . ranks - the neighbors ranks
7793502a2867SDave May 
7794502a2867SDave May  Notes:
7795502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
7796502a2867SDave May 
7797502a2867SDave May  Level: beginner
7798502a2867SDave May 
7799dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
7800502a2867SDave May @*/
7801502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
7802502a2867SDave May {
7803502a2867SDave May   PetscErrorCode ierr;
7804502a2867SDave May 
7805502a2867SDave May   PetscFunctionBegin;
7806502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78070918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
7808502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
7809502a2867SDave May   PetscFunctionReturn(0);
7810502a2867SDave May }
7811502a2867SDave May 
7812531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
7813531c7667SBarry Smith 
7814531c7667SBarry Smith /*
7815531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
7816531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
7817531c7667SBarry Smith */
7818531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
7819531c7667SBarry Smith {
7820531c7667SBarry Smith   PetscErrorCode ierr;
7821531c7667SBarry Smith 
7822531c7667SBarry Smith   PetscFunctionBegin;
7823531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7824531c7667SBarry Smith     Vec x1local;
7825531c7667SBarry Smith     DM  dm;
7826531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7827531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
7828531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
7829531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7830531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7831531c7667SBarry Smith     x1   = x1local;
7832531c7667SBarry Smith   }
7833531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
7834531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7835531c7667SBarry Smith     DM  dm;
7836531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7837531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
7838531c7667SBarry Smith   }
7839531c7667SBarry Smith   PetscFunctionReturn(0);
7840531c7667SBarry Smith }
7841531c7667SBarry Smith 
7842531c7667SBarry Smith /*@
7843531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
7844531c7667SBarry Smith 
7845531c7667SBarry Smith     Input Parameter:
7846531c7667SBarry Smith .    coloring - the MatFDColoring object
7847531c7667SBarry Smith 
784895452b02SPatrick Sanan     Developer Notes:
784995452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
7850531c7667SBarry Smith 
78511b266c99SBarry Smith     Level: advanced
78521b266c99SBarry Smith 
7853531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
7854531c7667SBarry Smith @*/
7855531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
7856531c7667SBarry Smith {
7857531c7667SBarry Smith   PetscFunctionBegin;
7858531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
7859531c7667SBarry Smith   PetscFunctionReturn(0);
7860531c7667SBarry Smith }
78618320bc6fSPatrick Sanan 
78628320bc6fSPatrick Sanan /*@
78638320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
78648320bc6fSPatrick Sanan 
78658320bc6fSPatrick Sanan     Collective
78668320bc6fSPatrick Sanan 
78678320bc6fSPatrick Sanan     Input Parameters:
78688320bc6fSPatrick Sanan +    dm - the first DM
78698320bc6fSPatrick Sanan -    dm2 - the second DM
78708320bc6fSPatrick Sanan 
78718320bc6fSPatrick Sanan     Output Parameters:
78728320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
78738320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
78748320bc6fSPatrick Sanan 
78758320bc6fSPatrick Sanan     Notes:
78768320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
78773d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
78788320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
78793d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
78803d862458SPatrick Sanan     decomposition, but hold different data.
78818320bc6fSPatrick Sanan 
78828320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
78838320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
78848320bc6fSPatrick Sanan 
78858320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
78868320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
78878320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
78888320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
78898320bc6fSPatrick Sanan 
78908320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
78918320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
78928320bc6fSPatrick Sanan .vb
78938320bc6fSPatrick Sanan   ...
78948320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
78958320bc6fSPatrick Sanan   if (set && compatible)  {
78968320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
78978320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
78983d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
78998320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
79008320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
79018320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
79028320bc6fSPatrick Sanan       }
79038320bc6fSPatrick Sanan     }
79048320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
79058320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
79068320bc6fSPatrick Sanan   } else {
79078320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
79088320bc6fSPatrick Sanan   }
79098320bc6fSPatrick Sanan   ...
79108320bc6fSPatrick Sanan .ve
79118320bc6fSPatrick Sanan 
79128320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
79138320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
79148320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
79158320bc6fSPatrick Sanan     always check the "set" output parameter.
79168320bc6fSPatrick Sanan 
79178320bc6fSPatrick Sanan     A DM is always compatible with itself.
79188320bc6fSPatrick Sanan 
79198320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
79208320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
79218320bc6fSPatrick Sanan     incompatible.
79228320bc6fSPatrick Sanan 
79238320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
79248320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
79258320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
79268320bc6fSPatrick Sanan 
79278320bc6fSPatrick Sanan     Developer Notes:
79283d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
79293d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
79308320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
79318320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
79328320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
79333d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
79343d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
79358320bc6fSPatrick Sanan 
79368320bc6fSPatrick Sanan     Level: advanced
79378320bc6fSPatrick Sanan 
79383d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
79398320bc6fSPatrick Sanan @*/
79408320bc6fSPatrick Sanan 
79418320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
79428320bc6fSPatrick Sanan {
79438320bc6fSPatrick Sanan   PetscErrorCode ierr;
79448320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
79458320bc6fSPatrick Sanan   DMType         type,type2;
79468320bc6fSPatrick Sanan   PetscBool      sameType;
79478320bc6fSPatrick Sanan 
79488320bc6fSPatrick Sanan   PetscFunctionBegin;
79498320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79508320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
79518320bc6fSPatrick Sanan 
79528320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
79538320bc6fSPatrick Sanan   if (dm == dm2) {
79548320bc6fSPatrick Sanan     *set = PETSC_TRUE;
79558320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
79568320bc6fSPatrick Sanan     PetscFunctionReturn(0);
79578320bc6fSPatrick Sanan   }
79588320bc6fSPatrick Sanan 
79598320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
79608320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
79618320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
79628320bc6fSPatrick Sanan      determined by the implementation-specific logic */
79638320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
79648320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
79658320bc6fSPatrick Sanan     *set = PETSC_TRUE;
79668320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
79678320bc6fSPatrick Sanan     PetscFunctionReturn(0);
79688320bc6fSPatrick Sanan   }
79698320bc6fSPatrick Sanan 
79708320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
79718320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
79728320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
7973b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
79748320bc6fSPatrick Sanan   }
79758320bc6fSPatrick Sanan 
79768320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
79778320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
79788320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
79798320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
79808320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
79818320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
79828320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
79838320bc6fSPatrick Sanan   } else {
79848320bc6fSPatrick Sanan     *set = PETSC_FALSE;
79858320bc6fSPatrick Sanan   }
79868320bc6fSPatrick Sanan   PetscFunctionReturn(0);
79878320bc6fSPatrick Sanan }
7988