xref: /petsc/src/dm/interface/dm.c (revision b2e4378d4b9d6aa5fb88b5b4eec64d3c2d3252df)
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);
531bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
54c58f1c22SToby Isaac   v->labels                   = NULL;
5534aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
5634aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
57c58f1c22SToby Isaac   v->depthLabel               = NULL;
581bb6d2a8SBarry Smith   v->localSection             = NULL;
591bb6d2a8SBarry 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 
1031bb6d2a8SBarry Smith   Notes: For some DM this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1041bb6d2a8SBarry Smith          DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does
1051bb6d2a8SBarry Smith          share the PetscSection of the original DM
1061bb6d2a8SBarry Smith 
1071bb6d2a8SBarry Smith .seealso: DMDestry(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1081bb6d2a8SBarry 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   }
4711bb6d2a8SBarry Smith   if (dm->sectionSF) {
4721bb6d2a8SBarry 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 
577f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(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   /* destroy the labels */
708f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
709f4cdcedcSVaclav Hapla   /* destroy the fields */
710e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
711f4cdcedcSVaclav Hapla   /* destroy the boundaries */
712e6f8dbb6SToby Isaac   {
713e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
714e6f8dbb6SToby Isaac     while (next) {
715e6f8dbb6SToby Isaac       DMBoundary b = next;
716e6f8dbb6SToby Isaac 
717e6f8dbb6SToby Isaac       next = b->next;
718e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
719e6f8dbb6SToby Isaac     }
720e6f8dbb6SToby Isaac   }
721b17ce1afSJed Brown 
72252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
72352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72552536dc3SBarry Smith 
7261a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7271a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7281a266240SBarry Smith   }
72987e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
73071cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7314dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7326bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7336bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
734073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
73588ed4aceSMatthew G Knepley 
7361bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7371bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7388b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
739fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
740fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
74188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7421bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
743736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
744736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7458e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
746736995cdSBlaise Bourdin     }
747736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
748736995cdSBlaise Bourdin   }
74988bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75088bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75188bdff64SToby Isaac   }
752a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75388bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
75488bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
75588bdff64SToby Isaac   }
75688bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
757f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7586636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7596636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7606636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7615dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
762ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
763ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
764ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7656636e97aSMatthew G Knepley 
766e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
76714f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
768e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
769e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
770732e2eb9SMatthew G Knepley 
771ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7726bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
773ed3c66a1SDave May   }
774435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
775732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
77647c6ae99SBarry Smith   PetscFunctionReturn(0);
77747c6ae99SBarry Smith }
77847c6ae99SBarry Smith 
779d7bf68aeSBarry Smith /*@
780d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
781d7bf68aeSBarry Smith 
782d083f849SBarry Smith     Collective on dm
783d7bf68aeSBarry Smith 
784d7bf68aeSBarry Smith     Input Parameter:
785d7bf68aeSBarry Smith .   dm - the DM object to setup
786d7bf68aeSBarry Smith 
787d7bf68aeSBarry Smith     Level: developer
788d7bf68aeSBarry Smith 
789e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
790d7bf68aeSBarry Smith 
791d7bf68aeSBarry Smith @*/
7927087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
793d7bf68aeSBarry Smith {
794d7bf68aeSBarry Smith   PetscErrorCode ierr;
795d7bf68aeSBarry Smith 
796d7bf68aeSBarry Smith   PetscFunctionBegin;
797171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7988387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
799d7bf68aeSBarry Smith   if (dm->ops->setup) {
800d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
801d7bf68aeSBarry Smith   }
8028387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
803d7bf68aeSBarry Smith   PetscFunctionReturn(0);
804d7bf68aeSBarry Smith }
805d7bf68aeSBarry Smith 
806d7bf68aeSBarry Smith /*@
807d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
808d7bf68aeSBarry Smith 
809d083f849SBarry Smith     Collective on dm
810d7bf68aeSBarry Smith 
811d7bf68aeSBarry Smith     Input Parameter:
812d7bf68aeSBarry Smith .   dm - the DM object to set options for
813d7bf68aeSBarry Smith 
814732e2eb9SMatthew G Knepley     Options Database:
8154164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8164164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8174164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8188f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
819732e2eb9SMatthew G Knepley 
820d7bf68aeSBarry Smith     Level: developer
821d7bf68aeSBarry Smith 
822e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
823d7bf68aeSBarry Smith 
824d7bf68aeSBarry Smith @*/
8257087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
826d7bf68aeSBarry Smith {
8277781c08eSBarry Smith   char           typeName[256];
828ca266f36SBarry Smith   PetscBool      flg;
829d7bf68aeSBarry Smith   PetscErrorCode ierr;
830d7bf68aeSBarry Smith 
831d7bf68aeSBarry Smith   PetscFunctionBegin;
832171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83349be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
83449be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8351bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8363194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8370298fd71SBarry 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);
838a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
839f9ba7244SBarry Smith   if (flg) {
840f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
841f9ba7244SBarry Smith   }
842a264d7a6SBarry 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);
843073dac72SJed Brown   if (flg) {
844521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
845073dac72SJed Brown   }
8468f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
847f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
848e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
849f9ba7244SBarry Smith   }
850f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8510633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
85282fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
853d7bf68aeSBarry Smith   PetscFunctionReturn(0);
854d7bf68aeSBarry Smith }
855d7bf68aeSBarry Smith 
856fc9bc008SSatish Balay /*@C
857224748a4SBarry Smith     DMView - Views a DM
85847c6ae99SBarry Smith 
859d083f849SBarry Smith     Collective on dm
86047c6ae99SBarry Smith 
86147c6ae99SBarry Smith     Input Parameter:
86247c6ae99SBarry Smith +   dm - the DM object to view
86347c6ae99SBarry Smith -   v - the viewer
86447c6ae99SBarry Smith 
865224748a4SBarry Smith     Level: beginner
86647c6ae99SBarry Smith 
867e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith @*/
8707087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
87147c6ae99SBarry Smith {
87247c6ae99SBarry Smith   PetscErrorCode    ierr;
87332c0f0efSBarry Smith   PetscBool         isbinary;
87476a8abe0SBarry Smith   PetscMPIInt       size;
87576a8abe0SBarry Smith   PetscViewerFormat format;
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith   PetscFunctionBegin;
878171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8793014e516SBarry Smith   if (!v) {
880ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8813014e516SBarry Smith   }
882b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
88374903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
88474903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
88574903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
88674903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
88774903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
88874903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
88974903a4fSStefano Zampini      in an error here */
89074903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
8918bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
892b1b135c8SBarry Smith 
89376a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
89476a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
89576a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
89698c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
89732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
89832c0f0efSBarry Smith   if (isbinary) {
89955849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
90032c0f0efSBarry Smith     char     type[256];
90132c0f0efSBarry Smith 
90232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
90332c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
90432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
90532c0f0efSBarry Smith   }
9060c010503SBarry Smith   if (dm->ops->view) {
9070c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
90847c6ae99SBarry Smith   }
90947c6ae99SBarry Smith   PetscFunctionReturn(0);
91047c6ae99SBarry Smith }
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith /*@
9138472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
91447c6ae99SBarry Smith 
915d083f849SBarry Smith     Collective on dm
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith     Input Parameter:
91847c6ae99SBarry Smith .   dm - the DM object
91947c6ae99SBarry Smith 
92047c6ae99SBarry Smith     Output Parameter:
92147c6ae99SBarry Smith .   vec - the global vector
92247c6ae99SBarry Smith 
923073dac72SJed Brown     Level: beginner
92447c6ae99SBarry Smith 
925e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith @*/
9287087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
92947c6ae99SBarry Smith {
93047c6ae99SBarry Smith   PetscErrorCode ierr;
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith   PetscFunctionBegin;
933171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
934b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
935b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
93647c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
93747c6ae99SBarry Smith   PetscFunctionReturn(0);
93847c6ae99SBarry Smith }
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith /*@
9418472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith     Not Collective
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith     Input Parameter:
94647c6ae99SBarry Smith .   dm - the DM object
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith     Output Parameter:
94947c6ae99SBarry Smith .   vec - the local vector
95047c6ae99SBarry Smith 
951073dac72SJed Brown     Level: beginner
95247c6ae99SBarry Smith 
953e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
95447c6ae99SBarry Smith 
95547c6ae99SBarry Smith @*/
9567087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
95747c6ae99SBarry Smith {
95847c6ae99SBarry Smith   PetscErrorCode ierr;
95947c6ae99SBarry Smith 
96047c6ae99SBarry Smith   PetscFunctionBegin;
961171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
962b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
963b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
96447c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
96547c6ae99SBarry Smith   PetscFunctionReturn(0);
96647c6ae99SBarry Smith }
96747c6ae99SBarry Smith 
9681411c6eeSJed Brown /*@
9691411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9701411c6eeSJed Brown 
971d083f849SBarry Smith    Collective on dm
9721411c6eeSJed Brown 
9731411c6eeSJed Brown    Input Parameter:
9741411c6eeSJed Brown .  dm - the DM that provides the mapping
9751411c6eeSJed Brown 
9761411c6eeSJed Brown    Output Parameter:
9771411c6eeSJed Brown .  ltog - the mapping
9781411c6eeSJed Brown 
9791411c6eeSJed Brown    Level: intermediate
9801411c6eeSJed Brown 
9811411c6eeSJed Brown    Notes:
9821411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9831411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9841411c6eeSJed Brown 
985fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9861411c6eeSJed Brown @*/
9877087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9881411c6eeSJed Brown {
9890be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9901411c6eeSJed Brown   PetscErrorCode ierr;
9911411c6eeSJed Brown 
9921411c6eeSJed Brown   PetscFunctionBegin;
9931411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9941411c6eeSJed Brown   PetscValidPointer(ltog,2);
9951411c6eeSJed Brown   if (!dm->ltogmap) {
99637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
99737d0c07bSMatthew G Knepley 
99892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
99937d0c07bSMatthew G Knepley     if (section) {
1000a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
100137d0c07bSMatthew G Knepley       PetscInt       *ltog;
1002ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
100337d0c07bSMatthew G Knepley 
1004e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
100537d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1006ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1007ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
100837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1009a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
101037d0c07bSMatthew G Knepley 
101137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
101237d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1013a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1014a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
101537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10161a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10171a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10181a7dc684SMatthew G. Knepley         if (dof) {
1019b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1020b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10211a7dc684SMatthew G. Knepley         }
102237d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1023a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1024a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
102537d0c07bSMatthew G Knepley         }
102637d0c07bSMatthew G Knepley       }
1027bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10280be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10290be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10300be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10310be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10327591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10337591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1034ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1035ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1036ccf3bd66SMatthew G. Knepley         n /= bs;
1037ccf3bd66SMatthew G. Knepley       }
1038ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10393bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
104037d0c07bSMatthew G Knepley     } else {
1041b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1042184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10431411c6eeSJed Brown     }
104437d0c07bSMatthew G Knepley   }
10451411c6eeSJed Brown   *ltog = dm->ltogmap;
10461411c6eeSJed Brown   PetscFunctionReturn(0);
10471411c6eeSJed Brown }
10481411c6eeSJed Brown 
10491411c6eeSJed Brown /*@
10501411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10511411c6eeSJed Brown 
10521411c6eeSJed Brown    Not Collective
10531411c6eeSJed Brown 
10541411c6eeSJed Brown    Input Parameter:
10551411c6eeSJed Brown .  dm - the DM with block structure
10561411c6eeSJed Brown 
10571411c6eeSJed Brown    Output Parameter:
10581411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10591411c6eeSJed Brown 
10601411c6eeSJed Brown    Level: intermediate
10611411c6eeSJed Brown 
1062fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10631411c6eeSJed Brown @*/
10647087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10651411c6eeSJed Brown {
10661411c6eeSJed Brown   PetscFunctionBegin;
10671411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1068534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
10691411c6eeSJed 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");
10701411c6eeSJed Brown   *bs = dm->bs;
10711411c6eeSJed Brown   PetscFunctionReturn(0);
10721411c6eeSJed Brown }
10731411c6eeSJed Brown 
107447c6ae99SBarry Smith /*@
10758472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
107647c6ae99SBarry Smith 
1077d083f849SBarry Smith     Collective on dm1
107847c6ae99SBarry Smith 
107947c6ae99SBarry Smith     Input Parameter:
108047c6ae99SBarry Smith +   dm1 - the DM object
108147c6ae99SBarry Smith -   dm2 - the second, finer DM object
108247c6ae99SBarry Smith 
108347c6ae99SBarry Smith     Output Parameter:
108447c6ae99SBarry Smith +  mat - the interpolation
108547c6ae99SBarry Smith -  vec - the scaling (optional)
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith     Level: developer
108847c6ae99SBarry Smith 
108995452b02SPatrick Sanan     Notes:
109095452b02SPatrick 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
109185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1092d52bd9f3SBarry Smith 
10931f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1094d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
109585afcc9aSBarry Smith 
109685afcc9aSBarry Smith 
10973ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith @*/
1100e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
110147c6ae99SBarry Smith {
110247c6ae99SBarry Smith   PetscErrorCode ierr;
110347c6ae99SBarry Smith 
110447c6ae99SBarry Smith   PetscFunctionBegin;
1105171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1106171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1107c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1108b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dm1)->type_name);
1109cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1111cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
111247c6ae99SBarry Smith   PetscFunctionReturn(0);
111347c6ae99SBarry Smith }
111447c6ae99SBarry Smith 
11153ad4599aSBarry Smith /*@
11163ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11173ad4599aSBarry Smith 
1118d083f849SBarry Smith     Collective on dm1
11193ad4599aSBarry Smith 
11203ad4599aSBarry Smith     Input Parameter:
11213ad4599aSBarry Smith +   dm1 - the DM object
11223ad4599aSBarry Smith -   dm2 - the second, finer DM object
11233ad4599aSBarry Smith 
11243ad4599aSBarry Smith     Output Parameter:
11253ad4599aSBarry Smith .  mat - the restriction
11263ad4599aSBarry Smith 
11273ad4599aSBarry Smith 
11283ad4599aSBarry Smith     Level: developer
11293ad4599aSBarry Smith 
113095452b02SPatrick Sanan     Notes:
113195452b02SPatrick 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
11323ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11333ad4599aSBarry Smith 
11343ad4599aSBarry Smith 
11353ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11363ad4599aSBarry Smith 
11373ad4599aSBarry Smith @*/
11383ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11393ad4599aSBarry Smith {
11403ad4599aSBarry Smith   PetscErrorCode ierr;
11413ad4599aSBarry Smith 
11423ad4599aSBarry Smith   PetscFunctionBegin;
11433ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11443ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11455a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1146b9d85ea2SLisandro Dalcin   if (!dm1->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dm1)->type_name);
11473ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11483ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11493ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11503ad4599aSBarry Smith   PetscFunctionReturn(0);
11513ad4599aSBarry Smith }
11523ad4599aSBarry Smith 
115347c6ae99SBarry Smith /*@
11548472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
115547c6ae99SBarry Smith 
1156d083f849SBarry Smith     Collective on dm1
115747c6ae99SBarry Smith 
115847c6ae99SBarry Smith     Input Parameter:
115947c6ae99SBarry Smith +   dm1 - the DM object
116047c6ae99SBarry Smith -   dm2 - the second, finer DM object
116147c6ae99SBarry Smith 
116247c6ae99SBarry Smith     Output Parameter:
11636dbf9973SLawrence Mitchell .   mat - the injection
116447c6ae99SBarry Smith 
116547c6ae99SBarry Smith     Level: developer
116647c6ae99SBarry Smith 
116795452b02SPatrick Sanan    Notes:
116895452b02SPatrick 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
116985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
117085afcc9aSBarry Smith 
1171e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
117247c6ae99SBarry Smith 
117347c6ae99SBarry Smith @*/
11746dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
117547c6ae99SBarry Smith {
117647c6ae99SBarry Smith   PetscErrorCode ierr;
117747c6ae99SBarry Smith 
117847c6ae99SBarry Smith   PetscFunctionBegin;
1179171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1180171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11815a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1182b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dm1)->type_name);
11835a84ad33SLisandro Dalcin   ierr = PetscLogEventBegin(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
11845a84ad33SLisandro Dalcin   ierr = (*dm1->ops->createinjection)(dm1,dm2,mat);CHKERRQ(ierr);
11855a84ad33SLisandro Dalcin   ierr = PetscLogEventEnd(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
118647c6ae99SBarry Smith   PetscFunctionReturn(0);
118747c6ae99SBarry Smith }
118847c6ae99SBarry Smith 
1189b412c318SBarry Smith /*@
1190bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1191bd041c0cSMatthew G. Knepley 
1192d083f849SBarry Smith   Collective on dm1
1193bd041c0cSMatthew G. Knepley 
1194bd041c0cSMatthew G. Knepley   Input Parameter:
1195bd041c0cSMatthew G. Knepley + dm1 - the DM object
1196bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1197bd041c0cSMatthew G. Knepley 
1198bd041c0cSMatthew G. Knepley   Output Parameter:
1199bd041c0cSMatthew G. Knepley . mat - the interpolation
1200bd041c0cSMatthew G. Knepley 
1201bd041c0cSMatthew G. Knepley   Level: developer
1202bd041c0cSMatthew G. Knepley 
1203bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1204bd041c0cSMatthew G. Knepley @*/
1205bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1206bd041c0cSMatthew G. Knepley {
1207bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1208bd041c0cSMatthew G. Knepley 
1209bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1210bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1211bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
12125a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1213b9d85ea2SLisandro Dalcin   if (!dm1->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dm1)->type_name);
1214bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1215bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1216bd041c0cSMatthew G. Knepley }
1217bd041c0cSMatthew G. Knepley 
1218bd041c0cSMatthew G. Knepley /*@
1219b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
122047c6ae99SBarry Smith 
1221d083f849SBarry Smith     Collective on dm
122247c6ae99SBarry Smith 
122347c6ae99SBarry Smith     Input Parameter:
122447c6ae99SBarry Smith +   dm - the DM object
12255bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
122647c6ae99SBarry Smith 
122747c6ae99SBarry Smith     Output Parameter:
122847c6ae99SBarry Smith .   coloring - the coloring
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith     Level: developer
123147c6ae99SBarry Smith 
1232b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
123347c6ae99SBarry Smith 
1234aab9d709SJed Brown @*/
1235b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
123647c6ae99SBarry Smith {
123747c6ae99SBarry Smith   PetscErrorCode ierr;
123847c6ae99SBarry Smith 
123947c6ae99SBarry Smith   PetscFunctionBegin;
1240171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12415a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1242b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1243b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
124447c6ae99SBarry Smith   PetscFunctionReturn(0);
124547c6ae99SBarry Smith }
124647c6ae99SBarry Smith 
1247b412c318SBarry Smith /*@
12488472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
124947c6ae99SBarry Smith 
1250d083f849SBarry Smith     Collective on dm
125147c6ae99SBarry Smith 
125247c6ae99SBarry Smith     Input Parameter:
1253b412c318SBarry Smith .   dm - the DM object
125447c6ae99SBarry Smith 
125547c6ae99SBarry Smith     Output Parameter:
125647c6ae99SBarry Smith .   mat - the empty Jacobian
125747c6ae99SBarry Smith 
1258073dac72SJed Brown     Level: beginner
125947c6ae99SBarry Smith 
126095452b02SPatrick Sanan     Notes:
126195452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
126294013140SBarry Smith        do not need to do it yourself.
126394013140SBarry Smith 
126494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12657889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
126694013140SBarry Smith 
126794013140SBarry 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
126894013140SBarry Smith        internally by PETSc.
126994013140SBarry Smith 
127094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1271aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
127294013140SBarry Smith 
1273b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
127447c6ae99SBarry Smith 
1275aab9d709SJed Brown @*/
1276b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
127747c6ae99SBarry Smith {
127847c6ae99SBarry Smith   PetscErrorCode ierr;
127947c6ae99SBarry Smith 
128047c6ae99SBarry Smith   PetscFunctionBegin;
1281171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1282c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1283b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
12845a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1285fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1286b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1287e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1288e5e52638SMatthew G. Knepley   if (dm->Nf) {
1289e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1290e571a35bSMatthew G. Knepley     PetscInt     Nf;
1291e571a35bSMatthew G. Knepley 
1292e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1293e571a35bSMatthew G. Knepley     if (Nf == 1) {
1294e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1295e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1296e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1297e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1298e571a35bSMatthew G. Knepley       }
1299e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1300e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1301e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1302e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1303e571a35bSMatthew G. Knepley       }
1304e571a35bSMatthew G. Knepley     }
1305e571a35bSMatthew G. Knepley   }
1306fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
130747c6ae99SBarry Smith   PetscFunctionReturn(0);
130847c6ae99SBarry Smith }
130947c6ae99SBarry Smith 
1310732e2eb9SMatthew G Knepley /*@
1311950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1312732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1313732e2eb9SMatthew G Knepley 
1314d083f849SBarry Smith   Logically Collective on dm
1315732e2eb9SMatthew G Knepley 
1316732e2eb9SMatthew G Knepley   Input Parameter:
1317732e2eb9SMatthew G Knepley + dm - the DM
1318732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1319732e2eb9SMatthew G Knepley 
1320732e2eb9SMatthew G Knepley   Level: developer
1321b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1322732e2eb9SMatthew G Knepley @*/
1323732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1324732e2eb9SMatthew G Knepley {
1325732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1326732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1327732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1328732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1329732e2eb9SMatthew G Knepley }
1330732e2eb9SMatthew G Knepley 
1331b06ff27eSHong Zhang /*@
1332b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1333b06ff27eSHong Zhang     but the array for values will not be allocated.
1334b06ff27eSHong Zhang 
1335d083f849SBarry Smith   Logically Collective on dm
1336b06ff27eSHong Zhang 
1337b06ff27eSHong Zhang   Input Parameter:
1338b06ff27eSHong Zhang + dm - the DM
1339b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1340b06ff27eSHong Zhang 
1341b06ff27eSHong Zhang   Level: developer
1342b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1343b06ff27eSHong Zhang @*/
1344b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1345b06ff27eSHong Zhang {
1346b06ff27eSHong Zhang   PetscFunctionBegin;
1347b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1348b06ff27eSHong Zhang   dm->structure_only = only;
1349b06ff27eSHong Zhang   PetscFunctionReturn(0);
1350b06ff27eSHong Zhang }
1351b06ff27eSHong Zhang 
1352a89ea682SMatthew G Knepley /*@C
1353aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1354a89ea682SMatthew G Knepley 
1355a89ea682SMatthew G Knepley   Not Collective
1356a89ea682SMatthew G Knepley 
1357a89ea682SMatthew G Knepley   Input Parameters:
1358a89ea682SMatthew G Knepley + dm - the DM object
1359aa1993deSMatthew G Knepley . count - The minium size
136069291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1361a89ea682SMatthew G Knepley 
1362a89ea682SMatthew G Knepley   Output Parameter:
1363a89ea682SMatthew G Knepley . array - the work array
1364a89ea682SMatthew G Knepley 
1365a89ea682SMatthew G Knepley   Level: developer
1366a89ea682SMatthew G Knepley 
1367a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1368a89ea682SMatthew G Knepley @*/
136969291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1370a89ea682SMatthew G Knepley {
1371a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1372aa1993deSMatthew G Knepley   DMWorkLink     link;
137369291d52SBarry Smith   PetscMPIInt    dsize;
1374a89ea682SMatthew G Knepley 
1375a89ea682SMatthew G Knepley   PetscFunctionBegin;
1376a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1377aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1378aa1993deSMatthew G Knepley   if (dm->workin) {
1379aa1993deSMatthew G Knepley     link       = dm->workin;
1380aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1381aa1993deSMatthew G Knepley   } else {
1382b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1383a89ea682SMatthew G Knepley   }
138469291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13855056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1386aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1387854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1388abe1f95cSSatish Balay     ierr        = PetscMemzero(link->mem,dsize*count);CHKERRQ(ierr);
1389854ce69bSBarry Smith     link->bytes = dsize*count;
1390aa1993deSMatthew G Knepley   }
1391aa1993deSMatthew G Knepley   link->next   = dm->workout;
1392aa1993deSMatthew G Knepley   dm->workout  = link;
1393aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1394a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1395a89ea682SMatthew G Knepley }
1396a89ea682SMatthew G Knepley 
1397aa1993deSMatthew G Knepley /*@C
1398aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1399aa1993deSMatthew G Knepley 
1400aa1993deSMatthew G Knepley   Not Collective
1401aa1993deSMatthew G Knepley 
1402aa1993deSMatthew G Knepley   Input Parameters:
1403aa1993deSMatthew G Knepley + dm - the DM object
1404aa1993deSMatthew G Knepley . count - The minium size
140569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1406aa1993deSMatthew G Knepley 
1407aa1993deSMatthew G Knepley   Output Parameter:
1408aa1993deSMatthew G Knepley . array - the work array
1409aa1993deSMatthew G Knepley 
1410aa1993deSMatthew G Knepley   Level: developer
1411aa1993deSMatthew G Knepley 
141295452b02SPatrick Sanan   Developer Notes:
141395452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1414aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1415aa1993deSMatthew G Knepley @*/
141669291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1417aa1993deSMatthew G Knepley {
1418aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1419aa1993deSMatthew G Knepley 
1420aa1993deSMatthew G Knepley   PetscFunctionBegin;
1421aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1422aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1423aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1424aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1425aa1993deSMatthew G Knepley       *p           = link->next;
1426aa1993deSMatthew G Knepley       link->next   = dm->workin;
1427aa1993deSMatthew G Knepley       dm->workin   = link;
14280298fd71SBarry Smith       *(void**)mem = NULL;
1429aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1430aa1993deSMatthew G Knepley     }
1431aa1993deSMatthew G Knepley   }
1432aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1433aa1993deSMatthew G Knepley }
1434e7c4fc90SDmitry Karpeev 
1435435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1436435a35e8SMatthew G Knepley {
1437435a35e8SMatthew G Knepley   PetscFunctionBegin;
1438435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
143982f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1440435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1441435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1442435a35e8SMatthew G Knepley }
1443435a35e8SMatthew G Knepley 
14440a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14450a50eb56SMatthew G. Knepley {
14460a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14470a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1448f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14490a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14500a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14510a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14520a50eb56SMatthew G. Knepley }
14530a50eb56SMatthew G. Knepley 
1454f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1455f9d4088aSMatthew G. Knepley {
1456f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1457f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1458f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1459f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1460f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1461f9d4088aSMatthew G. Knepley }
1462f9d4088aSMatthew G. Knepley 
1463f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1464f9d4088aSMatthew G. Knepley {
1465f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1466f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1467f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1468f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1469f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1470f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1471f9d4088aSMatthew G. Knepley }
1472f9d4088aSMatthew G. Knepley 
14734f3b5142SJed Brown /*@C
14744d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14754d343eeaSMatthew G Knepley 
14764d343eeaSMatthew G Knepley   Not collective
14774d343eeaSMatthew G Knepley 
14784d343eeaSMatthew G Knepley   Input Parameter:
14794d343eeaSMatthew G Knepley . dm - the DM object
14804d343eeaSMatthew G Knepley 
14814d343eeaSMatthew G Knepley   Output Parameters:
14820298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14830298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14840298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14854d343eeaSMatthew G Knepley 
14864d343eeaSMatthew G Knepley   Level: intermediate
14874d343eeaSMatthew G Knepley 
148821c9b008SJed Brown   Notes:
148921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
149021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
149121c9b008SJed Brown   PetscFree().
149221c9b008SJed Brown 
14934d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14944d343eeaSMatthew G Knepley @*/
149537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14964d343eeaSMatthew G Knepley {
149737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14984d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14994d343eeaSMatthew G Knepley 
15004d343eeaSMatthew G Knepley   PetscFunctionBegin;
15014d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
150269ca1f37SDmitry Karpeev   if (numFields) {
1503534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
150469ca1f37SDmitry Karpeev     *numFields = 0;
150569ca1f37SDmitry Karpeev   }
150637d0c07bSMatthew G Knepley   if (fieldNames) {
150737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15080298fd71SBarry Smith     *fieldNames = NULL;
150969ca1f37SDmitry Karpeev   }
151069ca1f37SDmitry Karpeev   if (fields) {
151169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15120298fd71SBarry Smith     *fields = NULL;
151369ca1f37SDmitry Karpeev   }
151492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
151537d0c07bSMatthew G Knepley   if (section) {
15163a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
151737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
151837d0c07bSMatthew G Knepley 
1519e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
152037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15213a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
152237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
152337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
152437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15253a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
152637d0c07bSMatthew G Knepley     }
152737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
152837d0c07bSMatthew G Knepley       PetscInt gdof;
152937d0c07bSMatthew G Knepley 
153037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
153137d0c07bSMatthew G Knepley       if (gdof > 0) {
153237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15333a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
153437d0c07bSMatthew G Knepley 
153537d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
153637d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15373a544194SStefano Zampini           fpdof = fdof-fcdof;
15383a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15393a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15403a544194SStefano Zampini             fieldNc[f] = 1;
15413a544194SStefano Zampini           }
15423a544194SStefano Zampini           fieldSizes[f] += fpdof;
154337d0c07bSMatthew G Knepley         }
154437d0c07bSMatthew G Knepley       }
154537d0c07bSMatthew G Knepley     }
154637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1547785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
154837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
154937d0c07bSMatthew G Knepley     }
155037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
155137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
155237d0c07bSMatthew G Knepley 
155337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
155437d0c07bSMatthew G Knepley       if (gdof > 0) {
155537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
155637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
155737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
155837d0c07bSMatthew G Knepley 
155937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
156037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
156137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
156237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
156337d0c07bSMatthew G Knepley           }
156437d0c07bSMatthew G Knepley         }
156537d0c07bSMatthew G Knepley       }
156637d0c07bSMatthew G Knepley     }
15678865f1eaSKarl Rupp     if (numFields) *numFields = nF;
156837d0c07bSMatthew G Knepley     if (fieldNames) {
1569785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
157037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
157137d0c07bSMatthew G Knepley         const char *fieldName;
157237d0c07bSMatthew G Knepley 
157337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
157437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
157537d0c07bSMatthew G Knepley       }
157637d0c07bSMatthew G Knepley     }
157737d0c07bSMatthew G Knepley     if (fields) {
1578785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
157937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15803a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15813a544194SStefano Zampini 
158282f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15833a544194SStefano Zampini         in[0] = -fieldNc[f];
15843a544194SStefano Zampini         in[1] = fieldNc[f];
15853a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15863a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15873a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
158837d0c07bSMatthew G Knepley       }
158937d0c07bSMatthew G Knepley     }
15903a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15918865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15928865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
159369ca1f37SDmitry Karpeev   }
15944d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15954d343eeaSMatthew G Knepley }
15964d343eeaSMatthew G Knepley 
159716621825SDmitry Karpeev 
159816621825SDmitry Karpeev /*@C
159916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
160016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
160116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1602e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1603e7c4fc90SDmitry Karpeev 
1604e7c4fc90SDmitry Karpeev   Not collective
1605e7c4fc90SDmitry Karpeev 
1606e7c4fc90SDmitry Karpeev   Input Parameter:
1607e7c4fc90SDmitry Karpeev . dm - the DM object
1608e7c4fc90SDmitry Karpeev 
1609e7c4fc90SDmitry Karpeev   Output Parameters:
16100298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16110298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16120298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16130298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1614e7c4fc90SDmitry Karpeev 
1615e7c4fc90SDmitry Karpeev   Level: intermediate
1616e7c4fc90SDmitry Karpeev 
1617e7c4fc90SDmitry Karpeev   Notes:
1618e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1619e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1620e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1621e7c4fc90SDmitry Karpeev 
1622e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1623e7c4fc90SDmitry Karpeev @*/
162416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1625e7c4fc90SDmitry Karpeev {
1626e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1627e7c4fc90SDmitry Karpeev 
1628e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1629e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16308865f1eaSKarl Rupp   if (len) {
1631534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
16328865f1eaSKarl Rupp     *len = 0;
16338865f1eaSKarl Rupp   }
16348865f1eaSKarl Rupp   if (namelist) {
16358865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16368865f1eaSKarl Rupp     *namelist = 0;
16378865f1eaSKarl Rupp   }
16388865f1eaSKarl Rupp   if (islist) {
16398865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16408865f1eaSKarl Rupp     *islist = 0;
16418865f1eaSKarl Rupp   }
16428865f1eaSKarl Rupp   if (dmlist) {
16438865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16448865f1eaSKarl Rupp     *dmlist = 0;
16458865f1eaSKarl Rupp   }
1646f3f0edfdSDmitry Karpeev   /*
1647f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1648f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1649f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1650f3f0edfdSDmitry Karpeev    */
1651ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
165216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1653435a35e8SMatthew G Knepley     PetscSection section;
1654435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1655435a35e8SMatthew G Knepley 
165692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1657435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1658435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1659f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
166003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
166103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
166203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1663435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1664435a35e8SMatthew G Knepley         const char *fieldName;
1665435a35e8SMatthew G Knepley 
166603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
166703dc3394SMatthew G. Knepley         if (namelist) {
1668435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1669435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1670435a35e8SMatthew G Knepley         }
167103dc3394SMatthew G. Knepley       }
1672435a35e8SMatthew G Knepley     } else {
167369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1674e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16750298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1676e7c4fc90SDmitry Karpeev     }
16778865f1eaSKarl Rupp   } else {
167816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
167916621825SDmitry Karpeev   }
168016621825SDmitry Karpeev   PetscFunctionReturn(0);
168116621825SDmitry Karpeev }
168216621825SDmitry Karpeev 
1683564cec59SMatthew G. Knepley /*@
1684435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1685435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1686435a35e8SMatthew G Knepley 
1687435a35e8SMatthew G Knepley   Not collective
1688435a35e8SMatthew G Knepley 
1689435a35e8SMatthew G Knepley   Input Parameters:
16902adcc780SMatthew G. Knepley + dm        - The DM object
16912adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16922adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1693435a35e8SMatthew G Knepley 
1694435a35e8SMatthew G Knepley   Output Parameters:
16952adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16962adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1697435a35e8SMatthew G Knepley 
16985d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
16995d3b26e6SMatthew G. Knepley 
1700435a35e8SMatthew G Knepley   Level: intermediate
1701435a35e8SMatthew G Knepley 
17025d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1703435a35e8SMatthew G Knepley @*/
170437bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1705435a35e8SMatthew G Knepley {
1706435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1707435a35e8SMatthew G Knepley 
1708435a35e8SMatthew G Knepley   PetscFunctionBegin;
1709435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1710435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17118865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17128865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1713b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1714435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1715435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1716435a35e8SMatthew G Knepley }
1717435a35e8SMatthew G Knepley 
17182adcc780SMatthew G. Knepley /*@C
17192adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17202adcc780SMatthew G. Knepley 
17212adcc780SMatthew G. Knepley   Not collective
17222adcc780SMatthew G. Knepley 
17232adcc780SMatthew G. Knepley   Input Parameter:
17242adcc780SMatthew G. Knepley + dms - The DM objects
17252adcc780SMatthew G. Knepley - len - The number of DMs
17262adcc780SMatthew G. Knepley 
17272adcc780SMatthew G. Knepley   Output Parameters:
1728a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17292adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17302adcc780SMatthew G. Knepley 
17315d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17325d3b26e6SMatthew G. Knepley 
17332adcc780SMatthew G. Knepley   Level: intermediate
17342adcc780SMatthew G. Knepley 
17355d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17362adcc780SMatthew G. Knepley @*/
17372adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17382adcc780SMatthew G. Knepley {
17392adcc780SMatthew G. Knepley   PetscInt       i;
17402adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17412adcc780SMatthew G. Knepley 
17422adcc780SMatthew G. Knepley   PetscFunctionBegin;
17432adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17442adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17452adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1746a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17472adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17482adcc780SMatthew G. Knepley   if (len) {
1749b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1750b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1751b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
17522adcc780SMatthew G. Knepley   }
17532adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17542adcc780SMatthew G. Knepley }
17552adcc780SMatthew G. Knepley 
175616621825SDmitry Karpeev 
175716621825SDmitry Karpeev /*@C
17588d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17598d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17608d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17618d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17628d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
176316621825SDmitry Karpeev 
176416621825SDmitry Karpeev   Not collective
176516621825SDmitry Karpeev 
176616621825SDmitry Karpeev   Input Parameter:
176716621825SDmitry Karpeev . dm - the DM object
176816621825SDmitry Karpeev 
176916621825SDmitry Karpeev   Output Parameters:
17700298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17710298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17720298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17730298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17740298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
177516621825SDmitry Karpeev 
177616621825SDmitry Karpeev   Level: intermediate
177716621825SDmitry Karpeev 
177816621825SDmitry Karpeev   Notes:
177916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
178016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
178116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
178216621825SDmitry Karpeev 
17838d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
178416621825SDmitry Karpeev @*/
17858d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
178616621825SDmitry Karpeev {
178716621825SDmitry Karpeev   PetscErrorCode      ierr;
1788be081cd6SPeter Brune   DMSubDomainHookLink link;
1789be081cd6SPeter Brune   PetscInt            i,l;
179016621825SDmitry Karpeev 
179116621825SDmitry Karpeev   PetscFunctionBegin;
179216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
179314a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17940298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17950298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17960298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17970298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1798f3f0edfdSDmitry Karpeev   /*
1799f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1800f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1801f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1802f3f0edfdSDmitry Karpeev    */
1803ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
180416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1805be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
180614a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1807f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1808be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1809be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1810be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1811be081cd6SPeter Brune         }
1812648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1813e7c4fc90SDmitry Karpeev       }
181414a18fd3SPeter Brune     }
181514a18fd3SPeter Brune     if (len) *len = l;
181614a18fd3SPeter Brune   }
1817e30e807fSPeter Brune   PetscFunctionReturn(0);
1818e30e807fSPeter Brune }
1819e30e807fSPeter Brune 
1820e30e807fSPeter Brune 
1821e30e807fSPeter Brune /*@C
1822e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1823e30e807fSPeter Brune 
1824e30e807fSPeter Brune   Not collective
1825e30e807fSPeter Brune 
1826e30e807fSPeter Brune   Input Parameters:
1827e30e807fSPeter Brune + dm - the DM object
1828e30e807fSPeter Brune . n  - the number of subdomain scatters
1829e30e807fSPeter Brune - subdms - the local subdomains
1830e30e807fSPeter Brune 
1831e30e807fSPeter Brune   Output Parameters:
1832e30e807fSPeter Brune + n     - the number of scatters returned
1833e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1834e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1835e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1836e30e807fSPeter Brune 
183795452b02SPatrick Sanan   Notes:
183895452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1839e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1840e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1841e30e807fSPeter Brune   solution and residual data.
1842e30e807fSPeter Brune 
1843e30e807fSPeter Brune   Level: developer
1844e30e807fSPeter Brune 
1845e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1846e30e807fSPeter Brune @*/
1847e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1848e30e807fSPeter Brune {
1849e30e807fSPeter Brune   PetscErrorCode ierr;
1850e30e807fSPeter Brune 
1851e30e807fSPeter Brune   PetscFunctionBegin;
1852e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1853e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1854b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1855e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1856e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1857e7c4fc90SDmitry Karpeev }
1858e7c4fc90SDmitry Karpeev 
185947c6ae99SBarry Smith /*@
186047c6ae99SBarry Smith   DMRefine - Refines a DM object
186147c6ae99SBarry Smith 
1862d083f849SBarry Smith   Collective on dm
186347c6ae99SBarry Smith 
186447c6ae99SBarry Smith   Input Parameter:
186547c6ae99SBarry Smith + dm   - the DM object
186691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
186747c6ae99SBarry Smith 
186847c6ae99SBarry Smith   Output Parameter:
18690298fd71SBarry Smith . dmf - the refined DM, or NULL
1870ae0a1c52SMatthew G Knepley 
18710298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
187247c6ae99SBarry Smith 
187347c6ae99SBarry Smith   Level: developer
187447c6ae99SBarry Smith 
1875e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
187647c6ae99SBarry Smith @*/
18777087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
187847c6ae99SBarry Smith {
187947c6ae99SBarry Smith   PetscErrorCode   ierr;
1880c833c3b5SJed Brown   DMRefineHookLink link;
188147c6ae99SBarry Smith 
188247c6ae99SBarry Smith   PetscFunctionBegin;
1883732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1884b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
18851ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
188647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18874057135bSMatthew G Knepley   if (*dmf) {
188843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18898865f1eaSKarl Rupp 
18908cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18918865f1eaSKarl Rupp 
1892644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18930598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1894656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18958865f1eaSKarl Rupp 
1896e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1897c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18988865f1eaSKarl Rupp       if (link->refinehook) {
18998865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19008865f1eaSKarl Rupp       }
1901c833c3b5SJed Brown     }
1902c833c3b5SJed Brown   }
19031ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1904c833c3b5SJed Brown   PetscFunctionReturn(0);
1905c833c3b5SJed Brown }
1906c833c3b5SJed Brown 
1907bb9467b5SJed Brown /*@C
1908c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1909c833c3b5SJed Brown 
1910c833c3b5SJed Brown    Logically Collective
1911c833c3b5SJed Brown 
1912c833c3b5SJed Brown    Input Arguments:
1913c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1914c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1915c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19160298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1917c833c3b5SJed Brown 
1918c833c3b5SJed Brown    Calling sequence of refinehook:
1919c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1920c833c3b5SJed Brown 
1921c833c3b5SJed Brown +  coarse - coarse level DM
1922c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1923c833c3b5SJed Brown -  ctx - optional user-defined function context
1924c833c3b5SJed Brown 
1925c833c3b5SJed Brown    Calling sequence for interphook:
1926c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1927c833c3b5SJed Brown 
1928c833c3b5SJed Brown +  coarse - coarse level DM
1929c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1930c833c3b5SJed Brown .  fine - fine level DM to update
1931c833c3b5SJed Brown -  ctx - optional user-defined function context
1932c833c3b5SJed Brown 
1933c833c3b5SJed Brown    Level: advanced
1934c833c3b5SJed Brown 
1935c833c3b5SJed Brown    Notes:
1936c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1937c833c3b5SJed Brown 
1938c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1939c833c3b5SJed Brown 
1940bb9467b5SJed Brown    This function is currently not available from Fortran.
1941bb9467b5SJed Brown 
1942c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1943c833c3b5SJed Brown @*/
1944c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1945c833c3b5SJed Brown {
1946c833c3b5SJed Brown   PetscErrorCode   ierr;
1947c833c3b5SJed Brown   DMRefineHookLink link,*p;
1948c833c3b5SJed Brown 
1949c833c3b5SJed Brown   PetscFunctionBegin;
1950c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19513d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19523d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19533d8e3701SJed Brown   }
195495dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1955c833c3b5SJed Brown   link->refinehook = refinehook;
1956c833c3b5SJed Brown   link->interphook = interphook;
1957c833c3b5SJed Brown   link->ctx        = ctx;
19580298fd71SBarry Smith   link->next       = NULL;
1959c833c3b5SJed Brown   *p               = link;
1960c833c3b5SJed Brown   PetscFunctionReturn(0);
1961c833c3b5SJed Brown }
1962c833c3b5SJed Brown 
19633d8e3701SJed Brown /*@C
19643d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19653d8e3701SJed Brown 
19663d8e3701SJed Brown    Logically Collective
19673d8e3701SJed Brown 
19683d8e3701SJed Brown    Input Arguments:
19693d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19703d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19713d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19723d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19733d8e3701SJed Brown 
19743d8e3701SJed Brown    Level: advanced
19753d8e3701SJed Brown 
19763d8e3701SJed Brown    Notes:
19773d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19783d8e3701SJed Brown 
19793d8e3701SJed Brown    This function is currently not available from Fortran.
19803d8e3701SJed Brown 
19813d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19823d8e3701SJed Brown @*/
19833d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19843d8e3701SJed Brown {
19853d8e3701SJed Brown   PetscErrorCode   ierr;
19863d8e3701SJed Brown   DMRefineHookLink link,*p;
19873d8e3701SJed Brown 
19883d8e3701SJed Brown   PetscFunctionBegin;
19893d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19903d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19913d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19923d8e3701SJed Brown       link = *p;
19933d8e3701SJed Brown       *p = link->next;
19943d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19953d8e3701SJed Brown       break;
19963d8e3701SJed Brown     }
19973d8e3701SJed Brown   }
19983d8e3701SJed Brown   PetscFunctionReturn(0);
19993d8e3701SJed Brown }
20003d8e3701SJed Brown 
2001c833c3b5SJed Brown /*@
2002c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2003c833c3b5SJed Brown 
2004c833c3b5SJed Brown    Collective if any hooks are
2005c833c3b5SJed Brown 
2006c833c3b5SJed Brown    Input Arguments:
2007c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2008e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2009c833c3b5SJed Brown -  fine - finer DM to update
2010c833c3b5SJed Brown 
2011c833c3b5SJed Brown    Level: developer
2012c833c3b5SJed Brown 
2013c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2014c833c3b5SJed Brown @*/
2015c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2016c833c3b5SJed Brown {
2017c833c3b5SJed Brown   PetscErrorCode   ierr;
2018c833c3b5SJed Brown   DMRefineHookLink link;
2019c833c3b5SJed Brown 
2020c833c3b5SJed Brown   PetscFunctionBegin;
2021c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20228865f1eaSKarl Rupp     if (link->interphook) {
20238865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20248865f1eaSKarl Rupp     }
20254057135bSMatthew G Knepley   }
202647c6ae99SBarry Smith   PetscFunctionReturn(0);
202747c6ae99SBarry Smith }
202847c6ae99SBarry Smith 
2029eb3f98d2SBarry Smith /*@
2030eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2031eb3f98d2SBarry Smith 
2032eb3f98d2SBarry Smith     Not Collective
2033eb3f98d2SBarry Smith 
2034eb3f98d2SBarry Smith     Input Parameter:
2035eb3f98d2SBarry Smith .   dm - the DM object
2036eb3f98d2SBarry Smith 
2037eb3f98d2SBarry Smith     Output Parameter:
2038eb3f98d2SBarry Smith .   level - number of refinements
2039eb3f98d2SBarry Smith 
2040eb3f98d2SBarry Smith     Level: developer
2041eb3f98d2SBarry Smith 
20426a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2043eb3f98d2SBarry Smith 
2044eb3f98d2SBarry Smith @*/
2045eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2046eb3f98d2SBarry Smith {
2047eb3f98d2SBarry Smith   PetscFunctionBegin;
2048eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2049eb3f98d2SBarry Smith   *level = dm->levelup;
2050eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2051eb3f98d2SBarry Smith }
2052eb3f98d2SBarry Smith 
2053fef3a512SBarry Smith /*@
2054fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2055fef3a512SBarry Smith 
2056fef3a512SBarry Smith     Not Collective
2057fef3a512SBarry Smith 
2058fef3a512SBarry Smith     Input Parameter:
2059fef3a512SBarry Smith +   dm - the DM object
2060fef3a512SBarry Smith -   level - number of refinements
2061fef3a512SBarry Smith 
2062fef3a512SBarry Smith     Level: advanced
2063fef3a512SBarry Smith 
206495452b02SPatrick Sanan     Notes:
206595452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2066fef3a512SBarry Smith 
2067fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2068fef3a512SBarry Smith 
2069fef3a512SBarry Smith @*/
2070fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2071fef3a512SBarry Smith {
2072fef3a512SBarry Smith   PetscFunctionBegin;
2073fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2074fef3a512SBarry Smith   dm->levelup = level;
2075fef3a512SBarry Smith   PetscFunctionReturn(0);
2076fef3a512SBarry Smith }
2077fef3a512SBarry Smith 
2078ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2079ca3d3a14SMatthew G. Knepley {
2080ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2081ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2082ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2083ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2084ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2085ca3d3a14SMatthew G. Knepley }
2086ca3d3a14SMatthew G. Knepley 
2087ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2088ca3d3a14SMatthew G. Knepley {
2089ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2090ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2091ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2092ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2093ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2094ca3d3a14SMatthew G. Knepley }
2095ca3d3a14SMatthew G. Knepley 
2096ca3d3a14SMatthew G. Knepley /*@
2097c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2098ca3d3a14SMatthew G. Knepley 
2099ca3d3a14SMatthew G. Knepley   Input Parameter:
2100ca3d3a14SMatthew G. Knepley . dm - The DM
2101ca3d3a14SMatthew G. Knepley 
2102ca3d3a14SMatthew G. Knepley   Output Parameter:
2103ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2104ca3d3a14SMatthew G. Knepley 
2105ca3d3a14SMatthew G. Knepley   Level: developer
2106ca3d3a14SMatthew G. Knepley 
2107ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2108ca3d3a14SMatthew G. Knepley @*/
2109ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2110ca3d3a14SMatthew G. Knepley {
2111ca3d3a14SMatthew G. Knepley   Vec            tv;
2112ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2113ca3d3a14SMatthew G. Knepley 
2114ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2115ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2116534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2117ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2118ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2119ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2120ca3d3a14SMatthew G. Knepley }
2121ca3d3a14SMatthew G. Knepley 
2122ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2123ca3d3a14SMatthew G. Knepley {
2124ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2125ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2126ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2127ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2128ca3d3a14SMatthew G. Knepley 
2129ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2130ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
213192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2132ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2133ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2134ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
213592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2136ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2137ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2138ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2139ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2140ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2141ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2142ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2143ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2144ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2145ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2146ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2147ca3d3a14SMatthew G. Knepley     }
2148ca3d3a14SMatthew G. Knepley   }
2149ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2150ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2151ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2152ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2153ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2154ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2155ca3d3a14SMatthew G. Knepley       if (dof) {
2156ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2157ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2158ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2159ca3d3a14SMatthew G. Knepley 
2160ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2161ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2162ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2163580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2164ca3d3a14SMatthew G. Knepley       }
2165ca3d3a14SMatthew G. Knepley     }
2166ca3d3a14SMatthew G. Knepley   }
2167ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2168ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2169ca3d3a14SMatthew G. Knepley }
2170ca3d3a14SMatthew G. Knepley 
2171ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2172ca3d3a14SMatthew G. Knepley {
2173ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2174ca3d3a14SMatthew G. Knepley 
2175ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2176ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2177ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2178ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2179ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2180ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2181ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2182ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2183ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2184ca3d3a14SMatthew G. Knepley }
2185ca3d3a14SMatthew G. Knepley 
2186bb9467b5SJed Brown /*@C
2187baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2188baf369e7SPeter Brune 
2189baf369e7SPeter Brune    Logically Collective
2190baf369e7SPeter Brune 
2191baf369e7SPeter Brune    Input Arguments:
2192baf369e7SPeter Brune +  dm - the DM
2193baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2194baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
21950298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2196baf369e7SPeter Brune 
2197baf369e7SPeter Brune    Calling sequence for beginhook:
2198baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2199baf369e7SPeter Brune 
2200baf369e7SPeter Brune +  dm - global DM
2201baf369e7SPeter Brune .  g - global vector
2202baf369e7SPeter Brune .  mode - mode
2203baf369e7SPeter Brune .  l - local vector
2204baf369e7SPeter Brune -  ctx - optional user-defined function context
2205baf369e7SPeter Brune 
2206baf369e7SPeter Brune 
2207baf369e7SPeter Brune    Calling sequence for endhook:
2208ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2209baf369e7SPeter Brune 
2210baf369e7SPeter Brune +  global - global DM
2211baf369e7SPeter Brune -  ctx - optional user-defined function context
2212baf369e7SPeter Brune 
2213baf369e7SPeter Brune    Level: advanced
2214baf369e7SPeter Brune 
2215baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2216baf369e7SPeter Brune @*/
2217baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2218baf369e7SPeter Brune {
2219baf369e7SPeter Brune   PetscErrorCode          ierr;
2220baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2221baf369e7SPeter Brune 
2222baf369e7SPeter Brune   PetscFunctionBegin;
2223baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2224baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
222595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2226baf369e7SPeter Brune   link->beginhook = beginhook;
2227baf369e7SPeter Brune   link->endhook   = endhook;
2228baf369e7SPeter Brune   link->ctx       = ctx;
22290298fd71SBarry Smith   link->next      = NULL;
2230baf369e7SPeter Brune   *p              = link;
2231baf369e7SPeter Brune   PetscFunctionReturn(0);
2232baf369e7SPeter Brune }
2233baf369e7SPeter Brune 
22344c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
22354c274da1SToby Isaac {
22364c274da1SToby Isaac   Mat cMat;
22374c274da1SToby Isaac   Vec cVec;
22384c274da1SToby Isaac   PetscSection section, cSec;
22394c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22404c274da1SToby Isaac   PetscErrorCode ierr;
22414c274da1SToby Isaac 
22424c274da1SToby Isaac   PetscFunctionBegin;
22434c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22444c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22454c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
22465db9a05bSToby Isaac     PetscInt nRows;
22475db9a05bSToby Isaac 
22485db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22495db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
225092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
22517711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22524c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
22534c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22544c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22554c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22564c274da1SToby Isaac       if (dof) {
22574c274da1SToby Isaac         PetscScalar *vals;
22584c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
22594c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
22604c274da1SToby Isaac       }
22614c274da1SToby Isaac     }
22624c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22634c274da1SToby Isaac   }
22644c274da1SToby Isaac   PetscFunctionReturn(0);
22654c274da1SToby Isaac }
22664c274da1SToby Isaac 
226747c6ae99SBarry Smith /*@
226801729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
226901729b5cSPatrick Sanan 
2270d083f849SBarry Smith     Neighbor-wise Collective on dm
227101729b5cSPatrick Sanan 
227201729b5cSPatrick Sanan     Input Parameters:
227301729b5cSPatrick Sanan +   dm - the DM object
227401729b5cSPatrick Sanan .   g - the global vector
227501729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
227601729b5cSPatrick Sanan -   l - the local vector
227701729b5cSPatrick Sanan 
227801729b5cSPatrick Sanan     Notes:
227901729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
228001729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
228101729b5cSPatrick Sanan 
228201729b5cSPatrick Sanan     Level: beginner
228301729b5cSPatrick Sanan 
228401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
228501729b5cSPatrick Sanan 
228601729b5cSPatrick Sanan @*/
228701729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
228801729b5cSPatrick Sanan {
228901729b5cSPatrick Sanan   PetscErrorCode ierr;
229001729b5cSPatrick Sanan 
229101729b5cSPatrick Sanan   PetscFunctionBegin;
229201729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
229301729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
229401729b5cSPatrick Sanan   PetscFunctionReturn(0);
229501729b5cSPatrick Sanan }
229601729b5cSPatrick Sanan 
229701729b5cSPatrick Sanan /*@
229847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
229947c6ae99SBarry Smith 
2300d083f849SBarry Smith     Neighbor-wise Collective on dm
230147c6ae99SBarry Smith 
230247c6ae99SBarry Smith     Input Parameters:
230347c6ae99SBarry Smith +   dm - the DM object
230447c6ae99SBarry Smith .   g - the global vector
230547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
230647c6ae99SBarry Smith -   l - the local vector
230747c6ae99SBarry Smith 
230801729b5cSPatrick Sanan     Level: intermediate
230947c6ae99SBarry Smith 
231001729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
231147c6ae99SBarry Smith 
231247c6ae99SBarry Smith @*/
23137087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
231447c6ae99SBarry Smith {
23157128ae9fSMatthew G Knepley   PetscSF                 sf;
231647c6ae99SBarry Smith   PetscErrorCode          ierr;
2317baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
231847c6ae99SBarry Smith 
231947c6ae99SBarry Smith   PetscFunctionBegin;
2320171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2321baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23228865f1eaSKarl Rupp     if (link->beginhook) {
23238865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23248865f1eaSKarl Rupp     }
2325baf369e7SPeter Brune   }
23261bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
23277128ae9fSMatthew G Knepley   if (sf) {
2328ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2329ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
23307128ae9fSMatthew G Knepley 
233182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23327128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2333ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23347128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23357128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2336ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
23377128ae9fSMatthew G Knepley   } else {
233833907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2339843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23407128ae9fSMatthew G Knepley   }
234147c6ae99SBarry Smith   PetscFunctionReturn(0);
234247c6ae99SBarry Smith }
234347c6ae99SBarry Smith 
234447c6ae99SBarry Smith /*@
234547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
234647c6ae99SBarry Smith 
2347d083f849SBarry Smith     Neighbor-wise Collective on dm
234847c6ae99SBarry Smith 
234947c6ae99SBarry Smith     Input Parameters:
235047c6ae99SBarry Smith +   dm - the DM object
235147c6ae99SBarry Smith .   g - the global vector
235247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
235347c6ae99SBarry Smith -   l - the local vector
235447c6ae99SBarry Smith 
235501729b5cSPatrick Sanan     Level: intermediate
235647c6ae99SBarry Smith 
235701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
235847c6ae99SBarry Smith 
235947c6ae99SBarry Smith @*/
23607087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
236147c6ae99SBarry Smith {
23627128ae9fSMatthew G Knepley   PetscSF                 sf;
236347c6ae99SBarry Smith   PetscErrorCode          ierr;
2364ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2365ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2366ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2367baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
236847c6ae99SBarry Smith 
236947c6ae99SBarry Smith   PetscFunctionBegin;
2370171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23711bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2372ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
23737128ae9fSMatthew G Knepley   if (sf) {
237482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23757128ae9fSMatthew G Knepley 
23767128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2377ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23787128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23797128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2380ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2381ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
23827128ae9fSMatthew G Knepley   } else {
238333907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2384843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23857128ae9fSMatthew G Knepley   }
23864c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2387baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2388baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2389baf369e7SPeter Brune   }
239047c6ae99SBarry Smith   PetscFunctionReturn(0);
239147c6ae99SBarry Smith }
239247c6ae99SBarry Smith 
2393d4d07f1eSToby Isaac /*@C
2394d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2395d4d07f1eSToby Isaac 
2396d4d07f1eSToby Isaac    Logically Collective
2397d4d07f1eSToby Isaac 
2398d4d07f1eSToby Isaac    Input Arguments:
2399d4d07f1eSToby Isaac +  dm - the DM
2400d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2401d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2402d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2403d4d07f1eSToby Isaac 
2404d4d07f1eSToby Isaac    Calling sequence for beginhook:
2405d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2406d4d07f1eSToby Isaac 
2407d4d07f1eSToby Isaac +  dm - global DM
2408d4d07f1eSToby Isaac .  l - local vector
2409d4d07f1eSToby Isaac .  mode - mode
2410d4d07f1eSToby Isaac .  g - global vector
2411d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2412d4d07f1eSToby Isaac 
2413d4d07f1eSToby Isaac 
2414d4d07f1eSToby Isaac    Calling sequence for endhook:
2415d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2416d4d07f1eSToby Isaac 
2417d4d07f1eSToby Isaac +  global - global DM
2418d4d07f1eSToby Isaac .  l - local vector
2419d4d07f1eSToby Isaac .  mode - mode
2420d4d07f1eSToby Isaac .  g - global vector
2421d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2422d4d07f1eSToby Isaac 
2423d4d07f1eSToby Isaac    Level: advanced
2424d4d07f1eSToby Isaac 
2425d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2426d4d07f1eSToby Isaac @*/
2427d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2428d4d07f1eSToby Isaac {
2429d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2430d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2431d4d07f1eSToby Isaac 
2432d4d07f1eSToby Isaac   PetscFunctionBegin;
2433d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2434d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
243595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2436d4d07f1eSToby Isaac   link->beginhook = beginhook;
2437d4d07f1eSToby Isaac   link->endhook   = endhook;
2438d4d07f1eSToby Isaac   link->ctx       = ctx;
2439d4d07f1eSToby Isaac   link->next      = NULL;
2440d4d07f1eSToby Isaac   *p              = link;
2441d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2442d4d07f1eSToby Isaac }
2443d4d07f1eSToby Isaac 
24444c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
24454c274da1SToby Isaac {
24464c274da1SToby Isaac   Mat cMat;
24474c274da1SToby Isaac   Vec cVec;
24484c274da1SToby Isaac   PetscSection section, cSec;
24494c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24504c274da1SToby Isaac   PetscErrorCode ierr;
24514c274da1SToby Isaac 
24524c274da1SToby Isaac   PetscFunctionBegin;
24534c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24544c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24554c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
24565db9a05bSToby Isaac     PetscInt nRows;
24575db9a05bSToby Isaac 
24585db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24595db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
246092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
24617711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24624c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24634c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24644c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24654c274da1SToby Isaac       if (dof) {
24664c274da1SToby Isaac         PetscInt d;
24674c274da1SToby Isaac         PetscScalar *vals;
24684c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
24694c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
24704c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
24714c274da1SToby Isaac          * we just extracted */
24724c274da1SToby Isaac         for (d = 0; d < dof; d++) {
24734c274da1SToby Isaac           vals[d] = 0.;
24744c274da1SToby Isaac         }
24754c274da1SToby Isaac       }
24764c274da1SToby Isaac     }
24774c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
24784c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24794c274da1SToby Isaac   }
24804c274da1SToby Isaac   PetscFunctionReturn(0);
24814c274da1SToby Isaac }
248201729b5cSPatrick Sanan /*@
248301729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
248401729b5cSPatrick Sanan 
2485d083f849SBarry Smith     Neighbor-wise Collective on dm
248601729b5cSPatrick Sanan 
248701729b5cSPatrick Sanan     Input Parameters:
248801729b5cSPatrick Sanan +   dm - the DM object
248901729b5cSPatrick Sanan .   l - the local vector
249001729b5cSPatrick 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.
249101729b5cSPatrick Sanan -   g - the global vector
249201729b5cSPatrick Sanan 
249301729b5cSPatrick Sanan     Notes:
249401729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
249501729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
249601729b5cSPatrick Sanan 
249701729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
249801729b5cSPatrick 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.
249901729b5cSPatrick Sanan 
250001729b5cSPatrick Sanan     Level: beginner
250101729b5cSPatrick Sanan 
250201729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
250301729b5cSPatrick Sanan 
250401729b5cSPatrick Sanan @*/
250501729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
250601729b5cSPatrick Sanan {
250701729b5cSPatrick Sanan   PetscErrorCode ierr;
250801729b5cSPatrick Sanan 
250901729b5cSPatrick Sanan   PetscFunctionBegin;
251001729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
251101729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
251201729b5cSPatrick Sanan   PetscFunctionReturn(0);
251301729b5cSPatrick Sanan }
25144c274da1SToby Isaac 
251547c6ae99SBarry Smith /*@
251601729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25179a42bb27SBarry Smith 
2518d083f849SBarry Smith     Neighbor-wise Collective on dm
25199a42bb27SBarry Smith 
25209a42bb27SBarry Smith     Input Parameters:
25219a42bb27SBarry Smith +   dm - the DM object
2522f6813fd5SJed Brown .   l - the local vector
25231eb28f2eSBarry 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.
25241eb28f2eSBarry Smith -   g - the global vector
25259a42bb27SBarry Smith 
252695452b02SPatrick Sanan     Notes:
252795452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
252884330215SMatthew 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.
25299a42bb27SBarry Smith 
253001729b5cSPatrick Sanan     Level: intermediate
25319a42bb27SBarry Smith 
253201729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
25339a42bb27SBarry Smith 
25349a42bb27SBarry Smith @*/
25357087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
25369a42bb27SBarry Smith {
25377128ae9fSMatthew G Knepley   PetscSF                 sf;
253884330215SMatthew G. Knepley   PetscSection            s, gs;
2539d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2540ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2541ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2542ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2543ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
254484330215SMatthew G. Knepley   PetscErrorCode          ierr;
25459a42bb27SBarry Smith 
25469a42bb27SBarry Smith   PetscFunctionBegin;
2547171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2548d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2549d4d07f1eSToby Isaac     if (link->beginhook) {
2550d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2551d4d07f1eSToby Isaac     }
2552d4d07f1eSToby Isaac   }
25534c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
25541bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
255592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
25567128ae9fSMatthew G Knepley   switch (mode) {
25577128ae9fSMatthew G Knepley   case INSERT_VALUES:
25587128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2559304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
256084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25617128ae9fSMatthew G Knepley   case ADD_VALUES:
25627128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2563304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
256484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25657128ae9fSMatthew G Knepley   default:
256682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25677128ae9fSMatthew G Knepley   }
2568ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2569ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2570ca3d3a14SMatthew G. Knepley     if (transform) {
2571ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2572ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2573ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2574ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2575ca3d3a14SMatthew G. Knepley     } else {
2576ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2577ca3d3a14SMatthew G. Knepley     }
25787128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2579ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2580a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
258184330215SMatthew G. Knepley     } else if (s && isInsert) {
258284330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
258384330215SMatthew G. Knepley 
2584e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
258584330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
258684330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
258784330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2588b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
258984330215SMatthew G. Knepley 
259084330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
259103442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
259284330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2593b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
259484330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
259584330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2596b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
259703442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2598b3b16f48SMatthew 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);
2599b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2600b3b16f48SMatthew G. Knepley         if (!gcdof) {
260184330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2602b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2603b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
260484330215SMatthew G. Knepley           const PetscInt *cdofs;
260584330215SMatthew G. Knepley           PetscInt        cind = 0;
260684330215SMatthew G. Knepley 
260784330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2608b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
260984330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2610b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
261184330215SMatthew G. Knepley           }
2612b3b16f48SMatthew 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);
261384330215SMatthew G. Knepley       }
2614ca3d3a14SMatthew G. Knepley     }
26157128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2616ca3d3a14SMatthew G. Knepley     if (transform) {
2617ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2618ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2619ca3d3a14SMatthew G. Knepley     } else {
2620ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2621ca3d3a14SMatthew G. Knepley     }
26227128ae9fSMatthew G Knepley   } else {
2623b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2624843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26257128ae9fSMatthew G Knepley   }
26269a42bb27SBarry Smith   PetscFunctionReturn(0);
26279a42bb27SBarry Smith }
26289a42bb27SBarry Smith 
26299a42bb27SBarry Smith /*@
26309a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
263147c6ae99SBarry Smith 
2632d083f849SBarry Smith     Neighbor-wise Collective on dm
263347c6ae99SBarry Smith 
263447c6ae99SBarry Smith     Input Parameters:
263547c6ae99SBarry Smith +   dm - the DM object
2636f6813fd5SJed Brown .   l - the local vector
263747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2638f6813fd5SJed Brown -   g - the global vector
263947c6ae99SBarry Smith 
264001729b5cSPatrick Sanan     Level: intermediate
264147c6ae99SBarry Smith 
2642e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
264347c6ae99SBarry Smith 
264447c6ae99SBarry Smith @*/
26457087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
264647c6ae99SBarry Smith {
26477128ae9fSMatthew G Knepley   PetscSF                 sf;
264884330215SMatthew G. Knepley   PetscSection            s;
2649d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2650ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
265184330215SMatthew G. Knepley   PetscErrorCode          ierr;
265247c6ae99SBarry Smith 
265347c6ae99SBarry Smith   PetscFunctionBegin;
2654171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26551bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
265692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26577128ae9fSMatthew G Knepley   switch (mode) {
26587128ae9fSMatthew G Knepley   case INSERT_VALUES:
26597128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
266084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26617128ae9fSMatthew G Knepley   case ADD_VALUES:
26627128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
266384330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26647128ae9fSMatthew G Knepley   default:
266582f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26667128ae9fSMatthew G Knepley   }
266784330215SMatthew G. Knepley   if (sf && !isInsert) {
2668ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2669ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2670ca3d3a14SMatthew G. Knepley     Vec                tmpl;
267184330215SMatthew G. Knepley 
2672ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2673ca3d3a14SMatthew G. Knepley     if (transform) {
2674ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2675ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2676ca3d3a14SMatthew G. Knepley     } else {
2677ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2678ca3d3a14SMatthew G. Knepley     }
26797128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2680a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2681ca3d3a14SMatthew G. Knepley     if (transform) {
2682ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2683ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2684ca3d3a14SMatthew G. Knepley     } else {
2685ae5cfb4aSMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2686ca3d3a14SMatthew G. Knepley     }
26877128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
268884330215SMatthew G. Knepley   } else if (s && isInsert) {
26897128ae9fSMatthew G Knepley   } else {
2690b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2691843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26927128ae9fSMatthew G Knepley   }
2693d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2694d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2695d4d07f1eSToby Isaac   }
269647c6ae99SBarry Smith   PetscFunctionReturn(0);
269747c6ae99SBarry Smith }
269847c6ae99SBarry Smith 
2699f089877aSRichard Tran Mills /*@
2700bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2701bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2702d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2703f089877aSRichard Tran Mills 
2704d083f849SBarry Smith    Neighbor-wise Collective on dm
2705f089877aSRichard Tran Mills 
2706f089877aSRichard Tran Mills    Input Parameters:
2707f089877aSRichard Tran Mills +  dm - the DM object
2708bc0a1609SRichard Tran Mills .  g - the original local vector
2709bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2710f089877aSRichard Tran Mills 
2711bc0a1609SRichard Tran Mills    Output Parameter:
2712bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2713f089877aSRichard Tran Mills 
2714f089877aSRichard Tran Mills    Level: intermediate
2715f089877aSRichard Tran Mills 
2716bc0a1609SRichard Tran Mills    Notes:
2717bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2718bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2719bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2720bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2721bc0a1609SRichard Tran Mills 
2722bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2723f089877aSRichard Tran Mills 
2724f089877aSRichard Tran Mills @*/
2725f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2726f089877aSRichard Tran Mills {
2727f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2728f089877aSRichard Tran Mills 
2729f089877aSRichard Tran Mills   PetscFunctionBegin;
2730f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2731bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2732f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2733f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2734f089877aSRichard Tran Mills }
2735f089877aSRichard Tran Mills 
2736f089877aSRichard Tran Mills /*@
2737bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2738bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2739d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2740f089877aSRichard Tran Mills 
2741d083f849SBarry Smith    Neighbor-wise Collective on dm
2742f089877aSRichard Tran Mills 
2743f089877aSRichard Tran Mills    Input Parameters:
2744bc0a1609SRichard Tran Mills +  da - the DM object
2745bc0a1609SRichard Tran Mills .  g - the original local vector
2746bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2747f089877aSRichard Tran Mills 
2748bc0a1609SRichard Tran Mills    Output Parameter:
2749bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2750f089877aSRichard Tran Mills 
2751f089877aSRichard Tran Mills    Level: intermediate
2752f089877aSRichard Tran Mills 
2753bc0a1609SRichard Tran Mills    Notes:
2754bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2755bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2756bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2757bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2758bc0a1609SRichard Tran Mills 
2759bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2760f089877aSRichard Tran Mills 
2761f089877aSRichard Tran Mills @*/
2762f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2763f089877aSRichard Tran Mills {
2764f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2765f089877aSRichard Tran Mills 
2766f089877aSRichard Tran Mills   PetscFunctionBegin;
2767f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2768bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2769f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2770f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2771f089877aSRichard Tran Mills }
2772f089877aSRichard Tran Mills 
2773f089877aSRichard Tran Mills 
277447c6ae99SBarry Smith /*@
277547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
277647c6ae99SBarry Smith 
2777d083f849SBarry Smith     Collective on dm
277847c6ae99SBarry Smith 
277947c6ae99SBarry Smith     Input Parameter:
278047c6ae99SBarry Smith +   dm - the DM object
278191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
278247c6ae99SBarry Smith 
278347c6ae99SBarry Smith     Output Parameter:
278447c6ae99SBarry Smith .   dmc - the coarsened DM
278547c6ae99SBarry Smith 
278647c6ae99SBarry Smith     Level: developer
278747c6ae99SBarry Smith 
2788e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
278947c6ae99SBarry Smith 
279047c6ae99SBarry Smith @*/
27917087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
279247c6ae99SBarry Smith {
279347c6ae99SBarry Smith   PetscErrorCode    ierr;
2794b17ce1afSJed Brown   DMCoarsenHookLink link;
279547c6ae99SBarry Smith 
279647c6ae99SBarry Smith   PetscFunctionBegin;
2797171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2798b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
279947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
280047c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2801b9d85ea2SLisandro Dalcin   if (*dmc) {
2802a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
280343842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28048cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2805644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
28060598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2807656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2808e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2809b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2810b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2811b17ce1afSJed Brown     }
2812b9d85ea2SLisandro Dalcin   }
281347a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2814b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2815b17ce1afSJed Brown   PetscFunctionReturn(0);
2816b17ce1afSJed Brown }
2817b17ce1afSJed Brown 
2818bb9467b5SJed Brown /*@C
2819b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2820b17ce1afSJed Brown 
2821b17ce1afSJed Brown    Logically Collective
2822b17ce1afSJed Brown 
2823b17ce1afSJed Brown    Input Arguments:
2824b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2825b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2826b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
28270298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2828b17ce1afSJed Brown 
2829b17ce1afSJed Brown    Calling sequence of coarsenhook:
2830b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2831b17ce1afSJed Brown 
2832b17ce1afSJed Brown +  fine - fine level DM
2833b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2834b17ce1afSJed Brown -  ctx - optional user-defined function context
2835b17ce1afSJed Brown 
2836b17ce1afSJed Brown    Calling sequence for restricthook:
2837c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2838b17ce1afSJed Brown 
2839b17ce1afSJed Brown +  fine - fine level DM
2840b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2841c833c3b5SJed Brown .  rscale - scaling vector for restriction
2842c833c3b5SJed Brown .  inject - matrix restricting by injection
2843b17ce1afSJed Brown .  coarse - coarse level DM to update
2844b17ce1afSJed Brown -  ctx - optional user-defined function context
2845b17ce1afSJed Brown 
2846b17ce1afSJed Brown    Level: advanced
2847b17ce1afSJed Brown 
2848b17ce1afSJed Brown    Notes:
2849b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2850b17ce1afSJed Brown 
2851b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2852b17ce1afSJed Brown 
2853b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2854b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2855b17ce1afSJed Brown 
2856bb9467b5SJed Brown    This function is currently not available from Fortran.
2857bb9467b5SJed Brown 
2858dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2859b17ce1afSJed Brown @*/
2860b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2861b17ce1afSJed Brown {
2862b17ce1afSJed Brown   PetscErrorCode    ierr;
2863b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2864b17ce1afSJed Brown 
2865b17ce1afSJed Brown   PetscFunctionBegin;
2866b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
28671e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
28681e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
28691e3d8eccSJed Brown   }
287095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2871b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2872b17ce1afSJed Brown   link->restricthook = restricthook;
2873b17ce1afSJed Brown   link->ctx          = ctx;
28740298fd71SBarry Smith   link->next         = NULL;
2875b17ce1afSJed Brown   *p                 = link;
2876b17ce1afSJed Brown   PetscFunctionReturn(0);
2877b17ce1afSJed Brown }
2878b17ce1afSJed Brown 
2879dc822a44SJed Brown /*@C
2880dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2881dc822a44SJed Brown 
2882dc822a44SJed Brown    Logically Collective
2883dc822a44SJed Brown 
2884dc822a44SJed Brown    Input Arguments:
2885dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2886dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2887dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2888dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2889dc822a44SJed Brown 
2890dc822a44SJed Brown    Level: advanced
2891dc822a44SJed Brown 
2892dc822a44SJed Brown    Notes:
2893dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2894dc822a44SJed Brown 
2895dc822a44SJed Brown    This function is currently not available from Fortran.
2896dc822a44SJed Brown 
2897dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2898dc822a44SJed Brown @*/
2899dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2900dc822a44SJed Brown {
2901dc822a44SJed Brown   PetscErrorCode    ierr;
2902dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2903dc822a44SJed Brown 
2904dc822a44SJed Brown   PetscFunctionBegin;
2905dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2906dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2907dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2908dc822a44SJed Brown       link = *p;
2909dc822a44SJed Brown       *p = link->next;
2910dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2911dc822a44SJed Brown       break;
2912dc822a44SJed Brown     }
2913dc822a44SJed Brown   }
2914dc822a44SJed Brown   PetscFunctionReturn(0);
2915dc822a44SJed Brown }
2916dc822a44SJed Brown 
2917dc822a44SJed Brown 
2918b17ce1afSJed Brown /*@
2919b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2920b17ce1afSJed Brown 
2921b17ce1afSJed Brown    Collective if any hooks are
2922b17ce1afSJed Brown 
2923b17ce1afSJed Brown    Input Arguments:
2924b17ce1afSJed Brown +  fine - finer DM to use as a base
2925b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2926e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2927b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2928e91eccc2SStefano Zampini -  coarse - coarser DM to update
2929b17ce1afSJed Brown 
2930b17ce1afSJed Brown    Level: developer
2931b17ce1afSJed Brown 
2932b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2933b17ce1afSJed Brown @*/
2934b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2935b17ce1afSJed Brown {
2936b17ce1afSJed Brown   PetscErrorCode    ierr;
2937b17ce1afSJed Brown   DMCoarsenHookLink link;
2938b17ce1afSJed Brown 
2939b17ce1afSJed Brown   PetscFunctionBegin;
2940b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
29418865f1eaSKarl Rupp     if (link->restricthook) {
29428865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
29438865f1eaSKarl Rupp     }
2944b17ce1afSJed Brown   }
294547c6ae99SBarry Smith   PetscFunctionReturn(0);
294647c6ae99SBarry Smith }
294747c6ae99SBarry Smith 
2948bb9467b5SJed Brown /*@C
2949be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
29505dbd56e3SPeter Brune 
2951d083f849SBarry Smith    Logically Collective on global
29525dbd56e3SPeter Brune 
29535dbd56e3SPeter Brune    Input Arguments:
29545dbd56e3SPeter Brune +  global - global DM
2955ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
29565dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
29570298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
29585dbd56e3SPeter Brune 
2959ec4806b8SPeter Brune 
2960ec4806b8SPeter Brune    Calling sequence for ddhook:
2961ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2962ec4806b8SPeter Brune 
2963ec4806b8SPeter Brune +  global - global DM
2964ec4806b8SPeter Brune .  block  - block DM
2965ec4806b8SPeter Brune -  ctx - optional user-defined function context
2966ec4806b8SPeter Brune 
29675dbd56e3SPeter Brune    Calling sequence for restricthook:
2968ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
29695dbd56e3SPeter Brune 
29705dbd56e3SPeter Brune +  global - global DM
29715dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
29725dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2973ec4806b8SPeter Brune .  block  - block DM
29745dbd56e3SPeter Brune -  ctx - optional user-defined function context
29755dbd56e3SPeter Brune 
29765dbd56e3SPeter Brune    Level: advanced
29775dbd56e3SPeter Brune 
29785dbd56e3SPeter Brune    Notes:
2979ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
29805dbd56e3SPeter Brune 
29815dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
29825dbd56e3SPeter Brune 
29835dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2984ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
29855dbd56e3SPeter Brune 
2986bb9467b5SJed Brown    This function is currently not available from Fortran.
2987bb9467b5SJed Brown 
29885dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
29895dbd56e3SPeter Brune @*/
2990be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
29915dbd56e3SPeter Brune {
29925dbd56e3SPeter Brune   PetscErrorCode      ierr;
2993be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
29945dbd56e3SPeter Brune 
29955dbd56e3SPeter Brune   PetscFunctionBegin;
29965dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2997b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2998b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2999b3a6b972SJed Brown   }
300095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30015dbd56e3SPeter Brune   link->restricthook = restricthook;
3002be081cd6SPeter Brune   link->ddhook       = ddhook;
30035dbd56e3SPeter Brune   link->ctx          = ctx;
30040298fd71SBarry Smith   link->next         = NULL;
30055dbd56e3SPeter Brune   *p                 = link;
30065dbd56e3SPeter Brune   PetscFunctionReturn(0);
30075dbd56e3SPeter Brune }
30085dbd56e3SPeter Brune 
3009b3a6b972SJed Brown /*@C
3010b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3011b3a6b972SJed Brown 
3012b3a6b972SJed Brown    Logically Collective
3013b3a6b972SJed Brown 
3014b3a6b972SJed Brown    Input Arguments:
3015b3a6b972SJed Brown +  global - global DM
3016b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3017b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3018b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3019b3a6b972SJed Brown 
3020b3a6b972SJed Brown    Level: advanced
3021b3a6b972SJed Brown 
3022b3a6b972SJed Brown    Notes:
3023b3a6b972SJed Brown 
3024b3a6b972SJed Brown    This function is currently not available from Fortran.
3025b3a6b972SJed Brown 
3026b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3027b3a6b972SJed Brown @*/
3028b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3029b3a6b972SJed Brown {
3030b3a6b972SJed Brown   PetscErrorCode      ierr;
3031b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3032b3a6b972SJed Brown 
3033b3a6b972SJed Brown   PetscFunctionBegin;
3034b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3035b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3036b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3037b3a6b972SJed Brown       link = *p;
3038b3a6b972SJed Brown       *p = link->next;
3039b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3040b3a6b972SJed Brown       break;
3041b3a6b972SJed Brown     }
3042b3a6b972SJed Brown   }
3043b3a6b972SJed Brown   PetscFunctionReturn(0);
3044b3a6b972SJed Brown }
3045b3a6b972SJed Brown 
30465dbd56e3SPeter Brune /*@
3047be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
30485dbd56e3SPeter Brune 
30495dbd56e3SPeter Brune    Collective if any hooks are
30505dbd56e3SPeter Brune 
30515dbd56e3SPeter Brune    Input Arguments:
30525dbd56e3SPeter Brune +  fine - finer DM to use as a base
3053be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3054be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
30555dbd56e3SPeter Brune -  coarse - coarer DM to update
30565dbd56e3SPeter Brune 
30575dbd56e3SPeter Brune    Level: developer
30585dbd56e3SPeter Brune 
30595dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
30605dbd56e3SPeter Brune @*/
3061be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
30625dbd56e3SPeter Brune {
30635dbd56e3SPeter Brune   PetscErrorCode      ierr;
3064be081cd6SPeter Brune   DMSubDomainHookLink link;
30655dbd56e3SPeter Brune 
30665dbd56e3SPeter Brune   PetscFunctionBegin;
3067be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
30688865f1eaSKarl Rupp     if (link->restricthook) {
30698865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
30708865f1eaSKarl Rupp     }
30715dbd56e3SPeter Brune   }
30725dbd56e3SPeter Brune   PetscFunctionReturn(0);
30735dbd56e3SPeter Brune }
30745dbd56e3SPeter Brune 
30755fe1f584SPeter Brune /*@
30766a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
30775fe1f584SPeter Brune 
30785fe1f584SPeter Brune     Not Collective
30795fe1f584SPeter Brune 
30805fe1f584SPeter Brune     Input Parameter:
30815fe1f584SPeter Brune .   dm - the DM object
30825fe1f584SPeter Brune 
30835fe1f584SPeter Brune     Output Parameter:
30846a7d9d85SPeter Brune .   level - number of coarsenings
30855fe1f584SPeter Brune 
30865fe1f584SPeter Brune     Level: developer
30875fe1f584SPeter Brune 
30886a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
30895fe1f584SPeter Brune 
30905fe1f584SPeter Brune @*/
30915fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
30925fe1f584SPeter Brune {
30935fe1f584SPeter Brune   PetscFunctionBegin;
30945fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3095b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
30965fe1f584SPeter Brune   *level = dm->leveldown;
30975fe1f584SPeter Brune   PetscFunctionReturn(0);
30985fe1f584SPeter Brune }
30995fe1f584SPeter Brune 
31009a64c4a8SMatthew G. Knepley /*@
31019a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31029a64c4a8SMatthew G. Knepley 
31039a64c4a8SMatthew G. Knepley     Not Collective
31049a64c4a8SMatthew G. Knepley 
31059a64c4a8SMatthew G. Knepley     Input Parameters:
31069a64c4a8SMatthew G. Knepley +   dm - the DM object
31079a64c4a8SMatthew G. Knepley -   level - number of coarsenings
31089a64c4a8SMatthew G. Knepley 
31099a64c4a8SMatthew G. Knepley     Level: developer
31109a64c4a8SMatthew G. Knepley 
31119a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31129a64c4a8SMatthew G. Knepley @*/
31139a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
31149a64c4a8SMatthew G. Knepley {
31159a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
31169a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31179a64c4a8SMatthew G. Knepley   dm->leveldown = level;
31189a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
31199a64c4a8SMatthew G. Knepley }
31209a64c4a8SMatthew G. Knepley 
31215fe1f584SPeter Brune 
31225fe1f584SPeter Brune 
312347c6ae99SBarry Smith /*@C
312447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
312547c6ae99SBarry Smith 
3126d083f849SBarry Smith     Collective on dm
312747c6ae99SBarry Smith 
312847c6ae99SBarry Smith     Input Parameter:
312947c6ae99SBarry Smith +   dm - the DM object
313047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
313147c6ae99SBarry Smith 
313247c6ae99SBarry Smith     Output Parameter:
313347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
313447c6ae99SBarry Smith 
313547c6ae99SBarry Smith     Level: developer
313647c6ae99SBarry Smith 
3137e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
313847c6ae99SBarry Smith 
313947c6ae99SBarry Smith @*/
31407087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
314147c6ae99SBarry Smith {
314247c6ae99SBarry Smith   PetscErrorCode ierr;
314347c6ae99SBarry Smith 
314447c6ae99SBarry Smith   PetscFunctionBegin;
3145171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3146ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
314747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3148b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
314947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
315047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
315147c6ae99SBarry Smith   } else if (dm->ops->refine) {
315247c6ae99SBarry Smith     PetscInt i;
315347c6ae99SBarry Smith 
3154ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
315547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3156ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
315747c6ae99SBarry Smith     }
3158ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
315947c6ae99SBarry Smith   PetscFunctionReturn(0);
316047c6ae99SBarry Smith }
316147c6ae99SBarry Smith 
316247c6ae99SBarry Smith /*@C
316347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
316447c6ae99SBarry Smith 
3165d083f849SBarry Smith     Collective on dm
316647c6ae99SBarry Smith 
316747c6ae99SBarry Smith     Input Parameter:
316847c6ae99SBarry Smith +   dm - the DM object
316947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
317047c6ae99SBarry Smith 
317147c6ae99SBarry Smith     Output Parameter:
317247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
317347c6ae99SBarry Smith 
317447c6ae99SBarry Smith     Level: developer
317547c6ae99SBarry Smith 
3176e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
317747c6ae99SBarry Smith 
317847c6ae99SBarry Smith @*/
31797087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
318047c6ae99SBarry Smith {
318147c6ae99SBarry Smith   PetscErrorCode ierr;
318247c6ae99SBarry Smith 
318347c6ae99SBarry Smith   PetscFunctionBegin;
3184171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3185ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
318647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
318747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
318847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
318947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
319047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
319147c6ae99SBarry Smith     PetscInt i;
319247c6ae99SBarry Smith 
3193ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
319447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3195ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
319647c6ae99SBarry Smith     }
3197ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
319847c6ae99SBarry Smith   PetscFunctionReturn(0);
319947c6ae99SBarry Smith }
320047c6ae99SBarry Smith 
32011a266240SBarry Smith /*@C
32021a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32031a266240SBarry Smith 
32041a266240SBarry Smith     Not Collective
32051a266240SBarry Smith 
32061a266240SBarry Smith     Input Parameters:
32071a266240SBarry Smith +   dm - the DM object
32081a266240SBarry Smith -   destroy - the destroy function
32091a266240SBarry Smith 
32101a266240SBarry Smith     Level: intermediate
32111a266240SBarry Smith 
3212e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
32131a266240SBarry Smith 
3214f07f9ceaSJed Brown @*/
32151a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
32161a266240SBarry Smith {
32171a266240SBarry Smith   PetscFunctionBegin;
3218171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32191a266240SBarry Smith   dm->ctxdestroy = destroy;
32201a266240SBarry Smith   PetscFunctionReturn(0);
32211a266240SBarry Smith }
32221a266240SBarry Smith 
3223b07ff414SBarry Smith /*@
32241b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
322547c6ae99SBarry Smith 
322647c6ae99SBarry Smith     Not Collective
322747c6ae99SBarry Smith 
322847c6ae99SBarry Smith     Input Parameters:
322947c6ae99SBarry Smith +   dm - the DM object
323047c6ae99SBarry Smith -   ctx - the user context
323147c6ae99SBarry Smith 
323247c6ae99SBarry Smith     Level: intermediate
323347c6ae99SBarry Smith 
3234e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
323547c6ae99SBarry Smith 
323647c6ae99SBarry Smith @*/
32371b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
323847c6ae99SBarry Smith {
323947c6ae99SBarry Smith   PetscFunctionBegin;
3240171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
324147c6ae99SBarry Smith   dm->ctx = ctx;
324247c6ae99SBarry Smith   PetscFunctionReturn(0);
324347c6ae99SBarry Smith }
324447c6ae99SBarry Smith 
324547c6ae99SBarry Smith /*@
32461b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
324747c6ae99SBarry Smith 
324847c6ae99SBarry Smith     Not Collective
324947c6ae99SBarry Smith 
325047c6ae99SBarry Smith     Input Parameter:
325147c6ae99SBarry Smith .   dm - the DM object
325247c6ae99SBarry Smith 
325347c6ae99SBarry Smith     Output Parameter:
325447c6ae99SBarry Smith .   ctx - the user context
325547c6ae99SBarry Smith 
325647c6ae99SBarry Smith     Level: intermediate
325747c6ae99SBarry Smith 
3258e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
325947c6ae99SBarry Smith 
326047c6ae99SBarry Smith @*/
32611b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
326247c6ae99SBarry Smith {
326347c6ae99SBarry Smith   PetscFunctionBegin;
3264171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32651b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
326647c6ae99SBarry Smith   PetscFunctionReturn(0);
326747c6ae99SBarry Smith }
326847c6ae99SBarry Smith 
326908da532bSDmitry Karpeev /*@C
3270df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
327108da532bSDmitry Karpeev 
3272d083f849SBarry Smith     Logically Collective on dm
327308da532bSDmitry Karpeev 
327408da532bSDmitry Karpeev     Input Parameter:
327508da532bSDmitry Karpeev +   dm - the DM object
32760298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
327708da532bSDmitry Karpeev 
327808da532bSDmitry Karpeev     Level: intermediate
327908da532bSDmitry Karpeev 
3280835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
328108da532bSDmitry Karpeev          DMSetJacobian()
328208da532bSDmitry Karpeev 
328308da532bSDmitry Karpeev @*/
328408da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
328508da532bSDmitry Karpeev {
328608da532bSDmitry Karpeev   PetscFunctionBegin;
32875a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
328808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
328908da532bSDmitry Karpeev   PetscFunctionReturn(0);
329008da532bSDmitry Karpeev }
329108da532bSDmitry Karpeev 
329208da532bSDmitry Karpeev /*@
329308da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
329408da532bSDmitry Karpeev 
329508da532bSDmitry Karpeev     Not Collective
329608da532bSDmitry Karpeev 
329708da532bSDmitry Karpeev     Input Parameter:
329808da532bSDmitry Karpeev .   dm - the DM object to destroy
329908da532bSDmitry Karpeev 
330008da532bSDmitry Karpeev     Output Parameter:
330108da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
330208da532bSDmitry Karpeev 
330308da532bSDmitry Karpeev     Level: developer
330408da532bSDmitry Karpeev 
330574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
330608da532bSDmitry Karpeev 
330708da532bSDmitry Karpeev @*/
330808da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
330908da532bSDmitry Karpeev {
331008da532bSDmitry Karpeev   PetscFunctionBegin;
33115a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3312534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
331308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
331408da532bSDmitry Karpeev   PetscFunctionReturn(0);
331508da532bSDmitry Karpeev }
331608da532bSDmitry Karpeev 
331708da532bSDmitry Karpeev /*@C
331808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
331908da532bSDmitry Karpeev 
3320d083f849SBarry Smith     Logically Collective on dm
332108da532bSDmitry Karpeev 
332208da532bSDmitry Karpeev     Input Parameters:
3323907376e6SBarry Smith .   dm - the DM object
332408da532bSDmitry Karpeev 
332508da532bSDmitry Karpeev     Output parameters:
332608da532bSDmitry Karpeev +   xl - lower bound
332708da532bSDmitry Karpeev -   xu - upper bound
332808da532bSDmitry Karpeev 
3329907376e6SBarry Smith     Level: advanced
3330907376e6SBarry Smith 
333195452b02SPatrick Sanan     Notes:
333295452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
333308da532bSDmitry Karpeev 
333474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
333508da532bSDmitry Karpeev 
333608da532bSDmitry Karpeev @*/
333708da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
333808da532bSDmitry Karpeev {
333908da532bSDmitry Karpeev   PetscErrorCode ierr;
33405fd66863SKarl Rupp 
334108da532bSDmitry Karpeev   PetscFunctionBegin;
33425a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
334308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
33445a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3345b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
334608da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
334708da532bSDmitry Karpeev   PetscFunctionReturn(0);
334808da532bSDmitry Karpeev }
334908da532bSDmitry Karpeev 
3350b0ae01b7SPeter Brune /*@
3351b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3352b0ae01b7SPeter Brune 
3353b0ae01b7SPeter Brune     Not Collective
3354b0ae01b7SPeter Brune 
3355b0ae01b7SPeter Brune     Input Parameter:
3356b0ae01b7SPeter Brune .   dm - the DM object
3357b0ae01b7SPeter Brune 
3358b0ae01b7SPeter Brune     Output Parameter:
3359b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3360b0ae01b7SPeter Brune 
3361b0ae01b7SPeter Brune     Level: developer
3362b0ae01b7SPeter Brune 
33631565f0a7SPatrick Sanan .seealso DMCreateColoring()
3364b0ae01b7SPeter Brune 
3365b0ae01b7SPeter Brune @*/
3366b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3367b0ae01b7SPeter Brune {
3368b0ae01b7SPeter Brune   PetscFunctionBegin;
33695a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3370534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3371b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3372b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3373b0ae01b7SPeter Brune }
3374b0ae01b7SPeter Brune 
33753ad4599aSBarry Smith /*@
33763ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
33773ad4599aSBarry Smith 
33783ad4599aSBarry Smith     Not Collective
33793ad4599aSBarry Smith 
33803ad4599aSBarry Smith     Input Parameter:
33813ad4599aSBarry Smith .   dm - the DM object
33823ad4599aSBarry Smith 
33833ad4599aSBarry Smith     Output Parameter:
33843ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
33853ad4599aSBarry Smith 
33863ad4599aSBarry Smith     Level: developer
33873ad4599aSBarry Smith 
33881565f0a7SPatrick Sanan .seealso DMCreateRestriction()
33893ad4599aSBarry Smith 
33903ad4599aSBarry Smith @*/
33913ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
33923ad4599aSBarry Smith {
33933ad4599aSBarry Smith   PetscFunctionBegin;
33945a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3395534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
33963ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
33973ad4599aSBarry Smith   PetscFunctionReturn(0);
33983ad4599aSBarry Smith }
33993ad4599aSBarry Smith 
3400a7058e45SLawrence Mitchell 
3401a7058e45SLawrence Mitchell /*@
3402a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3403a7058e45SLawrence Mitchell 
3404a7058e45SLawrence Mitchell     Not Collective
3405a7058e45SLawrence Mitchell 
3406a7058e45SLawrence Mitchell     Input Parameter:
3407a7058e45SLawrence Mitchell .   dm - the DM object
3408a7058e45SLawrence Mitchell 
3409a7058e45SLawrence Mitchell     Output Parameter:
3410a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3411a7058e45SLawrence Mitchell 
3412a7058e45SLawrence Mitchell     Level: developer
3413a7058e45SLawrence Mitchell 
34141565f0a7SPatrick Sanan .seealso DMCreateInjection()
3415a7058e45SLawrence Mitchell 
3416a7058e45SLawrence Mitchell @*/
3417a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3418a7058e45SLawrence Mitchell {
34194a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
34205a84ad33SLisandro Dalcin 
3421a7058e45SLawrence Mitchell   PetscFunctionBegin;
34225a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3423534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34245a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
34254a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
34265a84ad33SLisandro Dalcin   } else {
34275a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
34285a84ad33SLisandro Dalcin   }
3429a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3430a7058e45SLawrence Mitchell }
3431a7058e45SLawrence Mitchell 
34325a84ad33SLisandro Dalcin 
3433748fac09SDmitry Karpeev /*@C
343408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
343508da532bSDmitry Karpeev 
3436d083f849SBarry Smith     Collective on dm
343708da532bSDmitry Karpeev 
343808da532bSDmitry Karpeev     Input Parameter:
343908da532bSDmitry Karpeev +   dm - the DM object
34400298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
344108da532bSDmitry Karpeev 
344208da532bSDmitry Karpeev     Level: developer
344308da532bSDmitry Karpeev 
344474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
344508da532bSDmitry Karpeev 
344608da532bSDmitry Karpeev @*/
344708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
344808da532bSDmitry Karpeev {
344908da532bSDmitry Karpeev   PetscErrorCode ierr;
34505fd66863SKarl Rupp 
345108da532bSDmitry Karpeev   PetscFunctionBegin;
3452b9d85ea2SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
345308da532bSDmitry Karpeev   if (x) {
345408da532bSDmitry Karpeev     if (!dm->x) {
345508da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
345608da532bSDmitry Karpeev     }
345708da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
34588865f1eaSKarl Rupp   } else if (dm->x) {
345908da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
346008da532bSDmitry Karpeev   }
346108da532bSDmitry Karpeev   PetscFunctionReturn(0);
346208da532bSDmitry Karpeev }
346308da532bSDmitry Karpeev 
34640298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3465264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3466264ace61SBarry Smith 
3467264ace61SBarry Smith /*@C
3468264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3469264ace61SBarry Smith 
3470d083f849SBarry Smith   Collective on dm
3471264ace61SBarry Smith 
3472264ace61SBarry Smith   Input Parameters:
3473264ace61SBarry Smith + dm     - The DM object
3474264ace61SBarry Smith - method - The name of the DM type
3475264ace61SBarry Smith 
3476264ace61SBarry Smith   Options Database Key:
3477264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3478264ace61SBarry Smith 
3479264ace61SBarry Smith   Notes:
3480e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3481264ace61SBarry Smith 
3482264ace61SBarry Smith   Level: intermediate
3483264ace61SBarry Smith 
3484264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3485264ace61SBarry Smith @*/
348619fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3487264ace61SBarry Smith {
3488264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3489264ace61SBarry Smith   PetscBool      match;
3490264ace61SBarry Smith   PetscErrorCode ierr;
3491264ace61SBarry Smith 
3492264ace61SBarry Smith   PetscFunctionBegin;
3493264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3494251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3495264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3496264ace61SBarry Smith 
34970f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
34981c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3499ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3500264ace61SBarry Smith 
3501264ace61SBarry Smith   if (dm->ops->destroy) {
3502264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3503264ace61SBarry Smith   }
3504d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3505264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3506d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3507264ace61SBarry Smith   PetscFunctionReturn(0);
3508264ace61SBarry Smith }
3509264ace61SBarry Smith 
3510264ace61SBarry Smith /*@C
3511264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3512264ace61SBarry Smith 
3513264ace61SBarry Smith   Not Collective
3514264ace61SBarry Smith 
3515264ace61SBarry Smith   Input Parameter:
3516264ace61SBarry Smith . dm  - The DM
3517264ace61SBarry Smith 
3518264ace61SBarry Smith   Output Parameter:
3519264ace61SBarry Smith . type - The DM type name
3520264ace61SBarry Smith 
3521264ace61SBarry Smith   Level: intermediate
3522264ace61SBarry Smith 
3523264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3524264ace61SBarry Smith @*/
352519fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3526264ace61SBarry Smith {
3527264ace61SBarry Smith   PetscErrorCode ierr;
3528264ace61SBarry Smith 
3529264ace61SBarry Smith   PetscFunctionBegin;
3530264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3531c959eef4SJed Brown   PetscValidPointer(type,2);
3532607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3533264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3534264ace61SBarry Smith   PetscFunctionReturn(0);
3535264ace61SBarry Smith }
3536264ace61SBarry Smith 
353767a56275SMatthew G Knepley /*@C
353867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
353967a56275SMatthew G Knepley 
3540d083f849SBarry Smith   Collective on dm
354167a56275SMatthew G Knepley 
354267a56275SMatthew G Knepley   Input Parameters:
354367a56275SMatthew G Knepley + dm - the DM
354467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
354567a56275SMatthew G Knepley 
354667a56275SMatthew G Knepley   Output Parameter:
354767a56275SMatthew G Knepley . M - pointer to new DM
354867a56275SMatthew G Knepley 
354967a56275SMatthew G Knepley   Notes:
355067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
355167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
355267a56275SMatthew G Knepley   of the input DM.
355367a56275SMatthew G Knepley 
355467a56275SMatthew G Knepley   Level: intermediate
355567a56275SMatthew G Knepley 
355667a56275SMatthew G Knepley .seealso: DMCreate()
355767a56275SMatthew G Knepley @*/
355819fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
355967a56275SMatthew G Knepley {
356067a56275SMatthew G Knepley   DM             B;
356167a56275SMatthew G Knepley   char           convname[256];
3562c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
356367a56275SMatthew G Knepley   PetscErrorCode ierr;
356467a56275SMatthew G Knepley 
356567a56275SMatthew G Knepley   PetscFunctionBegin;
356667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
356767a56275SMatthew G Knepley   PetscValidType(dm,1);
356867a56275SMatthew G Knepley   PetscValidPointer(M,3);
3569251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3570c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3571c067b6caSMatthew G. Knepley   if (sametype) {
3572c067b6caSMatthew G. Knepley     *M   = dm;
3573c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3574c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3575c067b6caSMatthew G. Knepley   } else {
35760298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
357767a56275SMatthew G Knepley 
357867a56275SMatthew G Knepley     /*
357967a56275SMatthew G Knepley        Order of precedence:
358067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
358167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
358267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
358367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
358467a56275SMatthew G Knepley        5) Use a really basic converter.
358567a56275SMatthew G Knepley     */
358667a56275SMatthew G Knepley 
358767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3588a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3589a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3590a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3591a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3592a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
35930005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
359467a56275SMatthew G Knepley     if (conv) goto foundconv;
359567a56275SMatthew G Knepley 
359667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
359782f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
359867a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3599a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3600a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3601a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3602a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3603a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36040005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
360567a56275SMatthew G Knepley     if (conv) {
3606fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
360767a56275SMatthew G Knepley       goto foundconv;
360867a56275SMatthew G Knepley     }
360967a56275SMatthew G Knepley 
361067a56275SMatthew G Knepley #if 0
361167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
361267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3613fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
361467a56275SMatthew G Knepley     if (conv) goto foundconv;
361567a56275SMatthew G Knepley 
361667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
361767a56275SMatthew G Knepley     if (dm->ops->convert) {
361867a56275SMatthew G Knepley       conv = dm->ops->convert;
361967a56275SMatthew G Knepley     }
362067a56275SMatthew G Knepley     if (conv) goto foundconv;
362167a56275SMatthew G Knepley #endif
362267a56275SMatthew G Knepley 
362367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
362482f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
362567a56275SMatthew G Knepley 
362667a56275SMatthew G Knepley foundconv:
362767a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
362867a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
362912fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
363090b157c4SStefano Zampini     {
363190b157c4SStefano Zampini       PetscBool             isper;
363212fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
363312fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
363490b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
363590b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
363612fa691eSMatthew G. Knepley     }
363767a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
363867a56275SMatthew G Knepley   }
363967a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
364067a56275SMatthew G Knepley   PetscFunctionReturn(0);
364167a56275SMatthew G Knepley }
3642264ace61SBarry Smith 
3643264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3644264ace61SBarry Smith 
3645264ace61SBarry Smith /*@C
36461c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
36471c84c290SBarry Smith 
36481c84c290SBarry Smith   Not Collective
36491c84c290SBarry Smith 
36501c84c290SBarry Smith   Input Parameters:
36511c84c290SBarry Smith + name        - The name of a new user-defined creation routine
36521c84c290SBarry Smith - create_func - The creation routine itself
36531c84c290SBarry Smith 
36541c84c290SBarry Smith   Notes:
36551c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
36561c84c290SBarry Smith 
36571c84c290SBarry Smith 
36581c84c290SBarry Smith   Sample usage:
36591c84c290SBarry Smith .vb
3660bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
36611c84c290SBarry Smith .ve
36621c84c290SBarry Smith 
36631c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
36641c84c290SBarry Smith .vb
36651c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
36661c84c290SBarry Smith     DMSetType(DM,"my_da");
36671c84c290SBarry Smith .ve
36681c84c290SBarry Smith    or at runtime via the option
36691c84c290SBarry Smith .vb
36701c84c290SBarry Smith     -da_type my_da
36711c84c290SBarry Smith .ve
3672264ace61SBarry Smith 
3673264ace61SBarry Smith   Level: advanced
36741c84c290SBarry Smith 
3675bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
36761c84c290SBarry Smith 
3677264ace61SBarry Smith @*/
3678bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3679264ace61SBarry Smith {
3680264ace61SBarry Smith   PetscErrorCode ierr;
3681264ace61SBarry Smith 
3682264ace61SBarry Smith   PetscFunctionBegin;
36831d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3684a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3685264ace61SBarry Smith   PetscFunctionReturn(0);
3686264ace61SBarry Smith }
3687264ace61SBarry Smith 
3688b859378eSBarry Smith /*@C
368955849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3690b859378eSBarry Smith 
3691d083f849SBarry Smith   Collective on viewer
3692b859378eSBarry Smith 
3693b859378eSBarry Smith   Input Parameters:
3694b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3695b859378eSBarry Smith            some related function before a call to DMLoad().
3696b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3697b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3698b859378eSBarry Smith 
3699b859378eSBarry Smith    Level: intermediate
3700b859378eSBarry Smith 
3701b859378eSBarry Smith   Notes:
370255849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3703b859378eSBarry Smith 
3704b859378eSBarry Smith   Notes for advanced users:
3705b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3706b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3707b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3708b859378eSBarry Smith   format is
3709b859378eSBarry Smith .vb
3710b859378eSBarry Smith      has not yet been determined
3711b859378eSBarry Smith .ve
3712b859378eSBarry Smith 
3713b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3714b859378eSBarry Smith @*/
3715b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3716b859378eSBarry Smith {
37179331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3718b859378eSBarry Smith   PetscErrorCode ierr;
3719b859378eSBarry Smith 
3720b859378eSBarry Smith   PetscFunctionBegin;
3721b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3722b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3723fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
372432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37259331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
37269331c7a4SMatthew G. Knepley   if (isbinary) {
37279331c7a4SMatthew G. Knepley     PetscInt classid;
37289331c7a4SMatthew G. Knepley     char     type[256];
3729b859378eSBarry Smith 
3730060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37319200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3732060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
373332c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37349331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37359331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37369331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37379331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3738b859378eSBarry Smith   PetscFunctionReturn(0);
3739b859378eSBarry Smith }
3740b859378eSBarry Smith 
3741*b2e4378dSMatthew G. Knepley /*@
3742*b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3743*b2e4378dSMatthew G. Knepley 
3744*b2e4378dSMatthew G. Knepley   Not collective
3745*b2e4378dSMatthew G. Knepley 
3746*b2e4378dSMatthew G. Knepley   Input Parameter:
3747*b2e4378dSMatthew G. Knepley . dm - the DM
3748*b2e4378dSMatthew G. Knepley 
3749*b2e4378dSMatthew G. Knepley   Output Parameters:
3750*b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3751*b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3752*b2e4378dSMatthew G. Knepley 
3753*b2e4378dSMatthew G. Knepley   Level: beginner
3754*b2e4378dSMatthew G. Knepley 
3755*b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3756*b2e4378dSMatthew G. Knepley 
3757*b2e4378dSMatthew G. Knepley   Not supported from Fortran
3758*b2e4378dSMatthew G. Knepley 
3759*b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3760*b2e4378dSMatthew G. Knepley @*/
3761*b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3762*b2e4378dSMatthew G. Knepley {
3763*b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3764*b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3765*b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3766*b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3767*b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3768*b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3769*b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3770*b2e4378dSMatthew G. Knepley 
3771*b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3772*b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3773*b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3774*b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3775*b2e4378dSMatthew G. Knepley   if (coords) {
3776*b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3777*b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3778*b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3779*b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3780*b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3781*b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3782*b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3783*b2e4378dSMatthew G. Knepley       }
3784*b2e4378dSMatthew G. Knepley     }
3785*b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3786*b2e4378dSMatthew G. Knepley   } else {
3787*b2e4378dSMatthew G. Knepley     PetscBool isda;
3788*b2e4378dSMatthew G. Knepley 
3789*b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3790*b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3791*b2e4378dSMatthew G. Knepley   }
3792*b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3793*b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3794*b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3795*b2e4378dSMatthew G. Knepley }
3796*b2e4378dSMatthew G. Knepley 
3797*b2e4378dSMatthew G. Knepley /*@
3798*b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3799*b2e4378dSMatthew G. Knepley 
3800*b2e4378dSMatthew G. Knepley   Collective
3801*b2e4378dSMatthew G. Knepley 
3802*b2e4378dSMatthew G. Knepley   Input Parameter:
3803*b2e4378dSMatthew G. Knepley . dm - the DM
3804*b2e4378dSMatthew G. Knepley 
3805*b2e4378dSMatthew G. Knepley   Output Parameters:
3806*b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3807*b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3808*b2e4378dSMatthew G. Knepley 
3809*b2e4378dSMatthew G. Knepley   Level: beginner
3810*b2e4378dSMatthew G. Knepley 
3811*b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3812*b2e4378dSMatthew G. Knepley @*/
3813*b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3814*b2e4378dSMatthew G. Knepley {
3815*b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
3816*b2e4378dSMatthew G. Knepley   PetscMPIInt    cdim, count;
3817*b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3818*b2e4378dSMatthew G. Knepley 
3819*b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3820*b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3821*b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3822*b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3823*b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3824*b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3825*b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3826*b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3827*b2e4378dSMatthew G. Knepley }
3828*b2e4378dSMatthew G. Knepley 
38297da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38307da65231SMatthew G Knepley 
3831a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3832a6dfd86eSKarl Rupp {
38331d47ebbbSSatish Balay   PetscInt       f;
38341b30c384SMatthew G Knepley   PetscErrorCode ierr;
38351b30c384SMatthew G Knepley 
38367da65231SMatthew G Knepley   PetscFunctionBegin;
383774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38381d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
383957622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
38407da65231SMatthew G Knepley   }
38417da65231SMatthew G Knepley   PetscFunctionReturn(0);
38427da65231SMatthew G Knepley }
38437da65231SMatthew G Knepley 
3844a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3845a6dfd86eSKarl Rupp {
38461b30c384SMatthew G Knepley   PetscInt       f, g;
38477da65231SMatthew G Knepley   PetscErrorCode ierr;
38487da65231SMatthew G Knepley 
38497da65231SMatthew G Knepley   PetscFunctionBegin;
385074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38511d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
385274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
38531d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3854e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
38557da65231SMatthew G Knepley     }
385674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
38577da65231SMatthew G Knepley   }
38587da65231SMatthew G Knepley   PetscFunctionReturn(0);
38597da65231SMatthew G Knepley }
3860e7c4fc90SDmitry Karpeev 
38616113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3862e759306cSMatthew G. Knepley {
38630c5b8624SToby Isaac   PetscInt          localSize, bs;
38640c5b8624SToby Isaac   PetscMPIInt       size;
38650c5b8624SToby Isaac   Vec               x, xglob;
38660c5b8624SToby Isaac   const PetscScalar *xarray;
3867e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3868e759306cSMatthew G. Knepley 
3869e759306cSMatthew G. Knepley   PetscFunctionBegin;
38709852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3871e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3872e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
38736113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
38740c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
38750c5b8624SToby Isaac   if (size > 1) {
38760c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
38770c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
38780c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
38790c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
38800c5b8624SToby Isaac   } else {
38810c5b8624SToby Isaac     xglob = x;
38820c5b8624SToby Isaac   }
38830c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
38840c5b8624SToby Isaac   if (size > 1) {
38850c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
38860c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
38870c5b8624SToby Isaac   }
3888e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3889e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3890e759306cSMatthew G. Knepley }
3891e759306cSMatthew G. Knepley 
389288ed4aceSMatthew G Knepley /*@
38931bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3894061576a5SJed Brown 
3895061576a5SJed Brown   Input Parameter:
3896061576a5SJed Brown . dm - The DM
3897061576a5SJed Brown 
3898061576a5SJed Brown   Output Parameter:
3899061576a5SJed Brown . section - The PetscSection
3900061576a5SJed Brown 
3901061576a5SJed Brown   Options Database Keys:
3902061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3903061576a5SJed Brown 
3904061576a5SJed Brown   Level: advanced
3905061576a5SJed Brown 
3906061576a5SJed Brown   Notes:
3907061576a5SJed Brown   Use DMGetLocalSection() in new code.
3908061576a5SJed Brown 
3909061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3910061576a5SJed Brown 
3911061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3912061576a5SJed Brown @*/
3913061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3914061576a5SJed Brown {
3915061576a5SJed Brown   PetscErrorCode ierr;
3916061576a5SJed Brown 
3917061576a5SJed Brown   PetscFunctionBegin;
3918061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3919061576a5SJed Brown   PetscFunctionReturn(0);
3920061576a5SJed Brown }
3921061576a5SJed Brown 
3922061576a5SJed Brown /*@
3923061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
392488ed4aceSMatthew G Knepley 
392588ed4aceSMatthew G Knepley   Input Parameter:
392688ed4aceSMatthew G Knepley . dm - The DM
392788ed4aceSMatthew G Knepley 
392888ed4aceSMatthew G Knepley   Output Parameter:
392988ed4aceSMatthew G Knepley . section - The PetscSection
393088ed4aceSMatthew G Knepley 
3931e5893cccSMatthew G. Knepley   Options Database Keys:
3932e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3933e5893cccSMatthew G. Knepley 
393488ed4aceSMatthew G Knepley   Level: intermediate
393588ed4aceSMatthew G Knepley 
393688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
393788ed4aceSMatthew G Knepley 
3938061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
393988ed4aceSMatthew G Knepley @*/
3940061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
39410adebc6cSBarry Smith {
3942fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3943fd59a867SMatthew G. Knepley 
394488ed4aceSMatthew G Knepley   PetscFunctionBegin;
394588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
394688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
39471bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
3948e5e52638SMatthew G. Knepley     PetscInt d;
3949e5e52638SMatthew G. Knepley 
3950e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
39511bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
39521bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
39532f0f8703SMatthew G. Knepley   }
39541bb6d2a8SBarry Smith   *section = dm->localSection;
395588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
395688ed4aceSMatthew G Knepley }
395788ed4aceSMatthew G Knepley 
395888ed4aceSMatthew G Knepley /*@
39591bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
3960061576a5SJed Brown 
3961061576a5SJed Brown   Input Parameters:
3962061576a5SJed Brown + dm - The DM
3963061576a5SJed Brown - section - The PetscSection
3964061576a5SJed Brown 
3965061576a5SJed Brown   Level: advanced
3966061576a5SJed Brown 
3967061576a5SJed Brown   Notes:
3968061576a5SJed Brown   Use DMSetLocalSection() in new code.
3969061576a5SJed Brown 
3970061576a5SJed Brown   Any existing Section will be destroyed
3971061576a5SJed Brown 
3972061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
3973061576a5SJed Brown @*/
3974061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
3975061576a5SJed Brown {
3976061576a5SJed Brown   PetscErrorCode ierr;
3977061576a5SJed Brown 
3978061576a5SJed Brown   PetscFunctionBegin;
3979061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
3980061576a5SJed Brown   PetscFunctionReturn(0);
3981061576a5SJed Brown }
3982061576a5SJed Brown 
3983061576a5SJed Brown /*@
3984061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
398588ed4aceSMatthew G Knepley 
398688ed4aceSMatthew G Knepley   Input Parameters:
398788ed4aceSMatthew G Knepley + dm - The DM
398888ed4aceSMatthew G Knepley - section - The PetscSection
398988ed4aceSMatthew G Knepley 
399088ed4aceSMatthew G Knepley   Level: intermediate
399188ed4aceSMatthew G Knepley 
399288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
399388ed4aceSMatthew G Knepley 
3994061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
399588ed4aceSMatthew G Knepley @*/
3996061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
39970adebc6cSBarry Smith {
3998c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3999af122d2aSMatthew G Knepley   PetscInt       f;
400088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
400188ed4aceSMatthew G Knepley 
400288ed4aceSMatthew G Knepley   PetscFunctionBegin;
400388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4004b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40051d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40061bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
40071bb6d2a8SBarry Smith   dm->localSection = section;
40081bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4009af122d2aSMatthew G Knepley   if (numFields) {
4010af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4011af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40120f21e855SMatthew G. Knepley       PetscObject disc;
4013af122d2aSMatthew G Knepley       const char *name;
4014af122d2aSMatthew G Knepley 
40151bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
401644a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40170f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4018af122d2aSMatthew G Knepley     }
4019af122d2aSMatthew G Knepley   }
4020e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40211bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
402288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
402388ed4aceSMatthew G Knepley }
402488ed4aceSMatthew G Knepley 
40259435951eSToby Isaac /*@
4026b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40279435951eSToby Isaac 
4028e228b242SToby Isaac   not collective
4029e228b242SToby Isaac 
40309435951eSToby Isaac   Input Parameter:
40319435951eSToby Isaac . dm - The DM
40329435951eSToby Isaac 
40339435951eSToby Isaac   Output Parameter:
40349435951eSToby 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.
40359435951eSToby 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.
40369435951eSToby Isaac 
40379435951eSToby Isaac   Level: advanced
40389435951eSToby Isaac 
40399435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
40409435951eSToby Isaac 
40419435951eSToby Isaac .seealso: DMSetDefaultConstraints()
40429435951eSToby Isaac @*/
40439435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
40449435951eSToby Isaac {
40459435951eSToby Isaac   PetscErrorCode ierr;
40469435951eSToby Isaac 
40479435951eSToby Isaac   PetscFunctionBegin;
40489435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40499435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
405045a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
405145a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
40529435951eSToby Isaac   PetscFunctionReturn(0);
40539435951eSToby Isaac }
40549435951eSToby Isaac 
40559435951eSToby Isaac /*@
4056b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
40579435951eSToby Isaac 
40589435951eSToby 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().
40599435951eSToby Isaac 
40609435951eSToby 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.
40619435951eSToby Isaac 
4062e228b242SToby Isaac   collective on dm
4063e228b242SToby Isaac 
40649435951eSToby Isaac   Input Parameters:
40659435951eSToby Isaac + dm - The DM
4066e228b242SToby 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).
4067e228b242SToby 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).
40689435951eSToby Isaac 
40699435951eSToby Isaac   Level: advanced
40709435951eSToby Isaac 
40719435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
40729435951eSToby Isaac 
40739435951eSToby Isaac .seealso: DMGetDefaultConstraints()
40749435951eSToby Isaac @*/
40759435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
40769435951eSToby Isaac {
4077e228b242SToby Isaac   PetscMPIInt result;
40789435951eSToby Isaac   PetscErrorCode ierr;
40799435951eSToby Isaac 
40809435951eSToby Isaac   PetscFunctionBegin;
40819435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4082e228b242SToby Isaac   if (section) {
4083e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4084e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4085f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4086e228b242SToby Isaac   }
4087e228b242SToby Isaac   if (mat) {
4088e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4089e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4090f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4091e228b242SToby Isaac   }
40929435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40939435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
40949435951eSToby Isaac   dm->defaultConstraintSection = section;
40959435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
40969435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
40979435951eSToby Isaac   dm->defaultConstraintMat = mat;
40989435951eSToby Isaac   PetscFunctionReturn(0);
40999435951eSToby Isaac }
41009435951eSToby Isaac 
4101497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4102507e4973SMatthew G. Knepley /*
4103507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4104507e4973SMatthew G. Knepley 
4105507e4973SMatthew G. Knepley   Input Parameters:
4106507e4973SMatthew G. Knepley + dm - The DM
4107507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4108507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4109507e4973SMatthew G. Knepley 
4110507e4973SMatthew G. Knepley   Level: intermediate
4111507e4973SMatthew G. Knepley 
41121bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4113507e4973SMatthew G. Knepley */
4114f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4115507e4973SMatthew G. Knepley {
4116507e4973SMatthew G. Knepley   MPI_Comm        comm;
4117507e4973SMatthew G. Knepley   PetscLayout     layout;
4118507e4973SMatthew G. Knepley   const PetscInt *ranges;
4119507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4120507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4121507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4122507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4123507e4973SMatthew G. Knepley 
4124507e4973SMatthew G. Knepley   PetscFunctionBegin;
4125507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4126507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4127507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4128507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4129507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4130507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4131507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4132507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4133507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4134507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4135507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4136507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4137f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4138507e4973SMatthew G. Knepley 
4139507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4140507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4141507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4142507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4143507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4144507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4145507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4146507e4973SMatthew 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;}
4147507e4973SMatthew 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;}
4148507e4973SMatthew G. Knepley     if (gdof < 0) {
4149507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4150507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4151507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4152507e4973SMatthew G. Knepley 
4153507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4154507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4155507e4973SMatthew 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;}
4156507e4973SMatthew G. Knepley       }
4157507e4973SMatthew G. Knepley     }
4158507e4973SMatthew G. Knepley   }
4159507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4160507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4161b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4162507e4973SMatthew G. Knepley   if (!gvalid) {
4163507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4164507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4165507e4973SMatthew G. Knepley   }
4166507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4167507e4973SMatthew G. Knepley }
4168f741bcd2SMatthew G. Knepley #endif
4169507e4973SMatthew G. Knepley 
417088ed4aceSMatthew G Knepley /*@
4171e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
417288ed4aceSMatthew G Knepley 
4173d083f849SBarry Smith   Collective on dm
41748b1ab98fSJed Brown 
417588ed4aceSMatthew G Knepley   Input Parameter:
417688ed4aceSMatthew G Knepley . dm - The DM
417788ed4aceSMatthew G Knepley 
417888ed4aceSMatthew G Knepley   Output Parameter:
417988ed4aceSMatthew G Knepley . section - The PetscSection
418088ed4aceSMatthew G Knepley 
418188ed4aceSMatthew G Knepley   Level: intermediate
418288ed4aceSMatthew G Knepley 
418388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
418488ed4aceSMatthew G Knepley 
418592fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
418688ed4aceSMatthew G Knepley @*/
4187e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
41880adebc6cSBarry Smith {
418988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
419088ed4aceSMatthew G Knepley 
419188ed4aceSMatthew G Knepley   PetscFunctionBegin;
419288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
419388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41941bb6d2a8SBarry Smith   if (!dm->globalSection) {
4195fd59a867SMatthew G. Knepley     PetscSection s;
4196fd59a867SMatthew G. Knepley 
419792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4198fd59a867SMatthew 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");
419933907cc2SStefano 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");
42001bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4201cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
42021bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
42031bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
420488ed4aceSMatthew G Knepley   }
42051bb6d2a8SBarry Smith   *section = dm->globalSection;
420688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
420788ed4aceSMatthew G Knepley }
420888ed4aceSMatthew G Knepley 
4209b21d0597SMatthew G Knepley /*@
4210e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4211b21d0597SMatthew G Knepley 
4212b21d0597SMatthew G Knepley   Input Parameters:
4213b21d0597SMatthew G Knepley + dm - The DM
42145080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4215b21d0597SMatthew G Knepley 
4216b21d0597SMatthew G Knepley   Level: intermediate
4217b21d0597SMatthew G Knepley 
4218b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4219b21d0597SMatthew G Knepley 
422092fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4221b21d0597SMatthew G Knepley @*/
4222e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42230adebc6cSBarry Smith {
4224b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4225b21d0597SMatthew G Knepley 
4226b21d0597SMatthew G Knepley   PetscFunctionBegin;
4227b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42285080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42291d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42301bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
42311bb6d2a8SBarry Smith   dm->globalSection = section;
4232497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
42331bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4234507e4973SMatthew G. Knepley #endif
4235b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4236b21d0597SMatthew G Knepley }
4237b21d0597SMatthew G Knepley 
423888ed4aceSMatthew G Knepley /*@
42391bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
424088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
424188ed4aceSMatthew G Knepley 
424288ed4aceSMatthew G Knepley   Input Parameter:
424388ed4aceSMatthew G Knepley . dm - The DM
424488ed4aceSMatthew G Knepley 
424588ed4aceSMatthew G Knepley   Output Parameter:
424688ed4aceSMatthew G Knepley . sf - The PetscSF
424788ed4aceSMatthew G Knepley 
424888ed4aceSMatthew G Knepley   Level: intermediate
424988ed4aceSMatthew G Knepley 
425088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
425188ed4aceSMatthew G Knepley 
42521bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
425388ed4aceSMatthew G Knepley @*/
42541bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
42550adebc6cSBarry Smith {
425688ed4aceSMatthew G Knepley   PetscInt       nroots;
425788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
425888ed4aceSMatthew G Knepley 
425988ed4aceSMatthew G Knepley   PetscFunctionBegin;
426088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
426188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
42621bb6d2a8SBarry Smith   if (!dm->sectionSF) {
42631bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
426433907cc2SStefano Zampini   }
42651bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
426688ed4aceSMatthew G Knepley   if (nroots < 0) {
426788ed4aceSMatthew G Knepley     PetscSection section, gSection;
426888ed4aceSMatthew G Knepley 
426992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
427031ea6d37SMatthew G Knepley     if (section) {
4271e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
42721bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
427331ea6d37SMatthew G Knepley     } else {
42740298fd71SBarry Smith       *sf = NULL;
427531ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
427631ea6d37SMatthew G Knepley     }
427788ed4aceSMatthew G Knepley   }
42781bb6d2a8SBarry Smith   *sf = dm->sectionSF;
427988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
428088ed4aceSMatthew G Knepley }
428188ed4aceSMatthew G Knepley 
428288ed4aceSMatthew G Knepley /*@
42831bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
428488ed4aceSMatthew G Knepley 
428588ed4aceSMatthew G Knepley   Input Parameters:
428688ed4aceSMatthew G Knepley + dm - The DM
428788ed4aceSMatthew G Knepley - sf - The PetscSF
428888ed4aceSMatthew G Knepley 
428988ed4aceSMatthew G Knepley   Level: intermediate
429088ed4aceSMatthew G Knepley 
429188ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
429288ed4aceSMatthew G Knepley 
42931bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
429488ed4aceSMatthew G Knepley @*/
42951bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
42960adebc6cSBarry Smith {
429788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
429888ed4aceSMatthew G Knepley 
429988ed4aceSMatthew G Knepley   PetscFunctionBegin;
430088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4301b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
430233907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
43031bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
43041bb6d2a8SBarry Smith   dm->sectionSF = sf;
430588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
430688ed4aceSMatthew G Knepley }
430788ed4aceSMatthew G Knepley 
430888ed4aceSMatthew G Knepley /*@C
43091bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
431088ed4aceSMatthew G Knepley   describing the data layout.
431188ed4aceSMatthew G Knepley 
431288ed4aceSMatthew G Knepley   Input Parameters:
431388ed4aceSMatthew G Knepley + dm - The DM
431488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
431588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
431688ed4aceSMatthew G Knepley 
43171bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
431888ed4aceSMatthew G Knepley 
43191bb6d2a8SBarry Smith   Level: developer
43201bb6d2a8SBarry Smith 
43211bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43221bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43231bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43241bb6d2a8SBarry Smith 
43251bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
432688ed4aceSMatthew G Knepley @*/
43271bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
432888ed4aceSMatthew G Knepley {
432982f516ccSBarry Smith   MPI_Comm       comm;
433088ed4aceSMatthew G Knepley   PetscLayout    layout;
433188ed4aceSMatthew G Knepley   const PetscInt *ranges;
433288ed4aceSMatthew G Knepley   PetscInt       *local;
433388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4334ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
433588ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
433688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
433788ed4aceSMatthew G Knepley 
433888ed4aceSMatthew G Knepley   PetscFunctionBegin;
433988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4340367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
434188ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
434288ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
434388ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
434488ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
434588ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
434688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
434788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
434888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
434988ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4350ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
43516636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
435288ed4aceSMatthew G Knepley 
43536636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
43546636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4355235fbf56SMatthew 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));
43566636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
435788ed4aceSMatthew G Knepley   }
4358785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4359785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
436088ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
43611f588964SMatthew G Knepley     const PetscInt *cind;
43626636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
436388ed4aceSMatthew G Knepley 
436488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
436588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
436688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
436788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
436888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
43696636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
437088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
43716636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
43726636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
43736636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4374057b4bcdSMatthew 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);
43756636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
43766636e97aSMatthew G Knepley     }
437788ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
437888ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
437988ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
438088ed4aceSMatthew G Knepley     }
438188ed4aceSMatthew G Knepley     if (gdof < 0) {
43826636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
438388ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
438488ed4aceSMatthew G Knepley 
438505376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
438631d3f06eSJed Brown         if (r < 0) r = -(r+2);
438705376888SMatthew 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);
438888ed4aceSMatthew G Knepley         remote[l].rank  = r;
438988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
439088ed4aceSMatthew G Knepley       }
439188ed4aceSMatthew G Knepley     } else {
43926636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
439388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
439488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
439588ed4aceSMatthew G Knepley       }
439688ed4aceSMatthew G Knepley     }
439788ed4aceSMatthew G Knepley   }
43986636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
439988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
44001bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
440188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
440288ed4aceSMatthew G Knepley }
4403af122d2aSMatthew G Knepley 
4404b21d0597SMatthew G Knepley /*@
4405b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4406b21d0597SMatthew G Knepley 
4407b21d0597SMatthew G Knepley   Input Parameter:
4408b21d0597SMatthew G Knepley . dm - The DM
4409b21d0597SMatthew G Knepley 
4410b21d0597SMatthew G Knepley   Output Parameter:
4411b21d0597SMatthew G Knepley . sf - The PetscSF
4412b21d0597SMatthew G Knepley 
4413b21d0597SMatthew G Knepley   Level: intermediate
4414b21d0597SMatthew G Knepley 
4415b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4416b21d0597SMatthew G Knepley 
44171bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4418b21d0597SMatthew G Knepley @*/
44190adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44200adebc6cSBarry Smith {
4421b21d0597SMatthew G Knepley   PetscFunctionBegin;
4422b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4423b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4424b21d0597SMatthew G Knepley   *sf = dm->sf;
4425b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4426b21d0597SMatthew G Knepley }
4427b21d0597SMatthew G Knepley 
4428057b4bcdSMatthew G Knepley /*@
4429057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4430057b4bcdSMatthew G Knepley 
4431057b4bcdSMatthew G Knepley   Input Parameters:
4432057b4bcdSMatthew G Knepley + dm - The DM
4433057b4bcdSMatthew G Knepley - sf - The PetscSF
4434057b4bcdSMatthew G Knepley 
4435057b4bcdSMatthew G Knepley   Level: intermediate
4436057b4bcdSMatthew G Knepley 
44371bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4438057b4bcdSMatthew G Knepley @*/
44390adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
44400adebc6cSBarry Smith {
4441057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4442057b4bcdSMatthew G Knepley 
4443057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4444057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4445b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4446057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
444733907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4448057b4bcdSMatthew G Knepley   dm->sf = sf;
4449057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4450057b4bcdSMatthew G Knepley }
4451057b4bcdSMatthew G Knepley 
445234aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
445334aa8a36SMatthew G. Knepley {
445434aa8a36SMatthew G. Knepley   PetscClassId   id;
445534aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
445634aa8a36SMatthew G. Knepley 
445734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
445834aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
445934aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
446034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
446134aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
446234aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
446317c1d62eSMatthew G. Knepley   } else {
446417c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
446534aa8a36SMatthew G. Knepley   }
446634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
446734aa8a36SMatthew G. Knepley }
446834aa8a36SMatthew G. Knepley 
446944a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
447044a7f3ddSMatthew G. Knepley {
447144a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
447244a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
447344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
447444a7f3ddSMatthew G. Knepley 
447544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
447644a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
447744a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
447844a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
447944a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
448044a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
448144a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
448244a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
448344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
448444a7f3ddSMatthew G. Knepley }
448544a7f3ddSMatthew G. Knepley 
448644a7f3ddSMatthew G. Knepley /*@
448744a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
448844a7f3ddSMatthew G. Knepley 
4489d083f849SBarry Smith   Logically collective on dm
449044a7f3ddSMatthew G. Knepley 
449144a7f3ddSMatthew G. Knepley   Input Parameter:
449244a7f3ddSMatthew G. Knepley . dm - The DM
449344a7f3ddSMatthew G. Knepley 
449444a7f3ddSMatthew G. Knepley   Level: intermediate
449544a7f3ddSMatthew G. Knepley 
449644a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
449744a7f3ddSMatthew G. Knepley @*/
449844a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
449944a7f3ddSMatthew G. Knepley {
450044a7f3ddSMatthew G. Knepley   PetscInt       f;
450144a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
450244a7f3ddSMatthew G. Knepley 
450344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
450444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
450544a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
450644a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
450744a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
450844a7f3ddSMatthew G. Knepley   }
450944a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
451044a7f3ddSMatthew G. Knepley   dm->fields = NULL;
451144a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
451244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
451344a7f3ddSMatthew G. Knepley }
451444a7f3ddSMatthew G. Knepley 
4515689b5837SMatthew G. Knepley /*@
4516689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4517689b5837SMatthew G. Knepley 
4518689b5837SMatthew G. Knepley   Not collective
4519689b5837SMatthew G. Knepley 
4520689b5837SMatthew G. Knepley   Input Parameter:
4521689b5837SMatthew G. Knepley . dm - The DM
4522689b5837SMatthew G. Knepley 
4523689b5837SMatthew G. Knepley   Output Parameter:
4524689b5837SMatthew G. Knepley . Nf - The number of fields
4525689b5837SMatthew G. Knepley 
4526689b5837SMatthew G. Knepley   Level: intermediate
4527689b5837SMatthew G. Knepley 
4528689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4529689b5837SMatthew G. Knepley @*/
45300f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
45310f21e855SMatthew G. Knepley {
45320f21e855SMatthew G. Knepley   PetscFunctionBegin;
45330f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4534534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
453544a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4536af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4537af122d2aSMatthew G Knepley }
4538af122d2aSMatthew G Knepley 
4539689b5837SMatthew G. Knepley /*@
4540689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4541689b5837SMatthew G. Knepley 
4542d083f849SBarry Smith   Logically collective on dm
4543689b5837SMatthew G. Knepley 
4544689b5837SMatthew G. Knepley   Input Parameters:
4545689b5837SMatthew G. Knepley + dm - The DM
4546689b5837SMatthew G. Knepley - Nf - The number of fields
4547689b5837SMatthew G. Knepley 
4548689b5837SMatthew G. Knepley   Level: intermediate
4549689b5837SMatthew G. Knepley 
4550689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4551689b5837SMatthew G. Knepley @*/
4552af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4553af122d2aSMatthew G Knepley {
45540f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4555af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4556af122d2aSMatthew G Knepley 
4557af122d2aSMatthew G Knepley   PetscFunctionBegin;
4558af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
455944a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
45600f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
45610f21e855SMatthew G. Knepley     PetscContainer obj;
45620f21e855SMatthew G. Knepley 
45630f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
456444a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
45650f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4566af122d2aSMatthew G Knepley   }
4567af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4568af122d2aSMatthew G Knepley }
4569af122d2aSMatthew G Knepley 
4570c1929be8SMatthew G. Knepley /*@
4571c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4572c1929be8SMatthew G. Knepley 
4573c1929be8SMatthew G. Knepley   Not collective
4574c1929be8SMatthew G. Knepley 
4575c1929be8SMatthew G. Knepley   Input Parameters:
4576c1929be8SMatthew G. Knepley + dm - The DM
4577c1929be8SMatthew G. Knepley - f  - The field number
4578c1929be8SMatthew G. Knepley 
457944a7f3ddSMatthew G. Knepley   Output Parameters:
458044a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
458144a7f3ddSMatthew G. Knepley - field - The discretization object
4582c1929be8SMatthew G. Knepley 
458344a7f3ddSMatthew G. Knepley   Level: intermediate
4584c1929be8SMatthew G. Knepley 
458544a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4586c1929be8SMatthew G. Knepley @*/
458744a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4588af122d2aSMatthew G Knepley {
4589af122d2aSMatthew G Knepley   PetscFunctionBegin;
4590af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
459144a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
459244a7f3ddSMatthew 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);
459344a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
459444a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4595decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4596decb47aaSMatthew G. Knepley }
4597decb47aaSMatthew G. Knepley 
4598c1929be8SMatthew G. Knepley /*@
4599c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4600c1929be8SMatthew G. Knepley 
4601d083f849SBarry Smith   Logically collective on dm
4602c1929be8SMatthew G. Knepley 
4603c1929be8SMatthew G. Knepley   Input Parameters:
4604c1929be8SMatthew G. Knepley + dm    - The DM
4605c1929be8SMatthew G. Knepley . f     - The field number
460644a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4607c1929be8SMatthew G. Knepley - field - The discretization object
4608c1929be8SMatthew G. Knepley 
460944a7f3ddSMatthew G. Knepley   Level: intermediate
4610c1929be8SMatthew G. Knepley 
461144a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4612c1929be8SMatthew G. Knepley @*/
461344a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4614decb47aaSMatthew G. Knepley {
4615decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4616decb47aaSMatthew G. Knepley 
4617decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4618decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4619e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
462044a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4621e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
462244a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
462344a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
462444a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
462544a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
462644a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
462744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
462844a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
462934aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
46302df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
463144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
463244a7f3ddSMatthew G. Knepley }
463344a7f3ddSMatthew G. Knepley 
463444a7f3ddSMatthew G. Knepley /*@
463544a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
463644a7f3ddSMatthew G. Knepley 
4637d083f849SBarry Smith   Logically collective on dm
463844a7f3ddSMatthew G. Knepley 
463944a7f3ddSMatthew G. Knepley   Input Parameters:
464044a7f3ddSMatthew G. Knepley + dm    - The DM
464144a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
464244a7f3ddSMatthew G. Knepley - field - The discretization object
464344a7f3ddSMatthew G. Knepley 
464444a7f3ddSMatthew G. Knepley   Level: intermediate
464544a7f3ddSMatthew G. Knepley 
464644a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
464744a7f3ddSMatthew G. Knepley @*/
464844a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
464944a7f3ddSMatthew G. Knepley {
465044a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
465144a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
465244a7f3ddSMatthew G. Knepley 
465344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
465444a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
465544a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
465644a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
465744a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
465844a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
465944a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
466044a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
466144a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
466234aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
46632df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4664af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4665af122d2aSMatthew G Knepley }
46666636e97aSMatthew G Knepley 
4667e5e52638SMatthew G. Knepley /*@
4668e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4669e5e52638SMatthew G. Knepley 
4670d083f849SBarry Smith   Collective on dm
4671e5e52638SMatthew G. Knepley 
4672e5e52638SMatthew G. Knepley   Input Parameter:
4673e5e52638SMatthew G. Knepley . dm - The DM
4674e5e52638SMatthew G. Knepley 
4675e5e52638SMatthew G. Knepley   Output Parameter:
4676e5e52638SMatthew G. Knepley . newdm - The DM
4677e5e52638SMatthew G. Knepley 
4678e5e52638SMatthew G. Knepley   Level: advanced
4679e5e52638SMatthew G. Knepley 
4680e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4681e5e52638SMatthew G. Knepley @*/
4682e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4683e5e52638SMatthew G. Knepley {
4684e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4685e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4686e5e52638SMatthew G. Knepley 
4687e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4688e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4689e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4690e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4691e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4692e5e52638SMatthew G. Knepley     DMLabel     label;
4693e5e52638SMatthew G. Knepley     PetscObject field;
469434aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4695e5e52638SMatthew G. Knepley 
4696e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4697e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
469834aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
469934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
470034aa8a36SMatthew G. Knepley   }
470134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
470234aa8a36SMatthew G. Knepley }
470334aa8a36SMatthew G. Knepley 
470434aa8a36SMatthew G. Knepley /*@
470534aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
470634aa8a36SMatthew G. Knepley 
470734aa8a36SMatthew G. Knepley   Not collective
470834aa8a36SMatthew G. Knepley 
470934aa8a36SMatthew G. Knepley   Input Parameters:
471034aa8a36SMatthew G. Knepley + dm - The DM object
471134aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
471234aa8a36SMatthew G. Knepley 
471334aa8a36SMatthew G. Knepley   Output Parameter:
471434aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
471534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
471634aa8a36SMatthew G. Knepley 
471734aa8a36SMatthew G. Knepley   Notes:
471834aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
471934aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
472034aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4721979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
472234aa8a36SMatthew G. Knepley 
472334aa8a36SMatthew G. Knepley   Level: developer
472434aa8a36SMatthew G. Knepley 
472534aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
472634aa8a36SMatthew G. Knepley @*/
472734aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
472834aa8a36SMatthew G. Knepley {
472934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
473034aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4731534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4732534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
473334aa8a36SMatthew G. Knepley   if (f < 0) {
473434aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
473534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
473634aa8a36SMatthew G. Knepley   } else {
473734aa8a36SMatthew G. Knepley     PetscInt       Nf;
473834aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
473934aa8a36SMatthew G. Knepley 
474034aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
474134aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
474234aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
474334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
474434aa8a36SMatthew G. Knepley   }
474534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
474634aa8a36SMatthew G. Knepley }
474734aa8a36SMatthew G. Knepley 
474834aa8a36SMatthew G. Knepley /*@
474934aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
475034aa8a36SMatthew G. Knepley 
475134aa8a36SMatthew G. Knepley   Not collective
475234aa8a36SMatthew G. Knepley 
475334aa8a36SMatthew G. Knepley   Input Parameters:
475434aa8a36SMatthew G. Knepley + dm         - The DM object
475534aa8a36SMatthew G. Knepley . f          - The field number
475634aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
475734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
475834aa8a36SMatthew G. Knepley 
475934aa8a36SMatthew G. Knepley   Notes:
476034aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
476134aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
476234aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4763979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
476434aa8a36SMatthew G. Knepley 
476534aa8a36SMatthew G. Knepley   Level: developer
476634aa8a36SMatthew G. Knepley 
476734aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
476834aa8a36SMatthew G. Knepley @*/
476934aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
477034aa8a36SMatthew G. Knepley {
477134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
477234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477334aa8a36SMatthew G. Knepley   if (f < 0) {
477434aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
477534aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
477634aa8a36SMatthew G. Knepley   } else {
477734aa8a36SMatthew G. Knepley     PetscInt       Nf;
477834aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
477934aa8a36SMatthew G. Knepley 
478034aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
478134aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
478234aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
478334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4784e5e52638SMatthew G. Knepley   }
4785e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4786e5e52638SMatthew G. Knepley }
4787e5e52638SMatthew G. Knepley 
4788b0441da4SMatthew G. Knepley /*@
4789b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4790b0441da4SMatthew G. Knepley 
4791b0441da4SMatthew G. Knepley   Not collective
4792b0441da4SMatthew G. Knepley 
4793b0441da4SMatthew G. Knepley   Input Parameters:
4794b0441da4SMatthew G. Knepley . dm - The DM object
4795b0441da4SMatthew G. Knepley 
4796b0441da4SMatthew G. Knepley   Output Parameter:
4797b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4798b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4799b0441da4SMatthew G. Knepley 
4800b0441da4SMatthew G. Knepley   Notes:
4801b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4802b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4803b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4804b0441da4SMatthew G. Knepley 
4805b0441da4SMatthew G. Knepley   Level: developer
4806b0441da4SMatthew G. Knepley 
4807b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4808b0441da4SMatthew G. Knepley @*/
4809b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4810b0441da4SMatthew G. Knepley {
4811b0441da4SMatthew G. Knepley   PetscInt       Nf;
4812b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4813b0441da4SMatthew G. Knepley 
4814b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4815b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4816534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4817534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4818b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4819b0441da4SMatthew G. Knepley   if (!Nf) {
4820b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4821b0441da4SMatthew G. Knepley   } else {
4822b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4823b0441da4SMatthew G. Knepley   }
4824b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4825b0441da4SMatthew G. Knepley }
4826b0441da4SMatthew G. Knepley 
4827b0441da4SMatthew G. Knepley /*@
4828b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4829b0441da4SMatthew G. Knepley 
4830b0441da4SMatthew G. Knepley   Not collective
4831b0441da4SMatthew G. Knepley 
4832b0441da4SMatthew G. Knepley   Input Parameters:
4833b0441da4SMatthew G. Knepley + dm         - The DM object
4834b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4835b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4836b0441da4SMatthew G. Knepley 
4837b0441da4SMatthew G. Knepley   Notes:
4838b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4839b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4840b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4841b0441da4SMatthew G. Knepley 
4842b0441da4SMatthew G. Knepley   Level: developer
4843b0441da4SMatthew G. Knepley 
4844b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4845b0441da4SMatthew G. Knepley @*/
4846b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4847b0441da4SMatthew G. Knepley {
4848b0441da4SMatthew G. Knepley   PetscInt       Nf;
4849b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4850b0441da4SMatthew G. Knepley 
4851b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4852b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4853b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4854b0441da4SMatthew G. Knepley   if (!Nf) {
4855b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4856b0441da4SMatthew G. Knepley   } else {
4857b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4858e5e52638SMatthew G. Knepley   }
4859e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4860e5e52638SMatthew G. Knepley }
4861e5e52638SMatthew G. Knepley 
4862e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4863e5e52638SMatthew G. Knepley {
4864e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4865e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4866e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4867e5e52638SMatthew G. Knepley 
4868e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4869e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4870e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4871e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4872b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4873e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4874e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4875e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4876e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4877e5e52638SMatthew G. Knepley }
4878e5e52638SMatthew G. Knepley 
4879e5e52638SMatthew G. Knepley /*@
4880e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4881e5e52638SMatthew G. Knepley 
4882e5e52638SMatthew G. Knepley   Not collective
4883e5e52638SMatthew G. Knepley 
4884e5e52638SMatthew G. Knepley   Input Parameter:
4885e5e52638SMatthew G. Knepley . dm - The DM
4886e5e52638SMatthew G. Knepley 
4887e5e52638SMatthew G. Knepley   Output Parameter:
4888e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4889e5e52638SMatthew G. Knepley 
4890e5e52638SMatthew G. Knepley   Level: intermediate
4891e5e52638SMatthew G. Knepley 
4892e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4893e5e52638SMatthew G. Knepley @*/
4894e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4895e5e52638SMatthew G. Knepley {
4896e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4897e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4898534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
4899e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4900e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4901e5e52638SMatthew G. Knepley }
4902e5e52638SMatthew G. Knepley 
4903e5e52638SMatthew G. Knepley /*@
4904e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4905e5e52638SMatthew G. Knepley 
4906d083f849SBarry Smith   Logically collective on dm
4907e5e52638SMatthew G. Knepley 
4908e5e52638SMatthew G. Knepley   Input Parameter:
4909e5e52638SMatthew G. Knepley . dm - The DM
4910e5e52638SMatthew G. Knepley 
4911e5e52638SMatthew G. Knepley   Level: intermediate
4912e5e52638SMatthew G. Knepley 
4913e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4914e5e52638SMatthew G. Knepley @*/
4915e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4916e5e52638SMatthew G. Knepley {
4917e5e52638SMatthew G. Knepley   PetscInt       s;
4918e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4919e5e52638SMatthew G. Knepley 
4920e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4921e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4922e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4923e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4924e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4925b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4926e5e52638SMatthew G. Knepley   }
4927e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4928e5e52638SMatthew G. Knepley   dm->probs = NULL;
4929e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4930e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4931e5e52638SMatthew G. Knepley }
4932e5e52638SMatthew G. Knepley 
4933e5e52638SMatthew G. Knepley /*@
4934e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4935e5e52638SMatthew G. Knepley 
4936e5e52638SMatthew G. Knepley   Not collective
4937e5e52638SMatthew G. Knepley 
4938e5e52638SMatthew G. Knepley   Input Parameter:
4939e5e52638SMatthew G. Knepley . dm    - The DM
4940e5e52638SMatthew G. Knepley 
4941e5e52638SMatthew G. Knepley   Output Parameter:
4942e5e52638SMatthew G. Knepley . prob - The default PetscDS
4943e5e52638SMatthew G. Knepley 
4944e5e52638SMatthew G. Knepley   Level: intermediate
4945e5e52638SMatthew G. Knepley 
4946e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4947e5e52638SMatthew G. Knepley @*/
4948e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4949e5e52638SMatthew G. Knepley {
4950b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4951b0143b4dSMatthew G. Knepley 
4952e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4953e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4954e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4955b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4956b0143b4dSMatthew G. Knepley     PetscDS ds;
4957b0143b4dSMatthew G. Knepley 
4958b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4959b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
4960b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4961b0143b4dSMatthew G. Knepley   }
4962b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4963e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4964e5e52638SMatthew G. Knepley }
4965e5e52638SMatthew G. Knepley 
4966e5e52638SMatthew G. Knepley /*@
4967e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
4968e5e52638SMatthew G. Knepley 
4969e5e52638SMatthew G. Knepley   Not collective
4970e5e52638SMatthew G. Knepley 
4971e5e52638SMatthew G. Knepley   Input Parameters:
4972e5e52638SMatthew G. Knepley + dm    - The DM
4973e5e52638SMatthew G. Knepley - point - Cell for the DS
4974e5e52638SMatthew G. Knepley 
4975e5e52638SMatthew G. Knepley   Output Parameter:
4976e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
4977e5e52638SMatthew G. Knepley 
4978e5e52638SMatthew G. Knepley   Level: developer
4979e5e52638SMatthew G. Knepley 
4980b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
4981e5e52638SMatthew G. Knepley @*/
4982e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
4983e5e52638SMatthew G. Knepley {
4984e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
4985e5e52638SMatthew G. Knepley   PetscInt       s;
4986e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4987e5e52638SMatthew G. Knepley 
4988e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4989e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4990e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
4991e5e52638SMatthew G. Knepley   *prob = NULL;
4992e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4993e5e52638SMatthew G. Knepley     PetscInt val;
4994e5e52638SMatthew G. Knepley 
4995e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
4996e5e52638SMatthew G. Knepley     else {
4997e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
4998e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
4999e5e52638SMatthew G. Knepley     }
5000e5e52638SMatthew G. Knepley   }
5001e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5002e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5003e5e52638SMatthew G. Knepley }
5004e5e52638SMatthew G. Knepley 
5005e5e52638SMatthew G. Knepley /*@
5006e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5007e5e52638SMatthew G. Knepley 
5008e5e52638SMatthew G. Knepley   Not collective
5009e5e52638SMatthew G. Knepley 
5010e5e52638SMatthew G. Knepley   Input Parameters:
5011e5e52638SMatthew G. Knepley + dm    - The DM
5012e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5013e5e52638SMatthew G. Knepley 
5014b3cf3223SMatthew G. Knepley   Output Parameters:
5015b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5016b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5017e5e52638SMatthew G. Knepley 
5018e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5019e5e52638SMatthew G. Knepley 
5020e5e52638SMatthew G. Knepley   Level: advanced
5021e5e52638SMatthew G. Knepley 
5022e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5023e5e52638SMatthew G. Knepley @*/
5024b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5025e5e52638SMatthew G. Knepley {
5026e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5027e5e52638SMatthew G. Knepley 
5028e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5029e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5030e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5031b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5032b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5033e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5034b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5035b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5036b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5037b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5038b3cf3223SMatthew G. Knepley     }
5039e5e52638SMatthew G. Knepley   }
50402df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5041e5e52638SMatthew G. Knepley }
5042e5e52638SMatthew G. Knepley 
5043e5e52638SMatthew G. Knepley /*@
5044e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5045e5e52638SMatthew G. Knepley 
5046e5e52638SMatthew G. Knepley   Not collective
5047e5e52638SMatthew G. Knepley 
5048e5e52638SMatthew G. Knepley   Input Parameters:
5049e5e52638SMatthew G. Knepley + dm  - The DM
5050e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5051e5e52638SMatthew G. Knepley 
5052e5e52638SMatthew G. Knepley   Output Parameters:
5053b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5054b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5055b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
5056e5e52638SMatthew G. Knepley 
5057e5e52638SMatthew G. Knepley   Level: advanced
5058e5e52638SMatthew G. Knepley 
5059e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5060e5e52638SMatthew G. Knepley @*/
5061b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5062e5e52638SMatthew G. Knepley {
5063e5e52638SMatthew G. Knepley   PetscInt       Nds;
5064e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5065e5e52638SMatthew G. Knepley 
5066e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5067e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5068e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5069e5e52638SMatthew 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);
5070e5e52638SMatthew G. Knepley   if (label) {
5071e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5072e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5073e5e52638SMatthew G. Knepley   }
5074b3cf3223SMatthew G. Knepley   if (fields) {
5075b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5076b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5077b3cf3223SMatthew G. Knepley   }
5078e5e52638SMatthew G. Knepley   if (ds) {
5079b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5080e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5081e5e52638SMatthew G. Knepley   }
5082e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5083e5e52638SMatthew G. Knepley }
5084e5e52638SMatthew G. Knepley 
5085e5e52638SMatthew G. Knepley /*@
5086e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5087e5e52638SMatthew G. Knepley 
5088d083f849SBarry Smith   Collective on dm
5089e5e52638SMatthew G. Knepley 
5090e5e52638SMatthew G. Knepley   Input Parameters:
5091e5e52638SMatthew G. Knepley + dm     - The DM
5092e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5093b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5094e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5095e5e52638SMatthew G. Knepley 
5096b3cf3223SMatthew 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,
5097b3cf3223SMatthew G. Knepley   the fields argument is ignored.
5098e5e52638SMatthew G. Knepley 
5099e5e52638SMatthew G. Knepley   Level: advanced
5100e5e52638SMatthew G. Knepley 
5101e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
5102e5e52638SMatthew G. Knepley @*/
5103b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5104e5e52638SMatthew G. Knepley {
5105e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5106e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5107e5e52638SMatthew G. Knepley 
5108e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5109e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5110e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5111e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5112e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5113e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
5114b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5115e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
5116e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
5117e5e52638SMatthew G. Knepley     }
5118e5e52638SMatthew G. Knepley   }
51191b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5120e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5121b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5122e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5123e5e52638SMatthew G. Knepley   if (!label) {
5124e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5125e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5126e5e52638SMatthew G. Knepley     Nds = 0;
5127e5e52638SMatthew G. Knepley   }
5128e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
5129b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5130e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5131e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5132e5e52638SMatthew G. Knepley }
5133e5e52638SMatthew G. Knepley 
5134e5e52638SMatthew G. Knepley /*@
5135e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5136e5e52638SMatthew G. Knepley 
5137d083f849SBarry Smith   Collective on dm
5138e5e52638SMatthew G. Knepley 
5139e5e52638SMatthew G. Knepley   Input Parameter:
5140e5e52638SMatthew G. Knepley . dm - The DM
5141e5e52638SMatthew G. Knepley 
5142e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5143e5e52638SMatthew G. Knepley 
5144e5e52638SMatthew G. Knepley   Level: intermediate
5145e5e52638SMatthew G. Knepley 
5146e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5147e5e52638SMatthew G. Knepley @*/
5148e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5149e5e52638SMatthew G. Knepley {
5150e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5151e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5152b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5153e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5154e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5155e5e52638SMatthew G. Knepley 
5156e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5157e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5158e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5159e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5160e5e52638SMatthew G. Knepley    1) NULL
5161e5e52638SMatthew G. Knepley    2) Hybrid cells
5162e5e52638SMatthew G. Knepley   */
5163e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5164e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5165e5e52638SMatthew G. Knepley   /* Create default DS */
5166b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
51672df9ee95SMatthew G. Knepley   if (!prob) {
5168b3cf3223SMatthew G. Knepley     IS        fields;
5169b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5170b3cf3223SMatthew G. Knepley 
5171b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5172b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5173b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
517488f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
517588f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
517688f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
517788f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
517888f0c812SMatthew G. Knepley 
51792df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5180b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
51812df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5182b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5183b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
51842df9ee95SMatthew G. Knepley   }
5185e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5186e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5187b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5188e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5189e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5190e5e52638SMatthew G. Knepley 
5191e5e52638SMatthew G. Knepley     if (label) {
51920122748bSMatthew G. Knepley       DM        plex;
5193a2d47024SMatthew G. Knepley       IS        fields;
5194a2d47024SMatthew G. Knepley       PetscInt *fld;
51950122748bSMatthew G. Knepley       PetscInt  depth, pMax[4];
51960122748bSMatthew G. Knepley 
51970122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
51980122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
51990122748bSMatthew 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);
52000122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
52010122748bSMatthew G. Knepley 
5202e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5203e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
5204e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5205a2d47024SMatthew G. Knepley       ierr = PetscMalloc1(1, &fld);CHKERRQ(ierr);
5206a2d47024SMatthew G. Knepley       fld[0] = f;
5207a2d47024SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5208a2d47024SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5209a2d47024SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5210a2d47024SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, 1, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5211a2d47024SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, fields, probh);CHKERRQ(ierr);
5212a2d47024SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
5213e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5214e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5215e5e52638SMatthew G. Knepley       break;
5216e5e52638SMatthew G. Knepley     }
5217e5e52638SMatthew G. Knepley   }
5218e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5219b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5220e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5221e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5222e5e52638SMatthew G. Knepley 
5223e5e52638SMatthew G. Knepley     if (!label) {
5224e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5225e5e52638SMatthew G. Knepley       if (probh) {
5226e5e52638SMatthew G. Knepley         PetscFE subfe;
5227e5e52638SMatthew G. Knepley 
5228e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5229e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5230e5e52638SMatthew G. Knepley       }
5231e5e52638SMatthew G. Knepley     } else {
5232e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5233e5e52638SMatthew G. Knepley     }
5234e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5235e5e52638SMatthew G. Knepley     {
5236e5e52638SMatthew G. Knepley       PetscClassId id;
5237e5e52638SMatthew G. Knepley 
5238e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5239e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5240e5e52638SMatthew G. Knepley     }
5241e5e52638SMatthew G. Knepley   }
5242e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5243e5e52638SMatthew G. Knepley   /* Setup DSes */
5244e5e52638SMatthew G. Knepley   if (doSetup) {
5245e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5246e5e52638SMatthew G. Knepley   }
5247e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5248e5e52638SMatthew G. Knepley }
5249e5e52638SMatthew G. Knepley 
5250e5e52638SMatthew G. Knepley /*@
5251e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5252e5e52638SMatthew G. Knepley 
5253d083f849SBarry Smith   Collective on dm
5254e5e52638SMatthew G. Knepley 
5255e5e52638SMatthew G. Knepley   Input Parameter:
5256e5e52638SMatthew G. Knepley . dm - The DM
5257e5e52638SMatthew G. Knepley 
5258e5e52638SMatthew G. Knepley   Output Parameter:
5259e5e52638SMatthew G. Knepley . newdm - The DM
5260e5e52638SMatthew G. Knepley 
5261e5e52638SMatthew G. Knepley   Level: advanced
5262e5e52638SMatthew G. Knepley 
5263e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5264e5e52638SMatthew G. Knepley @*/
5265e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5266e5e52638SMatthew G. Knepley {
5267e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5268e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5269e5e52638SMatthew G. Knepley 
5270e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5271e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5272e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5273e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5274e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5275e5e52638SMatthew G. Knepley     DMLabel label;
5276b3cf3223SMatthew G. Knepley     IS      fields;
5277e5e52638SMatthew G. Knepley     PetscDS ds;
5278e5e52638SMatthew G. Knepley 
5279b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5280b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5281e5e52638SMatthew G. Knepley   }
5282e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5283e5e52638SMatthew G. Knepley }
5284e5e52638SMatthew G. Knepley 
5285e5e52638SMatthew G. Knepley /*@
5286e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5287e5e52638SMatthew G. Knepley 
5288d083f849SBarry Smith   Collective on dm
5289e5e52638SMatthew G. Knepley 
5290e5e52638SMatthew G. Knepley   Input Parameter:
5291e5e52638SMatthew G. Knepley . dm - The DM
5292e5e52638SMatthew G. Knepley 
5293e5e52638SMatthew G. Knepley   Output Parameter:
5294e5e52638SMatthew G. Knepley . newdm - The DM
5295e5e52638SMatthew G. Knepley 
5296e5e52638SMatthew G. Knepley   Level: advanced
5297e5e52638SMatthew G. Knepley 
5298e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5299e5e52638SMatthew G. Knepley @*/
5300e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5301e5e52638SMatthew G. Knepley {
5302e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5303e5e52638SMatthew G. Knepley 
5304e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5305e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5306e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5307e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5308e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5309e5e52638SMatthew G. Knepley }
5310e5e52638SMatthew G. Knepley 
5311b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5312b64e0483SPeter Brune {
5313b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5314b64e0483SPeter Brune   PetscErrorCode ierr;
5315b64e0483SPeter Brune   Vec coords,ccoords;
53166dbf9973SLawrence Mitchell   Mat inject;
5317b64e0483SPeter Brune   PetscFunctionBegin;
5318b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5319b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5320b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5321b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5322b64e0483SPeter Brune   if (coords && !ccoords) {
5323b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
53246668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
53256dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
53262adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
53276dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5328b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5329b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5330b64e0483SPeter Brune   }
5331b64e0483SPeter Brune   PetscFunctionReturn(0);
5332b64e0483SPeter Brune }
5333b64e0483SPeter Brune 
533403dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
533503dadc2fSPeter Brune {
533603dadc2fSPeter Brune   DM dm_coord,subdm_coord;
533703dadc2fSPeter Brune   PetscErrorCode ierr;
533803dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
533903dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
534003dadc2fSPeter Brune   PetscFunctionBegin;
534103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
534203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
534303dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
534403dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
534503dadc2fSPeter Brune   if (coords && !ccoords) {
534603dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
53476668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
534803dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
534924640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
535003dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
535103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
535203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
53531ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
535403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
535503dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
535603dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
535703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
535803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
535903dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
536003dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
536103dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
536203dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
536303dadc2fSPeter Brune   }
536403dadc2fSPeter Brune   PetscFunctionReturn(0);
536503dadc2fSPeter Brune }
536603dadc2fSPeter Brune 
5367c73cfb54SMatthew G. Knepley /*@
5368c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5369c73cfb54SMatthew G. Knepley 
5370c73cfb54SMatthew G. Knepley   Not collective
5371c73cfb54SMatthew G. Knepley 
5372c73cfb54SMatthew G. Knepley   Input Parameter:
5373c73cfb54SMatthew G. Knepley . dm - The DM
5374c73cfb54SMatthew G. Knepley 
5375c73cfb54SMatthew G. Knepley   Output Parameter:
5376c73cfb54SMatthew G. Knepley . dim - The topological dimension
5377c73cfb54SMatthew G. Knepley 
5378c73cfb54SMatthew G. Knepley   Level: beginner
5379c73cfb54SMatthew G. Knepley 
5380c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5381c73cfb54SMatthew G. Knepley @*/
5382c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5383c73cfb54SMatthew G. Knepley {
5384c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5385c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5386534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5387c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5388c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5389c73cfb54SMatthew G. Knepley }
5390c73cfb54SMatthew G. Knepley 
5391c73cfb54SMatthew G. Knepley /*@
5392c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5393c73cfb54SMatthew G. Knepley 
5394c73cfb54SMatthew G. Knepley   Collective on dm
5395c73cfb54SMatthew G. Knepley 
5396c73cfb54SMatthew G. Knepley   Input Parameters:
5397c73cfb54SMatthew G. Knepley + dm - The DM
5398c73cfb54SMatthew G. Knepley - dim - The topological dimension
5399c73cfb54SMatthew G. Knepley 
5400c73cfb54SMatthew G. Knepley   Level: beginner
5401c73cfb54SMatthew G. Knepley 
5402c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5403c73cfb54SMatthew G. Knepley @*/
5404c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5405c73cfb54SMatthew G. Knepley {
5406e5e52638SMatthew G. Knepley   PetscDS        ds;
5407f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5408f17e8794SMatthew G. Knepley 
5409c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5410c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5411c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5412c73cfb54SMatthew G. Knepley   dm->dim = dim;
5413e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5414e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5415c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5416c73cfb54SMatthew G. Knepley }
5417c73cfb54SMatthew G. Knepley 
5418793f3fe5SMatthew G. Knepley /*@
5419793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5420793f3fe5SMatthew G. Knepley 
5421d083f849SBarry Smith   Collective on dm
5422793f3fe5SMatthew G. Knepley 
5423793f3fe5SMatthew G. Knepley   Input Parameters:
5424793f3fe5SMatthew G. Knepley + dm - the DM
5425793f3fe5SMatthew G. Knepley - dim - the dimension
5426793f3fe5SMatthew G. Knepley 
5427793f3fe5SMatthew G. Knepley   Output Parameters:
5428793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5429aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5430793f3fe5SMatthew G. Knepley 
5431793f3fe5SMatthew G. Knepley   Note:
5432793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5433a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5434793f3fe5SMatthew G. Knepley   then the interval is empty.
5435793f3fe5SMatthew G. Knepley 
5436793f3fe5SMatthew G. Knepley   Level: intermediate
5437793f3fe5SMatthew G. Knepley 
5438793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5439793f3fe5SMatthew G. Knepley @*/
5440793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5441793f3fe5SMatthew G. Knepley {
5442793f3fe5SMatthew G. Knepley   PetscInt       d;
5443793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5444793f3fe5SMatthew G. Knepley 
5445793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5446793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5447793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5448793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5449b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5450793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5451793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5452793f3fe5SMatthew G. Knepley }
5453793f3fe5SMatthew G. Knepley 
54546636e97aSMatthew G Knepley /*@
54556636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
54566636e97aSMatthew G Knepley 
5457d083f849SBarry Smith   Collective on dm
54586636e97aSMatthew G Knepley 
54596636e97aSMatthew G Knepley   Input Parameters:
54606636e97aSMatthew G Knepley + dm - the DM
54616636e97aSMatthew G Knepley - c - coordinate vector
54626636e97aSMatthew G Knepley 
54632dd40e9bSPatrick Sanan   Notes:
54642dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
54652dd40e9bSPatrick Sanan 
54662dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
54676636e97aSMatthew G Knepley 
54686636e97aSMatthew G Knepley   Level: intermediate
54696636e97aSMatthew G Knepley 
54702dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
54716636e97aSMatthew G Knepley @*/
54726636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
54736636e97aSMatthew G Knepley {
54746636e97aSMatthew G Knepley   PetscErrorCode ierr;
54756636e97aSMatthew G Knepley 
54766636e97aSMatthew G Knepley   PetscFunctionBegin;
54776636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54786636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
54796636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
54806636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
54816636e97aSMatthew G Knepley   dm->coordinates = c;
54826636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5483b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
548403dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
54856636e97aSMatthew G Knepley   PetscFunctionReturn(0);
54866636e97aSMatthew G Knepley }
54876636e97aSMatthew G Knepley 
54886636e97aSMatthew G Knepley /*@
54896636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
54906636e97aSMatthew G Knepley 
54917058e716SVaclav Hapla   Not collective
54926636e97aSMatthew G Knepley 
54936636e97aSMatthew G Knepley    Input Parameters:
54946636e97aSMatthew G Knepley +  dm - the DM
54956636e97aSMatthew G Knepley -  c - coordinate vector
54966636e97aSMatthew G Knepley 
54972dd40e9bSPatrick Sanan   Notes:
54986636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
54996636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
55006636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
55016636e97aSMatthew G Knepley 
55022dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
55032dd40e9bSPatrick Sanan 
55046636e97aSMatthew G Knepley   Level: intermediate
55056636e97aSMatthew G Knepley 
55066636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
55076636e97aSMatthew G Knepley @*/
55086636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
55096636e97aSMatthew G Knepley {
55106636e97aSMatthew G Knepley   PetscErrorCode ierr;
55116636e97aSMatthew G Knepley 
55126636e97aSMatthew G Knepley   PetscFunctionBegin;
55136636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55146636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55156636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55166636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
55178865f1eaSKarl Rupp 
55186636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
55198865f1eaSKarl Rupp 
55206636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55216636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55226636e97aSMatthew G Knepley }
55236636e97aSMatthew G Knepley 
55246636e97aSMatthew G Knepley /*@
55256636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
55266636e97aSMatthew G Knepley 
5527d083f849SBarry Smith   Collective on dm
55286636e97aSMatthew G Knepley 
55296636e97aSMatthew G Knepley   Input Parameter:
55306636e97aSMatthew G Knepley . dm - the DM
55316636e97aSMatthew G Knepley 
55326636e97aSMatthew G Knepley   Output Parameter:
55336636e97aSMatthew G Knepley . c - global coordinate vector
55346636e97aSMatthew G Knepley 
55356636e97aSMatthew G Knepley   Note:
55366636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
55376636e97aSMatthew G Knepley 
55386636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
55396636e97aSMatthew G Knepley 
55406636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
55416636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
55426636e97aSMatthew G Knepley 
55436636e97aSMatthew G Knepley   Level: intermediate
55446636e97aSMatthew G Knepley 
55456636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
55466636e97aSMatthew G Knepley @*/
55476636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
55486636e97aSMatthew G Knepley {
55496636e97aSMatthew G Knepley   PetscErrorCode ierr;
55506636e97aSMatthew G Knepley 
55516636e97aSMatthew G Knepley   PetscFunctionBegin;
55526636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55536636e97aSMatthew G Knepley   PetscValidPointer(c,2);
55541f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
55550298fd71SBarry Smith     DM        cdm = NULL;
55561970a576SMatthew G. Knepley     PetscBool localized;
55576636e97aSMatthew G Knepley 
55586636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
55596636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
55601970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
55611970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
55621970a576SMatthew G. Knepley     if (localized) {
55631970a576SMatthew G. Knepley       PetscInt cdim;
55641970a576SMatthew G. Knepley 
55651970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
55661970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
55671970a576SMatthew G. Knepley     }
55686636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
55696636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
55706636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
55716636e97aSMatthew G Knepley   }
55726636e97aSMatthew G Knepley   *c = dm->coordinates;
55736636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55746636e97aSMatthew G Knepley }
55756636e97aSMatthew G Knepley 
55766636e97aSMatthew G Knepley /*@
557781e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
557881e9a530SVaclav Hapla 
5579d083f849SBarry Smith   Collective on dm
558081e9a530SVaclav Hapla 
558181e9a530SVaclav Hapla   Input Parameter:
558281e9a530SVaclav Hapla . dm - the DM
558381e9a530SVaclav Hapla 
558481e9a530SVaclav Hapla   Level: advanced
558581e9a530SVaclav Hapla 
558681e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
558781e9a530SVaclav Hapla @*/
558881e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
558981e9a530SVaclav Hapla {
559081e9a530SVaclav Hapla   PetscErrorCode ierr;
559181e9a530SVaclav Hapla 
559281e9a530SVaclav Hapla   PetscFunctionBegin;
559381e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
559481e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
55951970a576SMatthew G. Knepley     DM        cdm = NULL;
55961970a576SMatthew G. Knepley     PetscBool localized;
55971970a576SMatthew G. Knepley 
559881e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
559981e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
56001970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
56011970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
56021970a576SMatthew G. Knepley     if (localized) {
56031970a576SMatthew G. Knepley       PetscInt cdim;
56041970a576SMatthew G. Knepley 
56051970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
56061970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
56071970a576SMatthew G. Knepley     }
560881e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
560981e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
561081e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
561181e9a530SVaclav Hapla   }
561281e9a530SVaclav Hapla   PetscFunctionReturn(0);
561381e9a530SVaclav Hapla }
561481e9a530SVaclav Hapla 
561581e9a530SVaclav Hapla /*@
56166636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
56176636e97aSMatthew G Knepley 
5618d083f849SBarry Smith   Collective on dm
56196636e97aSMatthew G Knepley 
56206636e97aSMatthew G Knepley   Input Parameter:
56216636e97aSMatthew G Knepley . dm - the DM
56226636e97aSMatthew G Knepley 
56236636e97aSMatthew G Knepley   Output Parameter:
56246636e97aSMatthew G Knepley . c - coordinate vector
56256636e97aSMatthew G Knepley 
56266636e97aSMatthew G Knepley   Note:
56276636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
56286636e97aSMatthew G Knepley 
56296636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
56306636e97aSMatthew G Knepley 
56316636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56326636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56336636e97aSMatthew G Knepley 
56346636e97aSMatthew G Knepley   Level: intermediate
56356636e97aSMatthew G Knepley 
563681e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
56376636e97aSMatthew G Knepley @*/
56386636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
56396636e97aSMatthew G Knepley {
56406636e97aSMatthew G Knepley   PetscErrorCode ierr;
56416636e97aSMatthew G Knepley 
56426636e97aSMatthew G Knepley   PetscFunctionBegin;
56436636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56446636e97aSMatthew G Knepley   PetscValidPointer(c,2);
564581e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
56466636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
56476636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56486636e97aSMatthew G Knepley }
56496636e97aSMatthew G Knepley 
565081e9a530SVaclav Hapla /*@
565181e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
565281e9a530SVaclav Hapla 
565381e9a530SVaclav Hapla   Not collective
565481e9a530SVaclav Hapla 
565581e9a530SVaclav Hapla   Input Parameter:
565681e9a530SVaclav Hapla . dm - the DM
565781e9a530SVaclav Hapla 
565881e9a530SVaclav Hapla   Output Parameter:
565981e9a530SVaclav Hapla . c - coordinate vector
566081e9a530SVaclav Hapla 
566181e9a530SVaclav Hapla   Level: advanced
566281e9a530SVaclav Hapla 
566381e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
566481e9a530SVaclav Hapla @*/
566581e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
566681e9a530SVaclav Hapla {
566781e9a530SVaclav Hapla   PetscFunctionBegin;
566881e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
566981e9a530SVaclav Hapla   PetscValidPointer(c,2);
567081e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
56716636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
56726636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56736636e97aSMatthew G Knepley }
56746636e97aSMatthew G Knepley 
56752db98f8dSVaclav Hapla /*@
56762db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
56772db98f8dSVaclav Hapla 
56782db98f8dSVaclav Hapla   Not collective
56792db98f8dSVaclav Hapla 
56802db98f8dSVaclav Hapla   Input Parameter:
56812db98f8dSVaclav Hapla + dm - the DM
56822db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
56832db98f8dSVaclav Hapla 
56842db98f8dSVaclav Hapla   Output Parameter:
56852db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
56862db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
56872db98f8dSVaclav Hapla 
56882db98f8dSVaclav Hapla   Note:
56892db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
56902db98f8dSVaclav Hapla 
56912db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
56922db98f8dSVaclav Hapla 
56932db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
56942db98f8dSVaclav Hapla 
56952db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56962db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56972db98f8dSVaclav Hapla 
56982db98f8dSVaclav Hapla   Level: advanced
56992db98f8dSVaclav Hapla 
57002db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
57012db98f8dSVaclav Hapla @*/
57022db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
57032db98f8dSVaclav Hapla {
57042db98f8dSVaclav Hapla   PetscSection        cs, newcs;
57052db98f8dSVaclav Hapla   Vec                 coords;
57062db98f8dSVaclav Hapla   const PetscScalar   *arr;
57072db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
57082db98f8dSVaclav Hapla   PetscInt            n;
57092db98f8dSVaclav Hapla   PetscErrorCode      ierr;
57102db98f8dSVaclav Hapla 
57112db98f8dSVaclav Hapla   PetscFunctionBegin;
57122db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57132db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
57142db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
57152db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
57162db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
57171bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
57181bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
57192db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
57202db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
57212db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
57222db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
57232db98f8dSVaclav Hapla   if (pCoord) {
57242db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
57252db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
57262db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
57272db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5728ad9ac99dSVaclav Hapla   } else {
5729ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
57302db98f8dSVaclav Hapla   }
5731ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5732ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
57332db98f8dSVaclav Hapla   PetscFunctionReturn(0);
57342db98f8dSVaclav Hapla }
57352db98f8dSVaclav Hapla 
5736f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5737f19dbd58SToby Isaac {
5738f19dbd58SToby Isaac   PetscErrorCode ierr;
5739f19dbd58SToby Isaac 
5740f19dbd58SToby Isaac   PetscFunctionBegin;
5741f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5742f19dbd58SToby Isaac   PetscValidPointer(field,2);
5743f19dbd58SToby Isaac   if (!dm->coordinateField) {
5744f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5745f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5746f19dbd58SToby Isaac     }
5747f19dbd58SToby Isaac   }
5748f19dbd58SToby Isaac   *field = dm->coordinateField;
5749f19dbd58SToby Isaac   PetscFunctionReturn(0);
5750f19dbd58SToby Isaac }
5751f19dbd58SToby Isaac 
5752f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5753f19dbd58SToby Isaac {
5754f19dbd58SToby Isaac   PetscErrorCode ierr;
5755f19dbd58SToby Isaac 
5756f19dbd58SToby Isaac   PetscFunctionBegin;
5757f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5758f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5759c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5760f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5761f19dbd58SToby Isaac   dm->coordinateField = field;
5762f19dbd58SToby Isaac   PetscFunctionReturn(0);
5763f19dbd58SToby Isaac }
5764f19dbd58SToby Isaac 
57656636e97aSMatthew G Knepley /*@
57661cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
57676636e97aSMatthew G Knepley 
5768d083f849SBarry Smith   Collective on dm
57696636e97aSMatthew G Knepley 
57706636e97aSMatthew G Knepley   Input Parameter:
57716636e97aSMatthew G Knepley . dm - the DM
57726636e97aSMatthew G Knepley 
57736636e97aSMatthew G Knepley   Output Parameter:
57746636e97aSMatthew G Knepley . cdm - coordinate DM
57756636e97aSMatthew G Knepley 
57766636e97aSMatthew G Knepley   Level: intermediate
57776636e97aSMatthew G Knepley 
57781cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
57796636e97aSMatthew G Knepley @*/
57806636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
57816636e97aSMatthew G Knepley {
57826636e97aSMatthew G Knepley   PetscErrorCode ierr;
57836636e97aSMatthew G Knepley 
57846636e97aSMatthew G Knepley   PetscFunctionBegin;
57856636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57866636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
57876636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5788308f8a94SToby Isaac     DM cdm;
5789308f8a94SToby Isaac 
579082f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5791308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5792308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5793308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5794308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5795308f8a94SToby Isaac     dm->coordinateDM = cdm;
57966636e97aSMatthew G Knepley   }
57976636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
57986636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57996636e97aSMatthew G Knepley }
5800e87bb0d3SMatthew G Knepley 
58011cfe2091SMatthew G. Knepley /*@
58021cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
58031cfe2091SMatthew G. Knepley 
5804d083f849SBarry Smith   Logically Collective on dm
58051cfe2091SMatthew G. Knepley 
58061cfe2091SMatthew G. Knepley   Input Parameters:
58071cfe2091SMatthew G. Knepley + dm - the DM
58081cfe2091SMatthew G. Knepley - cdm - coordinate DM
58091cfe2091SMatthew G. Knepley 
58101cfe2091SMatthew G. Knepley   Level: intermediate
58111cfe2091SMatthew G. Knepley 
58121cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58131cfe2091SMatthew G. Knepley @*/
58141cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
58151cfe2091SMatthew G. Knepley {
58161cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
58171cfe2091SMatthew G. Knepley 
58181cfe2091SMatthew G. Knepley   PetscFunctionBegin;
58191cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58201cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5821f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
58221cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
58231cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
58241cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
58251cfe2091SMatthew G. Knepley }
58261cfe2091SMatthew G. Knepley 
582746e270d4SMatthew G. Knepley /*@
582846e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
582946e270d4SMatthew G. Knepley 
583046e270d4SMatthew G. Knepley   Not Collective
583146e270d4SMatthew G. Knepley 
583246e270d4SMatthew G. Knepley   Input Parameter:
583346e270d4SMatthew G. Knepley . dm - The DM object
583446e270d4SMatthew G. Knepley 
583546e270d4SMatthew G. Knepley   Output Parameter:
583646e270d4SMatthew G. Knepley . dim - The embedding dimension
583746e270d4SMatthew G. Knepley 
583846e270d4SMatthew G. Knepley   Level: intermediate
583946e270d4SMatthew G. Knepley 
584092fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
584146e270d4SMatthew G. Knepley @*/
584246e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
584346e270d4SMatthew G. Knepley {
584446e270d4SMatthew G. Knepley   PetscFunctionBegin;
584546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5846534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
58479a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
58489a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
58499a9a41abSToby Isaac   }
585046e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
585146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
585246e270d4SMatthew G. Knepley }
585346e270d4SMatthew G. Knepley 
585446e270d4SMatthew G. Knepley /*@
585546e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
585646e270d4SMatthew G. Knepley 
585746e270d4SMatthew G. Knepley   Not Collective
585846e270d4SMatthew G. Knepley 
585946e270d4SMatthew G. Knepley   Input Parameters:
586046e270d4SMatthew G. Knepley + dm  - The DM object
586146e270d4SMatthew G. Knepley - dim - The embedding dimension
586246e270d4SMatthew G. Knepley 
586346e270d4SMatthew G. Knepley   Level: intermediate
586446e270d4SMatthew G. Knepley 
586592fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
586646e270d4SMatthew G. Knepley @*/
586746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
586846e270d4SMatthew G. Knepley {
5869e5e52638SMatthew G. Knepley   PetscDS        ds;
5870f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5871f17e8794SMatthew G. Knepley 
587246e270d4SMatthew G. Knepley   PetscFunctionBegin;
587346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
587446e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5875e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5876e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
587746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
587846e270d4SMatthew G. Knepley }
587946e270d4SMatthew G. Knepley 
5880e8abe2deSMatthew G. Knepley /*@
5881e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5882e8abe2deSMatthew G. Knepley 
5883d083f849SBarry Smith   Collective on dm
5884e8abe2deSMatthew G. Knepley 
5885e8abe2deSMatthew G. Knepley   Input Parameter:
5886e8abe2deSMatthew G. Knepley . dm - The DM object
5887e8abe2deSMatthew G. Knepley 
5888e8abe2deSMatthew G. Knepley   Output Parameter:
5889e8abe2deSMatthew G. Knepley . section - The PetscSection object
5890e8abe2deSMatthew G. Knepley 
5891e8abe2deSMatthew G. Knepley   Level: intermediate
5892e8abe2deSMatthew G. Knepley 
589392fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
5894e8abe2deSMatthew G. Knepley @*/
5895e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5896e8abe2deSMatthew G. Knepley {
5897e8abe2deSMatthew G. Knepley   DM             cdm;
5898e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5899e8abe2deSMatthew G. Knepley 
5900e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5901e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5902e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5903e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
590492fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
5905e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5906e8abe2deSMatthew G. Knepley }
5907e8abe2deSMatthew G. Knepley 
5908e8abe2deSMatthew G. Knepley /*@
5909e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5910e8abe2deSMatthew G. Knepley 
5911e8abe2deSMatthew G. Knepley   Not Collective
5912e8abe2deSMatthew G. Knepley 
5913e8abe2deSMatthew G. Knepley   Input Parameters:
5914e8abe2deSMatthew G. Knepley + dm      - The DM object
591546e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5916e8abe2deSMatthew G. Knepley - section - The PetscSection object
5917e8abe2deSMatthew G. Knepley 
5918e8abe2deSMatthew G. Knepley   Level: intermediate
5919e8abe2deSMatthew G. Knepley 
592092fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
5921e8abe2deSMatthew G. Knepley @*/
592246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5923e8abe2deSMatthew G. Knepley {
5924e8abe2deSMatthew G. Knepley   DM             cdm;
5925e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5926e8abe2deSMatthew G. Knepley 
5927e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5928e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
592946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5930e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
593192fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
593246e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
59334c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
593446e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
593546e270d4SMatthew G. Knepley 
593646e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
593746e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
593846e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
593946e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
594046e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
594146e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
594246e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
594346e270d4SMatthew G. Knepley     }
5944ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
594546e270d4SMatthew G. Knepley   }
5946e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5947e8abe2deSMatthew G. Knepley }
5948e8abe2deSMatthew G. Knepley 
59495dc8c3f7SMatthew G. Knepley /*@C
595090b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
59515dc8c3f7SMatthew G. Knepley 
59525dc8c3f7SMatthew G. Knepley   Input Parameters:
595390b157c4SStefano Zampini . dm      - The DM object
595490b157c4SStefano Zampini 
595590b157c4SStefano Zampini   Output Parameters:
595690b157c4SStefano Zampini + per     - Whether the DM is periodic or not
59575dc8c3f7SMatthew 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
59585dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
59595dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
59605dc8c3f7SMatthew G. Knepley 
59615dc8c3f7SMatthew G. Knepley   Level: developer
59625dc8c3f7SMatthew G. Knepley 
59635dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
59645dc8c3f7SMatthew G. Knepley @*/
596590b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5966c6b900c6SMatthew G. Knepley {
5967c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5968c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
596990b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5970c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5971c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
59725dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5973c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5974c6b900c6SMatthew G. Knepley }
5975c6b900c6SMatthew G. Knepley 
59765dc8c3f7SMatthew G. Knepley /*@C
59775dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
59785dc8c3f7SMatthew G. Knepley 
59795dc8c3f7SMatthew G. Knepley   Input Parameters:
59805dc8c3f7SMatthew G. Knepley + dm      - The DM object
598190b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
59825dc8c3f7SMatthew 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
59835dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
59845dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
59855dc8c3f7SMatthew G. Knepley 
59865dc8c3f7SMatthew G. Knepley   Level: developer
59875dc8c3f7SMatthew G. Knepley 
59885dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
59895dc8c3f7SMatthew G. Knepley @*/
599090b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
5991c6b900c6SMatthew G. Knepley {
5992c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
5993c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
5994c6b900c6SMatthew G. Knepley 
5995c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5996c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
599790b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
599890b157c4SStefano Zampini   if (maxCell) {
5999534a8f05SLisandro Dalcin     PetscValidRealPointer(maxCell,3);
6000534a8f05SLisandro Dalcin     PetscValidRealPointer(L,4);
600190b157c4SStefano Zampini     PetscValidPointer(bd,5);
600290b157c4SStefano Zampini   }
60035dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
60045dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
600590b157c4SStefano Zampini   if (maxCell) {
60065dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
60075dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
600890b157c4SStefano Zampini   }
6009072d7d67SStefano Zampini   dm->periodic = per;
6010c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6011c6b900c6SMatthew G. Knepley }
6012c6b900c6SMatthew G. Knepley 
60132e17dfb7SMatthew G. Knepley /*@
60142e17dfb7SMatthew 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.
60152e17dfb7SMatthew G. Knepley 
60162e17dfb7SMatthew G. Knepley   Input Parameters:
60172e17dfb7SMatthew G. Knepley + dm     - The DM
601865da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
601965da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
60202e17dfb7SMatthew G. Knepley 
60212e17dfb7SMatthew G. Knepley   Output Parameter:
60222e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60232e17dfb7SMatthew G. Knepley 
60242e17dfb7SMatthew G. Knepley   Level: developer
60252e17dfb7SMatthew G. Knepley 
60262e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60272e17dfb7SMatthew G. Knepley @*/
602865da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
60292e17dfb7SMatthew G. Knepley {
60302e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
60312e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
60322e17dfb7SMatthew G. Knepley 
60332e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60342e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
60352e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60362e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60372e17dfb7SMatthew G. Knepley   } else {
603865da65dcSMatthew G. Knepley     if (endpoint) {
603965da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6040da3333bfSMatthew 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)) {
6041da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
604265da65dcSMatthew G. Knepley         } else {
6043da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
604465da65dcSMatthew G. Knepley         }
604565da65dcSMatthew G. Knepley       }
604665da65dcSMatthew G. Knepley     } else {
60472e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
60481118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
60492e17dfb7SMatthew G. Knepley       }
60502e17dfb7SMatthew G. Knepley     }
605165da65dcSMatthew G. Knepley   }
60522e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60532e17dfb7SMatthew G. Knepley }
60542e17dfb7SMatthew G. Knepley 
60552e17dfb7SMatthew G. Knepley /*
60562e17dfb7SMatthew 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.
60572e17dfb7SMatthew G. Knepley 
60582e17dfb7SMatthew G. Knepley   Input Parameters:
60592e17dfb7SMatthew G. Knepley + dm     - The DM
60602e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
60612e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
60622e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
60632e17dfb7SMatthew G. Knepley 
60642e17dfb7SMatthew G. Knepley   Output Parameter:
60652e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60662e17dfb7SMatthew G. Knepley 
60672e17dfb7SMatthew G. Knepley   Level: developer
60682e17dfb7SMatthew G. Knepley 
60692e17dfb7SMatthew 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
60702e17dfb7SMatthew G. Knepley 
60712e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60722e17dfb7SMatthew G. Knepley */
60732e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
60742e17dfb7SMatthew G. Knepley {
60752e17dfb7SMatthew G. Knepley   PetscInt d;
60762e17dfb7SMatthew G. Knepley 
60772e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60782e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60792e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60802e17dfb7SMatthew G. Knepley   } else {
60812e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6082908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
60832e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
60842e17dfb7SMatthew G. Knepley       } else {
60852e17dfb7SMatthew G. Knepley         out[d] = in[d];
60862e17dfb7SMatthew G. Knepley       }
60872e17dfb7SMatthew G. Knepley     }
60882e17dfb7SMatthew G. Knepley   }
60892e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60902e17dfb7SMatthew G. Knepley }
60912e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
60922e17dfb7SMatthew G. Knepley {
60932e17dfb7SMatthew G. Knepley   PetscInt d;
60942e17dfb7SMatthew G. Knepley 
60952e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60962e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60972e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60982e17dfb7SMatthew G. Knepley   } else {
60992e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6100908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
61012e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
61022e17dfb7SMatthew G. Knepley       } else {
61032e17dfb7SMatthew G. Knepley         out[d] = in[d];
61042e17dfb7SMatthew G. Knepley       }
61052e17dfb7SMatthew G. Knepley     }
61062e17dfb7SMatthew G. Knepley   }
61072e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61082e17dfb7SMatthew G. Knepley }
61092e17dfb7SMatthew G. Knepley 
61102e17dfb7SMatthew G. Knepley /*
61112e17dfb7SMatthew 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.
61122e17dfb7SMatthew G. Knepley 
61132e17dfb7SMatthew G. Knepley   Input Parameters:
61142e17dfb7SMatthew G. Knepley + dm     - The DM
61152e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
61162e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
61172e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
61182e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
61192e17dfb7SMatthew G. Knepley 
61202e17dfb7SMatthew G. Knepley   Output Parameter:
61212e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
61222e17dfb7SMatthew G. Knepley 
61232e17dfb7SMatthew G. Knepley   Level: developer
61242e17dfb7SMatthew G. Knepley 
61252e17dfb7SMatthew 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
61262e17dfb7SMatthew G. Knepley 
61272e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
61282e17dfb7SMatthew G. Knepley */
61292e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
61302e17dfb7SMatthew G. Knepley {
61312e17dfb7SMatthew G. Knepley   PetscInt d;
61322e17dfb7SMatthew G. Knepley 
61332e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61342e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61352e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
61362e17dfb7SMatthew G. Knepley   } else {
61372e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6138908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
61392e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
61402e17dfb7SMatthew G. Knepley       } else {
61412e17dfb7SMatthew G. Knepley         out[d] += in[d];
61422e17dfb7SMatthew G. Knepley       }
61432e17dfb7SMatthew G. Knepley     }
61442e17dfb7SMatthew G. Knepley   }
61452e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61462e17dfb7SMatthew G. Knepley }
61472e17dfb7SMatthew G. Knepley 
614836447a5eSToby Isaac /*@
61498f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
61508f700142SStefano Zampini 
61518f700142SStefano Zampini   Not collective
615236447a5eSToby Isaac 
615336447a5eSToby Isaac   Input Parameter:
615436447a5eSToby Isaac . dm - The DM
615536447a5eSToby Isaac 
615636447a5eSToby Isaac   Output Parameter:
615736447a5eSToby Isaac   areLocalized - True if localized
615836447a5eSToby Isaac 
615936447a5eSToby Isaac   Level: developer
616036447a5eSToby Isaac 
61618f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
616236447a5eSToby Isaac @*/
61638f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
616436447a5eSToby Isaac {
616536447a5eSToby Isaac   DM             cdm;
616636447a5eSToby Isaac   PetscSection   coordSection;
616746a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
616846a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
616936447a5eSToby Isaac   PetscErrorCode ierr;
617036447a5eSToby Isaac 
617136447a5eSToby Isaac   PetscFunctionBegin;
617236447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6173534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
61748b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
617546a3a80fSLisandro Dalcin 
617636447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
617736447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
617846a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
61799f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
618046a3a80fSLisandro Dalcin 
61819f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
618246a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
618336447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
618436447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
618546a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
618646a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
618736447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
618846a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
618936447a5eSToby Isaac   }
61908f700142SStefano Zampini   *areLocalized = alreadyLocalized;
619136447a5eSToby Isaac   PetscFunctionReturn(0);
619236447a5eSToby Isaac }
619336447a5eSToby Isaac 
61948f700142SStefano Zampini /*@
61958f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
61968f700142SStefano Zampini 
61978f700142SStefano Zampini   Collective on dm
61988f700142SStefano Zampini 
61998f700142SStefano Zampini   Input Parameter:
62008f700142SStefano Zampini . dm - The DM
62018f700142SStefano Zampini 
62028f700142SStefano Zampini   Output Parameter:
62038f700142SStefano Zampini   areLocalized - True if localized
62048f700142SStefano Zampini 
62058f700142SStefano Zampini   Level: developer
62068f700142SStefano Zampini 
62078f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
62088f700142SStefano Zampini @*/
62098f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
62108f700142SStefano Zampini {
62118f700142SStefano Zampini   PetscBool      localized;
62128f700142SStefano Zampini   PetscErrorCode ierr;
62138f700142SStefano Zampini 
62148f700142SStefano Zampini   PetscFunctionBegin;
62158f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6216534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62178f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
62188f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
62198f700142SStefano Zampini   PetscFunctionReturn(0);
62208f700142SStefano Zampini }
622136447a5eSToby Isaac 
62222e17dfb7SMatthew G. Knepley /*@
6223492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
62242e17dfb7SMatthew G. Knepley 
62258f700142SStefano Zampini   Collective on dm
62268f700142SStefano Zampini 
62272e17dfb7SMatthew G. Knepley   Input Parameter:
62282e17dfb7SMatthew G. Knepley . dm - The DM
62292e17dfb7SMatthew G. Knepley 
62302e17dfb7SMatthew G. Knepley   Level: developer
62312e17dfb7SMatthew G. Knepley 
62328f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
62332e17dfb7SMatthew G. Knepley @*/
62342e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
62352e17dfb7SMatthew G. Knepley {
62362e17dfb7SMatthew G. Knepley   DM             cdm;
62372e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
62382e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
62393e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
62403e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6241e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
62423e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
62433e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
62442e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
62452e17dfb7SMatthew G. Knepley 
62462e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
62472e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
624892c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6249f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6250f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6251f7cbd40bSStefano Zampini 
62522e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
62532e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62542e17dfb7SMatthew G. Knepley   {
62552e17dfb7SMatthew G. Knepley     PetscBool isplex;
62562e17dfb7SMatthew G. Knepley 
62572e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
62582e17dfb7SMatthew G. Knepley     if (isplex) {
62592e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
62603e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
626169291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
62623e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
62633e922f36SToby Isaac       newStart = vStart;
62643e922f36SToby Isaac       newEnd   = vEnd;
62653e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
62663e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
62673e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
62683e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
62693e922f36SToby Isaac       }
62702e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
62712e17dfb7SMatthew G. Knepley   }
62722e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
627343eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
62742e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
62753e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6276e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
62773e922f36SToby Isaac 
62782e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
62792e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
62802e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
62812e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
62823e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
62833e922f36SToby Isaac 
628469291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
62853e922f36SToby Isaac   localized = &anchor[bs];
62863e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
62873e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
62883e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
62893e922f36SToby Isaac 
62903e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
62913e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
62923e922f36SToby Isaac       PetscInt     b;
62933e922f36SToby Isaac 
62943e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
62953e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62963e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
62973e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
62983e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
62993e922f36SToby Isaac         for (b = 0; b < bs; b++) {
63003e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
63013e922f36SToby Isaac         }
63023e922f36SToby Isaac         if (b < bs) break;
63033e922f36SToby Isaac       }
63043e922f36SToby Isaac       if (d < dof/bs) {
63053e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
63063e922f36SToby Isaac           PetscInt cdof;
63073e922f36SToby Isaac 
63083e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
63093e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
63103e922f36SToby Isaac         }
63113e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
63123e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
63133e922f36SToby Isaac       }
63143e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63153e922f36SToby Isaac     }
63163e922f36SToby Isaac   }
63173e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
63183e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
631969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63203e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
632169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
63223e922f36SToby Isaac     PetscFunctionReturn(0);
63233e922f36SToby Isaac   }
63242e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63252e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63262e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
63272e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
63282e17dfb7SMatthew G. Knepley   }
63292e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
63302e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6331c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
63322e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
63332e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
63342e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
63352e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6336c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63372e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
63382e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63392e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63402e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
63412e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
63422e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
63432e17dfb7SMatthew G. Knepley   }
63443e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63453e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63463e922f36SToby Isaac 
63472e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
63482e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
63493e922f36SToby Isaac       PetscInt     b, cdof;
63502e17dfb7SMatthew G. Knepley 
63513e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
63523e922f36SToby Isaac       if (!cdof) continue;
63532e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63542e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
63552e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63562e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
63572e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63582e17dfb7SMatthew G. Knepley     }
63593e922f36SToby Isaac   }
636069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
636169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6362c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63632e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
63642e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
63652e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
63662e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
63672e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
63682e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63692e17dfb7SMatthew G. Knepley }
63702e17dfb7SMatthew G. Knepley 
6371e87bb0d3SMatthew G Knepley /*@
63723a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6373e87bb0d3SMatthew G Knepley 
6374d083f849SBarry Smith   Collective on v (see explanation below)
6375e87bb0d3SMatthew G Knepley 
6376e87bb0d3SMatthew G Knepley   Input Parameters:
6377e87bb0d3SMatthew G Knepley + dm - The DM
63783a93e3b7SToby Isaac . v - The Vec of points
637962a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
63803a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6381e87bb0d3SMatthew G Knepley 
638261e3bb9bSMatthew G Knepley   Output Parameter:
638362a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
638462a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
63853a93e3b7SToby Isaac 
6386e87bb0d3SMatthew G Knepley 
6387e87bb0d3SMatthew G Knepley   Level: developer
638861e3bb9bSMatthew G Knepley 
638962a38674SMatthew G. Knepley   Notes:
63903a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
639162a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
63923a93e3b7SToby Isaac 
63933a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
639462a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
63953a93e3b7SToby Isaac 
63963a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
63973a93e3b7SToby Isaac 
639862a38674SMatthew G. Knepley $    const PetscSFNode *cells;
639962a38674SMatthew G. Knepley $    PetscInt           nFound;
6400a6216909SToby Isaac $    const PetscInt    *found;
640162a38674SMatthew G. Knepley $
6402a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
64033a93e3b7SToby Isaac 
64043a93e3b7SToby 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
64053a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
64063a93e3b7SToby Isaac 
640762a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
640861e3bb9bSMatthew G Knepley @*/
640962a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6410e87bb0d3SMatthew G Knepley {
6411735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6412735aa83eSMatthew G Knepley 
6413e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6414e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6415e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6416e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
64173a93e3b7SToby Isaac   if (*cellSF) {
64183a93e3b7SToby Isaac     PetscMPIInt result;
64193a93e3b7SToby Isaac 
6420e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6421a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
64223a93e3b7SToby 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");
6423e0fc9d1bSMatthew G. Knepley   } else {
64243a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
64253a93e3b7SToby Isaac   }
6426b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
642747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
642862a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
642947a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6430e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6431e87bb0d3SMatthew G Knepley }
643214f150ffSMatthew G. Knepley 
6433f4d763aaSMatthew G. Knepley /*@
6434f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6435f4d763aaSMatthew G. Knepley 
64368f700142SStefano Zampini   Collective on dm
64378f700142SStefano Zampini 
6438f4d763aaSMatthew G. Knepley   Input Parameter:
6439f4d763aaSMatthew G. Knepley . dm - The original DM
6440f4d763aaSMatthew G. Knepley 
6441f4d763aaSMatthew G. Knepley   Output Parameter:
6442f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6443f4d763aaSMatthew G. Knepley 
6444f4d763aaSMatthew G. Knepley   Level: intermediate
6445f4d763aaSMatthew G. Knepley 
6446e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6447f4d763aaSMatthew G. Knepley @*/
644814f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
644914f150ffSMatthew G. Knepley {
6450c26acbdeSMatthew G. Knepley   PetscSection   section;
64512d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
645214f150ffSMatthew G. Knepley   PetscErrorCode ierr;
645314f150ffSMatthew G. Knepley 
645414f150ffSMatthew G. Knepley   PetscFunctionBegin;
645514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
645614f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
645792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6458c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6459127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
64602d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6461c26acbdeSMatthew G. Knepley     *odm = dm;
6462c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6463c26acbdeSMatthew G. Knepley   }
646414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6465c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
646614f150ffSMatthew G. Knepley     PetscSF      sf;
646714f150ffSMatthew G. Knepley 
646814f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6469e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
647014f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
647192fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
647214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
647314f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
647415b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6475e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
647614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
647714f150ffSMatthew G. Knepley   }
647814f150ffSMatthew G. Knepley   *odm = dm->dmBC;
647914f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
648014f150ffSMatthew G. Knepley }
6481f4d763aaSMatthew G. Knepley 
6482f4d763aaSMatthew G. Knepley /*@
6483cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6484f4d763aaSMatthew G. Knepley 
6485f4d763aaSMatthew G. Knepley   Input Parameter:
6486f4d763aaSMatthew G. Knepley . dm - The original DM
6487f4d763aaSMatthew G. Knepley 
6488cdb7a50dSMatthew G. Knepley   Output Parameters:
6489cdb7a50dSMatthew G. Knepley + num - The output sequence number
6490cdb7a50dSMatthew G. Knepley - val - The output sequence value
6491f4d763aaSMatthew G. Knepley 
6492f4d763aaSMatthew G. Knepley   Level: intermediate
6493f4d763aaSMatthew G. Knepley 
6494f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6495f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6496f4d763aaSMatthew G. Knepley 
6497f4d763aaSMatthew G. Knepley .seealso: VecView()
6498f4d763aaSMatthew G. Knepley @*/
6499cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6500f4d763aaSMatthew G. Knepley {
6501f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6502f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6503534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6504534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6505f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6506f4d763aaSMatthew G. Knepley }
6507f4d763aaSMatthew G. Knepley 
6508f4d763aaSMatthew G. Knepley /*@
6509cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6510f4d763aaSMatthew G. Knepley 
6511f4d763aaSMatthew G. Knepley   Input Parameters:
6512f4d763aaSMatthew G. Knepley + dm - The original DM
6513cdb7a50dSMatthew G. Knepley . num - The output sequence number
6514cdb7a50dSMatthew G. Knepley - val - The output sequence value
6515f4d763aaSMatthew G. Knepley 
6516f4d763aaSMatthew G. Knepley   Level: intermediate
6517f4d763aaSMatthew G. Knepley 
6518f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6519f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6520f4d763aaSMatthew G. Knepley 
6521f4d763aaSMatthew G. Knepley .seealso: VecView()
6522f4d763aaSMatthew G. Knepley @*/
6523cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6524f4d763aaSMatthew G. Knepley {
6525f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6526f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6527f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6528cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6529cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6530cdb7a50dSMatthew G. Knepley }
6531cdb7a50dSMatthew G. Knepley 
6532cdb7a50dSMatthew G. Knepley /*@C
6533cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6534cdb7a50dSMatthew G. Knepley 
6535cdb7a50dSMatthew G. Knepley   Input Parameters:
6536cdb7a50dSMatthew G. Knepley + dm   - The original DM
6537cdb7a50dSMatthew G. Knepley . name - The sequence name
6538cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6539cdb7a50dSMatthew G. Knepley 
6540cdb7a50dSMatthew G. Knepley   Output Parameter:
6541cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6542cdb7a50dSMatthew G. Knepley 
6543cdb7a50dSMatthew G. Knepley   Level: intermediate
6544cdb7a50dSMatthew G. Knepley 
6545cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6546cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6547cdb7a50dSMatthew G. Knepley 
6548cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6549cdb7a50dSMatthew G. Knepley @*/
6550cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6551cdb7a50dSMatthew G. Knepley {
6552cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6553cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6554cdb7a50dSMatthew G. Knepley 
6555cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6556cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6557cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6558534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6559cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6560cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6561cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6562cdb7a50dSMatthew G. Knepley     PetscScalar value;
6563cdb7a50dSMatthew G. Knepley 
656439d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
65654aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6566cdb7a50dSMatthew G. Knepley #endif
6567cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6568f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6569f4d763aaSMatthew G. Knepley }
65708e4ac7eaSMatthew G. Knepley 
65718e4ac7eaSMatthew G. Knepley /*@
65728e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
65738e4ac7eaSMatthew G. Knepley 
65748e4ac7eaSMatthew G. Knepley   Not collective
65758e4ac7eaSMatthew G. Knepley 
65768e4ac7eaSMatthew G. Knepley   Input Parameter:
65778e4ac7eaSMatthew G. Knepley . dm - The DM
65788e4ac7eaSMatthew G. Knepley 
65798e4ac7eaSMatthew G. Knepley   Output Parameter:
65808e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
65818e4ac7eaSMatthew G. Knepley 
65828e4ac7eaSMatthew G. Knepley   Level: beginner
65838e4ac7eaSMatthew G. Knepley 
65848e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
65858e4ac7eaSMatthew G. Knepley @*/
65868e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
65878e4ac7eaSMatthew G. Knepley {
65888e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65898e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6590534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
65918e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
65928e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
65938e4ac7eaSMatthew G. Knepley }
65948e4ac7eaSMatthew G. Knepley 
65958e4ac7eaSMatthew G. Knepley /*@
65965d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
65978e4ac7eaSMatthew G. Knepley 
65988e4ac7eaSMatthew G. Knepley   Collective on dm
65998e4ac7eaSMatthew G. Knepley 
66008e4ac7eaSMatthew G. Knepley   Input Parameters:
66018e4ac7eaSMatthew G. Knepley + dm - The DM
66028e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
66038e4ac7eaSMatthew G. Knepley 
66045d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
66055d3b26e6SMatthew G. Knepley 
66068e4ac7eaSMatthew G. Knepley   Level: beginner
66078e4ac7eaSMatthew G. Knepley 
66085d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
66098e4ac7eaSMatthew G. Knepley @*/
66108e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
66118e4ac7eaSMatthew G. Knepley {
66128e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66138e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66148833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66158e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66168e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66178e4ac7eaSMatthew G. Knepley }
6618c58f1c22SToby Isaac 
6619c58f1c22SToby Isaac 
6620c58f1c22SToby Isaac /*@C
6621c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6622c58f1c22SToby Isaac 
6623c58f1c22SToby Isaac   Not Collective
6624c58f1c22SToby Isaac 
6625c58f1c22SToby Isaac   Input Parameters:
6626c58f1c22SToby Isaac + dm   - The DM object
6627c58f1c22SToby Isaac - name - The label name
6628c58f1c22SToby Isaac 
6629c58f1c22SToby Isaac   Level: intermediate
6630c58f1c22SToby Isaac 
6631c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6632c58f1c22SToby Isaac @*/
6633c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6634c58f1c22SToby Isaac {
6635c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
6636c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
6637d67d17b1SMatthew G. Knepley   const char    *lname;
6638c58f1c22SToby Isaac   PetscErrorCode ierr;
6639c58f1c22SToby Isaac 
6640c58f1c22SToby Isaac   PetscFunctionBegin;
6641c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6642c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6643c58f1c22SToby Isaac   while (next) {
6644d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6645d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6646c58f1c22SToby Isaac     if (flg) break;
6647c58f1c22SToby Isaac     next = next->next;
6648c58f1c22SToby Isaac   }
6649c58f1c22SToby Isaac   if (!flg) {
6650c58f1c22SToby Isaac     DMLabelLink tmpLabel;
6651c58f1c22SToby Isaac 
6652c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6653d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &tmpLabel->label);CHKERRQ(ierr);
6654c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
6655c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
6656c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
6657c58f1c22SToby Isaac   }
6658c58f1c22SToby Isaac   PetscFunctionReturn(0);
6659c58f1c22SToby Isaac }
6660c58f1c22SToby Isaac 
6661c58f1c22SToby Isaac /*@C
6662c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6663c58f1c22SToby Isaac 
6664c58f1c22SToby Isaac   Not Collective
6665c58f1c22SToby Isaac 
6666c58f1c22SToby Isaac   Input Parameters:
6667c58f1c22SToby Isaac + dm   - The DM object
6668c58f1c22SToby Isaac . name - The label name
6669c58f1c22SToby Isaac - point - The mesh point
6670c58f1c22SToby Isaac 
6671c58f1c22SToby Isaac   Output Parameter:
6672c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6673c58f1c22SToby Isaac 
6674c58f1c22SToby Isaac   Level: beginner
6675c58f1c22SToby Isaac 
6676c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6677c58f1c22SToby Isaac @*/
6678c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6679c58f1c22SToby Isaac {
6680c58f1c22SToby Isaac   DMLabel        label;
6681c58f1c22SToby Isaac   PetscErrorCode ierr;
6682c58f1c22SToby Isaac 
6683c58f1c22SToby Isaac   PetscFunctionBegin;
6684c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6685c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6686c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
668713903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6688c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6689c58f1c22SToby Isaac   PetscFunctionReturn(0);
6690c58f1c22SToby Isaac }
6691c58f1c22SToby Isaac 
6692c58f1c22SToby Isaac /*@C
6693c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6694c58f1c22SToby Isaac 
6695c58f1c22SToby Isaac   Not Collective
6696c58f1c22SToby Isaac 
6697c58f1c22SToby Isaac   Input Parameters:
6698c58f1c22SToby Isaac + dm   - The DM object
6699c58f1c22SToby Isaac . name - The label name
6700c58f1c22SToby Isaac . point - The mesh point
6701c58f1c22SToby Isaac - value - The label value for this point
6702c58f1c22SToby Isaac 
6703c58f1c22SToby Isaac   Output Parameter:
6704c58f1c22SToby Isaac 
6705c58f1c22SToby Isaac   Level: beginner
6706c58f1c22SToby Isaac 
6707c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6708c58f1c22SToby Isaac @*/
6709c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6710c58f1c22SToby Isaac {
6711c58f1c22SToby Isaac   DMLabel        label;
6712c58f1c22SToby Isaac   PetscErrorCode ierr;
6713c58f1c22SToby Isaac 
6714c58f1c22SToby Isaac   PetscFunctionBegin;
6715c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6716c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6717c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6718c58f1c22SToby Isaac   if (!label) {
6719c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6720c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6721c58f1c22SToby Isaac   }
6722c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6723c58f1c22SToby Isaac   PetscFunctionReturn(0);
6724c58f1c22SToby Isaac }
6725c58f1c22SToby Isaac 
6726c58f1c22SToby Isaac /*@C
6727c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6728c58f1c22SToby Isaac 
6729c58f1c22SToby Isaac   Not Collective
6730c58f1c22SToby Isaac 
6731c58f1c22SToby Isaac   Input Parameters:
6732c58f1c22SToby Isaac + dm   - The DM object
6733c58f1c22SToby Isaac . name - The label name
6734c58f1c22SToby Isaac . point - The mesh point
6735c58f1c22SToby Isaac - value - The label value for this point
6736c58f1c22SToby Isaac 
6737c58f1c22SToby Isaac   Output Parameter:
6738c58f1c22SToby Isaac 
6739c58f1c22SToby Isaac   Level: beginner
6740c58f1c22SToby Isaac 
6741c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6742c58f1c22SToby Isaac @*/
6743c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6744c58f1c22SToby Isaac {
6745c58f1c22SToby Isaac   DMLabel        label;
6746c58f1c22SToby Isaac   PetscErrorCode ierr;
6747c58f1c22SToby Isaac 
6748c58f1c22SToby Isaac   PetscFunctionBegin;
6749c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6750c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6751c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6752c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6753c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6754c58f1c22SToby Isaac   PetscFunctionReturn(0);
6755c58f1c22SToby Isaac }
6756c58f1c22SToby Isaac 
6757c58f1c22SToby Isaac /*@C
6758c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6759c58f1c22SToby Isaac 
6760c58f1c22SToby Isaac   Not Collective
6761c58f1c22SToby Isaac 
6762c58f1c22SToby Isaac   Input Parameters:
6763c58f1c22SToby Isaac + dm   - The DM object
6764c58f1c22SToby Isaac - name - The label name
6765c58f1c22SToby Isaac 
6766c58f1c22SToby Isaac   Output Parameter:
6767c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6768c58f1c22SToby Isaac 
6769c58f1c22SToby Isaac   Level: beginner
6770c58f1c22SToby Isaac 
6771df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6772c58f1c22SToby Isaac @*/
6773c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6774c58f1c22SToby Isaac {
6775c58f1c22SToby Isaac   DMLabel        label;
6776c58f1c22SToby Isaac   PetscErrorCode ierr;
6777c58f1c22SToby Isaac 
6778c58f1c22SToby Isaac   PetscFunctionBegin;
6779c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6780c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6781534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
6782c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6783c58f1c22SToby Isaac   *size = 0;
6784c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6785c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6786c58f1c22SToby Isaac   PetscFunctionReturn(0);
6787c58f1c22SToby Isaac }
6788c58f1c22SToby Isaac 
6789c58f1c22SToby Isaac /*@C
6790c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6791c58f1c22SToby Isaac 
6792c58f1c22SToby Isaac   Not Collective
6793c58f1c22SToby Isaac 
6794c58f1c22SToby Isaac   Input Parameters:
6795c58f1c22SToby Isaac + mesh - The DM object
6796c58f1c22SToby Isaac - name - The label name
6797c58f1c22SToby Isaac 
6798c58f1c22SToby Isaac   Output Parameter:
6799c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6800c58f1c22SToby Isaac 
6801c58f1c22SToby Isaac   Level: beginner
6802c58f1c22SToby Isaac 
6803c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6804c58f1c22SToby Isaac @*/
6805c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6806c58f1c22SToby Isaac {
6807c58f1c22SToby Isaac   DMLabel        label;
6808c58f1c22SToby Isaac   PetscErrorCode ierr;
6809c58f1c22SToby Isaac 
6810c58f1c22SToby Isaac   PetscFunctionBegin;
6811c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6812c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6813c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6814c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6815c58f1c22SToby Isaac   *ids = NULL;
6816dab2e251SBlaise Bourdin  if (label) {
6817c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6818dab2e251SBlaise Bourdin   } else {
6819dab2e251SBlaise Bourdin     /* returning an empty IS */
6820dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6821dab2e251SBlaise Bourdin   }
6822c58f1c22SToby Isaac   PetscFunctionReturn(0);
6823c58f1c22SToby Isaac }
6824c58f1c22SToby Isaac 
6825c58f1c22SToby Isaac /*@C
6826c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6827c58f1c22SToby Isaac 
6828c58f1c22SToby Isaac   Not Collective
6829c58f1c22SToby Isaac 
6830c58f1c22SToby Isaac   Input Parameters:
6831c58f1c22SToby Isaac + dm - The DM object
6832c58f1c22SToby Isaac . name - The label name
6833c58f1c22SToby Isaac - value - The stratum value
6834c58f1c22SToby Isaac 
6835c58f1c22SToby Isaac   Output Parameter:
6836c58f1c22SToby Isaac . size - The stratum size
6837c58f1c22SToby Isaac 
6838c58f1c22SToby Isaac   Level: beginner
6839c58f1c22SToby Isaac 
6840c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6841c58f1c22SToby Isaac @*/
6842c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6843c58f1c22SToby Isaac {
6844c58f1c22SToby Isaac   DMLabel        label;
6845c58f1c22SToby Isaac   PetscErrorCode ierr;
6846c58f1c22SToby Isaac 
6847c58f1c22SToby Isaac   PetscFunctionBegin;
6848c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6849c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6850534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
6851c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6852c58f1c22SToby Isaac   *size = 0;
6853c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6854c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6855c58f1c22SToby Isaac   PetscFunctionReturn(0);
6856c58f1c22SToby Isaac }
6857c58f1c22SToby Isaac 
6858c58f1c22SToby Isaac /*@C
6859c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6860c58f1c22SToby Isaac 
6861c58f1c22SToby Isaac   Not Collective
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac   Input Parameters:
6864c58f1c22SToby Isaac + dm - The DM object
6865c58f1c22SToby Isaac . name - The label name
6866c58f1c22SToby Isaac - value - The stratum value
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Output Parameter:
6869c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6870c58f1c22SToby Isaac 
6871c58f1c22SToby Isaac   Level: beginner
6872c58f1c22SToby Isaac 
6873c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6874c58f1c22SToby Isaac @*/
6875c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6876c58f1c22SToby Isaac {
6877c58f1c22SToby Isaac   DMLabel        label;
6878c58f1c22SToby Isaac   PetscErrorCode ierr;
6879c58f1c22SToby Isaac 
6880c58f1c22SToby Isaac   PetscFunctionBegin;
6881c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6882c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6883c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6884c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6885c58f1c22SToby Isaac   *points = NULL;
6886c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6887c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6888c58f1c22SToby Isaac   PetscFunctionReturn(0);
6889c58f1c22SToby Isaac }
6890c58f1c22SToby Isaac 
68914de306b1SToby Isaac /*@C
68929044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68934de306b1SToby Isaac 
68944de306b1SToby Isaac   Not Collective
68954de306b1SToby Isaac 
68964de306b1SToby Isaac   Input Parameters:
68974de306b1SToby Isaac + dm - The DM object
68984de306b1SToby Isaac . name - The label name
68994de306b1SToby Isaac . value - The stratum value
69004de306b1SToby Isaac - points - The stratum points
69014de306b1SToby Isaac 
69024de306b1SToby Isaac   Level: beginner
69034de306b1SToby Isaac 
69044de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
69054de306b1SToby Isaac @*/
69064de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
69074de306b1SToby Isaac {
69084de306b1SToby Isaac   DMLabel        label;
69094de306b1SToby Isaac   PetscErrorCode ierr;
69104de306b1SToby Isaac 
69114de306b1SToby Isaac   PetscFunctionBegin;
69124de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69134de306b1SToby Isaac   PetscValidCharPointer(name, 2);
69144de306b1SToby Isaac   PetscValidPointer(points, 4);
69154de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
69164de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
69174de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
69184de306b1SToby Isaac   PetscFunctionReturn(0);
69194de306b1SToby Isaac }
69204de306b1SToby Isaac 
6921c58f1c22SToby Isaac /*@C
6922c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6923c58f1c22SToby Isaac 
6924c58f1c22SToby Isaac   Not Collective
6925c58f1c22SToby Isaac 
6926c58f1c22SToby Isaac   Input Parameters:
6927c58f1c22SToby Isaac + dm   - The DM object
6928c58f1c22SToby Isaac . name - The label name
6929c58f1c22SToby Isaac - value - The label value for this point
6930c58f1c22SToby Isaac 
6931c58f1c22SToby Isaac   Output Parameter:
6932c58f1c22SToby Isaac 
6933c58f1c22SToby Isaac   Level: beginner
6934c58f1c22SToby Isaac 
6935c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6936c58f1c22SToby Isaac @*/
6937c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6938c58f1c22SToby Isaac {
6939c58f1c22SToby Isaac   DMLabel        label;
6940c58f1c22SToby Isaac   PetscErrorCode ierr;
6941c58f1c22SToby Isaac 
6942c58f1c22SToby Isaac   PetscFunctionBegin;
6943c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6944c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6945c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6946c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6947c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6948c58f1c22SToby Isaac   PetscFunctionReturn(0);
6949c58f1c22SToby Isaac }
6950c58f1c22SToby Isaac 
6951c58f1c22SToby Isaac /*@
6952c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6953c58f1c22SToby Isaac 
6954c58f1c22SToby Isaac   Not Collective
6955c58f1c22SToby Isaac 
6956c58f1c22SToby Isaac   Input Parameter:
6957c58f1c22SToby Isaac . dm   - The DM object
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Output Parameter:
6960c58f1c22SToby Isaac . numLabels - the number of Labels
6961c58f1c22SToby Isaac 
6962c58f1c22SToby Isaac   Level: intermediate
6963c58f1c22SToby Isaac 
6964c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6965c58f1c22SToby Isaac @*/
6966c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6967c58f1c22SToby Isaac {
6968c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6969c58f1c22SToby Isaac   PetscInt  n    = 0;
6970c58f1c22SToby Isaac 
6971c58f1c22SToby Isaac   PetscFunctionBegin;
6972c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6973534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
6974c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6975c58f1c22SToby Isaac   *numLabels = n;
6976c58f1c22SToby Isaac   PetscFunctionReturn(0);
6977c58f1c22SToby Isaac }
6978c58f1c22SToby Isaac 
6979c58f1c22SToby Isaac /*@C
6980c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6981c58f1c22SToby Isaac 
6982c58f1c22SToby Isaac   Not Collective
6983c58f1c22SToby Isaac 
6984c58f1c22SToby Isaac   Input Parameters:
6985c58f1c22SToby Isaac + dm - The DM object
6986c58f1c22SToby Isaac - n  - the label number
6987c58f1c22SToby Isaac 
6988c58f1c22SToby Isaac   Output Parameter:
6989c58f1c22SToby Isaac . name - the label name
6990c58f1c22SToby Isaac 
6991c58f1c22SToby Isaac   Level: intermediate
6992c58f1c22SToby Isaac 
6993c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6994c58f1c22SToby Isaac @*/
6995c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6996c58f1c22SToby Isaac {
6997c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6998c58f1c22SToby Isaac   PetscInt       l    = 0;
6999d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7000c58f1c22SToby Isaac 
7001c58f1c22SToby Isaac   PetscFunctionBegin;
7002c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7003c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7004c58f1c22SToby Isaac   while (next) {
7005c58f1c22SToby Isaac     if (l == n) {
7006d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7007c58f1c22SToby Isaac       PetscFunctionReturn(0);
7008c58f1c22SToby Isaac     }
7009c58f1c22SToby Isaac     ++l;
7010c58f1c22SToby Isaac     next = next->next;
7011c58f1c22SToby Isaac   }
7012c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7013c58f1c22SToby Isaac }
7014c58f1c22SToby Isaac 
7015c58f1c22SToby Isaac /*@C
7016c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7017c58f1c22SToby Isaac 
7018c58f1c22SToby Isaac   Not Collective
7019c58f1c22SToby Isaac 
7020c58f1c22SToby Isaac   Input Parameters:
7021c58f1c22SToby Isaac + dm   - The DM object
7022c58f1c22SToby Isaac - name - The label name
7023c58f1c22SToby Isaac 
7024c58f1c22SToby Isaac   Output Parameter:
7025c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7026c58f1c22SToby Isaac 
7027c58f1c22SToby Isaac   Level: intermediate
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7030c58f1c22SToby Isaac @*/
7031c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7032c58f1c22SToby Isaac {
7033c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7034d67d17b1SMatthew G. Knepley   const char    *lname;
7035c58f1c22SToby Isaac   PetscErrorCode ierr;
7036c58f1c22SToby Isaac 
7037c58f1c22SToby Isaac   PetscFunctionBegin;
7038c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7039c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7040534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7041c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7042c58f1c22SToby Isaac   while (next) {
7043d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7044d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7045c58f1c22SToby Isaac     if (*hasLabel) break;
7046c58f1c22SToby Isaac     next = next->next;
7047c58f1c22SToby Isaac   }
7048c58f1c22SToby Isaac   PetscFunctionReturn(0);
7049c58f1c22SToby Isaac }
7050c58f1c22SToby Isaac 
7051c58f1c22SToby Isaac /*@C
7052c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7053c58f1c22SToby Isaac 
7054c58f1c22SToby Isaac   Not Collective
7055c58f1c22SToby Isaac 
7056c58f1c22SToby Isaac   Input Parameters:
7057c58f1c22SToby Isaac + dm   - The DM object
7058c58f1c22SToby Isaac - name - The label name
7059c58f1c22SToby Isaac 
7060c58f1c22SToby Isaac   Output Parameter:
7061c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7062c58f1c22SToby Isaac 
7063c58f1c22SToby Isaac   Level: intermediate
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7066c58f1c22SToby Isaac @*/
7067c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7068c58f1c22SToby Isaac {
7069c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7070c58f1c22SToby Isaac   PetscBool      hasLabel;
7071d67d17b1SMatthew G. Knepley   const char    *lname;
7072c58f1c22SToby Isaac   PetscErrorCode ierr;
7073c58f1c22SToby Isaac 
7074c58f1c22SToby Isaac   PetscFunctionBegin;
7075c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7076c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7077c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7078c58f1c22SToby Isaac   *label = NULL;
7079c58f1c22SToby Isaac   while (next) {
7080d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7081d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7082c58f1c22SToby Isaac     if (hasLabel) {
7083c58f1c22SToby Isaac       *label = next->label;
7084c58f1c22SToby Isaac       break;
7085c58f1c22SToby Isaac     }
7086c58f1c22SToby Isaac     next = next->next;
7087c58f1c22SToby Isaac   }
7088c58f1c22SToby Isaac   PetscFunctionReturn(0);
7089c58f1c22SToby Isaac }
7090c58f1c22SToby Isaac 
7091c58f1c22SToby Isaac /*@C
7092c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7093c58f1c22SToby Isaac 
7094c58f1c22SToby Isaac   Not Collective
7095c58f1c22SToby Isaac 
7096c58f1c22SToby Isaac   Input Parameters:
7097c58f1c22SToby Isaac + dm - The DM object
7098c58f1c22SToby Isaac - n  - the label number
7099c58f1c22SToby Isaac 
7100c58f1c22SToby Isaac   Output Parameter:
7101c58f1c22SToby Isaac . label - the label
7102c58f1c22SToby Isaac 
7103c58f1c22SToby Isaac   Level: intermediate
7104c58f1c22SToby Isaac 
7105c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7106c58f1c22SToby Isaac @*/
7107c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7108c58f1c22SToby Isaac {
7109c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
7110c58f1c22SToby Isaac   PetscInt    l    = 0;
7111c58f1c22SToby Isaac 
7112c58f1c22SToby Isaac   PetscFunctionBegin;
7113c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7114c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7115c58f1c22SToby Isaac   while (next) {
7116c58f1c22SToby Isaac     if (l == n) {
7117c58f1c22SToby Isaac       *label = next->label;
7118c58f1c22SToby Isaac       PetscFunctionReturn(0);
7119c58f1c22SToby Isaac     }
7120c58f1c22SToby Isaac     ++l;
7121c58f1c22SToby Isaac     next = next->next;
7122c58f1c22SToby Isaac   }
7123c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7124c58f1c22SToby Isaac }
7125c58f1c22SToby Isaac 
7126c58f1c22SToby Isaac /*@C
7127c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7128c58f1c22SToby Isaac 
7129c58f1c22SToby Isaac   Not Collective
7130c58f1c22SToby Isaac 
7131c58f1c22SToby Isaac   Input Parameters:
7132c58f1c22SToby Isaac + dm   - The DM object
7133c58f1c22SToby Isaac - label - The DMLabel
7134c58f1c22SToby Isaac 
7135c58f1c22SToby Isaac   Level: developer
7136c58f1c22SToby Isaac 
7137c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7138c58f1c22SToby Isaac @*/
7139c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7140c58f1c22SToby Isaac {
7141c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
7142c58f1c22SToby Isaac   PetscBool      hasLabel;
7143d67d17b1SMatthew G. Knepley   const char    *lname;
7144c58f1c22SToby Isaac   PetscErrorCode ierr;
7145c58f1c22SToby Isaac 
7146c58f1c22SToby Isaac   PetscFunctionBegin;
7147c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7148d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7149d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7150d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7151c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7152c58f1c22SToby Isaac   tmpLabel->label  = label;
7153c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
7154c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
7155c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
715608f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
7157c58f1c22SToby Isaac   PetscFunctionReturn(0);
7158c58f1c22SToby Isaac }
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac /*@C
7161e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
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 
7169c58f1c22SToby Isaac   Output Parameter:
7170c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7171c58f1c22SToby Isaac 
7172c58f1c22SToby Isaac   Level: developer
7173c58f1c22SToby Isaac 
7174e5472504SVaclav Hapla   Notes:
7175e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7176e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7177e5472504SVaclav Hapla 
7178e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7179e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7180e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7181e5472504SVaclav Hapla 
7182e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7183c58f1c22SToby Isaac @*/
7184c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7185c58f1c22SToby Isaac {
718695d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7187c58f1c22SToby Isaac   PetscBool      hasLabel;
7188d67d17b1SMatthew G. Knepley   const char    *lname;
7189c58f1c22SToby Isaac   PetscErrorCode ierr;
7190c58f1c22SToby Isaac 
7191c58f1c22SToby Isaac   PetscFunctionBegin;
7192c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7193e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7194e5472504SVaclav Hapla   if (label) {
7195e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7196c58f1c22SToby Isaac     *label = NULL;
7197e5472504SVaclav Hapla   }
719895d578d6SVaclav Hapla   for (pnext=&dm->labels->next; (link=*pnext); pnext=&link->next) {
719995d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7200d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7201c58f1c22SToby Isaac     if (hasLabel) {
720295d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7203c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
720495d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
720595d578d6SVaclav Hapla       if (label) *label = link->label;
720695d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
720795d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7208c58f1c22SToby Isaac       break;
7209c58f1c22SToby Isaac     }
7210c58f1c22SToby Isaac   }
7211c58f1c22SToby Isaac   PetscFunctionReturn(0);
7212c58f1c22SToby Isaac }
7213c58f1c22SToby Isaac 
7214306894acSVaclav Hapla /*@
7215306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7216306894acSVaclav Hapla 
7217306894acSVaclav Hapla   Not Collective
7218306894acSVaclav Hapla 
7219306894acSVaclav Hapla   Input Parameters:
7220306894acSVaclav Hapla + dm   - The DM object
7221306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7222306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7223306894acSVaclav Hapla 
7224306894acSVaclav Hapla   Level: developer
7225306894acSVaclav Hapla 
7226306894acSVaclav Hapla   Notes:
7227306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7228306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7229306894acSVaclav Hapla   *label nullified.
7230306894acSVaclav Hapla 
7231306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7232306894acSVaclav Hapla @*/
7233306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7234306894acSVaclav Hapla {
723543e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7236306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7237306894acSVaclav Hapla   PetscErrorCode ierr;
7238306894acSVaclav Hapla 
7239306894acSVaclav Hapla   PetscFunctionBegin;
7240306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7241306894acSVaclav Hapla   PetscValidPointer(label, 2);
7242f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7243306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7244306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
724543e45a93SVaclav Hapla   for (pnext=&dm->labels->next; (link=*pnext); pnext=&link->next) {
724643e45a93SVaclav Hapla     if (*label == link->label) {
7247306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
724843e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7249306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
725043e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
725143e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
725243e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7253306894acSVaclav Hapla       break;
7254306894acSVaclav Hapla     }
7255306894acSVaclav Hapla   }
7256306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7257306894acSVaclav Hapla   PetscFunctionReturn(0);
7258306894acSVaclav Hapla }
7259306894acSVaclav Hapla 
7260c58f1c22SToby Isaac /*@C
7261c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7262c58f1c22SToby Isaac 
7263c58f1c22SToby Isaac   Not Collective
7264c58f1c22SToby Isaac 
7265c58f1c22SToby Isaac   Input Parameters:
7266c58f1c22SToby Isaac + dm   - The DM object
7267c58f1c22SToby Isaac - name - The label name
7268c58f1c22SToby Isaac 
7269c58f1c22SToby Isaac   Output Parameter:
7270c58f1c22SToby Isaac . output - The flag for output
7271c58f1c22SToby Isaac 
7272c58f1c22SToby Isaac   Level: developer
7273c58f1c22SToby Isaac 
7274c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7275c58f1c22SToby Isaac @*/
7276c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7277c58f1c22SToby Isaac {
7278c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7279d67d17b1SMatthew G. Knepley   const char    *lname;
7280c58f1c22SToby Isaac   PetscErrorCode ierr;
7281c58f1c22SToby Isaac 
7282c58f1c22SToby Isaac   PetscFunctionBegin;
7283c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7284c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7285c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7286c58f1c22SToby Isaac   while (next) {
7287c58f1c22SToby Isaac     PetscBool flg;
7288c58f1c22SToby Isaac 
7289d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7290d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7291c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7292c58f1c22SToby Isaac     next = next->next;
7293c58f1c22SToby Isaac   }
7294c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7295c58f1c22SToby Isaac }
7296c58f1c22SToby Isaac 
7297c58f1c22SToby Isaac /*@C
7298c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7299c58f1c22SToby Isaac 
7300c58f1c22SToby Isaac   Not Collective
7301c58f1c22SToby Isaac 
7302c58f1c22SToby Isaac   Input Parameters:
7303c58f1c22SToby Isaac + dm     - The DM object
7304c58f1c22SToby Isaac . name   - The label name
7305c58f1c22SToby Isaac - output - The flag for output
7306c58f1c22SToby Isaac 
7307c58f1c22SToby Isaac   Level: developer
7308c58f1c22SToby Isaac 
7309c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7310c58f1c22SToby Isaac @*/
7311c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7312c58f1c22SToby Isaac {
7313c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
7314d67d17b1SMatthew G. Knepley   const char    *lname;
7315c58f1c22SToby Isaac   PetscErrorCode ierr;
7316c58f1c22SToby Isaac 
7317c58f1c22SToby Isaac   PetscFunctionBegin;
7318c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7319534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7320c58f1c22SToby Isaac   while (next) {
7321c58f1c22SToby Isaac     PetscBool flg;
7322c58f1c22SToby Isaac 
7323d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7324d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7325c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7326c58f1c22SToby Isaac     next = next->next;
7327c58f1c22SToby Isaac   }
7328c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7329c58f1c22SToby Isaac }
7330c58f1c22SToby Isaac 
7331c58f1c22SToby Isaac 
7332c58f1c22SToby Isaac /*@
7333c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7334c58f1c22SToby Isaac 
7335d083f849SBarry Smith   Collective on dmA
7336c58f1c22SToby Isaac 
7337c58f1c22SToby Isaac   Input Parameter:
73382e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
7339c58f1c22SToby Isaac 
7340c58f1c22SToby Isaac   Output Parameter:
73412e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
7342c58f1c22SToby Isaac 
7343c58f1c22SToby Isaac   Level: intermediate
7344c58f1c22SToby Isaac 
7345c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7346c58f1c22SToby Isaac 
7347367003a6SStefano Zampini .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
7348c58f1c22SToby Isaac @*/
7349c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
7350c58f1c22SToby Isaac {
7351c58f1c22SToby Isaac   PetscInt       numLabels, l;
7352c58f1c22SToby Isaac   PetscErrorCode ierr;
7353c58f1c22SToby Isaac 
7354c58f1c22SToby Isaac   PetscFunctionBegin;
7355c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
7356c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
7357c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
7358c58f1c22SToby Isaac     DMLabel     label, labelNew;
7359c58f1c22SToby Isaac     const char *name;
7360c58f1c22SToby Isaac     PetscBool   flg;
7361c58f1c22SToby Isaac 
7362c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
7363c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7364c58f1c22SToby Isaac     if (flg) continue;
73657d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
73667d5acc75SStefano Zampini     if (flg) continue;
7367c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
7368c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
7369c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
737008f633c4SVaclav Hapla     ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);
7371c58f1c22SToby Isaac   }
7372c58f1c22SToby Isaac   PetscFunctionReturn(0);
7373c58f1c22SToby Isaac }
7374a8fb8f29SToby Isaac 
7375a8fb8f29SToby Isaac /*@
7376a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7377a8fb8f29SToby Isaac 
7378a8fb8f29SToby Isaac   Input Parameter:
7379a8fb8f29SToby Isaac . dm - The DM object
7380a8fb8f29SToby Isaac 
7381a8fb8f29SToby Isaac   Output Parameter:
7382a8fb8f29SToby Isaac . cdm - The coarse DM
7383a8fb8f29SToby Isaac 
7384a8fb8f29SToby Isaac   Level: intermediate
7385a8fb8f29SToby Isaac 
7386a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7387a8fb8f29SToby Isaac @*/
7388a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7389a8fb8f29SToby Isaac {
7390a8fb8f29SToby Isaac   PetscFunctionBegin;
7391a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7392a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7393a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7394a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7395a8fb8f29SToby Isaac }
7396a8fb8f29SToby Isaac 
7397a8fb8f29SToby Isaac /*@
7398a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7399a8fb8f29SToby Isaac 
7400a8fb8f29SToby Isaac   Input Parameters:
7401a8fb8f29SToby Isaac + dm - The DM object
7402a8fb8f29SToby Isaac - cdm - The coarse DM
7403a8fb8f29SToby Isaac 
7404a8fb8f29SToby Isaac   Level: intermediate
7405a8fb8f29SToby Isaac 
7406a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7407a8fb8f29SToby Isaac @*/
7408a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7409a8fb8f29SToby Isaac {
7410a8fb8f29SToby Isaac   PetscErrorCode ierr;
7411a8fb8f29SToby Isaac 
7412a8fb8f29SToby Isaac   PetscFunctionBegin;
7413a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7414a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7415a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7416a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7417a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7418a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7419a8fb8f29SToby Isaac }
7420a8fb8f29SToby Isaac 
742188bdff64SToby Isaac /*@
742288bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
742388bdff64SToby Isaac 
742488bdff64SToby Isaac   Input Parameter:
742588bdff64SToby Isaac . dm - The DM object
742688bdff64SToby Isaac 
742788bdff64SToby Isaac   Output Parameter:
742888bdff64SToby Isaac . fdm - The fine DM
742988bdff64SToby Isaac 
743088bdff64SToby Isaac   Level: intermediate
743188bdff64SToby Isaac 
743288bdff64SToby Isaac .seealso: DMSetFineDM()
743388bdff64SToby Isaac @*/
743488bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
743588bdff64SToby Isaac {
743688bdff64SToby Isaac   PetscFunctionBegin;
743788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
743888bdff64SToby Isaac   PetscValidPointer(fdm, 2);
743988bdff64SToby Isaac   *fdm = dm->fineMesh;
744088bdff64SToby Isaac   PetscFunctionReturn(0);
744188bdff64SToby Isaac }
744288bdff64SToby Isaac 
744388bdff64SToby Isaac /*@
744488bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
744588bdff64SToby Isaac 
744688bdff64SToby Isaac   Input Parameters:
744788bdff64SToby Isaac + dm - The DM object
744888bdff64SToby Isaac - fdm - The fine DM
744988bdff64SToby Isaac 
745088bdff64SToby Isaac   Level: intermediate
745188bdff64SToby Isaac 
745288bdff64SToby Isaac .seealso: DMGetFineDM()
745388bdff64SToby Isaac @*/
745488bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
745588bdff64SToby Isaac {
745688bdff64SToby Isaac   PetscErrorCode ierr;
745788bdff64SToby Isaac 
745888bdff64SToby Isaac   PetscFunctionBegin;
745988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
746088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
746188bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
746288bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
746388bdff64SToby Isaac   dm->fineMesh = fdm;
746488bdff64SToby Isaac   PetscFunctionReturn(0);
746588bdff64SToby Isaac }
746688bdff64SToby Isaac 
7467a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7468a6ba4734SToby Isaac 
7469a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7470a6ba4734SToby Isaac {
7471e5e52638SMatthew G. Knepley   PetscInt       d;
7472a6ba4734SToby Isaac   PetscErrorCode ierr;
7473a6ba4734SToby Isaac 
7474a6ba4734SToby Isaac   PetscFunctionBegin;
7475e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7476e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7477e5e52638SMatthew G. Knepley   }
7478a6ba4734SToby Isaac   PetscFunctionReturn(0);
7479a6ba4734SToby Isaac }
7480a6ba4734SToby Isaac 
7481a6ba4734SToby Isaac /*@C
7482a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7483a6ba4734SToby Isaac 
7484a6ba4734SToby Isaac   Input Parameters:
74854c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7486f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7487a6ba4734SToby Isaac . name        - The BC name
7488a6ba4734SToby Isaac . labelname   - The label defining constrained points
7489a6ba4734SToby Isaac . field       - The field to constrain
7490e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7491a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7492a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7493a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7494a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7495a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7496a6ba4734SToby Isaac 
7497a6ba4734SToby Isaac   Options Database Keys:
7498a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7499a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7500a6ba4734SToby Isaac 
7501a6ba4734SToby Isaac   Level: developer
7502a6ba4734SToby Isaac 
7503a6ba4734SToby Isaac .seealso: DMGetBoundary()
7504a6ba4734SToby Isaac @*/
7505a30ec4eaSSatish 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)
7506a6ba4734SToby Isaac {
7507e5e52638SMatthew G. Knepley   PetscDS        ds;
7508a6ba4734SToby Isaac   PetscErrorCode ierr;
7509a6ba4734SToby Isaac 
7510a6ba4734SToby Isaac   PetscFunctionBegin;
7511a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7512e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7513e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7514a6ba4734SToby Isaac   PetscFunctionReturn(0);
7515a6ba4734SToby Isaac }
7516a6ba4734SToby Isaac 
7517a6ba4734SToby Isaac /*@
7518a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7519a6ba4734SToby Isaac 
7520a6ba4734SToby Isaac   Input Parameters:
7521a6ba4734SToby Isaac . dm - The mesh object
7522a6ba4734SToby Isaac 
7523a6ba4734SToby Isaac   Output Parameters:
7524a6ba4734SToby Isaac . numBd - The number of BC
7525a6ba4734SToby Isaac 
7526a6ba4734SToby Isaac   Level: intermediate
7527a6ba4734SToby Isaac 
7528a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7529a6ba4734SToby Isaac @*/
7530a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7531a6ba4734SToby Isaac {
7532e5e52638SMatthew G. Knepley   PetscDS        ds;
753358ebd649SToby Isaac   PetscErrorCode ierr;
7534a6ba4734SToby Isaac 
7535a6ba4734SToby Isaac   PetscFunctionBegin;
7536a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7537e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7538e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7539a6ba4734SToby Isaac   PetscFunctionReturn(0);
7540a6ba4734SToby Isaac }
7541a6ba4734SToby Isaac 
7542a6ba4734SToby Isaac /*@C
75431c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7544a6ba4734SToby Isaac 
7545a6ba4734SToby Isaac   Input Parameters:
7546a6ba4734SToby Isaac + dm          - The mesh object
7547a6ba4734SToby Isaac - bd          - The BC number
7548a6ba4734SToby Isaac 
7549a6ba4734SToby Isaac   Output Parameters:
7550f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7551a6ba4734SToby Isaac . name        - The BC name
7552a6ba4734SToby Isaac . labelname   - The label defining constrained points
7553a6ba4734SToby Isaac . field       - The field to constrain
7554a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7555a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7556a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7557a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7558a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7559a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7560a6ba4734SToby Isaac 
7561a6ba4734SToby Isaac   Options Database Keys:
7562a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7563a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7564a6ba4734SToby Isaac 
7565a6ba4734SToby Isaac   Level: developer
7566a6ba4734SToby Isaac 
7567a6ba4734SToby Isaac .seealso: DMAddBoundary()
7568a6ba4734SToby Isaac @*/
7569a30ec4eaSSatish 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)
7570a6ba4734SToby Isaac {
7571e5e52638SMatthew G. Knepley   PetscDS        ds;
757258ebd649SToby Isaac   PetscErrorCode ierr;
7573a6ba4734SToby Isaac 
7574a6ba4734SToby Isaac   PetscFunctionBegin;
7575a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7576e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7577e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7578a6ba4734SToby Isaac   PetscFunctionReturn(0);
7579a6ba4734SToby Isaac }
7580a6ba4734SToby Isaac 
7581e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7582e6f8dbb6SToby Isaac {
7583e5e52638SMatthew G. Knepley   PetscDS        ds;
7584dff059c6SToby Isaac   DMBoundary    *lastnext;
7585e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7586e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7587e6f8dbb6SToby Isaac 
7588e6f8dbb6SToby Isaac   PetscFunctionBegin;
7589e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7590e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
759147a1f5adSToby Isaac   if (dm->boundary) {
759247a1f5adSToby Isaac     DMBoundary next = dm->boundary;
759347a1f5adSToby Isaac 
759447a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
759547a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
759647a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
759747a1f5adSToby Isaac     while (next) {
759847a1f5adSToby Isaac       DMBoundary b = next;
759947a1f5adSToby Isaac 
760047a1f5adSToby Isaac       next = b->next;
760147a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7602a6ba4734SToby Isaac     }
760347a1f5adSToby Isaac     dm->boundary = NULL;
7604a6ba4734SToby Isaac   }
760547a1f5adSToby Isaac 
7606dff059c6SToby Isaac   lastnext = &(dm->boundary);
7607e6f8dbb6SToby Isaac   while (dsbound) {
7608e6f8dbb6SToby Isaac     DMBoundary dmbound;
7609e6f8dbb6SToby Isaac 
7610e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7611e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7612e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7613994fe344SLisandro 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);}
761447a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7615dff059c6SToby Isaac     *lastnext = dmbound;
7616dff059c6SToby Isaac     lastnext = &(dmbound->next);
7617dff059c6SToby Isaac     dsbound = dsbound->next;
7618a6ba4734SToby Isaac   }
7619a6ba4734SToby Isaac   PetscFunctionReturn(0);
7620a6ba4734SToby Isaac }
7621a6ba4734SToby Isaac 
7622a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7623a6ba4734SToby Isaac {
7624b95f2879SToby Isaac   DMBoundary     b;
7625a6ba4734SToby Isaac   PetscErrorCode ierr;
7626a6ba4734SToby Isaac 
7627a6ba4734SToby Isaac   PetscFunctionBegin;
7628a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7629534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7630a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7631e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7632b95f2879SToby Isaac   b = dm->boundary;
7633a6ba4734SToby Isaac   while (b && !(*isBd)) {
7634e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7635e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
76363424c85cSToby Isaac 
76373424c85cSToby Isaac     if (label) {
7638a6ba4734SToby Isaac       PetscInt i;
7639a6ba4734SToby Isaac 
7640e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7641e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7642a6ba4734SToby Isaac       }
7643a6ba4734SToby Isaac     }
7644a6ba4734SToby Isaac     b = b->next;
7645a6ba4734SToby Isaac   }
7646a6ba4734SToby Isaac   PetscFunctionReturn(0);
7647a6ba4734SToby Isaac }
76484d6f44ffSToby Isaac 
76494d6f44ffSToby Isaac /*@C
76504d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
76514d6f44ffSToby Isaac 
76524d6f44ffSToby Isaac   Input Parameters:
76534d6f44ffSToby Isaac + dm      - The DM
76540709b2feSToby Isaac . time    - The time
76554d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
76564d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
76574d6f44ffSToby Isaac - mode    - The insertion mode for values
76584d6f44ffSToby Isaac 
76594d6f44ffSToby Isaac   Output Parameter:
76604d6f44ffSToby Isaac . X - vector
76614d6f44ffSToby Isaac 
76624d6f44ffSToby Isaac    Calling sequence of func:
76630709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
76644d6f44ffSToby Isaac 
76654d6f44ffSToby Isaac +  dim - The spatial dimension
76664d6f44ffSToby Isaac .  x   - The coordinates
76674d6f44ffSToby Isaac .  Nf  - The number of fields
76684d6f44ffSToby Isaac .  u   - The output field values
76694d6f44ffSToby Isaac -  ctx - optional user-defined function context
76704d6f44ffSToby Isaac 
76714d6f44ffSToby Isaac   Level: developer
76724d6f44ffSToby Isaac 
76732716604bSToby Isaac .seealso: DMComputeL2Diff()
76744d6f44ffSToby Isaac @*/
76750709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
76764d6f44ffSToby Isaac {
76774d6f44ffSToby Isaac   Vec            localX;
76784d6f44ffSToby Isaac   PetscErrorCode ierr;
76794d6f44ffSToby Isaac 
76804d6f44ffSToby Isaac   PetscFunctionBegin;
76814d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76824d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
76830709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
76844d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
76854d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
76864d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
76874d6f44ffSToby Isaac   PetscFunctionReturn(0);
76884d6f44ffSToby Isaac }
76894d6f44ffSToby Isaac 
76900709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
76914d6f44ffSToby Isaac {
76924d6f44ffSToby Isaac   PetscErrorCode ierr;
76934d6f44ffSToby Isaac 
76944d6f44ffSToby Isaac   PetscFunctionBegin;
76954d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
76964d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
76970918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
76980709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
76994d6f44ffSToby Isaac   PetscFunctionReturn(0);
77004d6f44ffSToby Isaac }
77014d6f44ffSToby Isaac 
77022c53366bSMatthew 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)
77032c53366bSMatthew G. Knepley {
77042c53366bSMatthew G. Knepley   Vec            localX;
77052c53366bSMatthew G. Knepley   PetscErrorCode ierr;
77062c53366bSMatthew G. Knepley 
77072c53366bSMatthew G. Knepley   PetscFunctionBegin;
77082c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77092c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
77102c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77112c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
77122c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
77132c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
77142c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
77152c53366bSMatthew G. Knepley }
77162c53366bSMatthew G. Knepley 
77171c531cf8SMatthew 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)
77184d6f44ffSToby Isaac {
77194d6f44ffSToby Isaac   PetscErrorCode ierr;
77204d6f44ffSToby Isaac 
77214d6f44ffSToby Isaac   PetscFunctionBegin;
77224d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77234d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
77240918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
77251c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77264d6f44ffSToby Isaac   PetscFunctionReturn(0);
77274d6f44ffSToby Isaac }
77282716604bSToby Isaac 
77298c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
77308c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
77318c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
77328c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7733191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
77348c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
77358c6c5593SMatthew G. Knepley {
77368c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
77378c6c5593SMatthew G. Knepley 
77388c6c5593SMatthew G. Knepley   PetscFunctionBegin;
77398c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77408c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
77418c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
77420918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
77438c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
77448c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
77458c6c5593SMatthew G. Knepley }
77468c6c5593SMatthew G. Knepley 
77471c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
77488c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
77498c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
77508c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7751191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
77528c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
77538c6c5593SMatthew G. Knepley {
77548c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
77558c6c5593SMatthew G. Knepley 
77568c6c5593SMatthew G. Knepley   PetscFunctionBegin;
77578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77588c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
77598c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
77600918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
77611c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
77628c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
77638c6c5593SMatthew G. Knepley }
77648c6c5593SMatthew G. Knepley 
77652716604bSToby Isaac /*@C
77662716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
77672716604bSToby Isaac 
77682716604bSToby Isaac   Input Parameters:
77692716604bSToby Isaac + dm    - The DM
77700709b2feSToby Isaac . time  - The time
77712716604bSToby Isaac . funcs - The functions to evaluate for each field component
77722716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7773574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
77742716604bSToby Isaac 
77752716604bSToby Isaac   Output Parameter:
77762716604bSToby Isaac . diff - The diff ||u - u_h||_2
77772716604bSToby Isaac 
77782716604bSToby Isaac   Level: developer
77792716604bSToby Isaac 
77801189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
77812716604bSToby Isaac @*/
77820709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
77832716604bSToby Isaac {
77842716604bSToby Isaac   PetscErrorCode ierr;
77852716604bSToby Isaac 
77862716604bSToby Isaac   PetscFunctionBegin;
77872716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7788b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
77890918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
77900709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
77912716604bSToby Isaac   PetscFunctionReturn(0);
77922716604bSToby Isaac }
7793b698f381SToby Isaac 
7794b698f381SToby Isaac /*@C
7795b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
7796b698f381SToby Isaac 
7797d083f849SBarry Smith   Collective on dm
7798d083f849SBarry Smith 
7799b698f381SToby Isaac   Input Parameters:
7800b698f381SToby Isaac + dm    - The DM
7801b698f381SToby Isaac , time  - The time
7802b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
7803b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7804574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
7805b698f381SToby Isaac - n     - The vector to project along
7806b698f381SToby Isaac 
7807b698f381SToby Isaac   Output Parameter:
7808b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
7809b698f381SToby Isaac 
7810b698f381SToby Isaac   Level: developer
7811b698f381SToby Isaac 
7812b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
7813b698f381SToby Isaac @*/
7814b698f381SToby 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)
7815b698f381SToby Isaac {
7816b698f381SToby Isaac   PetscErrorCode ierr;
7817b698f381SToby Isaac 
7818b698f381SToby Isaac   PetscFunctionBegin;
7819b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7820b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7821b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
7822b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
7823b698f381SToby Isaac   PetscFunctionReturn(0);
7824b698f381SToby Isaac }
7825b698f381SToby Isaac 
78262a16baeaSToby Isaac /*@C
78272a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
78282a16baeaSToby Isaac 
7829d083f849SBarry Smith   Collective on dm
7830d083f849SBarry Smith 
78312a16baeaSToby Isaac   Input Parameters:
78322a16baeaSToby Isaac + dm    - The DM
78332a16baeaSToby Isaac . time  - The time
78342a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
78352a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7836574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
78372a16baeaSToby Isaac 
78382a16baeaSToby Isaac   Output Parameter:
78392a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
78402a16baeaSToby Isaac 
78412a16baeaSToby Isaac   Level: developer
78422a16baeaSToby Isaac 
78431189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
78442a16baeaSToby Isaac @*/
78451189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
78462a16baeaSToby Isaac {
78472a16baeaSToby Isaac   PetscErrorCode ierr;
78482a16baeaSToby Isaac 
78492a16baeaSToby Isaac   PetscFunctionBegin;
78502a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78512a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
78520918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
78532a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
78542a16baeaSToby Isaac   PetscFunctionReturn(0);
78552a16baeaSToby Isaac }
78562a16baeaSToby Isaac 
7857df0b854cSToby Isaac /*@C
7858df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
7859cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
7860df0b854cSToby Isaac 
7861df0b854cSToby Isaac   Collective on dm
7862df0b854cSToby Isaac 
7863df0b854cSToby Isaac   Input parameters:
7864df0b854cSToby Isaac + dm - the pre-adaptation DM object
7865a1b0c543SToby Isaac - label - label with the flags
7866df0b854cSToby Isaac 
7867df0b854cSToby Isaac   Output parameters:
78680d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
7869df0b854cSToby Isaac 
7870df0b854cSToby Isaac   Level: intermediate
78710d1cd5e0SMatthew G. Knepley 
78720d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
7873df0b854cSToby Isaac @*/
78740d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
7875df0b854cSToby Isaac {
7876df0b854cSToby Isaac   PetscErrorCode ierr;
7877df0b854cSToby Isaac 
7878df0b854cSToby Isaac   PetscFunctionBegin;
7879df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7880a1b0c543SToby Isaac   PetscValidPointer(label,2);
78810d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
78820d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
78836f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
78840d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
78850d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
78860d1cd5e0SMatthew G. Knepley }
78870d1cd5e0SMatthew G. Knepley 
78880d1cd5e0SMatthew G. Knepley /*@C
78890d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
78900d1cd5e0SMatthew G. Knepley 
78910d1cd5e0SMatthew G. Knepley   Input Parameters:
78920d1cd5e0SMatthew G. Knepley + dm - The DM object
78930d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
78946f25b0d8SLisandro 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_".
78950d1cd5e0SMatthew G. Knepley 
78960d1cd5e0SMatthew G. Knepley   Output Parameter:
78970d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
78980d1cd5e0SMatthew G. Knepley 
78990d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
79000d1cd5e0SMatthew G. Knepley 
79010d1cd5e0SMatthew G. Knepley   Level: advanced
79020d1cd5e0SMatthew G. Knepley 
79030d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
79040d1cd5e0SMatthew G. Knepley @*/
79050d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
79060d1cd5e0SMatthew G. Knepley {
79070d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
79080d1cd5e0SMatthew G. Knepley 
79090d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
79100d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79110d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
79120d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
79130d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
79146f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
79156f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
79160d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
7917df0b854cSToby Isaac   PetscFunctionReturn(0);
7918df0b854cSToby Isaac }
7919c4088d22SMatthew G. Knepley 
7920502a2867SDave May /*@C
7921502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
7922502a2867SDave May 
7923502a2867SDave May  Not Collective
7924502a2867SDave May 
7925502a2867SDave May  Input Parameter:
7926502a2867SDave May  . dm    - The DM
7927502a2867SDave May 
7928502a2867SDave May  Output Parameter:
7929502a2867SDave May  . nranks - the number of neighbours
7930502a2867SDave May  . ranks - the neighbors ranks
7931502a2867SDave May 
7932502a2867SDave May  Notes:
7933502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
7934502a2867SDave May 
7935502a2867SDave May  Level: beginner
7936502a2867SDave May 
7937dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
7938502a2867SDave May @*/
7939502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
7940502a2867SDave May {
7941502a2867SDave May   PetscErrorCode ierr;
7942502a2867SDave May 
7943502a2867SDave May   PetscFunctionBegin;
7944502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79450918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
7946502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
7947502a2867SDave May   PetscFunctionReturn(0);
7948502a2867SDave May }
7949502a2867SDave May 
7950531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
7951531c7667SBarry Smith 
7952531c7667SBarry Smith /*
7953531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
7954531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
7955531c7667SBarry Smith */
7956531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
7957531c7667SBarry Smith {
7958531c7667SBarry Smith   PetscErrorCode ierr;
7959531c7667SBarry Smith 
7960531c7667SBarry Smith   PetscFunctionBegin;
7961531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7962531c7667SBarry Smith     Vec x1local;
7963531c7667SBarry Smith     DM  dm;
7964531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7965531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
7966531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
7967531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7968531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7969531c7667SBarry Smith     x1   = x1local;
7970531c7667SBarry Smith   }
7971531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
7972531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7973531c7667SBarry Smith     DM  dm;
7974531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7975531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
7976531c7667SBarry Smith   }
7977531c7667SBarry Smith   PetscFunctionReturn(0);
7978531c7667SBarry Smith }
7979531c7667SBarry Smith 
7980531c7667SBarry Smith /*@
7981531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
7982531c7667SBarry Smith 
7983531c7667SBarry Smith     Input Parameter:
7984531c7667SBarry Smith .    coloring - the MatFDColoring object
7985531c7667SBarry Smith 
798695452b02SPatrick Sanan     Developer Notes:
798795452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
7988531c7667SBarry Smith 
79891b266c99SBarry Smith     Level: advanced
79901b266c99SBarry Smith 
7991531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
7992531c7667SBarry Smith @*/
7993531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
7994531c7667SBarry Smith {
7995531c7667SBarry Smith   PetscFunctionBegin;
7996531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
7997531c7667SBarry Smith   PetscFunctionReturn(0);
7998531c7667SBarry Smith }
79998320bc6fSPatrick Sanan 
80008320bc6fSPatrick Sanan /*@
80018320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
80028320bc6fSPatrick Sanan 
80038320bc6fSPatrick Sanan     Collective
80048320bc6fSPatrick Sanan 
80058320bc6fSPatrick Sanan     Input Parameters:
80068320bc6fSPatrick Sanan +    dm - the first DM
80078320bc6fSPatrick Sanan -    dm2 - the second DM
80088320bc6fSPatrick Sanan 
80098320bc6fSPatrick Sanan     Output Parameters:
80108320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
80118320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
80128320bc6fSPatrick Sanan 
80138320bc6fSPatrick Sanan     Notes:
80148320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
80153d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
80168320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
80173d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
80183d862458SPatrick Sanan     decomposition, but hold different data.
80198320bc6fSPatrick Sanan 
80208320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
80218320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
80228320bc6fSPatrick Sanan 
80238320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
80248320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
80258320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
80268320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
80278320bc6fSPatrick Sanan 
80288320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
80298320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
80308320bc6fSPatrick Sanan .vb
80318320bc6fSPatrick Sanan   ...
80328320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
80338320bc6fSPatrick Sanan   if (set && compatible)  {
80348320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
80358320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
80363d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
80378320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
80388320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
80398320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
80408320bc6fSPatrick Sanan       }
80418320bc6fSPatrick Sanan     }
80428320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
80438320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
80448320bc6fSPatrick Sanan   } else {
80458320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
80468320bc6fSPatrick Sanan   }
80478320bc6fSPatrick Sanan   ...
80488320bc6fSPatrick Sanan .ve
80498320bc6fSPatrick Sanan 
80508320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
80518320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
80528320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
80538320bc6fSPatrick Sanan     always check the "set" output parameter.
80548320bc6fSPatrick Sanan 
80558320bc6fSPatrick Sanan     A DM is always compatible with itself.
80568320bc6fSPatrick Sanan 
80578320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
80588320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
80598320bc6fSPatrick Sanan     incompatible.
80608320bc6fSPatrick Sanan 
80618320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
80628320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
80638320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
80648320bc6fSPatrick Sanan 
80658320bc6fSPatrick Sanan     Developer Notes:
80663d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
80673d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
80688320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
80698320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
80708320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
80713d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
80723d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
80738320bc6fSPatrick Sanan 
80748320bc6fSPatrick Sanan     Level: advanced
80758320bc6fSPatrick Sanan 
80763d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
80778320bc6fSPatrick Sanan @*/
80788320bc6fSPatrick Sanan 
80798320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
80808320bc6fSPatrick Sanan {
80818320bc6fSPatrick Sanan   PetscErrorCode ierr;
80828320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
80838320bc6fSPatrick Sanan   DMType         type,type2;
80848320bc6fSPatrick Sanan   PetscBool      sameType;
80858320bc6fSPatrick Sanan 
80868320bc6fSPatrick Sanan   PetscFunctionBegin;
80878320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80888320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
80898320bc6fSPatrick Sanan 
80908320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
80918320bc6fSPatrick Sanan   if (dm == dm2) {
80928320bc6fSPatrick Sanan     *set = PETSC_TRUE;
80938320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
80948320bc6fSPatrick Sanan     PetscFunctionReturn(0);
80958320bc6fSPatrick Sanan   }
80968320bc6fSPatrick Sanan 
80978320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
80988320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
80998320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
81008320bc6fSPatrick Sanan      determined by the implementation-specific logic */
81018320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
81028320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
81038320bc6fSPatrick Sanan     *set = PETSC_TRUE;
81048320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
81058320bc6fSPatrick Sanan     PetscFunctionReturn(0);
81068320bc6fSPatrick Sanan   }
81078320bc6fSPatrick Sanan 
81088320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
81098320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
81108320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
8111b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
81128320bc6fSPatrick Sanan   }
81138320bc6fSPatrick Sanan 
81148320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
81158320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
81168320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
81178320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
81188320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
81198320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
81208320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
81218320bc6fSPatrick Sanan   } else {
81228320bc6fSPatrick Sanan     *set = PETSC_FALSE;
81238320bc6fSPatrick Sanan   }
81248320bc6fSPatrick Sanan   PetscFunctionReturn(0);
81258320bc6fSPatrick Sanan }
8126