xref: /petsc/src/dm/interface/dm.c (revision f19dbd5846ffd4db2e55501a1bb9a58c867a074c)
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>
5*f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
101ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1167a56275SMatthew G Knepley 
12e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
13bff4a2f0SMatthew G. Knepley 
14a4121054SBarry Smith /*@
15de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
16a4121054SBarry Smith 
17a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
18a4121054SBarry Smith    error when you try to use the vector.
19a4121054SBarry Smith 
20a4121054SBarry Smith   Collective on MPI_Comm
21a4121054SBarry Smith 
22a4121054SBarry Smith   Input Parameter:
23a4121054SBarry Smith . comm - The communicator for the DM object
24a4121054SBarry Smith 
25a4121054SBarry Smith   Output Parameter:
26a4121054SBarry Smith . dm - The DM object
27a4121054SBarry Smith 
28a4121054SBarry Smith   Level: beginner
29a4121054SBarry Smith 
308472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
31a4121054SBarry Smith @*/
327087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
33a4121054SBarry Smith {
34a4121054SBarry Smith   DM             v;
35a4121054SBarry Smith   PetscErrorCode ierr;
36a4121054SBarry Smith 
37a4121054SBarry Smith   PetscFunctionBegin;
381411c6eeSJed Brown   PetscValidPointer(dm,2);
390298fd71SBarry Smith   *dm = NULL;
408b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
42607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
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 
470298fd71SBarry Smith   v->ltogmap                  = NULL;
481411c6eeSJed Brown   v->bs                       = 1;
49171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5188ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
52c58f1c22SToby Isaac   v->labels                   = NULL;
53c58f1c22SToby Isaac   v->depthLabel               = NULL;
540298fd71SBarry Smith   v->defaultSection           = NULL;
550298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
56fba222abSToby Isaac   v->defaultConstraintSection = NULL;
57fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
58c6b900c6SMatthew G. Knepley   v->L                        = NULL;
59c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
605dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
619a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
62435a35e8SMatthew G Knepley   {
63435a35e8SMatthew G Knepley     PetscInt i;
64435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
650298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
66435a35e8SMatthew G Knepley     }
67435a35e8SMatthew G Knepley   }
682764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6914f150ffSMatthew G. Knepley   v->dmBC = NULL;
70a8fb8f29SToby Isaac   v->coarseMesh = NULL;
71f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
72cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
73c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
74b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
75bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
76c58f1c22SToby Isaac   v->labels->refct = 1;
771411c6eeSJed Brown   *dm = v;
78a4121054SBarry Smith   PetscFunctionReturn(0);
79a4121054SBarry Smith }
80a4121054SBarry Smith 
8138221697SMatthew G. Knepley /*@
8238221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
8338221697SMatthew G. Knepley 
8438221697SMatthew G. Knepley   Collective on MPI_Comm
8538221697SMatthew G. Knepley 
8638221697SMatthew G. Knepley   Input Parameter:
8738221697SMatthew G. Knepley . dm - The original DM object
8838221697SMatthew G. Knepley 
8938221697SMatthew G. Knepley   Output Parameter:
9038221697SMatthew G. Knepley . newdm  - The new DM object
9138221697SMatthew G. Knepley 
9238221697SMatthew G. Knepley   Level: beginner
9338221697SMatthew G. Knepley 
9438221697SMatthew G. Knepley .keywords: DM, topology, create
9538221697SMatthew G. Knepley @*/
9638221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9738221697SMatthew G. Knepley {
9838221697SMatthew G. Knepley   PetscSF        sf;
9938221697SMatthew G. Knepley   Vec            coords;
10038221697SMatthew G. Knepley   void          *ctx;
101a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
10238221697SMatthew G. Knepley   PetscErrorCode ierr;
10338221697SMatthew G. Knepley 
10438221697SMatthew G. Knepley   PetscFunctionBegin;
10538221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10638221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10738221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
108c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
109c58f1c22SToby Isaac   dm->labels->refct++;
110c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
111c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1121de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1131de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
11438221697SMatthew G. Knepley   if (dm->ops->clone) {
11538221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
11638221697SMatthew G. Knepley   }
1173f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
11838221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
11938221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
12038221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
12138221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
122be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
123be4c1c3eSMatthew G. Knepley     DM           ncdm;
124be4c1c3eSMatthew G. Knepley     PetscSection cs;
1255a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
126be4c1c3eSMatthew G. Knepley 
127be4c1c3eSMatthew G. Knepley     ierr = DMGetDefaultSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
128be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1295a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1305a0206caSToby Isaac     if (pEndMax >= 0) {
131be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
13213bffc4cSMatthew G. Knepley       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
133a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
134be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
135be4c1c3eSMatthew G. Knepley     }
136be4c1c3eSMatthew G. Knepley   }
137a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
138a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   if (coords) {
14138221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
14238221697SMatthew G. Knepley   } else {
14338221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
14438221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
14538221697SMatthew G. Knepley   }
14690b157c4SStefano Zampini   {
14790b157c4SStefano Zampini     PetscBool             isper;
148c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1495dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
15090b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
15190b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
152c6b900c6SMatthew G. Knepley   }
15338221697SMatthew G. Knepley   PetscFunctionReturn(0);
15438221697SMatthew G. Knepley }
15538221697SMatthew G. Knepley 
1569a42bb27SBarry Smith /*@C
157564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1589a42bb27SBarry Smith 
1598472ad0fSDave May    Logically Collective on DM
1609a42bb27SBarry Smith 
1619a42bb27SBarry Smith    Input Parameter:
1629a42bb27SBarry Smith +  da - initial distributed array
1638154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1649a42bb27SBarry Smith 
1659a42bb27SBarry Smith    Options Database:
166dd85299cSBarry Smith .   -dm_vec_type ctype
1679a42bb27SBarry Smith 
1689a42bb27SBarry Smith    Level: intermediate
1699a42bb27SBarry Smith 
1708472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1719a42bb27SBarry Smith @*/
17219fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1739a42bb27SBarry Smith {
1749a42bb27SBarry Smith   PetscErrorCode ierr;
1759a42bb27SBarry Smith 
1769a42bb27SBarry Smith   PetscFunctionBegin;
1779a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1789a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
17919fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1809a42bb27SBarry Smith   PetscFunctionReturn(0);
1819a42bb27SBarry Smith }
1829a42bb27SBarry Smith 
183c0dedaeaSBarry Smith /*@C
184c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
185c0dedaeaSBarry Smith 
1868472ad0fSDave May    Logically Collective on DM
187c0dedaeaSBarry Smith 
188c0dedaeaSBarry Smith    Input Parameter:
189c0dedaeaSBarry Smith .  da - initial distributed array
190c0dedaeaSBarry Smith 
191c0dedaeaSBarry Smith    Output Parameter:
192c0dedaeaSBarry Smith .  ctype - the vector type
193c0dedaeaSBarry Smith 
194c0dedaeaSBarry Smith    Level: intermediate
195c0dedaeaSBarry Smith 
1968472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
197c0dedaeaSBarry Smith @*/
198c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
199c0dedaeaSBarry Smith {
200c0dedaeaSBarry Smith   PetscFunctionBegin;
201c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
202c0dedaeaSBarry Smith   *ctype = da->vectype;
203c0dedaeaSBarry Smith   PetscFunctionReturn(0);
204c0dedaeaSBarry Smith }
205c0dedaeaSBarry Smith 
2065f1ad066SMatthew G Knepley /*@
20734f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2085f1ad066SMatthew G Knepley 
2095f1ad066SMatthew G Knepley   Not collective
2105f1ad066SMatthew G Knepley 
2115f1ad066SMatthew G Knepley   Input Parameter:
2125f1ad066SMatthew G Knepley . v - The Vec
2135f1ad066SMatthew G Knepley 
2145f1ad066SMatthew G Knepley   Output Parameter:
2155f1ad066SMatthew G Knepley . dm - The DM
2165f1ad066SMatthew G Knepley 
2175f1ad066SMatthew G Knepley   Level: intermediate
2185f1ad066SMatthew G Knepley 
2195f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2205f1ad066SMatthew G Knepley @*/
2215f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2225f1ad066SMatthew G Knepley {
2235f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2245f1ad066SMatthew G Knepley 
2255f1ad066SMatthew G Knepley   PetscFunctionBegin;
2265f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2275f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2285f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2295f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2305f1ad066SMatthew G Knepley }
2315f1ad066SMatthew G Knepley 
2325f1ad066SMatthew G Knepley /*@
233d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2345f1ad066SMatthew G Knepley 
2355f1ad066SMatthew G Knepley   Not collective
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley   Input Parameters:
2385f1ad066SMatthew G Knepley + v - The Vec
2395f1ad066SMatthew G Knepley - dm - The DM
2405f1ad066SMatthew G Knepley 
241d9805387SMatthew 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.
242d9805387SMatthew G. Knepley 
2435f1ad066SMatthew G Knepley   Level: intermediate
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2465f1ad066SMatthew G Knepley @*/
2475f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2485f1ad066SMatthew G Knepley {
2495f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley   PetscFunctionBegin;
2525f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
253d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2545f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2555f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2565f1ad066SMatthew G Knepley }
2575f1ad066SMatthew G Knepley 
258521d9a4cSLisandro Dalcin /*@C
259521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
260521d9a4cSLisandro Dalcin 
261521d9a4cSLisandro Dalcin    Logically Collective on DM
262521d9a4cSLisandro Dalcin 
263521d9a4cSLisandro Dalcin    Input Parameter:
264521d9a4cSLisandro Dalcin +  dm - the DM context
265a2b5a043SBarry Smith -  ctype - the matrix type
266521d9a4cSLisandro Dalcin 
267521d9a4cSLisandro Dalcin    Options Database:
268521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
269521d9a4cSLisandro Dalcin 
270521d9a4cSLisandro Dalcin    Level: intermediate
271521d9a4cSLisandro Dalcin 
272c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
273521d9a4cSLisandro Dalcin @*/
27419fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
275521d9a4cSLisandro Dalcin {
276521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
27788f0584fSBarry Smith 
278521d9a4cSLisandro Dalcin   PetscFunctionBegin;
279521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
280521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
28119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
282521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
283521d9a4cSLisandro Dalcin }
284521d9a4cSLisandro Dalcin 
285c0dedaeaSBarry Smith /*@C
286c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
287c0dedaeaSBarry Smith 
288c0dedaeaSBarry Smith    Logically Collective on DM
289c0dedaeaSBarry Smith 
290c0dedaeaSBarry Smith    Input Parameter:
291c0dedaeaSBarry Smith .  dm - the DM context
292c0dedaeaSBarry Smith 
293c0dedaeaSBarry Smith    Output Parameter:
294c0dedaeaSBarry Smith .  ctype - the matrix type
295c0dedaeaSBarry Smith 
296c0dedaeaSBarry Smith    Options Database:
297c0dedaeaSBarry Smith .   -dm_mat_type ctype
298c0dedaeaSBarry Smith 
299c0dedaeaSBarry Smith    Level: intermediate
300c0dedaeaSBarry Smith 
301c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
302c0dedaeaSBarry Smith @*/
303c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
304c0dedaeaSBarry Smith {
305c0dedaeaSBarry Smith   PetscFunctionBegin;
306c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
307c0dedaeaSBarry Smith   *ctype = dm->mattype;
308c0dedaeaSBarry Smith   PetscFunctionReturn(0);
309c0dedaeaSBarry Smith }
310c0dedaeaSBarry Smith 
311c688c046SMatthew G Knepley /*@
31234f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
313c688c046SMatthew G Knepley 
314c688c046SMatthew G Knepley   Not collective
315c688c046SMatthew G Knepley 
316c688c046SMatthew G Knepley   Input Parameter:
317c688c046SMatthew G Knepley . A - The Mat
318c688c046SMatthew G Knepley 
319c688c046SMatthew G Knepley   Output Parameter:
320c688c046SMatthew G Knepley . dm - The DM
321c688c046SMatthew G Knepley 
322c688c046SMatthew G Knepley   Level: intermediate
323c688c046SMatthew G Knepley 
324c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
325c688c046SMatthew G Knepley @*/
326c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
327c688c046SMatthew G Knepley {
328c688c046SMatthew G Knepley   PetscErrorCode ierr;
329c688c046SMatthew G Knepley 
330c688c046SMatthew G Knepley   PetscFunctionBegin;
331c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
332c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
333c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
334c688c046SMatthew G Knepley   PetscFunctionReturn(0);
335c688c046SMatthew G Knepley }
336c688c046SMatthew G Knepley 
337c688c046SMatthew G Knepley /*@
338c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
339c688c046SMatthew G Knepley 
340c688c046SMatthew G Knepley   Not collective
341c688c046SMatthew G Knepley 
342c688c046SMatthew G Knepley   Input Parameters:
343c688c046SMatthew G Knepley + A - The Mat
344c688c046SMatthew G Knepley - dm - The DM
345c688c046SMatthew G Knepley 
346c688c046SMatthew G Knepley   Level: intermediate
347c688c046SMatthew G Knepley 
348c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
349c688c046SMatthew G Knepley @*/
350c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
351c688c046SMatthew G Knepley {
352c688c046SMatthew G Knepley   PetscErrorCode ierr;
353c688c046SMatthew G Knepley 
354c688c046SMatthew G Knepley   PetscFunctionBegin;
355c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3568865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
357c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
358c688c046SMatthew G Knepley   PetscFunctionReturn(0);
359c688c046SMatthew G Knepley }
360c688c046SMatthew G Knepley 
3619a42bb27SBarry Smith /*@C
3629a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3636757b960SDave May    DM options in the database.
3649a42bb27SBarry Smith 
3658353ddbbSDave May    Logically Collective on DM
3669a42bb27SBarry Smith 
3679a42bb27SBarry Smith    Input Parameter:
3688353ddbbSDave May +  da - the DM context
3699a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3709a42bb27SBarry Smith 
3719a42bb27SBarry Smith    Notes:
3729a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3739a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3749a42bb27SBarry Smith 
3759a42bb27SBarry Smith    Level: advanced
3769a42bb27SBarry Smith 
3778353ddbbSDave May .keywords: DM, set, options, prefix, database
3789a42bb27SBarry Smith 
3799a42bb27SBarry Smith .seealso: DMSetFromOptions()
3809a42bb27SBarry Smith @*/
3817087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3829a42bb27SBarry Smith {
3839a42bb27SBarry Smith   PetscErrorCode ierr;
3849a42bb27SBarry Smith 
3859a42bb27SBarry Smith   PetscFunctionBegin;
3869a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3879a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
388691be533SLawrence Mitchell   if (dm->sf) {
389691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
390691be533SLawrence Mitchell   }
391691be533SLawrence Mitchell   if (dm->defaultSF) {
392691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
393691be533SLawrence Mitchell   }
3949a42bb27SBarry Smith   PetscFunctionReturn(0);
3959a42bb27SBarry Smith }
3969a42bb27SBarry Smith 
39731697293SDave May /*@C
39831697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
39931697293SDave May    DM options in the database.
40031697293SDave May 
40131697293SDave May    Logically Collective on DM
40231697293SDave May 
40331697293SDave May    Input Parameters:
40431697293SDave May +  dm - the DM context
40531697293SDave May -  prefix - the prefix string to prepend to all DM option requests
40631697293SDave May 
40731697293SDave May    Notes:
40831697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
40931697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
41031697293SDave May 
41131697293SDave May    Level: advanced
41231697293SDave May 
41331697293SDave May .keywords: DM, append, options, prefix, database
41431697293SDave May 
41531697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
41631697293SDave May @*/
41731697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
41831697293SDave May {
41931697293SDave May   PetscErrorCode ierr;
42031697293SDave May 
42131697293SDave May   PetscFunctionBegin;
42231697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42331697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
42431697293SDave May   PetscFunctionReturn(0);
42531697293SDave May }
42631697293SDave May 
42731697293SDave May /*@C
42831697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
42931697293SDave May    DM options in the database.
43031697293SDave May 
43131697293SDave May    Not Collective
43231697293SDave May 
43331697293SDave May    Input Parameters:
43431697293SDave May .  dm - the DM context
43531697293SDave May 
43631697293SDave May    Output Parameters:
43731697293SDave May .  prefix - pointer to the prefix string used is returned
43831697293SDave May 
43931697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
44031697293SDave May    sufficient length to hold the prefix.
44131697293SDave May 
44231697293SDave May    Level: advanced
44331697293SDave May 
44431697293SDave May .keywords: DM, set, options, prefix, database
44531697293SDave May 
44631697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
44731697293SDave May @*/
44831697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
44931697293SDave May {
45031697293SDave May   PetscErrorCode ierr;
45131697293SDave May 
45231697293SDave May   PetscFunctionBegin;
45331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45431697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
45531697293SDave May   PetscFunctionReturn(0);
45631697293SDave May }
45731697293SDave May 
45888bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
45988bdff64SToby Isaac {
46088bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
46188bdff64SToby Isaac   DMNamedVecLink nlink;
46288bdff64SToby Isaac   PetscErrorCode ierr;
46388bdff64SToby Isaac 
46488bdff64SToby Isaac   PetscFunctionBegin;
465aab5bcd8SJed Brown   *ncrefct = 0;
46688bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
46788bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
46888bdff64SToby Isaac     if (dm->localin[i])  refct--;
46988bdff64SToby Isaac     if (dm->globalin[i]) refct--;
47088bdff64SToby Isaac   }
47188bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
47288bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
47388bdff64SToby Isaac   if (dm->x) {
47488bdff64SToby Isaac     DM obj;
47588bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
47688bdff64SToby Isaac     if (obj == dm) refct--;
47788bdff64SToby Isaac   }
47888bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
47988bdff64SToby Isaac     refct--;
48088bdff64SToby Isaac     if (recurseCoarse) {
48188bdff64SToby Isaac       PetscInt coarseCount;
48288bdff64SToby Isaac 
48388bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
48488bdff64SToby Isaac       refct += coarseCount;
48588bdff64SToby Isaac     }
48688bdff64SToby Isaac   }
48788bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
48888bdff64SToby Isaac     refct--;
48988bdff64SToby Isaac     if (recurseFine) {
49088bdff64SToby Isaac       PetscInt fineCount;
49188bdff64SToby Isaac 
49288bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
49388bdff64SToby Isaac       refct += fineCount;
49488bdff64SToby Isaac     }
49588bdff64SToby Isaac   }
49688bdff64SToby Isaac   *ncrefct = refct;
49788bdff64SToby Isaac   PetscFunctionReturn(0);
49888bdff64SToby Isaac }
49988bdff64SToby Isaac 
500354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
501354557abSToby Isaac {
502354557abSToby Isaac   PetscErrorCode ierr;
503354557abSToby Isaac 
504354557abSToby Isaac   PetscFunctionBegin;
505354557abSToby Isaac   if (!--(dm->labels->refct)) {
506354557abSToby Isaac     DMLabelLink next = dm->labels->next;
507354557abSToby Isaac 
508354557abSToby Isaac     /* destroy the labels */
509354557abSToby Isaac     while (next) {
510354557abSToby Isaac       DMLabelLink tmp = next->next;
511354557abSToby Isaac 
512354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
513354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
514354557abSToby Isaac       next = tmp;
515354557abSToby Isaac     }
516354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
517354557abSToby Isaac   }
518354557abSToby Isaac   PetscFunctionReturn(0);
519354557abSToby Isaac }
520354557abSToby Isaac 
52147c6ae99SBarry Smith /*@
5228472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
52347c6ae99SBarry Smith 
52447c6ae99SBarry Smith     Collective on DM
52547c6ae99SBarry Smith 
52647c6ae99SBarry Smith     Input Parameter:
52747c6ae99SBarry Smith .   dm - the DM object to destroy
52847c6ae99SBarry Smith 
52947c6ae99SBarry Smith     Level: developer
53047c6ae99SBarry Smith 
531e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
53247c6ae99SBarry Smith 
53347c6ae99SBarry Smith @*/
534fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
53547c6ae99SBarry Smith {
53688bdff64SToby Isaac   PetscInt       i, cnt;
537dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
53847c6ae99SBarry Smith   PetscErrorCode ierr;
53947c6ae99SBarry Smith 
54047c6ae99SBarry Smith   PetscFunctionBegin;
5416bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5426bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
54387e657c6SBarry Smith 
54488bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
54588bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
54688bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
54788bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
548732e2eb9SMatthew G Knepley   /*
549732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
550732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
551732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
552732e2eb9SMatthew G Knepley   */
5536bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5546bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
555732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5566bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5576bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
558732e2eb9SMatthew G Knepley   }
559f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5600298fd71SBarry Smith   (*dm)->namedglobal = NULL;
561f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5622348bcf4SPeter Brune     nnext = nlink->next;
5632348bcf4SPeter 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);
5642348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5652348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5662348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5672348bcf4SPeter Brune   }
568f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5690298fd71SBarry Smith   (*dm)->namedlocal = NULL;
570f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
571f490541aSPeter Brune     nnext = nlink->next;
572f490541aSPeter 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);
573f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
574f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
575f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
576f490541aSPeter Brune   }
5772348bcf4SPeter Brune 
578b17ce1afSJed Brown   /* Destroy the list of hooks */
579c833c3b5SJed Brown   {
580c833c3b5SJed Brown     DMCoarsenHookLink link,next;
581b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
582b17ce1afSJed Brown       next = link->next;
583b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
584b17ce1afSJed Brown     }
5850298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
586c833c3b5SJed Brown   }
587c833c3b5SJed Brown   {
588c833c3b5SJed Brown     DMRefineHookLink link,next;
589c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
590c833c3b5SJed Brown       next = link->next;
591c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
592c833c3b5SJed Brown     }
5930298fd71SBarry Smith     (*dm)->refinehook = NULL;
594c833c3b5SJed Brown   }
595be081cd6SPeter Brune   {
596be081cd6SPeter Brune     DMSubDomainHookLink link,next;
597be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
598be081cd6SPeter Brune       next = link->next;
599be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
600be081cd6SPeter Brune     }
6010298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
602be081cd6SPeter Brune   }
603baf369e7SPeter Brune   {
604baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
605baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
606baf369e7SPeter Brune       next = link->next;
607baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
608baf369e7SPeter Brune     }
6090298fd71SBarry Smith     (*dm)->gtolhook = NULL;
610baf369e7SPeter Brune   }
611d4d07f1eSToby Isaac   {
612d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
613d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
614d4d07f1eSToby Isaac       next = link->next;
615d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
616d4d07f1eSToby Isaac     }
617d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
618d4d07f1eSToby Isaac   }
619aa1993deSMatthew G Knepley   /* Destroy the work arrays */
620aa1993deSMatthew G Knepley   {
621aa1993deSMatthew G Knepley     DMWorkLink link,next;
622aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
623aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
624aa1993deSMatthew G Knepley       next = link->next;
625aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
626aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
627aa1993deSMatthew G Knepley     }
6280298fd71SBarry Smith     (*dm)->workin = NULL;
629aa1993deSMatthew G Knepley   }
630c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
631c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
632c58f1c22SToby Isaac 
633c58f1c22SToby Isaac     /* destroy the labels */
634c58f1c22SToby Isaac     while (next) {
635c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
636c58f1c22SToby Isaac 
637c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
638c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
639c58f1c22SToby Isaac       next = tmp;
640c58f1c22SToby Isaac     }
641c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
642c58f1c22SToby Isaac   }
643e6f8dbb6SToby Isaac   {
644e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
645e6f8dbb6SToby Isaac     while (next) {
646e6f8dbb6SToby Isaac       DMBoundary b = next;
647e6f8dbb6SToby Isaac 
648e6f8dbb6SToby Isaac       next = b->next;
649e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
650e6f8dbb6SToby Isaac     }
651e6f8dbb6SToby Isaac   }
652b17ce1afSJed Brown 
65352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
65452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
65552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
65652536dc3SBarry Smith 
6571a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6581a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6591a266240SBarry Smith   }
66087e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
66171cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6624dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6636bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6646bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
665073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
66688ed4aceSMatthew G Knepley 
66788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
66888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6698b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
670fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
671fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
67288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
67388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6748e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
675af122d2aSMatthew G Knepley 
67688bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
67788bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
67888bdff64SToby Isaac   }
679a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
68088bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
68188bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
68288bdff64SToby Isaac   }
68388bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
684*f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
6856636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6866636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6876636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6885dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6896636e97aSMatthew G Knepley 
6902764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
69114f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
692e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
693e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
694732e2eb9SMatthew G Knepley 
6956bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
696435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
697732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
69847c6ae99SBarry Smith   PetscFunctionReturn(0);
69947c6ae99SBarry Smith }
70047c6ae99SBarry Smith 
701d7bf68aeSBarry Smith /*@
702d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
703d7bf68aeSBarry Smith 
704d7bf68aeSBarry Smith     Collective on DM
705d7bf68aeSBarry Smith 
706d7bf68aeSBarry Smith     Input Parameter:
707d7bf68aeSBarry Smith .   dm - the DM object to setup
708d7bf68aeSBarry Smith 
709d7bf68aeSBarry Smith     Level: developer
710d7bf68aeSBarry Smith 
711e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
712d7bf68aeSBarry Smith 
713d7bf68aeSBarry Smith @*/
7147087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
715d7bf68aeSBarry Smith {
716d7bf68aeSBarry Smith   PetscErrorCode ierr;
717d7bf68aeSBarry Smith 
718d7bf68aeSBarry Smith   PetscFunctionBegin;
719171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7208387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
721d7bf68aeSBarry Smith   if (dm->ops->setup) {
722d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
723d7bf68aeSBarry Smith   }
7248387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
725d7bf68aeSBarry Smith   PetscFunctionReturn(0);
726d7bf68aeSBarry Smith }
727d7bf68aeSBarry Smith 
728d7bf68aeSBarry Smith /*@
729d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
730d7bf68aeSBarry Smith 
731d7bf68aeSBarry Smith     Collective on DM
732d7bf68aeSBarry Smith 
733d7bf68aeSBarry Smith     Input Parameter:
734d7bf68aeSBarry Smith .   dm - the DM object to set options for
735d7bf68aeSBarry Smith 
736732e2eb9SMatthew G Knepley     Options Database:
7374164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7384164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7394164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7404164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
741732e2eb9SMatthew G Knepley 
742d7bf68aeSBarry Smith     Level: developer
743d7bf68aeSBarry Smith 
744e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
745d7bf68aeSBarry Smith 
746d7bf68aeSBarry Smith @*/
7477087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
748d7bf68aeSBarry Smith {
7497781c08eSBarry Smith   char           typeName[256];
750ca266f36SBarry Smith   PetscBool      flg;
751d7bf68aeSBarry Smith   PetscErrorCode ierr;
752d7bf68aeSBarry Smith 
753d7bf68aeSBarry Smith   PetscFunctionBegin;
754171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
755f1fd5e65SToby Isaac   if (dm->prob) {
756f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
757f1fd5e65SToby Isaac   }
758691be533SLawrence Mitchell   if (dm->sf) {
759691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
760691be533SLawrence Mitchell   }
761691be533SLawrence Mitchell   if (dm->defaultSF) {
762691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
763691be533SLawrence Mitchell   }
7643194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7650298fd71SBarry 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);
766a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
767f9ba7244SBarry Smith   if (flg) {
768f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
769f9ba7244SBarry Smith   }
770a264d7a6SBarry 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);
771073dac72SJed Brown   if (flg) {
772521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
773073dac72SJed Brown   }
7740298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
775f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
776e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
777f9ba7244SBarry Smith   }
778f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
7790633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
78082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
781d7bf68aeSBarry Smith   PetscFunctionReturn(0);
782d7bf68aeSBarry Smith }
783d7bf68aeSBarry Smith 
784fc9bc008SSatish Balay /*@C
785224748a4SBarry Smith     DMView - Views a DM
78647c6ae99SBarry Smith 
78747c6ae99SBarry Smith     Collective on DM
78847c6ae99SBarry Smith 
78947c6ae99SBarry Smith     Input Parameter:
79047c6ae99SBarry Smith +   dm - the DM object to view
79147c6ae99SBarry Smith -   v - the viewer
79247c6ae99SBarry Smith 
793224748a4SBarry Smith     Level: beginner
79447c6ae99SBarry Smith 
795e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
79647c6ae99SBarry Smith 
79747c6ae99SBarry Smith @*/
7987087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
79947c6ae99SBarry Smith {
80047c6ae99SBarry Smith   PetscErrorCode    ierr;
80132c0f0efSBarry Smith   PetscBool         isbinary;
80276a8abe0SBarry Smith   PetscMPIInt       size;
80376a8abe0SBarry Smith   PetscViewerFormat format;
80447c6ae99SBarry Smith 
80547c6ae99SBarry Smith   PetscFunctionBegin;
806171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8073014e516SBarry Smith   if (!v) {
808ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8093014e516SBarry Smith   }
81076a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
81176a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
81276a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
81398c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
81432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
81532c0f0efSBarry Smith   if (isbinary) {
81655849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
81732c0f0efSBarry Smith     char     type[256];
81832c0f0efSBarry Smith 
81932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
82032c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
82132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
82232c0f0efSBarry Smith   }
8230c010503SBarry Smith   if (dm->ops->view) {
8240c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
82547c6ae99SBarry Smith   }
82647c6ae99SBarry Smith   PetscFunctionReturn(0);
82747c6ae99SBarry Smith }
82847c6ae99SBarry Smith 
82947c6ae99SBarry Smith /*@
8308472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith     Collective on DM
83347c6ae99SBarry Smith 
83447c6ae99SBarry Smith     Input Parameter:
83547c6ae99SBarry Smith .   dm - the DM object
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith     Output Parameter:
83847c6ae99SBarry Smith .   vec - the global vector
83947c6ae99SBarry Smith 
840073dac72SJed Brown     Level: beginner
84147c6ae99SBarry Smith 
842e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith @*/
8457087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
84647c6ae99SBarry Smith {
84747c6ae99SBarry Smith   PetscErrorCode ierr;
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith   PetscFunctionBegin;
850171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85147c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
85247c6ae99SBarry Smith   PetscFunctionReturn(0);
85347c6ae99SBarry Smith }
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith /*@
8568472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
85747c6ae99SBarry Smith 
85847c6ae99SBarry Smith     Not Collective
85947c6ae99SBarry Smith 
86047c6ae99SBarry Smith     Input Parameter:
86147c6ae99SBarry Smith .   dm - the DM object
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith     Output Parameter:
86447c6ae99SBarry Smith .   vec - the local vector
86547c6ae99SBarry Smith 
866073dac72SJed Brown     Level: beginner
86747c6ae99SBarry Smith 
868e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
86947c6ae99SBarry Smith 
87047c6ae99SBarry Smith @*/
8717087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
87247c6ae99SBarry Smith {
87347c6ae99SBarry Smith   PetscErrorCode ierr;
87447c6ae99SBarry Smith 
87547c6ae99SBarry Smith   PetscFunctionBegin;
876171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
87847c6ae99SBarry Smith   PetscFunctionReturn(0);
87947c6ae99SBarry Smith }
88047c6ae99SBarry Smith 
8811411c6eeSJed Brown /*@
8821411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8831411c6eeSJed Brown 
8841411c6eeSJed Brown    Collective on DM
8851411c6eeSJed Brown 
8861411c6eeSJed Brown    Input Parameter:
8871411c6eeSJed Brown .  dm - the DM that provides the mapping
8881411c6eeSJed Brown 
8891411c6eeSJed Brown    Output Parameter:
8901411c6eeSJed Brown .  ltog - the mapping
8911411c6eeSJed Brown 
8921411c6eeSJed Brown    Level: intermediate
8931411c6eeSJed Brown 
8941411c6eeSJed Brown    Notes:
8951411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8961411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8971411c6eeSJed Brown 
898fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8991411c6eeSJed Brown @*/
9007087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9011411c6eeSJed Brown {
9020be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9031411c6eeSJed Brown   PetscErrorCode ierr;
9041411c6eeSJed Brown 
9051411c6eeSJed Brown   PetscFunctionBegin;
9061411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9071411c6eeSJed Brown   PetscValidPointer(ltog,2);
9081411c6eeSJed Brown   if (!dm->ltogmap) {
90937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
91037d0c07bSMatthew G Knepley 
91137d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
91237d0c07bSMatthew G Knepley     if (section) {
913a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
91437d0c07bSMatthew G Knepley       PetscInt       *ltog;
915ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
91637d0c07bSMatthew G Knepley 
91737d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
91837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
919ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
920ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
92137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
922a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
92337d0c07bSMatthew G Knepley 
92437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
92537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
926a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
927a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
92837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
9291a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
9301a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
9311a7dc684SMatthew G. Knepley         if (dof) {
932b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
933b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
9341a7dc684SMatthew G. Knepley         }
93537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
936a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
937a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
93837d0c07bSMatthew G Knepley         }
93937d0c07bSMatthew G Knepley       }
940bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
9410be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
9420be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
9430be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
9440be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
9457591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
9467591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
947ccf3bd66SMatthew G. Knepley       if (bs > 1) {
948ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
949ccf3bd66SMatthew G. Knepley         n /= bs;
950ccf3bd66SMatthew G. Knepley       }
951ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9523bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
95337d0c07bSMatthew G Knepley     } else {
954184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
955184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9561411c6eeSJed Brown     }
95737d0c07bSMatthew G Knepley   }
9581411c6eeSJed Brown   *ltog = dm->ltogmap;
9591411c6eeSJed Brown   PetscFunctionReturn(0);
9601411c6eeSJed Brown }
9611411c6eeSJed Brown 
9621411c6eeSJed Brown /*@
9631411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9641411c6eeSJed Brown 
9651411c6eeSJed Brown    Not Collective
9661411c6eeSJed Brown 
9671411c6eeSJed Brown    Input Parameter:
9681411c6eeSJed Brown .  dm - the DM with block structure
9691411c6eeSJed Brown 
9701411c6eeSJed Brown    Output Parameter:
9711411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9721411c6eeSJed Brown 
9731411c6eeSJed Brown    Level: intermediate
9741411c6eeSJed Brown 
975fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9761411c6eeSJed Brown @*/
9777087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9781411c6eeSJed Brown {
9791411c6eeSJed Brown   PetscFunctionBegin;
9801411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9811411c6eeSJed Brown   PetscValidPointer(bs,2);
9821411c6eeSJed 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");
9831411c6eeSJed Brown   *bs = dm->bs;
9841411c6eeSJed Brown   PetscFunctionReturn(0);
9851411c6eeSJed Brown }
9861411c6eeSJed Brown 
98747c6ae99SBarry Smith /*@
9888472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
98947c6ae99SBarry Smith 
99047c6ae99SBarry Smith     Collective on DM
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith     Input Parameter:
99347c6ae99SBarry Smith +   dm1 - the DM object
99447c6ae99SBarry Smith -   dm2 - the second, finer DM object
99547c6ae99SBarry Smith 
99647c6ae99SBarry Smith     Output Parameter:
99747c6ae99SBarry Smith +  mat - the interpolation
99847c6ae99SBarry Smith -  vec - the scaling (optional)
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith     Level: developer
100147c6ae99SBarry Smith 
100285afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
100385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1004d52bd9f3SBarry Smith 
10051f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1006d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
100785afcc9aSBarry Smith 
100885afcc9aSBarry Smith 
10093ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith @*/
1012e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
101347c6ae99SBarry Smith {
101447c6ae99SBarry Smith   PetscErrorCode ierr;
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith   PetscFunctionBegin;
1017171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1018171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1019cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
102025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1021cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
102247c6ae99SBarry Smith   PetscFunctionReturn(0);
102347c6ae99SBarry Smith }
102447c6ae99SBarry Smith 
10253ad4599aSBarry Smith /*@
10263ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10273ad4599aSBarry Smith 
10283ad4599aSBarry Smith     Collective on DM
10293ad4599aSBarry Smith 
10303ad4599aSBarry Smith     Input Parameter:
10313ad4599aSBarry Smith +   dm1 - the DM object
10323ad4599aSBarry Smith -   dm2 - the second, finer DM object
10333ad4599aSBarry Smith 
10343ad4599aSBarry Smith     Output Parameter:
10353ad4599aSBarry Smith .  mat - the restriction
10363ad4599aSBarry Smith 
10373ad4599aSBarry Smith 
10383ad4599aSBarry Smith     Level: developer
10393ad4599aSBarry Smith 
10403ad4599aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
10413ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10423ad4599aSBarry Smith 
10433ad4599aSBarry Smith 
10443ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10453ad4599aSBarry Smith 
10463ad4599aSBarry Smith @*/
10473ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10483ad4599aSBarry Smith {
10493ad4599aSBarry Smith   PetscErrorCode ierr;
10503ad4599aSBarry Smith 
10513ad4599aSBarry Smith   PetscFunctionBegin;
10523ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10533ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10543ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10557294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
10563ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10573ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10583ad4599aSBarry Smith   PetscFunctionReturn(0);
10593ad4599aSBarry Smith }
10603ad4599aSBarry Smith 
106147c6ae99SBarry Smith /*@
10628472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
106347c6ae99SBarry Smith 
106447c6ae99SBarry Smith     Collective on DM
106547c6ae99SBarry Smith 
106647c6ae99SBarry Smith     Input Parameter:
106747c6ae99SBarry Smith +   dm1 - the DM object
106847c6ae99SBarry Smith -   dm2 - the second, finer DM object
106947c6ae99SBarry Smith 
107047c6ae99SBarry Smith     Output Parameter:
10716dbf9973SLawrence Mitchell .   mat - the injection
107247c6ae99SBarry Smith 
107347c6ae99SBarry Smith     Level: developer
107447c6ae99SBarry Smith 
107585afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
107685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
107785afcc9aSBarry Smith 
1078e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith @*/
10816dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
108247c6ae99SBarry Smith {
108347c6ae99SBarry Smith   PetscErrorCode ierr;
108447c6ae99SBarry Smith 
108547c6ae99SBarry Smith   PetscFunctionBegin;
1086171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1087171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10880c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
10896dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
109047c6ae99SBarry Smith   PetscFunctionReturn(0);
109147c6ae99SBarry Smith }
109247c6ae99SBarry Smith 
1093b412c318SBarry Smith /*@
1094bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1095bd041c0cSMatthew G. Knepley 
1096bd041c0cSMatthew G. Knepley   Collective on DM
1097bd041c0cSMatthew G. Knepley 
1098bd041c0cSMatthew G. Knepley   Input Parameter:
1099bd041c0cSMatthew G. Knepley + dm1 - the DM object
1100bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1101bd041c0cSMatthew G. Knepley 
1102bd041c0cSMatthew G. Knepley   Output Parameter:
1103bd041c0cSMatthew G. Knepley . mat - the interpolation
1104bd041c0cSMatthew G. Knepley 
1105bd041c0cSMatthew G. Knepley   Level: developer
1106bd041c0cSMatthew G. Knepley 
1107bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1108bd041c0cSMatthew G. Knepley @*/
1109bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1110bd041c0cSMatthew G. Knepley {
1111bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1112bd041c0cSMatthew G. Knepley 
1113bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1114bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1115bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1116bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1117bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1118bd041c0cSMatthew G. Knepley }
1119bd041c0cSMatthew G. Knepley 
1120bd041c0cSMatthew G. Knepley /*@
1121b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
112247c6ae99SBarry Smith 
112347c6ae99SBarry Smith     Collective on DM
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith     Input Parameter:
112647c6ae99SBarry Smith +   dm - the DM object
11275bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
112847c6ae99SBarry Smith 
112947c6ae99SBarry Smith     Output Parameter:
113047c6ae99SBarry Smith .   coloring - the coloring
113147c6ae99SBarry Smith 
113247c6ae99SBarry Smith     Level: developer
113347c6ae99SBarry Smith 
1134b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
113547c6ae99SBarry Smith 
1136aab9d709SJed Brown @*/
1137b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
113847c6ae99SBarry Smith {
113947c6ae99SBarry Smith   PetscErrorCode ierr;
114047c6ae99SBarry Smith 
114147c6ae99SBarry Smith   PetscFunctionBegin;
1142171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1143ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1144b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
114547c6ae99SBarry Smith   PetscFunctionReturn(0);
114647c6ae99SBarry Smith }
114747c6ae99SBarry Smith 
1148b412c318SBarry Smith /*@
11498472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
115047c6ae99SBarry Smith 
115147c6ae99SBarry Smith     Collective on DM
115247c6ae99SBarry Smith 
115347c6ae99SBarry Smith     Input Parameter:
1154b412c318SBarry Smith .   dm - the DM object
115547c6ae99SBarry Smith 
115647c6ae99SBarry Smith     Output Parameter:
115747c6ae99SBarry Smith .   mat - the empty Jacobian
115847c6ae99SBarry Smith 
1159073dac72SJed Brown     Level: beginner
116047c6ae99SBarry Smith 
116194013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
116294013140SBarry Smith        do not need to do it yourself.
116394013140SBarry Smith 
116494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
11657889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
116694013140SBarry Smith 
116794013140SBarry 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
116894013140SBarry Smith        internally by PETSc.
116994013140SBarry Smith 
117094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1171aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
117294013140SBarry Smith 
1173b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
117447c6ae99SBarry Smith 
1175aab9d709SJed Brown @*/
1176b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
117747c6ae99SBarry Smith {
117847c6ae99SBarry Smith   PetscErrorCode ierr;
117947c6ae99SBarry Smith 
118047c6ae99SBarry Smith   PetscFunctionBegin;
1181171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1182607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1183c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1184c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1185b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
118647c6ae99SBarry Smith   PetscFunctionReturn(0);
118747c6ae99SBarry Smith }
118847c6ae99SBarry Smith 
1189732e2eb9SMatthew G Knepley /*@
1190950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1191732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1192732e2eb9SMatthew G Knepley 
11938472ad0fSDave May   Logically Collective on DM
1194732e2eb9SMatthew G Knepley 
1195732e2eb9SMatthew G Knepley   Input Parameter:
1196732e2eb9SMatthew G Knepley + dm - the DM
1197732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1198732e2eb9SMatthew G Knepley 
1199732e2eb9SMatthew G Knepley   Level: developer
1200b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1201732e2eb9SMatthew G Knepley @*/
1202732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1203732e2eb9SMatthew G Knepley {
1204732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1205732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1206732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1207732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1208732e2eb9SMatthew G Knepley }
1209732e2eb9SMatthew G Knepley 
1210b06ff27eSHong Zhang /*@
1211b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1212b06ff27eSHong Zhang     but the array for values will not be allocated.
1213b06ff27eSHong Zhang 
1214b06ff27eSHong Zhang   Logically Collective on DM
1215b06ff27eSHong Zhang 
1216b06ff27eSHong Zhang   Input Parameter:
1217b06ff27eSHong Zhang + dm - the DM
1218b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1219b06ff27eSHong Zhang 
1220b06ff27eSHong Zhang   Level: developer
1221b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1222b06ff27eSHong Zhang @*/
1223b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1224b06ff27eSHong Zhang {
1225b06ff27eSHong Zhang   PetscFunctionBegin;
1226b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1227b06ff27eSHong Zhang   dm->structure_only = only;
1228b06ff27eSHong Zhang   PetscFunctionReturn(0);
1229b06ff27eSHong Zhang }
1230b06ff27eSHong Zhang 
1231a89ea682SMatthew G Knepley /*@C
1232aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1233a89ea682SMatthew G Knepley 
1234a89ea682SMatthew G Knepley   Not Collective
1235a89ea682SMatthew G Knepley 
1236a89ea682SMatthew G Knepley   Input Parameters:
1237a89ea682SMatthew G Knepley + dm - the DM object
1238aa1993deSMatthew G Knepley . count - The minium size
123969291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1240a89ea682SMatthew G Knepley 
1241a89ea682SMatthew G Knepley   Output Parameter:
1242a89ea682SMatthew G Knepley . array - the work array
1243a89ea682SMatthew G Knepley 
1244a89ea682SMatthew G Knepley   Level: developer
1245a89ea682SMatthew G Knepley 
1246a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1247a89ea682SMatthew G Knepley @*/
124869291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1249a89ea682SMatthew G Knepley {
1250a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1251aa1993deSMatthew G Knepley   DMWorkLink     link;
125269291d52SBarry Smith   PetscMPIInt    dsize;
1253a89ea682SMatthew G Knepley 
1254a89ea682SMatthew G Knepley   PetscFunctionBegin;
1255a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1256aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1257aa1993deSMatthew G Knepley   if (dm->workin) {
1258aa1993deSMatthew G Knepley     link       = dm->workin;
1259aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1260aa1993deSMatthew G Knepley   } else {
1261b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1262a89ea682SMatthew G Knepley   }
126369291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
12645056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1265aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1266854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1267854ce69bSBarry Smith     link->bytes = dsize*count;
1268aa1993deSMatthew G Knepley   }
1269aa1993deSMatthew G Knepley   link->next   = dm->workout;
1270aa1993deSMatthew G Knepley   dm->workout  = link;
1271aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1272a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1273a89ea682SMatthew G Knepley }
1274a89ea682SMatthew G Knepley 
1275aa1993deSMatthew G Knepley /*@C
1276aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1277aa1993deSMatthew G Knepley 
1278aa1993deSMatthew G Knepley   Not Collective
1279aa1993deSMatthew G Knepley 
1280aa1993deSMatthew G Knepley   Input Parameters:
1281aa1993deSMatthew G Knepley + dm - the DM object
1282aa1993deSMatthew G Knepley . count - The minium size
128369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1284aa1993deSMatthew G Knepley 
1285aa1993deSMatthew G Knepley   Output Parameter:
1286aa1993deSMatthew G Knepley . array - the work array
1287aa1993deSMatthew G Knepley 
1288aa1993deSMatthew G Knepley   Level: developer
1289aa1993deSMatthew G Knepley 
129069291d52SBarry Smith   Developer Notes: count and dtype are ignored, they are only needed for DMGetWorkArray()
1291aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1292aa1993deSMatthew G Knepley @*/
129369291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1294aa1993deSMatthew G Knepley {
1295aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1296aa1993deSMatthew G Knepley 
1297aa1993deSMatthew G Knepley   PetscFunctionBegin;
1298aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1299aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1300aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1301aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1302aa1993deSMatthew G Knepley       *p           = link->next;
1303aa1993deSMatthew G Knepley       link->next   = dm->workin;
1304aa1993deSMatthew G Knepley       dm->workin   = link;
13050298fd71SBarry Smith       *(void**)mem = NULL;
1306aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1307aa1993deSMatthew G Knepley     }
1308aa1993deSMatthew G Knepley   }
1309aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1310aa1993deSMatthew G Knepley }
1311e7c4fc90SDmitry Karpeev 
1312435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1313435a35e8SMatthew G Knepley {
1314435a35e8SMatthew G Knepley   PetscFunctionBegin;
1315435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
131682f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1317435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1318435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1319435a35e8SMatthew G Knepley }
1320435a35e8SMatthew G Knepley 
13214f3b5142SJed Brown /*@C
13224d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
13234d343eeaSMatthew G Knepley 
13244d343eeaSMatthew G Knepley   Not collective
13254d343eeaSMatthew G Knepley 
13264d343eeaSMatthew G Knepley   Input Parameter:
13274d343eeaSMatthew G Knepley . dm - the DM object
13284d343eeaSMatthew G Knepley 
13294d343eeaSMatthew G Knepley   Output Parameters:
13300298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13310298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13320298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13334d343eeaSMatthew G Knepley 
13344d343eeaSMatthew G Knepley   Level: intermediate
13354d343eeaSMatthew G Knepley 
133621c9b008SJed Brown   Notes:
133721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
133821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
133921c9b008SJed Brown   PetscFree().
134021c9b008SJed Brown 
13414d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13424d343eeaSMatthew G Knepley @*/
134337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13444d343eeaSMatthew G Knepley {
134537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13464d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13474d343eeaSMatthew G Knepley 
13484d343eeaSMatthew G Knepley   PetscFunctionBegin;
13494d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
135069ca1f37SDmitry Karpeev   if (numFields) {
135169ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
135269ca1f37SDmitry Karpeev     *numFields = 0;
135369ca1f37SDmitry Karpeev   }
135437d0c07bSMatthew G Knepley   if (fieldNames) {
135537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13560298fd71SBarry Smith     *fieldNames = NULL;
135769ca1f37SDmitry Karpeev   }
135869ca1f37SDmitry Karpeev   if (fields) {
135969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13600298fd71SBarry Smith     *fields = NULL;
136169ca1f37SDmitry Karpeev   }
136237d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
136337d0c07bSMatthew G Knepley   if (section) {
136437d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
136537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
136637d0c07bSMatthew G Knepley 
136737d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
136837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1369dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
137037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
137137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
137237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
137337d0c07bSMatthew G Knepley     }
137437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
137537d0c07bSMatthew G Knepley       PetscInt gdof;
137637d0c07bSMatthew G Knepley 
137737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
137837d0c07bSMatthew G Knepley       if (gdof > 0) {
137937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
138037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
138137d0c07bSMatthew G Knepley 
138237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
138337d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
138437d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
138537d0c07bSMatthew G Knepley         }
138637d0c07bSMatthew G Knepley       }
138737d0c07bSMatthew G Knepley     }
138837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1389785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
139037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
139137d0c07bSMatthew G Knepley     }
139237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
139337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
139437d0c07bSMatthew G Knepley 
139537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
139637d0c07bSMatthew G Knepley       if (gdof > 0) {
139737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
139837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
139937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
140037d0c07bSMatthew G Knepley 
140137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
140237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
140337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
140437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
140537d0c07bSMatthew G Knepley           }
140637d0c07bSMatthew G Knepley         }
140737d0c07bSMatthew G Knepley       }
140837d0c07bSMatthew G Knepley     }
14098865f1eaSKarl Rupp     if (numFields) *numFields = nF;
141037d0c07bSMatthew G Knepley     if (fieldNames) {
1411785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
141237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
141337d0c07bSMatthew G Knepley         const char *fieldName;
141437d0c07bSMatthew G Knepley 
141537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
141637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
141737d0c07bSMatthew G Knepley       }
141837d0c07bSMatthew G Knepley     }
141937d0c07bSMatthew G Knepley     if (fields) {
1420785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
142137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
142282f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
142337d0c07bSMatthew G Knepley       }
142437d0c07bSMatthew G Knepley     }
142537d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
14268865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
14278865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
142869ca1f37SDmitry Karpeev   }
14294d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14304d343eeaSMatthew G Knepley }
14314d343eeaSMatthew G Knepley 
143216621825SDmitry Karpeev 
143316621825SDmitry Karpeev /*@C
143416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
143516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
143616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1437e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1438e7c4fc90SDmitry Karpeev 
1439e7c4fc90SDmitry Karpeev   Not collective
1440e7c4fc90SDmitry Karpeev 
1441e7c4fc90SDmitry Karpeev   Input Parameter:
1442e7c4fc90SDmitry Karpeev . dm - the DM object
1443e7c4fc90SDmitry Karpeev 
1444e7c4fc90SDmitry Karpeev   Output Parameters:
14450298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14460298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14470298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14480298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1449e7c4fc90SDmitry Karpeev 
1450e7c4fc90SDmitry Karpeev   Level: intermediate
1451e7c4fc90SDmitry Karpeev 
1452e7c4fc90SDmitry Karpeev   Notes:
1453e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1454e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1455e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1456e7c4fc90SDmitry Karpeev 
1457e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1458e7c4fc90SDmitry Karpeev @*/
145916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1460e7c4fc90SDmitry Karpeev {
1461e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1462e7c4fc90SDmitry Karpeev 
1463e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1464e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14658865f1eaSKarl Rupp   if (len) {
14668865f1eaSKarl Rupp     PetscValidPointer(len,2);
14678865f1eaSKarl Rupp     *len = 0;
14688865f1eaSKarl Rupp   }
14698865f1eaSKarl Rupp   if (namelist) {
14708865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14718865f1eaSKarl Rupp     *namelist = 0;
14728865f1eaSKarl Rupp   }
14738865f1eaSKarl Rupp   if (islist) {
14748865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14758865f1eaSKarl Rupp     *islist = 0;
14768865f1eaSKarl Rupp   }
14778865f1eaSKarl Rupp   if (dmlist) {
14788865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14798865f1eaSKarl Rupp     *dmlist = 0;
14808865f1eaSKarl Rupp   }
1481f3f0edfdSDmitry Karpeev   /*
1482f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1483f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1484f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1485f3f0edfdSDmitry Karpeev    */
1486ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
148716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1488435a35e8SMatthew G Knepley     PetscSection section;
1489435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1490435a35e8SMatthew G Knepley 
1491435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1492435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1493435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1494f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
149503dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
149603dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
149703dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1498435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1499435a35e8SMatthew G Knepley         const char *fieldName;
1500435a35e8SMatthew G Knepley 
150103dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
150203dc3394SMatthew G. Knepley         if (namelist) {
1503435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1504435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1505435a35e8SMatthew G Knepley         }
150603dc3394SMatthew G. Knepley       }
1507435a35e8SMatthew G Knepley     } else {
150869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1509e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
15100298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1511e7c4fc90SDmitry Karpeev     }
15128865f1eaSKarl Rupp   } else {
151316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
151416621825SDmitry Karpeev   }
151516621825SDmitry Karpeev   PetscFunctionReturn(0);
151616621825SDmitry Karpeev }
151716621825SDmitry Karpeev 
1518564cec59SMatthew G. Knepley /*@
1519435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1520435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1521435a35e8SMatthew G Knepley 
1522435a35e8SMatthew G Knepley   Not collective
1523435a35e8SMatthew G Knepley 
1524435a35e8SMatthew G Knepley   Input Parameters:
15252adcc780SMatthew G. Knepley + dm        - The DM object
15262adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
15272adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1528435a35e8SMatthew G Knepley 
1529435a35e8SMatthew G Knepley   Output Parameters:
15302adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15312adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1532435a35e8SMatthew G Knepley 
1533435a35e8SMatthew G Knepley   Level: intermediate
1534435a35e8SMatthew G Knepley 
1535435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1536435a35e8SMatthew G Knepley @*/
153737bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1538435a35e8SMatthew G Knepley {
1539435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1540435a35e8SMatthew G Knepley 
1541435a35e8SMatthew G Knepley   PetscFunctionBegin;
1542435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1543435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15448865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15458865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1546435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1547435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
154882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1549435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1550435a35e8SMatthew G Knepley }
1551435a35e8SMatthew G Knepley 
15522adcc780SMatthew G. Knepley /*@C
15532adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
15542adcc780SMatthew G. Knepley 
15552adcc780SMatthew G. Knepley   Not collective
15562adcc780SMatthew G. Knepley 
15572adcc780SMatthew G. Knepley   Input Parameter:
15582adcc780SMatthew G. Knepley + dms - The DM objects
15592adcc780SMatthew G. Knepley - len - The number of DMs
15602adcc780SMatthew G. Knepley 
15612adcc780SMatthew G. Knepley   Output Parameters:
15622adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15632adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
15642adcc780SMatthew G. Knepley 
15652adcc780SMatthew G. Knepley   Level: intermediate
15662adcc780SMatthew G. Knepley 
15672adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
15682adcc780SMatthew G. Knepley @*/
15692adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
15702adcc780SMatthew G. Knepley {
15712adcc780SMatthew G. Knepley   PetscInt       i;
15722adcc780SMatthew G. Knepley   PetscErrorCode ierr;
15732adcc780SMatthew G. Knepley 
15742adcc780SMatthew G. Knepley   PetscFunctionBegin;
15752adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
15762adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
15772adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
15782adcc780SMatthew G. Knepley   if (superdm) PetscValidPointer(superdm,4);
15792adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
15802adcc780SMatthew G. Knepley   if (len) {
15812adcc780SMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {
15822adcc780SMatthew G. Knepley       ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
15832adcc780SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
15842adcc780SMatthew G. Knepley   }
15852adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
15862adcc780SMatthew G. Knepley }
15872adcc780SMatthew G. Knepley 
158816621825SDmitry Karpeev 
158916621825SDmitry Karpeev /*@C
15908d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
15918d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
15928d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
15938d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
15948d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
159516621825SDmitry Karpeev 
159616621825SDmitry Karpeev   Not collective
159716621825SDmitry Karpeev 
159816621825SDmitry Karpeev   Input Parameter:
159916621825SDmitry Karpeev . dm - the DM object
160016621825SDmitry Karpeev 
160116621825SDmitry Karpeev   Output Parameters:
16020298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
16030298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
16040298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
16050298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
16060298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
160716621825SDmitry Karpeev 
160816621825SDmitry Karpeev   Level: intermediate
160916621825SDmitry Karpeev 
161016621825SDmitry Karpeev   Notes:
161116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
161216621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
161316621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
161416621825SDmitry Karpeev 
16158d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
161616621825SDmitry Karpeev @*/
16178d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
161816621825SDmitry Karpeev {
161916621825SDmitry Karpeev   PetscErrorCode      ierr;
1620be081cd6SPeter Brune   DMSubDomainHookLink link;
1621be081cd6SPeter Brune   PetscInt            i,l;
162216621825SDmitry Karpeev 
162316621825SDmitry Karpeev   PetscFunctionBegin;
162416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
162514a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
16260298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
16270298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
16280298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
16290298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1630f3f0edfdSDmitry Karpeev   /*
1631f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1632f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1633f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1634f3f0edfdSDmitry Karpeev    */
1635ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
163616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1637be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
163814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1639f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1640be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1641be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1642be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1643be081cd6SPeter Brune         }
1644648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1645e7c4fc90SDmitry Karpeev       }
164614a18fd3SPeter Brune     }
164714a18fd3SPeter Brune     if (len) *len = l;
164814a18fd3SPeter Brune   }
1649e30e807fSPeter Brune   PetscFunctionReturn(0);
1650e30e807fSPeter Brune }
1651e30e807fSPeter Brune 
1652e30e807fSPeter Brune 
1653e30e807fSPeter Brune /*@C
1654e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1655e30e807fSPeter Brune 
1656e30e807fSPeter Brune   Not collective
1657e30e807fSPeter Brune 
1658e30e807fSPeter Brune   Input Parameters:
1659e30e807fSPeter Brune + dm - the DM object
1660e30e807fSPeter Brune . n  - the number of subdomain scatters
1661e30e807fSPeter Brune - subdms - the local subdomains
1662e30e807fSPeter Brune 
1663e30e807fSPeter Brune   Output Parameters:
1664e30e807fSPeter Brune + n     - the number of scatters returned
1665e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1666e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1667e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1668e30e807fSPeter Brune 
1669e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1670e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1671e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1672e30e807fSPeter Brune   solution and residual data.
1673e30e807fSPeter Brune 
1674e30e807fSPeter Brune   Level: developer
1675e30e807fSPeter Brune 
1676e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1677e30e807fSPeter Brune @*/
1678e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1679e30e807fSPeter Brune {
1680e30e807fSPeter Brune   PetscErrorCode ierr;
1681e30e807fSPeter Brune 
1682e30e807fSPeter Brune   PetscFunctionBegin;
1683e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1684e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1685e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1686e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
16876668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1688e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1689e7c4fc90SDmitry Karpeev }
1690e7c4fc90SDmitry Karpeev 
169147c6ae99SBarry Smith /*@
169247c6ae99SBarry Smith   DMRefine - Refines a DM object
169347c6ae99SBarry Smith 
169447c6ae99SBarry Smith   Collective on DM
169547c6ae99SBarry Smith 
169647c6ae99SBarry Smith   Input Parameter:
169747c6ae99SBarry Smith + dm   - the DM object
169891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
169947c6ae99SBarry Smith 
170047c6ae99SBarry Smith   Output Parameter:
17010298fd71SBarry Smith . dmf - the refined DM, or NULL
1702ae0a1c52SMatthew G Knepley 
17030298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
170447c6ae99SBarry Smith 
170547c6ae99SBarry Smith   Level: developer
170647c6ae99SBarry Smith 
1707e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
170847c6ae99SBarry Smith @*/
17097087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
171047c6ae99SBarry Smith {
171147c6ae99SBarry Smith   PetscErrorCode   ierr;
1712c833c3b5SJed Brown   DMRefineHookLink link;
171347c6ae99SBarry Smith 
171447c6ae99SBarry Smith   PetscFunctionBegin;
1715732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17161ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1717fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
171847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
17194057135bSMatthew G Knepley   if (*dmf) {
172043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
17218865f1eaSKarl Rupp 
17228cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
17238865f1eaSKarl Rupp 
1724644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
17250598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1726656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
17278865f1eaSKarl Rupp 
1728e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1729c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
17308865f1eaSKarl Rupp       if (link->refinehook) {
17318865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
17328865f1eaSKarl Rupp       }
1733c833c3b5SJed Brown     }
1734c833c3b5SJed Brown   }
17351ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1736c833c3b5SJed Brown   PetscFunctionReturn(0);
1737c833c3b5SJed Brown }
1738c833c3b5SJed Brown 
1739bb9467b5SJed Brown /*@C
1740c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1741c833c3b5SJed Brown 
1742c833c3b5SJed Brown    Logically Collective
1743c833c3b5SJed Brown 
1744c833c3b5SJed Brown    Input Arguments:
1745c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1746c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1747c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17480298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1749c833c3b5SJed Brown 
1750c833c3b5SJed Brown    Calling sequence of refinehook:
1751c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1752c833c3b5SJed Brown 
1753c833c3b5SJed Brown +  coarse - coarse level DM
1754c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1755c833c3b5SJed Brown -  ctx - optional user-defined function context
1756c833c3b5SJed Brown 
1757c833c3b5SJed Brown    Calling sequence for interphook:
1758c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1759c833c3b5SJed Brown 
1760c833c3b5SJed Brown +  coarse - coarse level DM
1761c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1762c833c3b5SJed Brown .  fine - fine level DM to update
1763c833c3b5SJed Brown -  ctx - optional user-defined function context
1764c833c3b5SJed Brown 
1765c833c3b5SJed Brown    Level: advanced
1766c833c3b5SJed Brown 
1767c833c3b5SJed Brown    Notes:
1768c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1769c833c3b5SJed Brown 
1770c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1771c833c3b5SJed Brown 
1772bb9467b5SJed Brown    This function is currently not available from Fortran.
1773bb9467b5SJed Brown 
1774c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1775c833c3b5SJed Brown @*/
1776c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1777c833c3b5SJed Brown {
1778c833c3b5SJed Brown   PetscErrorCode   ierr;
1779c833c3b5SJed Brown   DMRefineHookLink link,*p;
1780c833c3b5SJed Brown 
1781c833c3b5SJed Brown   PetscFunctionBegin;
1782c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
17833d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
17843d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
17853d8e3701SJed Brown   }
178695dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1787c833c3b5SJed Brown   link->refinehook = refinehook;
1788c833c3b5SJed Brown   link->interphook = interphook;
1789c833c3b5SJed Brown   link->ctx        = ctx;
17900298fd71SBarry Smith   link->next       = NULL;
1791c833c3b5SJed Brown   *p               = link;
1792c833c3b5SJed Brown   PetscFunctionReturn(0);
1793c833c3b5SJed Brown }
1794c833c3b5SJed Brown 
17953d8e3701SJed Brown /*@C
17963d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
17973d8e3701SJed Brown 
17983d8e3701SJed Brown    Logically Collective
17993d8e3701SJed Brown 
18003d8e3701SJed Brown    Input Arguments:
18013d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
18023d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
18033d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18043d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
18053d8e3701SJed Brown 
18063d8e3701SJed Brown    Level: advanced
18073d8e3701SJed Brown 
18083d8e3701SJed Brown    Notes:
18093d8e3701SJed Brown    This function does nothing if the hook is not in the list.
18103d8e3701SJed Brown 
18113d8e3701SJed Brown    This function is currently not available from Fortran.
18123d8e3701SJed Brown 
18133d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
18143d8e3701SJed Brown @*/
18153d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
18163d8e3701SJed Brown {
18173d8e3701SJed Brown   PetscErrorCode   ierr;
18183d8e3701SJed Brown   DMRefineHookLink link,*p;
18193d8e3701SJed Brown 
18203d8e3701SJed Brown   PetscFunctionBegin;
18213d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
18223d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
18233d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
18243d8e3701SJed Brown       link = *p;
18253d8e3701SJed Brown       *p = link->next;
18263d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
18273d8e3701SJed Brown       break;
18283d8e3701SJed Brown     }
18293d8e3701SJed Brown   }
18303d8e3701SJed Brown   PetscFunctionReturn(0);
18313d8e3701SJed Brown }
18323d8e3701SJed Brown 
1833c833c3b5SJed Brown /*@
1834c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1835c833c3b5SJed Brown 
1836c833c3b5SJed Brown    Collective if any hooks are
1837c833c3b5SJed Brown 
1838c833c3b5SJed Brown    Input Arguments:
1839c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1840e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1841c833c3b5SJed Brown -  fine - finer DM to update
1842c833c3b5SJed Brown 
1843c833c3b5SJed Brown    Level: developer
1844c833c3b5SJed Brown 
1845c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1846c833c3b5SJed Brown @*/
1847c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1848c833c3b5SJed Brown {
1849c833c3b5SJed Brown   PetscErrorCode   ierr;
1850c833c3b5SJed Brown   DMRefineHookLink link;
1851c833c3b5SJed Brown 
1852c833c3b5SJed Brown   PetscFunctionBegin;
1853c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
18548865f1eaSKarl Rupp     if (link->interphook) {
18558865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
18568865f1eaSKarl Rupp     }
18574057135bSMatthew G Knepley   }
185847c6ae99SBarry Smith   PetscFunctionReturn(0);
185947c6ae99SBarry Smith }
186047c6ae99SBarry Smith 
1861eb3f98d2SBarry Smith /*@
1862eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1863eb3f98d2SBarry Smith 
1864eb3f98d2SBarry Smith     Not Collective
1865eb3f98d2SBarry Smith 
1866eb3f98d2SBarry Smith     Input Parameter:
1867eb3f98d2SBarry Smith .   dm - the DM object
1868eb3f98d2SBarry Smith 
1869eb3f98d2SBarry Smith     Output Parameter:
1870eb3f98d2SBarry Smith .   level - number of refinements
1871eb3f98d2SBarry Smith 
1872eb3f98d2SBarry Smith     Level: developer
1873eb3f98d2SBarry Smith 
18746a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1875eb3f98d2SBarry Smith 
1876eb3f98d2SBarry Smith @*/
1877eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1878eb3f98d2SBarry Smith {
1879eb3f98d2SBarry Smith   PetscFunctionBegin;
1880eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1881eb3f98d2SBarry Smith   *level = dm->levelup;
1882eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1883eb3f98d2SBarry Smith }
1884eb3f98d2SBarry Smith 
1885fef3a512SBarry Smith /*@
1886fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1887fef3a512SBarry Smith 
1888fef3a512SBarry Smith     Not Collective
1889fef3a512SBarry Smith 
1890fef3a512SBarry Smith     Input Parameter:
1891fef3a512SBarry Smith +   dm - the DM object
1892fef3a512SBarry Smith -   level - number of refinements
1893fef3a512SBarry Smith 
1894fef3a512SBarry Smith     Level: advanced
1895fef3a512SBarry Smith 
1896fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1897fef3a512SBarry Smith 
1898fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1899fef3a512SBarry Smith 
1900fef3a512SBarry Smith @*/
1901fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1902fef3a512SBarry Smith {
1903fef3a512SBarry Smith   PetscFunctionBegin;
1904fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1905fef3a512SBarry Smith   dm->levelup = level;
1906fef3a512SBarry Smith   PetscFunctionReturn(0);
1907fef3a512SBarry Smith }
1908fef3a512SBarry Smith 
1909bb9467b5SJed Brown /*@C
1910baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1911baf369e7SPeter Brune 
1912baf369e7SPeter Brune    Logically Collective
1913baf369e7SPeter Brune 
1914baf369e7SPeter Brune    Input Arguments:
1915baf369e7SPeter Brune +  dm - the DM
1916baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1917baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
19180298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1919baf369e7SPeter Brune 
1920baf369e7SPeter Brune    Calling sequence for beginhook:
1921baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1922baf369e7SPeter Brune 
1923baf369e7SPeter Brune +  dm - global DM
1924baf369e7SPeter Brune .  g - global vector
1925baf369e7SPeter Brune .  mode - mode
1926baf369e7SPeter Brune .  l - local vector
1927baf369e7SPeter Brune -  ctx - optional user-defined function context
1928baf369e7SPeter Brune 
1929baf369e7SPeter Brune 
1930baf369e7SPeter Brune    Calling sequence for endhook:
1931ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1932baf369e7SPeter Brune 
1933baf369e7SPeter Brune +  global - global DM
1934baf369e7SPeter Brune -  ctx - optional user-defined function context
1935baf369e7SPeter Brune 
1936baf369e7SPeter Brune    Level: advanced
1937baf369e7SPeter Brune 
1938baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1939baf369e7SPeter Brune @*/
1940baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1941baf369e7SPeter Brune {
1942baf369e7SPeter Brune   PetscErrorCode          ierr;
1943baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1944baf369e7SPeter Brune 
1945baf369e7SPeter Brune   PetscFunctionBegin;
1946baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1947baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
194895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1949baf369e7SPeter Brune   link->beginhook = beginhook;
1950baf369e7SPeter Brune   link->endhook   = endhook;
1951baf369e7SPeter Brune   link->ctx       = ctx;
19520298fd71SBarry Smith   link->next      = NULL;
1953baf369e7SPeter Brune   *p              = link;
1954baf369e7SPeter Brune   PetscFunctionReturn(0);
1955baf369e7SPeter Brune }
1956baf369e7SPeter Brune 
19574c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
19584c274da1SToby Isaac {
19594c274da1SToby Isaac   Mat cMat;
19604c274da1SToby Isaac   Vec cVec;
19614c274da1SToby Isaac   PetscSection section, cSec;
19624c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19634c274da1SToby Isaac   PetscErrorCode ierr;
19644c274da1SToby Isaac 
19654c274da1SToby Isaac   PetscFunctionBegin;
19664c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19674c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19684c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
19695db9a05bSToby Isaac     PetscInt nRows;
19705db9a05bSToby Isaac 
19715db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
19725db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
19734c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19747711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19754c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
19764c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19774c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19784c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19794c274da1SToby Isaac       if (dof) {
19804c274da1SToby Isaac         PetscScalar *vals;
19814c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
19824c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
19834c274da1SToby Isaac       }
19844c274da1SToby Isaac     }
19854c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19864c274da1SToby Isaac   }
19874c274da1SToby Isaac   PetscFunctionReturn(0);
19884c274da1SToby Isaac }
19894c274da1SToby Isaac 
199047c6ae99SBarry Smith /*@
199147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
199247c6ae99SBarry Smith 
199347c6ae99SBarry Smith     Neighbor-wise Collective on DM
199447c6ae99SBarry Smith 
199547c6ae99SBarry Smith     Input Parameters:
199647c6ae99SBarry Smith +   dm - the DM object
199747c6ae99SBarry Smith .   g - the global vector
199847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
199947c6ae99SBarry Smith -   l - the local vector
200047c6ae99SBarry Smith 
200147c6ae99SBarry Smith 
200247c6ae99SBarry Smith     Level: beginner
200347c6ae99SBarry Smith 
2004e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
200547c6ae99SBarry Smith 
200647c6ae99SBarry Smith @*/
20077087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
200847c6ae99SBarry Smith {
20097128ae9fSMatthew G Knepley   PetscSF                 sf;
201047c6ae99SBarry Smith   PetscErrorCode          ierr;
2011baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
201247c6ae99SBarry Smith 
201347c6ae99SBarry Smith   PetscFunctionBegin;
2014171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2015baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
20168865f1eaSKarl Rupp     if (link->beginhook) {
20178865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
20188865f1eaSKarl Rupp     }
2019baf369e7SPeter Brune   }
20207128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20217128ae9fSMatthew G Knepley   if (sf) {
2022ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2023ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
20247128ae9fSMatthew G Knepley 
202582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20267128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2027ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20287128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20297128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2030ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20317128ae9fSMatthew G Knepley   } else {
2032843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20337128ae9fSMatthew G Knepley   }
203447c6ae99SBarry Smith   PetscFunctionReturn(0);
203547c6ae99SBarry Smith }
203647c6ae99SBarry Smith 
203747c6ae99SBarry Smith /*@
203847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
203947c6ae99SBarry Smith 
204047c6ae99SBarry Smith     Neighbor-wise Collective on DM
204147c6ae99SBarry Smith 
204247c6ae99SBarry Smith     Input Parameters:
204347c6ae99SBarry Smith +   dm - the DM object
204447c6ae99SBarry Smith .   g - the global vector
204547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
204647c6ae99SBarry Smith -   l - the local vector
204747c6ae99SBarry Smith 
204847c6ae99SBarry Smith 
204947c6ae99SBarry Smith     Level: beginner
205047c6ae99SBarry Smith 
2051e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
205247c6ae99SBarry Smith 
205347c6ae99SBarry Smith @*/
20547087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
205547c6ae99SBarry Smith {
20567128ae9fSMatthew G Knepley   PetscSF                 sf;
205747c6ae99SBarry Smith   PetscErrorCode          ierr;
2058ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2059ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2060baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
206147c6ae99SBarry Smith 
206247c6ae99SBarry Smith   PetscFunctionBegin;
2063171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20647128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20657128ae9fSMatthew G Knepley   if (sf) {
206682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20677128ae9fSMatthew G Knepley 
20687128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2069ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20707128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20717128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2072ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20737128ae9fSMatthew G Knepley   } else {
2074843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20757128ae9fSMatthew G Knepley   }
20764c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2077baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2078baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2079baf369e7SPeter Brune   }
208047c6ae99SBarry Smith   PetscFunctionReturn(0);
208147c6ae99SBarry Smith }
208247c6ae99SBarry Smith 
2083d4d07f1eSToby Isaac /*@C
2084d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2085d4d07f1eSToby Isaac 
2086d4d07f1eSToby Isaac    Logically Collective
2087d4d07f1eSToby Isaac 
2088d4d07f1eSToby Isaac    Input Arguments:
2089d4d07f1eSToby Isaac +  dm - the DM
2090d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2091d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2092d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2093d4d07f1eSToby Isaac 
2094d4d07f1eSToby Isaac    Calling sequence for beginhook:
2095d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2096d4d07f1eSToby Isaac 
2097d4d07f1eSToby Isaac +  dm - global DM
2098d4d07f1eSToby Isaac .  l - local vector
2099d4d07f1eSToby Isaac .  mode - mode
2100d4d07f1eSToby Isaac .  g - global vector
2101d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2102d4d07f1eSToby Isaac 
2103d4d07f1eSToby Isaac 
2104d4d07f1eSToby Isaac    Calling sequence for endhook:
2105d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2106d4d07f1eSToby Isaac 
2107d4d07f1eSToby Isaac +  global - global DM
2108d4d07f1eSToby Isaac .  l - local vector
2109d4d07f1eSToby Isaac .  mode - mode
2110d4d07f1eSToby Isaac .  g - global vector
2111d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2112d4d07f1eSToby Isaac 
2113d4d07f1eSToby Isaac    Level: advanced
2114d4d07f1eSToby Isaac 
2115d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2116d4d07f1eSToby Isaac @*/
2117d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2118d4d07f1eSToby Isaac {
2119d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2120d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2121d4d07f1eSToby Isaac 
2122d4d07f1eSToby Isaac   PetscFunctionBegin;
2123d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2124d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
212595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2126d4d07f1eSToby Isaac   link->beginhook = beginhook;
2127d4d07f1eSToby Isaac   link->endhook   = endhook;
2128d4d07f1eSToby Isaac   link->ctx       = ctx;
2129d4d07f1eSToby Isaac   link->next      = NULL;
2130d4d07f1eSToby Isaac   *p              = link;
2131d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2132d4d07f1eSToby Isaac }
2133d4d07f1eSToby Isaac 
21344c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
21354c274da1SToby Isaac {
21364c274da1SToby Isaac   Mat cMat;
21374c274da1SToby Isaac   Vec cVec;
21384c274da1SToby Isaac   PetscSection section, cSec;
21394c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21404c274da1SToby Isaac   PetscErrorCode ierr;
21414c274da1SToby Isaac 
21424c274da1SToby Isaac   PetscFunctionBegin;
21434c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21444c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21454c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
21465db9a05bSToby Isaac     PetscInt nRows;
21475db9a05bSToby Isaac 
21485db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21495db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
21504c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
21517711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21524c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21534c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21544c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21554c274da1SToby Isaac       if (dof) {
21564c274da1SToby Isaac         PetscInt d;
21574c274da1SToby Isaac         PetscScalar *vals;
21584c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
21594c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
21604c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
21614c274da1SToby Isaac          * we just extracted */
21624c274da1SToby Isaac         for (d = 0; d < dof; d++) {
21634c274da1SToby Isaac           vals[d] = 0.;
21644c274da1SToby Isaac         }
21654c274da1SToby Isaac       }
21664c274da1SToby Isaac     }
21674c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
21684c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21694c274da1SToby Isaac   }
21704c274da1SToby Isaac   PetscFunctionReturn(0);
21714c274da1SToby Isaac }
21724c274da1SToby Isaac 
217347c6ae99SBarry Smith /*@
21749a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21759a42bb27SBarry Smith 
21769a42bb27SBarry Smith     Neighbor-wise Collective on DM
21779a42bb27SBarry Smith 
21789a42bb27SBarry Smith     Input Parameters:
21799a42bb27SBarry Smith +   dm - the DM object
2180f6813fd5SJed Brown .   l - the local vector
21811eb28f2eSBarry 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.
21821eb28f2eSBarry Smith -   g - the global vector
21839a42bb27SBarry Smith 
2184d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
218584330215SMatthew 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.
21869a42bb27SBarry Smith 
21879a42bb27SBarry Smith     Level: beginner
21889a42bb27SBarry Smith 
2189e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
21909a42bb27SBarry Smith 
21919a42bb27SBarry Smith @*/
21927087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
21939a42bb27SBarry Smith {
21947128ae9fSMatthew G Knepley   PetscSF                 sf;
219584330215SMatthew G. Knepley   PetscSection            s, gs;
2196d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2197ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2198ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
219984330215SMatthew G. Knepley   PetscBool               isInsert;
220084330215SMatthew G. Knepley   PetscErrorCode          ierr;
22019a42bb27SBarry Smith 
22029a42bb27SBarry Smith   PetscFunctionBegin;
2203171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2204d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2205d4d07f1eSToby Isaac     if (link->beginhook) {
2206d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2207d4d07f1eSToby Isaac     }
2208d4d07f1eSToby Isaac   }
22094c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
22107128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
221184330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22127128ae9fSMatthew G Knepley   switch (mode) {
22137128ae9fSMatthew G Knepley   case INSERT_VALUES:
22147128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2215304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
221684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22177128ae9fSMatthew G Knepley   case ADD_VALUES:
22187128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2219304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
222084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22217128ae9fSMatthew G Knepley   default:
222282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22237128ae9fSMatthew G Knepley   }
222484330215SMatthew G. Knepley   if (sf && !isInsert) {
2225ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22267128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2227a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2228ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
222984330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
223084330215SMatthew G. Knepley   } else if (s && isInsert) {
223184330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
223284330215SMatthew G. Knepley 
223384330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
223484330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
223584330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2236ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
223784330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
223884330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2239b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
224084330215SMatthew G. Knepley 
224184330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
224203442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
224384330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2244b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
224584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
224684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2247b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
224803442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2249b3b16f48SMatthew 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);
2250b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2251b3b16f48SMatthew G. Knepley       if (!gcdof) {
225284330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2253b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2254b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
225584330215SMatthew G. Knepley         const PetscInt *cdofs;
225684330215SMatthew G. Knepley         PetscInt        cind = 0;
225784330215SMatthew G. Knepley 
225884330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2259b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
226084330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2261b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
226284330215SMatthew G. Knepley         }
2263b3b16f48SMatthew 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);
226484330215SMatthew G. Knepley     }
2265ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22667128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
22677128ae9fSMatthew G Knepley   } else {
2268843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22697128ae9fSMatthew G Knepley   }
22709a42bb27SBarry Smith   PetscFunctionReturn(0);
22719a42bb27SBarry Smith }
22729a42bb27SBarry Smith 
22739a42bb27SBarry Smith /*@
22749a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
227547c6ae99SBarry Smith 
227647c6ae99SBarry Smith     Neighbor-wise Collective on DM
227747c6ae99SBarry Smith 
227847c6ae99SBarry Smith     Input Parameters:
227947c6ae99SBarry Smith +   dm - the DM object
2280f6813fd5SJed Brown .   l - the local vector
228147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2282f6813fd5SJed Brown -   g - the global vector
228347c6ae99SBarry Smith 
228447c6ae99SBarry Smith 
228547c6ae99SBarry Smith     Level: beginner
228647c6ae99SBarry Smith 
2287e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
228847c6ae99SBarry Smith 
228947c6ae99SBarry Smith @*/
22907087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
229147c6ae99SBarry Smith {
22927128ae9fSMatthew G Knepley   PetscSF                 sf;
229384330215SMatthew G. Knepley   PetscSection            s;
2294d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
229584330215SMatthew G. Knepley   PetscBool               isInsert;
229684330215SMatthew G. Knepley   PetscErrorCode          ierr;
229747c6ae99SBarry Smith 
229847c6ae99SBarry Smith   PetscFunctionBegin;
2299171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23007128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
230184330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
23027128ae9fSMatthew G Knepley   switch (mode) {
23037128ae9fSMatthew G Knepley   case INSERT_VALUES:
23047128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
230584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
23067128ae9fSMatthew G Knepley   case ADD_VALUES:
23077128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
230884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23097128ae9fSMatthew G Knepley   default:
231082f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23117128ae9fSMatthew G Knepley   }
231284330215SMatthew G. Knepley   if (sf && !isInsert) {
2313ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2314ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
231584330215SMatthew G. Knepley 
2316ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23177128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2318a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2319ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
23207128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
232184330215SMatthew G. Knepley   } else if (s && isInsert) {
23227128ae9fSMatthew G Knepley   } else {
2323843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
23247128ae9fSMatthew G Knepley   }
2325d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2326d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2327d4d07f1eSToby Isaac   }
232847c6ae99SBarry Smith   PetscFunctionReturn(0);
232947c6ae99SBarry Smith }
233047c6ae99SBarry Smith 
2331f089877aSRichard Tran Mills /*@
2332bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2333bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2334d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2335f089877aSRichard Tran Mills 
2336bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2337f089877aSRichard Tran Mills 
2338f089877aSRichard Tran Mills    Input Parameters:
2339f089877aSRichard Tran Mills +  dm - the DM object
2340bc0a1609SRichard Tran Mills .  g - the original local vector
2341bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2342f089877aSRichard Tran Mills 
2343bc0a1609SRichard Tran Mills    Output Parameter:
2344bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2345f089877aSRichard Tran Mills 
2346f089877aSRichard Tran Mills    Level: intermediate
2347f089877aSRichard Tran Mills 
2348bc0a1609SRichard Tran Mills    Notes:
2349bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2350bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2351bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2352bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2353bc0a1609SRichard Tran Mills 
2354bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2355bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2356f089877aSRichard Tran Mills 
2357f089877aSRichard Tran Mills @*/
2358f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2359f089877aSRichard Tran Mills {
2360f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2361f089877aSRichard Tran Mills 
2362f089877aSRichard Tran Mills   PetscFunctionBegin;
2363f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2364bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2365f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2366f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2367f089877aSRichard Tran Mills }
2368f089877aSRichard Tran Mills 
2369f089877aSRichard Tran Mills /*@
2370bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2371bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2372d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2373f089877aSRichard Tran Mills 
2374bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2375f089877aSRichard Tran Mills 
2376f089877aSRichard Tran Mills    Input Parameters:
2377bc0a1609SRichard Tran Mills +  da - the DM object
2378bc0a1609SRichard Tran Mills .  g - the original local vector
2379bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2380f089877aSRichard Tran Mills 
2381bc0a1609SRichard Tran Mills    Output Parameter:
2382bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2383f089877aSRichard Tran Mills 
2384f089877aSRichard Tran Mills    Level: intermediate
2385f089877aSRichard Tran Mills 
2386bc0a1609SRichard Tran Mills    Notes:
2387bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2388bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2389bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2390bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2391bc0a1609SRichard Tran Mills 
2392bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2393bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2394f089877aSRichard Tran Mills 
2395f089877aSRichard Tran Mills @*/
2396f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2397f089877aSRichard Tran Mills {
2398f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2399f089877aSRichard Tran Mills 
2400f089877aSRichard Tran Mills   PetscFunctionBegin;
2401f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2402bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2403f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2404f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2405f089877aSRichard Tran Mills }
2406f089877aSRichard Tran Mills 
2407f089877aSRichard Tran Mills 
240847c6ae99SBarry Smith /*@
240947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
241047c6ae99SBarry Smith 
241147c6ae99SBarry Smith     Collective on DM
241247c6ae99SBarry Smith 
241347c6ae99SBarry Smith     Input Parameter:
241447c6ae99SBarry Smith +   dm - the DM object
241591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
241647c6ae99SBarry Smith 
241747c6ae99SBarry Smith     Output Parameter:
241847c6ae99SBarry Smith .   dmc - the coarsened DM
241947c6ae99SBarry Smith 
242047c6ae99SBarry Smith     Level: developer
242147c6ae99SBarry Smith 
2422e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
242347c6ae99SBarry Smith 
242447c6ae99SBarry Smith @*/
24257087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
242647c6ae99SBarry Smith {
242747c6ae99SBarry Smith   PetscErrorCode    ierr;
2428b17ce1afSJed Brown   DMCoarsenHookLink link;
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith   PetscFunctionBegin;
2431171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2432fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
243347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
243447c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
24358892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2436a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
243743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
24388cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2439644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
24400598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2441656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2442e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2443b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2444b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2445b17ce1afSJed Brown   }
244647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2447b17ce1afSJed Brown   PetscFunctionReturn(0);
2448b17ce1afSJed Brown }
2449b17ce1afSJed Brown 
2450bb9467b5SJed Brown /*@C
2451b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2452b17ce1afSJed Brown 
2453b17ce1afSJed Brown    Logically Collective
2454b17ce1afSJed Brown 
2455b17ce1afSJed Brown    Input Arguments:
2456b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2457b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2458b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
24590298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2460b17ce1afSJed Brown 
2461b17ce1afSJed Brown    Calling sequence of coarsenhook:
2462b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2463b17ce1afSJed Brown 
2464b17ce1afSJed Brown +  fine - fine level DM
2465b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2466b17ce1afSJed Brown -  ctx - optional user-defined function context
2467b17ce1afSJed Brown 
2468b17ce1afSJed Brown    Calling sequence for restricthook:
2469c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2470b17ce1afSJed Brown 
2471b17ce1afSJed Brown +  fine - fine level DM
2472b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2473c833c3b5SJed Brown .  rscale - scaling vector for restriction
2474c833c3b5SJed Brown .  inject - matrix restricting by injection
2475b17ce1afSJed Brown .  coarse - coarse level DM to update
2476b17ce1afSJed Brown -  ctx - optional user-defined function context
2477b17ce1afSJed Brown 
2478b17ce1afSJed Brown    Level: advanced
2479b17ce1afSJed Brown 
2480b17ce1afSJed Brown    Notes:
2481b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2482b17ce1afSJed Brown 
2483b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2484b17ce1afSJed Brown 
2485b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2486b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2487b17ce1afSJed Brown 
2488bb9467b5SJed Brown    This function is currently not available from Fortran.
2489bb9467b5SJed Brown 
2490dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2491b17ce1afSJed Brown @*/
2492b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2493b17ce1afSJed Brown {
2494b17ce1afSJed Brown   PetscErrorCode    ierr;
2495b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2496b17ce1afSJed Brown 
2497b17ce1afSJed Brown   PetscFunctionBegin;
2498b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
24991e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
25001e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
25011e3d8eccSJed Brown   }
250295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2503b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2504b17ce1afSJed Brown   link->restricthook = restricthook;
2505b17ce1afSJed Brown   link->ctx          = ctx;
25060298fd71SBarry Smith   link->next         = NULL;
2507b17ce1afSJed Brown   *p                 = link;
2508b17ce1afSJed Brown   PetscFunctionReturn(0);
2509b17ce1afSJed Brown }
2510b17ce1afSJed Brown 
2511dc822a44SJed Brown /*@C
2512dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2513dc822a44SJed Brown 
2514dc822a44SJed Brown    Logically Collective
2515dc822a44SJed Brown 
2516dc822a44SJed Brown    Input Arguments:
2517dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2518dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2519dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2520dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2521dc822a44SJed Brown 
2522dc822a44SJed Brown    Level: advanced
2523dc822a44SJed Brown 
2524dc822a44SJed Brown    Notes:
2525dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2526dc822a44SJed Brown 
2527dc822a44SJed Brown    This function is currently not available from Fortran.
2528dc822a44SJed Brown 
2529dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2530dc822a44SJed Brown @*/
2531dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2532dc822a44SJed Brown {
2533dc822a44SJed Brown   PetscErrorCode    ierr;
2534dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2535dc822a44SJed Brown 
2536dc822a44SJed Brown   PetscFunctionBegin;
2537dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2538dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2539dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2540dc822a44SJed Brown       link = *p;
2541dc822a44SJed Brown       *p = link->next;
2542dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2543dc822a44SJed Brown       break;
2544dc822a44SJed Brown     }
2545dc822a44SJed Brown   }
2546dc822a44SJed Brown   PetscFunctionReturn(0);
2547dc822a44SJed Brown }
2548dc822a44SJed Brown 
2549dc822a44SJed Brown 
2550b17ce1afSJed Brown /*@
2551b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2552b17ce1afSJed Brown 
2553b17ce1afSJed Brown    Collective if any hooks are
2554b17ce1afSJed Brown 
2555b17ce1afSJed Brown    Input Arguments:
2556b17ce1afSJed Brown +  fine - finer DM to use as a base
2557b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2558e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2559b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2560e91eccc2SStefano Zampini -  coarse - coarser DM to update
2561b17ce1afSJed Brown 
2562b17ce1afSJed Brown    Level: developer
2563b17ce1afSJed Brown 
2564b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2565b17ce1afSJed Brown @*/
2566b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2567b17ce1afSJed Brown {
2568b17ce1afSJed Brown   PetscErrorCode    ierr;
2569b17ce1afSJed Brown   DMCoarsenHookLink link;
2570b17ce1afSJed Brown 
2571b17ce1afSJed Brown   PetscFunctionBegin;
2572b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
25738865f1eaSKarl Rupp     if (link->restricthook) {
25748865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
25758865f1eaSKarl Rupp     }
2576b17ce1afSJed Brown   }
257747c6ae99SBarry Smith   PetscFunctionReturn(0);
257847c6ae99SBarry Smith }
257947c6ae99SBarry Smith 
2580bb9467b5SJed Brown /*@C
2581be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
25825dbd56e3SPeter Brune 
25835dbd56e3SPeter Brune    Logically Collective
25845dbd56e3SPeter Brune 
25855dbd56e3SPeter Brune    Input Arguments:
25865dbd56e3SPeter Brune +  global - global DM
2587ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
25885dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
25890298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
25905dbd56e3SPeter Brune 
2591ec4806b8SPeter Brune 
2592ec4806b8SPeter Brune    Calling sequence for ddhook:
2593ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2594ec4806b8SPeter Brune 
2595ec4806b8SPeter Brune +  global - global DM
2596ec4806b8SPeter Brune .  block  - block DM
2597ec4806b8SPeter Brune -  ctx - optional user-defined function context
2598ec4806b8SPeter Brune 
25995dbd56e3SPeter Brune    Calling sequence for restricthook:
2600ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
26015dbd56e3SPeter Brune 
26025dbd56e3SPeter Brune +  global - global DM
26035dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
26045dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2605ec4806b8SPeter Brune .  block  - block DM
26065dbd56e3SPeter Brune -  ctx - optional user-defined function context
26075dbd56e3SPeter Brune 
26085dbd56e3SPeter Brune    Level: advanced
26095dbd56e3SPeter Brune 
26105dbd56e3SPeter Brune    Notes:
2611ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
26125dbd56e3SPeter Brune 
26135dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
26145dbd56e3SPeter Brune 
26155dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2616ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
26175dbd56e3SPeter Brune 
2618bb9467b5SJed Brown    This function is currently not available from Fortran.
2619bb9467b5SJed Brown 
26205dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
26215dbd56e3SPeter Brune @*/
2622be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
26235dbd56e3SPeter Brune {
26245dbd56e3SPeter Brune   PetscErrorCode      ierr;
2625be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
26265dbd56e3SPeter Brune 
26275dbd56e3SPeter Brune   PetscFunctionBegin;
26285dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2629b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2630b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2631b3a6b972SJed Brown   }
263295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
26335dbd56e3SPeter Brune   link->restricthook = restricthook;
2634be081cd6SPeter Brune   link->ddhook       = ddhook;
26355dbd56e3SPeter Brune   link->ctx          = ctx;
26360298fd71SBarry Smith   link->next         = NULL;
26375dbd56e3SPeter Brune   *p                 = link;
26385dbd56e3SPeter Brune   PetscFunctionReturn(0);
26395dbd56e3SPeter Brune }
26405dbd56e3SPeter Brune 
2641b3a6b972SJed Brown /*@C
2642b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2643b3a6b972SJed Brown 
2644b3a6b972SJed Brown    Logically Collective
2645b3a6b972SJed Brown 
2646b3a6b972SJed Brown    Input Arguments:
2647b3a6b972SJed Brown +  global - global DM
2648b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2649b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2650b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2651b3a6b972SJed Brown 
2652b3a6b972SJed Brown    Level: advanced
2653b3a6b972SJed Brown 
2654b3a6b972SJed Brown    Notes:
2655b3a6b972SJed Brown 
2656b3a6b972SJed Brown    This function is currently not available from Fortran.
2657b3a6b972SJed Brown 
2658b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2659b3a6b972SJed Brown @*/
2660b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2661b3a6b972SJed Brown {
2662b3a6b972SJed Brown   PetscErrorCode      ierr;
2663b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2664b3a6b972SJed Brown 
2665b3a6b972SJed Brown   PetscFunctionBegin;
2666b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2667b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2668b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2669b3a6b972SJed Brown       link = *p;
2670b3a6b972SJed Brown       *p = link->next;
2671b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2672b3a6b972SJed Brown       break;
2673b3a6b972SJed Brown     }
2674b3a6b972SJed Brown   }
2675b3a6b972SJed Brown   PetscFunctionReturn(0);
2676b3a6b972SJed Brown }
2677b3a6b972SJed Brown 
26785dbd56e3SPeter Brune /*@
2679be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
26805dbd56e3SPeter Brune 
26815dbd56e3SPeter Brune    Collective if any hooks are
26825dbd56e3SPeter Brune 
26835dbd56e3SPeter Brune    Input Arguments:
26845dbd56e3SPeter Brune +  fine - finer DM to use as a base
2685be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2686be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
26875dbd56e3SPeter Brune -  coarse - coarer DM to update
26885dbd56e3SPeter Brune 
26895dbd56e3SPeter Brune    Level: developer
26905dbd56e3SPeter Brune 
26915dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
26925dbd56e3SPeter Brune @*/
2693be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
26945dbd56e3SPeter Brune {
26955dbd56e3SPeter Brune   PetscErrorCode      ierr;
2696be081cd6SPeter Brune   DMSubDomainHookLink link;
26975dbd56e3SPeter Brune 
26985dbd56e3SPeter Brune   PetscFunctionBegin;
2699be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
27008865f1eaSKarl Rupp     if (link->restricthook) {
27018865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
27028865f1eaSKarl Rupp     }
27035dbd56e3SPeter Brune   }
27045dbd56e3SPeter Brune   PetscFunctionReturn(0);
27055dbd56e3SPeter Brune }
27065dbd56e3SPeter Brune 
27075fe1f584SPeter Brune /*@
27086a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
27095fe1f584SPeter Brune 
27105fe1f584SPeter Brune     Not Collective
27115fe1f584SPeter Brune 
27125fe1f584SPeter Brune     Input Parameter:
27135fe1f584SPeter Brune .   dm - the DM object
27145fe1f584SPeter Brune 
27155fe1f584SPeter Brune     Output Parameter:
27166a7d9d85SPeter Brune .   level - number of coarsenings
27175fe1f584SPeter Brune 
27185fe1f584SPeter Brune     Level: developer
27195fe1f584SPeter Brune 
27206a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
27215fe1f584SPeter Brune 
27225fe1f584SPeter Brune @*/
27235fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
27245fe1f584SPeter Brune {
27255fe1f584SPeter Brune   PetscFunctionBegin;
27265fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27275fe1f584SPeter Brune   *level = dm->leveldown;
27285fe1f584SPeter Brune   PetscFunctionReturn(0);
27295fe1f584SPeter Brune }
27305fe1f584SPeter Brune 
27315fe1f584SPeter Brune 
27325fe1f584SPeter Brune 
273347c6ae99SBarry Smith /*@C
273447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
273547c6ae99SBarry Smith 
273647c6ae99SBarry Smith     Collective on DM
273747c6ae99SBarry Smith 
273847c6ae99SBarry Smith     Input Parameter:
273947c6ae99SBarry Smith +   dm - the DM object
274047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
274147c6ae99SBarry Smith 
274247c6ae99SBarry Smith     Output Parameter:
274347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
274447c6ae99SBarry Smith 
274547c6ae99SBarry Smith     Level: developer
274647c6ae99SBarry Smith 
2747e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
274847c6ae99SBarry Smith 
274947c6ae99SBarry Smith @*/
27507087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
275147c6ae99SBarry Smith {
275247c6ae99SBarry Smith   PetscErrorCode ierr;
275347c6ae99SBarry Smith 
275447c6ae99SBarry Smith   PetscFunctionBegin;
2755171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2756ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
275747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
275847c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
275947c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
276047c6ae99SBarry Smith   } else if (dm->ops->refine) {
276147c6ae99SBarry Smith     PetscInt i;
276247c6ae99SBarry Smith 
2763ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
276447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2765ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
276647c6ae99SBarry Smith     }
2767ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
276847c6ae99SBarry Smith   PetscFunctionReturn(0);
276947c6ae99SBarry Smith }
277047c6ae99SBarry Smith 
277147c6ae99SBarry Smith /*@C
277247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
277347c6ae99SBarry Smith 
277447c6ae99SBarry Smith     Collective on DM
277547c6ae99SBarry Smith 
277647c6ae99SBarry Smith     Input Parameter:
277747c6ae99SBarry Smith +   dm - the DM object
277847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
277947c6ae99SBarry Smith 
278047c6ae99SBarry Smith     Output Parameter:
278147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
278247c6ae99SBarry Smith 
278347c6ae99SBarry Smith     Level: developer
278447c6ae99SBarry Smith 
2785e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
278647c6ae99SBarry Smith 
278747c6ae99SBarry Smith @*/
27887087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
278947c6ae99SBarry Smith {
279047c6ae99SBarry Smith   PetscErrorCode ierr;
279147c6ae99SBarry Smith 
279247c6ae99SBarry Smith   PetscFunctionBegin;
2793171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2794ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
279547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
279647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
279747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
279847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
279947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
280047c6ae99SBarry Smith     PetscInt i;
280147c6ae99SBarry Smith 
2802ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
280347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2804ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
280547c6ae99SBarry Smith     }
2806ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
280747c6ae99SBarry Smith   PetscFunctionReturn(0);
280847c6ae99SBarry Smith }
280947c6ae99SBarry Smith 
281047c6ae99SBarry Smith /*@
2811e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
281247c6ae99SBarry Smith    grids associated with two DMs.
281347c6ae99SBarry Smith 
281447c6ae99SBarry Smith    Collective on DM
281547c6ae99SBarry Smith 
281647c6ae99SBarry Smith    Input Parameters:
281747c6ae99SBarry Smith +  dmc - the coarse grid DM
281847c6ae99SBarry Smith -  dmf - the fine grid DM
281947c6ae99SBarry Smith 
282047c6ae99SBarry Smith    Output Parameters:
282147c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
282247c6ae99SBarry Smith 
282347c6ae99SBarry Smith    Level: intermediate
282447c6ae99SBarry Smith 
282547c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
282647c6ae99SBarry Smith 
2827e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
282847c6ae99SBarry Smith @*/
2829e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
283047c6ae99SBarry Smith {
283147c6ae99SBarry Smith   PetscErrorCode ierr;
283247c6ae99SBarry Smith 
283347c6ae99SBarry Smith   PetscFunctionBegin;
2834171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2835171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
283647c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
283747c6ae99SBarry Smith   PetscFunctionReturn(0);
283847c6ae99SBarry Smith }
283947c6ae99SBarry Smith 
28401a266240SBarry Smith /*@C
28411a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
28421a266240SBarry Smith 
28431a266240SBarry Smith     Not Collective
28441a266240SBarry Smith 
28451a266240SBarry Smith     Input Parameters:
28461a266240SBarry Smith +   dm - the DM object
28471a266240SBarry Smith -   destroy - the destroy function
28481a266240SBarry Smith 
28491a266240SBarry Smith     Level: intermediate
28501a266240SBarry Smith 
2851e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
28521a266240SBarry Smith 
2853f07f9ceaSJed Brown @*/
28541a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
28551a266240SBarry Smith {
28561a266240SBarry Smith   PetscFunctionBegin;
2857171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28581a266240SBarry Smith   dm->ctxdestroy = destroy;
28591a266240SBarry Smith   PetscFunctionReturn(0);
28601a266240SBarry Smith }
28611a266240SBarry Smith 
2862b07ff414SBarry Smith /*@
28631b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
286447c6ae99SBarry Smith 
286547c6ae99SBarry Smith     Not Collective
286647c6ae99SBarry Smith 
286747c6ae99SBarry Smith     Input Parameters:
286847c6ae99SBarry Smith +   dm - the DM object
286947c6ae99SBarry Smith -   ctx - the user context
287047c6ae99SBarry Smith 
287147c6ae99SBarry Smith     Level: intermediate
287247c6ae99SBarry Smith 
2873e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
287447c6ae99SBarry Smith 
287547c6ae99SBarry Smith @*/
28761b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
287747c6ae99SBarry Smith {
287847c6ae99SBarry Smith   PetscFunctionBegin;
2879171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
288047c6ae99SBarry Smith   dm->ctx = ctx;
288147c6ae99SBarry Smith   PetscFunctionReturn(0);
288247c6ae99SBarry Smith }
288347c6ae99SBarry Smith 
288447c6ae99SBarry Smith /*@
28851b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
288647c6ae99SBarry Smith 
288747c6ae99SBarry Smith     Not Collective
288847c6ae99SBarry Smith 
288947c6ae99SBarry Smith     Input Parameter:
289047c6ae99SBarry Smith .   dm - the DM object
289147c6ae99SBarry Smith 
289247c6ae99SBarry Smith     Output Parameter:
289347c6ae99SBarry Smith .   ctx - the user context
289447c6ae99SBarry Smith 
289547c6ae99SBarry Smith     Level: intermediate
289647c6ae99SBarry Smith 
2897e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
289847c6ae99SBarry Smith 
289947c6ae99SBarry Smith @*/
29001b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
290147c6ae99SBarry Smith {
290247c6ae99SBarry Smith   PetscFunctionBegin;
2903171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29041b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
290547c6ae99SBarry Smith   PetscFunctionReturn(0);
290647c6ae99SBarry Smith }
290747c6ae99SBarry Smith 
290808da532bSDmitry Karpeev /*@C
2909df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
291008da532bSDmitry Karpeev 
291108da532bSDmitry Karpeev     Logically Collective on DM
291208da532bSDmitry Karpeev 
291308da532bSDmitry Karpeev     Input Parameter:
291408da532bSDmitry Karpeev +   dm - the DM object
29150298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
291608da532bSDmitry Karpeev 
291708da532bSDmitry Karpeev     Level: intermediate
291808da532bSDmitry Karpeev 
2919835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
292008da532bSDmitry Karpeev          DMSetJacobian()
292108da532bSDmitry Karpeev 
292208da532bSDmitry Karpeev @*/
292308da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
292408da532bSDmitry Karpeev {
292508da532bSDmitry Karpeev   PetscFunctionBegin;
292608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
292708da532bSDmitry Karpeev   PetscFunctionReturn(0);
292808da532bSDmitry Karpeev }
292908da532bSDmitry Karpeev 
293008da532bSDmitry Karpeev /*@
293108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
293208da532bSDmitry Karpeev 
293308da532bSDmitry Karpeev     Not Collective
293408da532bSDmitry Karpeev 
293508da532bSDmitry Karpeev     Input Parameter:
293608da532bSDmitry Karpeev .   dm - the DM object to destroy
293708da532bSDmitry Karpeev 
293808da532bSDmitry Karpeev     Output Parameter:
293908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
294008da532bSDmitry Karpeev 
294108da532bSDmitry Karpeev     Level: developer
294208da532bSDmitry Karpeev 
294374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
294408da532bSDmitry Karpeev 
294508da532bSDmitry Karpeev @*/
294608da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
294708da532bSDmitry Karpeev {
294808da532bSDmitry Karpeev   PetscFunctionBegin;
294908da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
295008da532bSDmitry Karpeev   PetscFunctionReturn(0);
295108da532bSDmitry Karpeev }
295208da532bSDmitry Karpeev 
295308da532bSDmitry Karpeev /*@C
295408da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
295508da532bSDmitry Karpeev 
295608da532bSDmitry Karpeev     Logically Collective on DM
295708da532bSDmitry Karpeev 
295808da532bSDmitry Karpeev     Input Parameters:
2959907376e6SBarry Smith .   dm - the DM object
296008da532bSDmitry Karpeev 
296108da532bSDmitry Karpeev     Output parameters:
296208da532bSDmitry Karpeev +   xl - lower bound
296308da532bSDmitry Karpeev -   xu - upper bound
296408da532bSDmitry Karpeev 
2965907376e6SBarry Smith     Level: advanced
2966907376e6SBarry Smith 
2967907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
296808da532bSDmitry Karpeev 
296974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
297008da532bSDmitry Karpeev 
297108da532bSDmitry Karpeev @*/
297208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
297308da532bSDmitry Karpeev {
297408da532bSDmitry Karpeev   PetscErrorCode ierr;
29755fd66863SKarl Rupp 
297608da532bSDmitry Karpeev   PetscFunctionBegin;
297708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
297808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
297908da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
298008da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
29818865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
298208da532bSDmitry Karpeev   PetscFunctionReturn(0);
298308da532bSDmitry Karpeev }
298408da532bSDmitry Karpeev 
2985b0ae01b7SPeter Brune /*@
2986b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2987b0ae01b7SPeter Brune 
2988b0ae01b7SPeter Brune     Not Collective
2989b0ae01b7SPeter Brune 
2990b0ae01b7SPeter Brune     Input Parameter:
2991b0ae01b7SPeter Brune .   dm - the DM object
2992b0ae01b7SPeter Brune 
2993b0ae01b7SPeter Brune     Output Parameter:
2994b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2995b0ae01b7SPeter Brune 
2996b0ae01b7SPeter Brune     Level: developer
2997b0ae01b7SPeter Brune 
2998b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2999b0ae01b7SPeter Brune 
3000b0ae01b7SPeter Brune @*/
3001b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3002b0ae01b7SPeter Brune {
3003b0ae01b7SPeter Brune   PetscFunctionBegin;
3004b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3005b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3006b0ae01b7SPeter Brune }
3007b0ae01b7SPeter Brune 
30083ad4599aSBarry Smith /*@
30093ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
30103ad4599aSBarry Smith 
30113ad4599aSBarry Smith     Not Collective
30123ad4599aSBarry Smith 
30133ad4599aSBarry Smith     Input Parameter:
30143ad4599aSBarry Smith .   dm - the DM object
30153ad4599aSBarry Smith 
30163ad4599aSBarry Smith     Output Parameter:
30173ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
30183ad4599aSBarry Smith 
30193ad4599aSBarry Smith     Level: developer
30203ad4599aSBarry Smith 
30213ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
30223ad4599aSBarry Smith 
30233ad4599aSBarry Smith @*/
30243ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
30253ad4599aSBarry Smith {
30263ad4599aSBarry Smith   PetscFunctionBegin;
30273ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
30283ad4599aSBarry Smith   PetscFunctionReturn(0);
30293ad4599aSBarry Smith }
30303ad4599aSBarry Smith 
3031748fac09SDmitry Karpeev /*@C
303208da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
303308da532bSDmitry Karpeev 
303408da532bSDmitry Karpeev     Collective on DM
303508da532bSDmitry Karpeev 
303608da532bSDmitry Karpeev     Input Parameter:
303708da532bSDmitry Karpeev +   dm - the DM object
30380298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
303908da532bSDmitry Karpeev 
304008da532bSDmitry Karpeev     Level: developer
304108da532bSDmitry Karpeev 
304274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
304308da532bSDmitry Karpeev 
304408da532bSDmitry Karpeev @*/
304508da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
304608da532bSDmitry Karpeev {
304708da532bSDmitry Karpeev   PetscErrorCode ierr;
30485fd66863SKarl Rupp 
304908da532bSDmitry Karpeev   PetscFunctionBegin;
305008da532bSDmitry Karpeev   if (x) {
305108da532bSDmitry Karpeev     if (!dm->x) {
305208da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
305308da532bSDmitry Karpeev     }
305408da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
30558865f1eaSKarl Rupp   } else if (dm->x) {
305608da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
305708da532bSDmitry Karpeev   }
305808da532bSDmitry Karpeev   PetscFunctionReturn(0);
305908da532bSDmitry Karpeev }
306008da532bSDmitry Karpeev 
30610298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3062264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3063264ace61SBarry Smith 
3064264ace61SBarry Smith /*@C
3065264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3066264ace61SBarry Smith 
3067264ace61SBarry Smith   Collective on DM
3068264ace61SBarry Smith 
3069264ace61SBarry Smith   Input Parameters:
3070264ace61SBarry Smith + dm     - The DM object
3071264ace61SBarry Smith - method - The name of the DM type
3072264ace61SBarry Smith 
3073264ace61SBarry Smith   Options Database Key:
3074264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3075264ace61SBarry Smith 
3076264ace61SBarry Smith   Notes:
3077e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3078264ace61SBarry Smith 
3079264ace61SBarry Smith   Level: intermediate
3080264ace61SBarry Smith 
3081264ace61SBarry Smith .keywords: DM, set, type
3082264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3083264ace61SBarry Smith @*/
308419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3085264ace61SBarry Smith {
3086264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3087264ace61SBarry Smith   PetscBool      match;
3088264ace61SBarry Smith   PetscErrorCode ierr;
3089264ace61SBarry Smith 
3090264ace61SBarry Smith   PetscFunctionBegin;
3091264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3092251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3093264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3094264ace61SBarry Smith 
30950f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
30961c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3097ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3098264ace61SBarry Smith 
3099264ace61SBarry Smith   if (dm->ops->destroy) {
3100264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
31010298fd71SBarry Smith     dm->ops->destroy = NULL;
3102264ace61SBarry Smith   }
3103264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3104264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3105264ace61SBarry Smith   PetscFunctionReturn(0);
3106264ace61SBarry Smith }
3107264ace61SBarry Smith 
3108264ace61SBarry Smith /*@C
3109264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3110264ace61SBarry Smith 
3111264ace61SBarry Smith   Not Collective
3112264ace61SBarry Smith 
3113264ace61SBarry Smith   Input Parameter:
3114264ace61SBarry Smith . dm  - The DM
3115264ace61SBarry Smith 
3116264ace61SBarry Smith   Output Parameter:
3117264ace61SBarry Smith . type - The DM type name
3118264ace61SBarry Smith 
3119264ace61SBarry Smith   Level: intermediate
3120264ace61SBarry Smith 
3121264ace61SBarry Smith .keywords: DM, get, type, name
3122264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3123264ace61SBarry Smith @*/
312419fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3125264ace61SBarry Smith {
3126264ace61SBarry Smith   PetscErrorCode ierr;
3127264ace61SBarry Smith 
3128264ace61SBarry Smith   PetscFunctionBegin;
3129264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3130c959eef4SJed Brown   PetscValidPointer(type,2);
3131607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3132264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3133264ace61SBarry Smith   PetscFunctionReturn(0);
3134264ace61SBarry Smith }
3135264ace61SBarry Smith 
313667a56275SMatthew G Knepley /*@C
313767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
313867a56275SMatthew G Knepley 
313967a56275SMatthew G Knepley   Collective on DM
314067a56275SMatthew G Knepley 
314167a56275SMatthew G Knepley   Input Parameters:
314267a56275SMatthew G Knepley + dm - the DM
314367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
314467a56275SMatthew G Knepley 
314567a56275SMatthew G Knepley   Output Parameter:
314667a56275SMatthew G Knepley . M - pointer to new DM
314767a56275SMatthew G Knepley 
314867a56275SMatthew G Knepley   Notes:
314967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
315067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
315167a56275SMatthew G Knepley   of the input DM.
315267a56275SMatthew G Knepley 
315367a56275SMatthew G Knepley   Level: intermediate
315467a56275SMatthew G Knepley 
315567a56275SMatthew G Knepley .seealso: DMCreate()
315667a56275SMatthew G Knepley @*/
315719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
315867a56275SMatthew G Knepley {
315967a56275SMatthew G Knepley   DM             B;
316067a56275SMatthew G Knepley   char           convname[256];
3161c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
316267a56275SMatthew G Knepley   PetscErrorCode ierr;
316367a56275SMatthew G Knepley 
316467a56275SMatthew G Knepley   PetscFunctionBegin;
316567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
316667a56275SMatthew G Knepley   PetscValidType(dm,1);
316767a56275SMatthew G Knepley   PetscValidPointer(M,3);
3168251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3169c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3170c067b6caSMatthew G. Knepley   if (sametype) {
3171c067b6caSMatthew G. Knepley     *M   = dm;
3172c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3173c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3174c067b6caSMatthew G. Knepley   } else {
31750298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
317667a56275SMatthew G Knepley 
317767a56275SMatthew G Knepley     /*
317867a56275SMatthew G Knepley        Order of precedence:
317967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
318067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
318167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
318267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
318367a56275SMatthew G Knepley        5) Use a really basic converter.
318467a56275SMatthew G Knepley     */
318567a56275SMatthew G Knepley 
318667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
318767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
318867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
318967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
319067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
319167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
31920005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
319367a56275SMatthew G Knepley     if (conv) goto foundconv;
319467a56275SMatthew G Knepley 
319567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
319682f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
319767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
319867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
319967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
320067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
320167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
320267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
32030005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
320467a56275SMatthew G Knepley     if (conv) {
3205fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
320667a56275SMatthew G Knepley       goto foundconv;
320767a56275SMatthew G Knepley     }
320867a56275SMatthew G Knepley 
320967a56275SMatthew G Knepley #if 0
321067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
321167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3212fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
321367a56275SMatthew G Knepley     if (conv) goto foundconv;
321467a56275SMatthew G Knepley 
321567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
321667a56275SMatthew G Knepley     if (dm->ops->convert) {
321767a56275SMatthew G Knepley       conv = dm->ops->convert;
321867a56275SMatthew G Knepley     }
321967a56275SMatthew G Knepley     if (conv) goto foundconv;
322067a56275SMatthew G Knepley #endif
322167a56275SMatthew G Knepley 
322267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
322382f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
322467a56275SMatthew G Knepley 
322567a56275SMatthew G Knepley foundconv:
322667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
322767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
322812fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
322990b157c4SStefano Zampini     {
323090b157c4SStefano Zampini       PetscBool             isper;
323112fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
323212fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
323390b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
323490b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
323512fa691eSMatthew G. Knepley     }
323667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
323767a56275SMatthew G Knepley   }
323867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
323967a56275SMatthew G Knepley   PetscFunctionReturn(0);
324067a56275SMatthew G Knepley }
3241264ace61SBarry Smith 
3242264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3243264ace61SBarry Smith 
3244264ace61SBarry Smith /*@C
32451c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
32461c84c290SBarry Smith 
32471c84c290SBarry Smith   Not Collective
32481c84c290SBarry Smith 
32491c84c290SBarry Smith   Input Parameters:
32501c84c290SBarry Smith + name        - The name of a new user-defined creation routine
32511c84c290SBarry Smith - create_func - The creation routine itself
32521c84c290SBarry Smith 
32531c84c290SBarry Smith   Notes:
32541c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
32551c84c290SBarry Smith 
32561c84c290SBarry Smith 
32571c84c290SBarry Smith   Sample usage:
32581c84c290SBarry Smith .vb
3259bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
32601c84c290SBarry Smith .ve
32611c84c290SBarry Smith 
32621c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
32631c84c290SBarry Smith .vb
32641c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
32651c84c290SBarry Smith     DMSetType(DM,"my_da");
32661c84c290SBarry Smith .ve
32671c84c290SBarry Smith    or at runtime via the option
32681c84c290SBarry Smith .vb
32691c84c290SBarry Smith     -da_type my_da
32701c84c290SBarry Smith .ve
3271264ace61SBarry Smith 
3272264ace61SBarry Smith   Level: advanced
32731c84c290SBarry Smith 
32741c84c290SBarry Smith .keywords: DM, register
3275bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
32761c84c290SBarry Smith 
3277264ace61SBarry Smith @*/
3278bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3279264ace61SBarry Smith {
3280264ace61SBarry Smith   PetscErrorCode ierr;
3281264ace61SBarry Smith 
3282264ace61SBarry Smith   PetscFunctionBegin;
3283a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3284264ace61SBarry Smith   PetscFunctionReturn(0);
3285264ace61SBarry Smith }
3286264ace61SBarry Smith 
3287b859378eSBarry Smith /*@C
328855849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3289b859378eSBarry Smith 
3290b859378eSBarry Smith   Collective on PetscViewer
3291b859378eSBarry Smith 
3292b859378eSBarry Smith   Input Parameters:
3293b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3294b859378eSBarry Smith            some related function before a call to DMLoad().
3295b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3296b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3297b859378eSBarry Smith 
3298b859378eSBarry Smith    Level: intermediate
3299b859378eSBarry Smith 
3300b859378eSBarry Smith   Notes:
330155849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3302b859378eSBarry Smith 
3303b859378eSBarry Smith   Notes for advanced users:
3304b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3305b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3306b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3307b859378eSBarry Smith   format is
3308b859378eSBarry Smith .vb
3309b859378eSBarry Smith      has not yet been determined
3310b859378eSBarry Smith .ve
3311b859378eSBarry Smith 
3312b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3313b859378eSBarry Smith @*/
3314b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3315b859378eSBarry Smith {
33169331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3317b859378eSBarry Smith   PetscErrorCode ierr;
3318b859378eSBarry Smith 
3319b859378eSBarry Smith   PetscFunctionBegin;
3320b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3321b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
332232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
33239331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
33249331c7a4SMatthew G. Knepley   if (isbinary) {
33259331c7a4SMatthew G. Knepley     PetscInt classid;
33269331c7a4SMatthew G. Knepley     char     type[256];
3327b859378eSBarry Smith 
3328060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
33299200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3330060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
333132c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
33329331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33339331c7a4SMatthew G. Knepley   } else if (ishdf5) {
33349331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33359331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3336b859378eSBarry Smith   PetscFunctionReturn(0);
3337b859378eSBarry Smith }
3338b859378eSBarry Smith 
33397da65231SMatthew G Knepley /******************************** FEM Support **********************************/
33407da65231SMatthew G Knepley 
3341a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3342a6dfd86eSKarl Rupp {
33431d47ebbbSSatish Balay   PetscInt       f;
33441b30c384SMatthew G Knepley   PetscErrorCode ierr;
33451b30c384SMatthew G Knepley 
33467da65231SMatthew G Knepley   PetscFunctionBegin;
334774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33481d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
334957622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
33507da65231SMatthew G Knepley   }
33517da65231SMatthew G Knepley   PetscFunctionReturn(0);
33527da65231SMatthew G Knepley }
33537da65231SMatthew G Knepley 
3354a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3355a6dfd86eSKarl Rupp {
33561b30c384SMatthew G Knepley   PetscInt       f, g;
33577da65231SMatthew G Knepley   PetscErrorCode ierr;
33587da65231SMatthew G Knepley 
33597da65231SMatthew G Knepley   PetscFunctionBegin;
336074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33611d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
336274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
33631d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3364e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
33657da65231SMatthew G Knepley     }
336674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
33677da65231SMatthew G Knepley   }
33687da65231SMatthew G Knepley   PetscFunctionReturn(0);
33697da65231SMatthew G Knepley }
3370e7c4fc90SDmitry Karpeev 
33716113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3372e759306cSMatthew G. Knepley {
33730c5b8624SToby Isaac   PetscInt          localSize, bs;
33740c5b8624SToby Isaac   PetscMPIInt       size;
33750c5b8624SToby Isaac   Vec               x, xglob;
33760c5b8624SToby Isaac   const PetscScalar *xarray;
3377e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3378e759306cSMatthew G. Knepley 
3379e759306cSMatthew G. Knepley   PetscFunctionBegin;
33809852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3381e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3382e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
33836113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
33840c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
33850c5b8624SToby Isaac   if (size > 1) {
33860c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
33870c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
33880c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
33890c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
33900c5b8624SToby Isaac   } else {
33910c5b8624SToby Isaac     xglob = x;
33920c5b8624SToby Isaac   }
33930c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
33940c5b8624SToby Isaac   if (size > 1) {
33950c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
33960c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
33970c5b8624SToby Isaac   }
3398e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3399e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3400e759306cSMatthew G. Knepley }
3401e759306cSMatthew G. Knepley 
340288ed4aceSMatthew G Knepley /*@
340388ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
340488ed4aceSMatthew G Knepley 
340588ed4aceSMatthew G Knepley   Input Parameter:
340688ed4aceSMatthew G Knepley . dm - The DM
340788ed4aceSMatthew G Knepley 
340888ed4aceSMatthew G Knepley   Output Parameter:
340988ed4aceSMatthew G Knepley . section - The PetscSection
341088ed4aceSMatthew G Knepley 
341188ed4aceSMatthew G Knepley   Level: intermediate
341288ed4aceSMatthew G Knepley 
341388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
341488ed4aceSMatthew G Knepley 
341588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
341688ed4aceSMatthew G Knepley @*/
34170adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
34180adebc6cSBarry Smith {
3419fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3420fd59a867SMatthew G. Knepley 
342188ed4aceSMatthew G Knepley   PetscFunctionBegin;
342288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
342388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
34242f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
34252f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3426ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
34272f0f8703SMatthew G. Knepley   }
342888ed4aceSMatthew G Knepley   *section = dm->defaultSection;
342988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
343088ed4aceSMatthew G Knepley }
343188ed4aceSMatthew G Knepley 
343288ed4aceSMatthew G Knepley /*@
343388ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
343488ed4aceSMatthew G Knepley 
343588ed4aceSMatthew G Knepley   Input Parameters:
343688ed4aceSMatthew G Knepley + dm - The DM
343788ed4aceSMatthew G Knepley - section - The PetscSection
343888ed4aceSMatthew G Knepley 
343988ed4aceSMatthew G Knepley   Level: intermediate
344088ed4aceSMatthew G Knepley 
344188ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
344288ed4aceSMatthew G Knepley 
344388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
344488ed4aceSMatthew G Knepley @*/
34450adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
34460adebc6cSBarry Smith {
3447c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3448af122d2aSMatthew G Knepley   PetscInt       f;
344988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
345088ed4aceSMatthew G Knepley 
345188ed4aceSMatthew G Knepley   PetscFunctionBegin;
345288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3453c473ab19SMatthew G. Knepley   if (section) {
34541d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34551d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3456c473ab19SMatthew G. Knepley   }
345788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
345888ed4aceSMatthew G Knepley   dm->defaultSection = section;
3459c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3460af122d2aSMatthew G Knepley   if (numFields) {
3461af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3462af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
34630f21e855SMatthew G. Knepley       PetscObject disc;
3464af122d2aSMatthew G Knepley       const char *name;
3465af122d2aSMatthew G Knepley 
3466af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
34670f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
34680f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3469af122d2aSMatthew G Knepley     }
3470af122d2aSMatthew G Knepley   }
34711d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
34721d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
347388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
347488ed4aceSMatthew G Knepley }
347588ed4aceSMatthew G Knepley 
34769435951eSToby Isaac /*@
34779435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
34789435951eSToby Isaac 
3479e228b242SToby Isaac   not collective
3480e228b242SToby Isaac 
34819435951eSToby Isaac   Input Parameter:
34829435951eSToby Isaac . dm - The DM
34839435951eSToby Isaac 
34849435951eSToby Isaac   Output Parameter:
34859435951eSToby 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.
34869435951eSToby 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.
34879435951eSToby Isaac 
34889435951eSToby Isaac   Level: advanced
34899435951eSToby Isaac 
34909435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
34919435951eSToby Isaac 
34929435951eSToby Isaac .seealso: DMSetDefaultConstraints()
34939435951eSToby Isaac @*/
34949435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
34959435951eSToby Isaac {
34969435951eSToby Isaac   PetscErrorCode ierr;
34979435951eSToby Isaac 
34989435951eSToby Isaac   PetscFunctionBegin;
34999435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35009435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
350145a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
350245a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
35039435951eSToby Isaac   PetscFunctionReturn(0);
35049435951eSToby Isaac }
35059435951eSToby Isaac 
35069435951eSToby Isaac /*@
35079435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
35089435951eSToby Isaac 
35099435951eSToby 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().
35109435951eSToby Isaac 
35119435951eSToby 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.
35129435951eSToby Isaac 
3513e228b242SToby Isaac   collective on dm
3514e228b242SToby Isaac 
35159435951eSToby Isaac   Input Parameters:
35169435951eSToby Isaac + dm - The DM
3517e228b242SToby 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).
3518e228b242SToby 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).
35199435951eSToby Isaac 
35209435951eSToby Isaac   Level: advanced
35219435951eSToby Isaac 
35229435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
35239435951eSToby Isaac 
35249435951eSToby Isaac .seealso: DMGetDefaultConstraints()
35259435951eSToby Isaac @*/
35269435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
35279435951eSToby Isaac {
3528e228b242SToby Isaac   PetscMPIInt result;
35299435951eSToby Isaac   PetscErrorCode ierr;
35309435951eSToby Isaac 
35319435951eSToby Isaac   PetscFunctionBegin;
35329435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3533e228b242SToby Isaac   if (section) {
3534e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3535e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3536f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3537e228b242SToby Isaac   }
3538e228b242SToby Isaac   if (mat) {
3539e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3540e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3541f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3542e228b242SToby Isaac   }
35439435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
35449435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
35459435951eSToby Isaac   dm->defaultConstraintSection = section;
35469435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
35479435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
35489435951eSToby Isaac   dm->defaultConstraintMat = mat;
35499435951eSToby Isaac   PetscFunctionReturn(0);
35509435951eSToby Isaac }
35519435951eSToby Isaac 
3552f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3553507e4973SMatthew G. Knepley /*
3554507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3555507e4973SMatthew G. Knepley 
3556507e4973SMatthew G. Knepley   Input Parameters:
3557507e4973SMatthew G. Knepley + dm - The DM
3558507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3559507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3560507e4973SMatthew G. Knepley 
3561507e4973SMatthew G. Knepley   Level: intermediate
3562507e4973SMatthew G. Knepley 
3563507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3564507e4973SMatthew G. Knepley */
3565f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3566507e4973SMatthew G. Knepley {
3567507e4973SMatthew G. Knepley   MPI_Comm        comm;
3568507e4973SMatthew G. Knepley   PetscLayout     layout;
3569507e4973SMatthew G. Knepley   const PetscInt *ranges;
3570507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3571507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3572507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3573507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3574507e4973SMatthew G. Knepley 
3575507e4973SMatthew G. Knepley   PetscFunctionBegin;
3576507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3577507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3578507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3579507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3580507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3581507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3582507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3583507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3584507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3585507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3586507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3587507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3588f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3589507e4973SMatthew G. Knepley 
3590507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3591507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3592507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3593507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3594507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3595507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3596507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3597507e4973SMatthew 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;}
3598507e4973SMatthew 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;}
3599507e4973SMatthew G. Knepley     if (gdof < 0) {
3600507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3601507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3602507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3603507e4973SMatthew G. Knepley 
3604507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3605507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3606507e4973SMatthew 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;}
3607507e4973SMatthew G. Knepley       }
3608507e4973SMatthew G. Knepley     }
3609507e4973SMatthew G. Knepley   }
3610507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3611507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3612b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3613507e4973SMatthew G. Knepley   if (!gvalid) {
3614507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3615507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3616507e4973SMatthew G. Knepley   }
3617507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3618507e4973SMatthew G. Knepley }
3619f741bcd2SMatthew G. Knepley #endif
3620507e4973SMatthew G. Knepley 
362188ed4aceSMatthew G Knepley /*@
362288ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
362388ed4aceSMatthew G Knepley 
36248b1ab98fSJed Brown   Collective on DM
36258b1ab98fSJed Brown 
362688ed4aceSMatthew G Knepley   Input Parameter:
362788ed4aceSMatthew G Knepley . dm - The DM
362888ed4aceSMatthew G Knepley 
362988ed4aceSMatthew G Knepley   Output Parameter:
363088ed4aceSMatthew G Knepley . section - The PetscSection
363188ed4aceSMatthew G Knepley 
363288ed4aceSMatthew G Knepley   Level: intermediate
363388ed4aceSMatthew G Knepley 
363488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
363588ed4aceSMatthew G Knepley 
363688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
363788ed4aceSMatthew G Knepley @*/
36380adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
36390adebc6cSBarry Smith {
364088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
364188ed4aceSMatthew G Knepley 
364288ed4aceSMatthew G Knepley   PetscFunctionBegin;
364388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
364488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
364588ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3646fd59a867SMatthew G. Knepley     PetscSection s;
3647fd59a867SMatthew G. Knepley 
3648fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3649fd59a867SMatthew 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");
3650fd59a867SMatthew G. Knepley     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSF in order to create a global PetscSection");
365115b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3652cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3653ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3654685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
365588ed4aceSMatthew G Knepley   }
365688ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
365788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
365888ed4aceSMatthew G Knepley }
365988ed4aceSMatthew G Knepley 
3660b21d0597SMatthew G Knepley /*@
3661b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3662b21d0597SMatthew G Knepley 
3663b21d0597SMatthew G Knepley   Input Parameters:
3664b21d0597SMatthew G Knepley + dm - The DM
36655080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3666b21d0597SMatthew G Knepley 
3667b21d0597SMatthew G Knepley   Level: intermediate
3668b21d0597SMatthew G Knepley 
3669b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3670b21d0597SMatthew G Knepley 
3671b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3672b21d0597SMatthew G Knepley @*/
36730adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
36740adebc6cSBarry Smith {
3675b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3676b21d0597SMatthew G Knepley 
3677b21d0597SMatthew G Knepley   PetscFunctionBegin;
3678b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36795080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36801d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3681b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3682b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3683507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3684f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3685507e4973SMatthew G. Knepley #endif
3686b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3687b21d0597SMatthew G Knepley }
3688b21d0597SMatthew G Knepley 
368988ed4aceSMatthew G Knepley /*@
369088ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
369188ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
369288ed4aceSMatthew G Knepley 
369388ed4aceSMatthew G Knepley   Input Parameter:
369488ed4aceSMatthew G Knepley . dm - The DM
369588ed4aceSMatthew G Knepley 
369688ed4aceSMatthew G Knepley   Output Parameter:
369788ed4aceSMatthew G Knepley . sf - The PetscSF
369888ed4aceSMatthew G Knepley 
369988ed4aceSMatthew G Knepley   Level: intermediate
370088ed4aceSMatthew G Knepley 
370188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
370288ed4aceSMatthew G Knepley 
370388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
370488ed4aceSMatthew G Knepley @*/
37050adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
37060adebc6cSBarry Smith {
370788ed4aceSMatthew G Knepley   PetscInt       nroots;
370888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
370988ed4aceSMatthew G Knepley 
371088ed4aceSMatthew G Knepley   PetscFunctionBegin;
371188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
371288ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
37130298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
371488ed4aceSMatthew G Knepley   if (nroots < 0) {
371588ed4aceSMatthew G Knepley     PetscSection section, gSection;
371688ed4aceSMatthew G Knepley 
371788ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
371831ea6d37SMatthew G Knepley     if (section) {
371988ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
372088ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
372131ea6d37SMatthew G Knepley     } else {
37220298fd71SBarry Smith       *sf = NULL;
372331ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
372431ea6d37SMatthew G Knepley     }
372588ed4aceSMatthew G Knepley   }
372688ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
372788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
372888ed4aceSMatthew G Knepley }
372988ed4aceSMatthew G Knepley 
373088ed4aceSMatthew G Knepley /*@
373188ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
373288ed4aceSMatthew G Knepley 
373388ed4aceSMatthew G Knepley   Input Parameters:
373488ed4aceSMatthew G Knepley + dm - The DM
373588ed4aceSMatthew G Knepley - sf - The PetscSF
373688ed4aceSMatthew G Knepley 
373788ed4aceSMatthew G Knepley   Level: intermediate
373888ed4aceSMatthew G Knepley 
373988ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
374088ed4aceSMatthew G Knepley 
374188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
374288ed4aceSMatthew G Knepley @*/
37430adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
37440adebc6cSBarry Smith {
374588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
374688ed4aceSMatthew G Knepley 
374788ed4aceSMatthew G Knepley   PetscFunctionBegin;
374888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
375088ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
375188ed4aceSMatthew G Knepley   dm->defaultSF = sf;
375288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
375388ed4aceSMatthew G Knepley }
375488ed4aceSMatthew G Knepley 
375588ed4aceSMatthew G Knepley /*@C
375688ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
375788ed4aceSMatthew G Knepley   describing the data layout.
375888ed4aceSMatthew G Knepley 
375988ed4aceSMatthew G Knepley   Input Parameters:
376088ed4aceSMatthew G Knepley + dm - The DM
376188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
376288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
376388ed4aceSMatthew G Knepley 
376488ed4aceSMatthew G Knepley   Level: intermediate
376588ed4aceSMatthew G Knepley 
376688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
376788ed4aceSMatthew G Knepley @*/
376888ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
376988ed4aceSMatthew G Knepley {
377082f516ccSBarry Smith   MPI_Comm       comm;
377188ed4aceSMatthew G Knepley   PetscLayout    layout;
377288ed4aceSMatthew G Knepley   const PetscInt *ranges;
377388ed4aceSMatthew G Knepley   PetscInt       *local;
377488ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3775ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
377688ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
377788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
377888ed4aceSMatthew G Knepley 
377988ed4aceSMatthew G Knepley   PetscFunctionBegin;
378082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
378188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378288ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
378388ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
378488ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
378588ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
378688ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
378788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
378888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
378988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
379088ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3791ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
37926636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
379388ed4aceSMatthew G Knepley 
37946636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37956636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3796235fbf56SMatthew 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));
37976636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
379888ed4aceSMatthew G Knepley   }
3799785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3800785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
380188ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
38021f588964SMatthew G Knepley     const PetscInt *cind;
38036636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
380488ed4aceSMatthew G Knepley 
380588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
380688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
380788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
380888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
380988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
38106636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
381188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
38126636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
38136636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
38146636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3815057b4bcdSMatthew 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);
38166636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
38176636e97aSMatthew G Knepley     }
381888ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
381988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
382088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
382188ed4aceSMatthew G Knepley     }
382288ed4aceSMatthew G Knepley     if (gdof < 0) {
38236636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
382488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
382588ed4aceSMatthew G Knepley 
382605376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
382731d3f06eSJed Brown         if (r < 0) r = -(r+2);
382805376888SMatthew 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);
382988ed4aceSMatthew G Knepley         remote[l].rank  = r;
383088ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
383188ed4aceSMatthew G Knepley       }
383288ed4aceSMatthew G Knepley     } else {
38336636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
383488ed4aceSMatthew G Knepley         remote[l].rank  = rank;
383588ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
383688ed4aceSMatthew G Knepley       }
383788ed4aceSMatthew G Knepley     }
383888ed4aceSMatthew G Knepley   }
38396636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
384088ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
384188ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
384288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
384388ed4aceSMatthew G Knepley }
3844af122d2aSMatthew G Knepley 
3845b21d0597SMatthew G Knepley /*@
3846b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3847b21d0597SMatthew G Knepley 
3848b21d0597SMatthew G Knepley   Input Parameter:
3849b21d0597SMatthew G Knepley . dm - The DM
3850b21d0597SMatthew G Knepley 
3851b21d0597SMatthew G Knepley   Output Parameter:
3852b21d0597SMatthew G Knepley . sf - The PetscSF
3853b21d0597SMatthew G Knepley 
3854b21d0597SMatthew G Knepley   Level: intermediate
3855b21d0597SMatthew G Knepley 
3856b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3857b21d0597SMatthew G Knepley 
3858057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3859b21d0597SMatthew G Knepley @*/
38600adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
38610adebc6cSBarry Smith {
3862b21d0597SMatthew G Knepley   PetscFunctionBegin;
3863b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3864b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3865b21d0597SMatthew G Knepley   *sf = dm->sf;
3866b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3867b21d0597SMatthew G Knepley }
3868b21d0597SMatthew G Knepley 
3869057b4bcdSMatthew G Knepley /*@
3870057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3871057b4bcdSMatthew G Knepley 
3872057b4bcdSMatthew G Knepley   Input Parameters:
3873057b4bcdSMatthew G Knepley + dm - The DM
3874057b4bcdSMatthew G Knepley - sf - The PetscSF
3875057b4bcdSMatthew G Knepley 
3876057b4bcdSMatthew G Knepley   Level: intermediate
3877057b4bcdSMatthew G Knepley 
3878057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3879057b4bcdSMatthew G Knepley @*/
38800adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
38810adebc6cSBarry Smith {
3882057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3883057b4bcdSMatthew G Knepley 
3884057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3885057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3886057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3887057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3888057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3889057b4bcdSMatthew G Knepley   dm->sf = sf;
3890057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3891057b4bcdSMatthew G Knepley }
3892057b4bcdSMatthew G Knepley 
38932764a2aaSMatthew G. Knepley /*@
38942764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
38952764a2aaSMatthew G. Knepley 
38962764a2aaSMatthew G. Knepley   Input Parameter:
38972764a2aaSMatthew G. Knepley . dm - The DM
38982764a2aaSMatthew G. Knepley 
38992764a2aaSMatthew G. Knepley   Output Parameter:
39002764a2aaSMatthew G. Knepley . prob - The PetscDS
39012764a2aaSMatthew G. Knepley 
39022764a2aaSMatthew G. Knepley   Level: developer
39032764a2aaSMatthew G. Knepley 
39042764a2aaSMatthew G. Knepley .seealso: DMSetDS()
39052764a2aaSMatthew G. Knepley @*/
39062764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3907af122d2aSMatthew G Knepley {
3908af122d2aSMatthew G Knepley   PetscFunctionBegin;
3909af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39100f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
39110f21e855SMatthew G. Knepley   *prob = dm->prob;
39120f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39130f21e855SMatthew G. Knepley }
39140f21e855SMatthew G. Knepley 
39152764a2aaSMatthew G. Knepley /*@
39162764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
39172764a2aaSMatthew G. Knepley 
39182764a2aaSMatthew G. Knepley   Input Parameters:
39192764a2aaSMatthew G. Knepley + dm - The DM
39202764a2aaSMatthew G. Knepley - prob - The PetscDS
39212764a2aaSMatthew G. Knepley 
39222764a2aaSMatthew G. Knepley   Level: developer
39232764a2aaSMatthew G. Knepley 
39242764a2aaSMatthew G. Knepley .seealso: DMGetDS()
39252764a2aaSMatthew G. Knepley @*/
39262764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
39270f21e855SMatthew G. Knepley {
3928f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
39290f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39300f21e855SMatthew G. Knepley 
39310f21e855SMatthew G. Knepley   PetscFunctionBegin;
39320f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39332764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
393437316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
39352764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
39360f21e855SMatthew G. Knepley   dm->prob = prob;
3937f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
3938f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
39390f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39400f21e855SMatthew G. Knepley }
39410f21e855SMatthew G. Knepley 
39420f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
39430f21e855SMatthew G. Knepley {
39440f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39450f21e855SMatthew G. Knepley 
39460f21e855SMatthew G. Knepley   PetscFunctionBegin;
39470f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39482764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3949af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3950af122d2aSMatthew G Knepley }
3951af122d2aSMatthew G Knepley 
3952af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3953af122d2aSMatthew G Knepley {
39540f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3955af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3956af122d2aSMatthew G Knepley 
3957af122d2aSMatthew G Knepley   PetscFunctionBegin;
3958af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39592764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
39600f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
39610f21e855SMatthew G. Knepley     PetscContainer obj;
39620f21e855SMatthew G. Knepley 
39630f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
39642764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
39650f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3966af122d2aSMatthew G Knepley   }
3967af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3968af122d2aSMatthew G Knepley }
3969af122d2aSMatthew G Knepley 
3970c1929be8SMatthew G. Knepley /*@
3971c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3972c1929be8SMatthew G. Knepley 
3973c1929be8SMatthew G. Knepley   Not collective
3974c1929be8SMatthew G. Knepley 
3975c1929be8SMatthew G. Knepley   Input Parameters:
3976c1929be8SMatthew G. Knepley + dm - The DM
3977c1929be8SMatthew G. Knepley - f  - The field number
3978c1929be8SMatthew G. Knepley 
3979c1929be8SMatthew G. Knepley   Output Parameter:
3980c1929be8SMatthew G. Knepley . field - The discretization object
3981c1929be8SMatthew G. Knepley 
3982c1929be8SMatthew G. Knepley   Level: developer
3983c1929be8SMatthew G. Knepley 
3984c1929be8SMatthew G. Knepley .seealso: DMSetField()
3985c1929be8SMatthew G. Knepley @*/
3986af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3987af122d2aSMatthew G Knepley {
39880f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39890f21e855SMatthew G. Knepley 
3990af122d2aSMatthew G Knepley   PetscFunctionBegin;
3991af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39922764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3993decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3994decb47aaSMatthew G. Knepley }
3995decb47aaSMatthew G. Knepley 
3996c1929be8SMatthew G. Knepley /*@
3997c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3998c1929be8SMatthew G. Knepley 
3999c1929be8SMatthew G. Knepley   Logically collective on DM
4000c1929be8SMatthew G. Knepley 
4001c1929be8SMatthew G. Knepley   Input Parameters:
4002c1929be8SMatthew G. Knepley + dm - The DM
4003c1929be8SMatthew G. Knepley . f  - The field number
4004c1929be8SMatthew G. Knepley - field - The discretization object
4005c1929be8SMatthew G. Knepley 
4006c1929be8SMatthew G. Knepley   Level: developer
4007c1929be8SMatthew G. Knepley 
4008c1929be8SMatthew G. Knepley .seealso: DMGetField()
4009c1929be8SMatthew G. Knepley @*/
4010decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4011decb47aaSMatthew G. Knepley {
4012decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4013decb47aaSMatthew G. Knepley 
4014decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4015decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40162764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4017af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4018af122d2aSMatthew G Knepley }
40196636e97aSMatthew G Knepley 
4020b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4021b64e0483SPeter Brune {
4022b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4023b64e0483SPeter Brune   PetscErrorCode ierr;
4024b64e0483SPeter Brune   Vec coords,ccoords;
40256dbf9973SLawrence Mitchell   Mat inject;
4026b64e0483SPeter Brune   PetscFunctionBegin;
4027b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4028b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4029b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4030b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4031b64e0483SPeter Brune   if (coords && !ccoords) {
4032b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
40336668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
40346dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
40352adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
40366dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4037b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4038b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4039b64e0483SPeter Brune   }
4040b64e0483SPeter Brune   PetscFunctionReturn(0);
4041b64e0483SPeter Brune }
4042b64e0483SPeter Brune 
404303dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
404403dadc2fSPeter Brune {
404503dadc2fSPeter Brune   DM dm_coord,subdm_coord;
404603dadc2fSPeter Brune   PetscErrorCode ierr;
404703dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
404803dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
404903dadc2fSPeter Brune   PetscFunctionBegin;
405003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
405103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
405203dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
405303dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
405403dadc2fSPeter Brune   if (coords && !ccoords) {
405503dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
40566668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
405703dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
405824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
405903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
406003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
406103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
406203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
406303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
406403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
406503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
406603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
406703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
406803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
406903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
407003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
407103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
407203dadc2fSPeter Brune   }
407303dadc2fSPeter Brune   PetscFunctionReturn(0);
407403dadc2fSPeter Brune }
407503dadc2fSPeter Brune 
4076c73cfb54SMatthew G. Knepley /*@
4077c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4078c73cfb54SMatthew G. Knepley 
4079c73cfb54SMatthew G. Knepley   Not collective
4080c73cfb54SMatthew G. Knepley 
4081c73cfb54SMatthew G. Knepley   Input Parameter:
4082c73cfb54SMatthew G. Knepley . dm - The DM
4083c73cfb54SMatthew G. Knepley 
4084c73cfb54SMatthew G. Knepley   Output Parameter:
4085c73cfb54SMatthew G. Knepley . dim - The topological dimension
4086c73cfb54SMatthew G. Knepley 
4087c73cfb54SMatthew G. Knepley   Level: beginner
4088c73cfb54SMatthew G. Knepley 
4089c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4090c73cfb54SMatthew G. Knepley @*/
4091c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4092c73cfb54SMatthew G. Knepley {
4093c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4094c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4095c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4096c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4097c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4098c73cfb54SMatthew G. Knepley }
4099c73cfb54SMatthew G. Knepley 
4100c73cfb54SMatthew G. Knepley /*@
4101c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4102c73cfb54SMatthew G. Knepley 
4103c73cfb54SMatthew G. Knepley   Collective on dm
4104c73cfb54SMatthew G. Knepley 
4105c73cfb54SMatthew G. Knepley   Input Parameters:
4106c73cfb54SMatthew G. Knepley + dm - The DM
4107c73cfb54SMatthew G. Knepley - dim - The topological dimension
4108c73cfb54SMatthew G. Knepley 
4109c73cfb54SMatthew G. Knepley   Level: beginner
4110c73cfb54SMatthew G. Knepley 
4111c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4112c73cfb54SMatthew G. Knepley @*/
4113c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4114c73cfb54SMatthew G. Knepley {
4115f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4116f17e8794SMatthew G. Knepley 
4117c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4118c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4119c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4120c73cfb54SMatthew G. Knepley   dm->dim = dim;
4121f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4122c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4123c73cfb54SMatthew G. Knepley }
4124c73cfb54SMatthew G. Knepley 
4125793f3fe5SMatthew G. Knepley /*@
4126793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4127793f3fe5SMatthew G. Knepley 
4128793f3fe5SMatthew G. Knepley   Collective on DM
4129793f3fe5SMatthew G. Knepley 
4130793f3fe5SMatthew G. Knepley   Input Parameters:
4131793f3fe5SMatthew G. Knepley + dm - the DM
4132793f3fe5SMatthew G. Knepley - dim - the dimension
4133793f3fe5SMatthew G. Knepley 
4134793f3fe5SMatthew G. Knepley   Output Parameters:
4135793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4136793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4137793f3fe5SMatthew G. Knepley 
4138793f3fe5SMatthew G. Knepley   Note:
4139793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4140793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4141793f3fe5SMatthew G. Knepley   then the interval is empty.
4142793f3fe5SMatthew G. Knepley 
4143793f3fe5SMatthew G. Knepley   Level: intermediate
4144793f3fe5SMatthew G. Knepley 
4145793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4146793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4147793f3fe5SMatthew G. Knepley @*/
4148793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4149793f3fe5SMatthew G. Knepley {
4150793f3fe5SMatthew G. Knepley   PetscInt       d;
4151793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4152793f3fe5SMatthew G. Knepley 
4153793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4154793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4155793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4156793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4157793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4158793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4159793f3fe5SMatthew G. Knepley }
4160793f3fe5SMatthew G. Knepley 
41616636e97aSMatthew G Knepley /*@
41626636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
41636636e97aSMatthew G Knepley 
41646636e97aSMatthew G Knepley   Collective on DM
41656636e97aSMatthew G Knepley 
41666636e97aSMatthew G Knepley   Input Parameters:
41676636e97aSMatthew G Knepley + dm - the DM
41686636e97aSMatthew G Knepley - c - coordinate vector
41696636e97aSMatthew G Knepley 
41706636e97aSMatthew G Knepley   Note:
41716636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
41726636e97aSMatthew G Knepley 
41736636e97aSMatthew G Knepley   Level: intermediate
41746636e97aSMatthew G Knepley 
41756636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41766636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
41776636e97aSMatthew G Knepley @*/
41786636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
41796636e97aSMatthew G Knepley {
41806636e97aSMatthew G Knepley   PetscErrorCode ierr;
41816636e97aSMatthew G Knepley 
41826636e97aSMatthew G Knepley   PetscFunctionBegin;
41836636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41846636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41856636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41866636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41876636e97aSMatthew G Knepley   dm->coordinates = c;
41886636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4189b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
419003dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
41916636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41926636e97aSMatthew G Knepley }
41936636e97aSMatthew G Knepley 
41946636e97aSMatthew G Knepley /*@
41956636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
41966636e97aSMatthew G Knepley 
41976636e97aSMatthew G Knepley   Collective on DM
41986636e97aSMatthew G Knepley 
41996636e97aSMatthew G Knepley    Input Parameters:
42006636e97aSMatthew G Knepley +  dm - the DM
42016636e97aSMatthew G Knepley -  c - coordinate vector
42026636e97aSMatthew G Knepley 
42036636e97aSMatthew G Knepley   Note:
42046636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
42056636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
42066636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
42076636e97aSMatthew G Knepley 
42086636e97aSMatthew G Knepley   Level: intermediate
42096636e97aSMatthew G Knepley 
42106636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42116636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
42126636e97aSMatthew G Knepley @*/
42136636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
42146636e97aSMatthew G Knepley {
42156636e97aSMatthew G Knepley   PetscErrorCode ierr;
42166636e97aSMatthew G Knepley 
42176636e97aSMatthew G Knepley   PetscFunctionBegin;
42186636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42196636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
42206636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
42216636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
42228865f1eaSKarl Rupp 
42236636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
42248865f1eaSKarl Rupp 
42256636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42266636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42276636e97aSMatthew G Knepley }
42286636e97aSMatthew G Knepley 
42296636e97aSMatthew G Knepley /*@
42306636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
42316636e97aSMatthew G Knepley 
42326636e97aSMatthew G Knepley   Not Collective
42336636e97aSMatthew G Knepley 
42346636e97aSMatthew G Knepley   Input Parameter:
42356636e97aSMatthew G Knepley . dm - the DM
42366636e97aSMatthew G Knepley 
42376636e97aSMatthew G Knepley   Output Parameter:
42386636e97aSMatthew G Knepley . c - global coordinate vector
42396636e97aSMatthew G Knepley 
42406636e97aSMatthew G Knepley   Note:
42416636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42426636e97aSMatthew G Knepley 
42436636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
42446636e97aSMatthew G Knepley 
42456636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42466636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42476636e97aSMatthew G Knepley 
42486636e97aSMatthew G Knepley   Level: intermediate
42496636e97aSMatthew G Knepley 
42506636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42516636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
42526636e97aSMatthew G Knepley @*/
42536636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
42546636e97aSMatthew G Knepley {
42556636e97aSMatthew G Knepley   PetscErrorCode ierr;
42566636e97aSMatthew G Knepley 
42576636e97aSMatthew G Knepley   PetscFunctionBegin;
42586636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42596636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42601f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
42610298fd71SBarry Smith     DM cdm = NULL;
42626636e97aSMatthew G Knepley 
42636636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42646636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
42656636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
42666636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42676636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42686636e97aSMatthew G Knepley   }
42696636e97aSMatthew G Knepley   *c = dm->coordinates;
42706636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42716636e97aSMatthew G Knepley }
42726636e97aSMatthew G Knepley 
42736636e97aSMatthew G Knepley /*@
42746636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
42756636e97aSMatthew G Knepley 
42766636e97aSMatthew G Knepley   Collective on DM
42776636e97aSMatthew G Knepley 
42786636e97aSMatthew G Knepley   Input Parameter:
42796636e97aSMatthew G Knepley . dm - the DM
42806636e97aSMatthew G Knepley 
42816636e97aSMatthew G Knepley   Output Parameter:
42826636e97aSMatthew G Knepley . c - coordinate vector
42836636e97aSMatthew G Knepley 
42846636e97aSMatthew G Knepley   Note:
42856636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42866636e97aSMatthew G Knepley 
42876636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
42886636e97aSMatthew G Knepley 
42896636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42906636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42916636e97aSMatthew G Knepley 
42926636e97aSMatthew G Knepley   Level: intermediate
42936636e97aSMatthew G Knepley 
42946636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42956636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
42966636e97aSMatthew G Knepley @*/
42976636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
42986636e97aSMatthew G Knepley {
42996636e97aSMatthew G Knepley   PetscErrorCode ierr;
43006636e97aSMatthew G Knepley 
43016636e97aSMatthew G Knepley   PetscFunctionBegin;
43026636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43036636e97aSMatthew G Knepley   PetscValidPointer(c,2);
43041f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
43050298fd71SBarry Smith     DM cdm = NULL;
43066636e97aSMatthew G Knepley 
43076636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
43086636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
43096636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
43106636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
43116636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
43126636e97aSMatthew G Knepley   }
43136636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
43146636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43156636e97aSMatthew G Knepley }
43166636e97aSMatthew G Knepley 
4317*f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
4318*f19dbd58SToby Isaac {
4319*f19dbd58SToby Isaac   PetscErrorCode ierr;
4320*f19dbd58SToby Isaac 
4321*f19dbd58SToby Isaac   PetscFunctionBegin;
4322*f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4323*f19dbd58SToby Isaac   PetscValidPointer(field,2);
4324*f19dbd58SToby Isaac   if (!dm->coordinateField) {
4325*f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
4326*f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
4327*f19dbd58SToby Isaac     }
4328*f19dbd58SToby Isaac   }
4329*f19dbd58SToby Isaac   *field = dm->coordinateField;
4330*f19dbd58SToby Isaac   PetscFunctionReturn(0);
4331*f19dbd58SToby Isaac }
4332*f19dbd58SToby Isaac 
4333*f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
4334*f19dbd58SToby Isaac {
4335*f19dbd58SToby Isaac   PetscErrorCode ierr;
4336*f19dbd58SToby Isaac 
4337*f19dbd58SToby Isaac   PetscFunctionBegin;
4338*f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4339*f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
4340*f19dbd58SToby Isaac   ierr = PetscObjectReference((PetscObject)field);
4341*f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
4342*f19dbd58SToby Isaac   dm->coordinateField = field;
4343*f19dbd58SToby Isaac   PetscFunctionReturn(0);
4344*f19dbd58SToby Isaac }
4345*f19dbd58SToby Isaac 
43466636e97aSMatthew G Knepley /*@
43471cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
43486636e97aSMatthew G Knepley 
43496636e97aSMatthew G Knepley   Collective on DM
43506636e97aSMatthew G Knepley 
43516636e97aSMatthew G Knepley   Input Parameter:
43526636e97aSMatthew G Knepley . dm - the DM
43536636e97aSMatthew G Knepley 
43546636e97aSMatthew G Knepley   Output Parameter:
43556636e97aSMatthew G Knepley . cdm - coordinate DM
43566636e97aSMatthew G Knepley 
43576636e97aSMatthew G Knepley   Level: intermediate
43586636e97aSMatthew G Knepley 
43596636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43601cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43616636e97aSMatthew G Knepley @*/
43626636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
43636636e97aSMatthew G Knepley {
43646636e97aSMatthew G Knepley   PetscErrorCode ierr;
43656636e97aSMatthew G Knepley 
43666636e97aSMatthew G Knepley   PetscFunctionBegin;
43676636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43686636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
43696636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
437082f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
43716636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
43726636e97aSMatthew G Knepley   }
43736636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
43746636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43756636e97aSMatthew G Knepley }
4376e87bb0d3SMatthew G Knepley 
43771cfe2091SMatthew G. Knepley /*@
43781cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
43791cfe2091SMatthew G. Knepley 
43801cfe2091SMatthew G. Knepley   Logically Collective on DM
43811cfe2091SMatthew G. Knepley 
43821cfe2091SMatthew G. Knepley   Input Parameters:
43831cfe2091SMatthew G. Knepley + dm - the DM
43841cfe2091SMatthew G. Knepley - cdm - coordinate DM
43851cfe2091SMatthew G. Knepley 
43861cfe2091SMatthew G. Knepley   Level: intermediate
43871cfe2091SMatthew G. Knepley 
43881cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43891cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43901cfe2091SMatthew G. Knepley @*/
43911cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
43921cfe2091SMatthew G. Knepley {
43931cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
43941cfe2091SMatthew G. Knepley 
43951cfe2091SMatthew G. Knepley   PetscFunctionBegin;
43961cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43971cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4398f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
43991cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
44001cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
44011cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
44021cfe2091SMatthew G. Knepley }
44031cfe2091SMatthew G. Knepley 
440446e270d4SMatthew G. Knepley /*@
440546e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
440646e270d4SMatthew G. Knepley 
440746e270d4SMatthew G. Knepley   Not Collective
440846e270d4SMatthew G. Knepley 
440946e270d4SMatthew G. Knepley   Input Parameter:
441046e270d4SMatthew G. Knepley . dm - The DM object
441146e270d4SMatthew G. Knepley 
441246e270d4SMatthew G. Knepley   Output Parameter:
441346e270d4SMatthew G. Knepley . dim - The embedding dimension
441446e270d4SMatthew G. Knepley 
441546e270d4SMatthew G. Knepley   Level: intermediate
441646e270d4SMatthew G. Knepley 
441746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
441846e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
441946e270d4SMatthew G. Knepley @*/
442046e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
442146e270d4SMatthew G. Knepley {
442246e270d4SMatthew G. Knepley   PetscFunctionBegin;
442346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
442446e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
44259a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
44269a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
44279a9a41abSToby Isaac   }
442846e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
442946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
443046e270d4SMatthew G. Knepley }
443146e270d4SMatthew G. Knepley 
443246e270d4SMatthew G. Knepley /*@
443346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
443446e270d4SMatthew G. Knepley 
443546e270d4SMatthew G. Knepley   Not Collective
443646e270d4SMatthew G. Knepley 
443746e270d4SMatthew G. Knepley   Input Parameters:
443846e270d4SMatthew G. Knepley + dm  - The DM object
443946e270d4SMatthew G. Knepley - dim - The embedding dimension
444046e270d4SMatthew G. Knepley 
444146e270d4SMatthew G. Knepley   Level: intermediate
444246e270d4SMatthew G. Knepley 
444346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
444446e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
444546e270d4SMatthew G. Knepley @*/
444646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
444746e270d4SMatthew G. Knepley {
4448f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4449f17e8794SMatthew G. Knepley 
445046e270d4SMatthew G. Knepley   PetscFunctionBegin;
445146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
445246e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4453f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
445446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
445546e270d4SMatthew G. Knepley }
445646e270d4SMatthew G. Knepley 
4457e8abe2deSMatthew G. Knepley /*@
4458e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4459e8abe2deSMatthew G. Knepley 
4460e8abe2deSMatthew G. Knepley   Not Collective
4461e8abe2deSMatthew G. Knepley 
4462e8abe2deSMatthew G. Knepley   Input Parameter:
4463e8abe2deSMatthew G. Knepley . dm - The DM object
4464e8abe2deSMatthew G. Knepley 
4465e8abe2deSMatthew G. Knepley   Output Parameter:
4466e8abe2deSMatthew G. Knepley . section - The PetscSection object
4467e8abe2deSMatthew G. Knepley 
4468e8abe2deSMatthew G. Knepley   Level: intermediate
4469e8abe2deSMatthew G. Knepley 
4470e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4471e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4472e8abe2deSMatthew G. Knepley @*/
4473e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4474e8abe2deSMatthew G. Knepley {
4475e8abe2deSMatthew G. Knepley   DM             cdm;
4476e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4477e8abe2deSMatthew G. Knepley 
4478e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4479e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4480e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4481e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4482e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4483e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4484e8abe2deSMatthew G. Knepley }
4485e8abe2deSMatthew G. Knepley 
4486e8abe2deSMatthew G. Knepley /*@
4487e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4488e8abe2deSMatthew G. Knepley 
4489e8abe2deSMatthew G. Knepley   Not Collective
4490e8abe2deSMatthew G. Knepley 
4491e8abe2deSMatthew G. Knepley   Input Parameters:
4492e8abe2deSMatthew G. Knepley + dm      - The DM object
449346e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4494e8abe2deSMatthew G. Knepley - section - The PetscSection object
4495e8abe2deSMatthew G. Knepley 
4496e8abe2deSMatthew G. Knepley   Level: intermediate
4497e8abe2deSMatthew G. Knepley 
4498e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4499e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4500e8abe2deSMatthew G. Knepley @*/
450146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4502e8abe2deSMatthew G. Knepley {
4503e8abe2deSMatthew G. Knepley   DM             cdm;
4504e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4505e8abe2deSMatthew G. Knepley 
4506e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4507e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
450846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4509e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4510e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
451146e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
45124c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
451346e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
451446e270d4SMatthew G. Knepley 
451546e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
451646e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
451746e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
451846e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
451946e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
452046e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
452146e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
452246e270d4SMatthew G. Knepley     }
45236be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
452446e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
452546e270d4SMatthew G. Knepley   }
4526e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4527e8abe2deSMatthew G. Knepley }
4528e8abe2deSMatthew G. Knepley 
45295dc8c3f7SMatthew G. Knepley /*@C
453090b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
45315dc8c3f7SMatthew G. Knepley 
45325dc8c3f7SMatthew G. Knepley   Input Parameters:
453390b157c4SStefano Zampini . dm      - The DM object
453490b157c4SStefano Zampini 
453590b157c4SStefano Zampini   Output Parameters:
453690b157c4SStefano Zampini + per     - Whether the DM is periodic or not
45375dc8c3f7SMatthew 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
45385dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45395dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45405dc8c3f7SMatthew G. Knepley 
45415dc8c3f7SMatthew G. Knepley   Level: developer
45425dc8c3f7SMatthew G. Knepley 
45435dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45445dc8c3f7SMatthew G. Knepley @*/
454590b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4546c6b900c6SMatthew G. Knepley {
4547c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4548c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
454990b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4550c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4551c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
45525dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4553c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4554c6b900c6SMatthew G. Knepley }
4555c6b900c6SMatthew G. Knepley 
45565dc8c3f7SMatthew G. Knepley /*@C
45575dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
45585dc8c3f7SMatthew G. Knepley 
45595dc8c3f7SMatthew G. Knepley   Input Parameters:
45605dc8c3f7SMatthew G. Knepley + dm      - The DM object
456190b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
45625dc8c3f7SMatthew 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
45635dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45645dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45655dc8c3f7SMatthew G. Knepley 
45665dc8c3f7SMatthew G. Knepley   Level: developer
45675dc8c3f7SMatthew G. Knepley 
45685dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45695dc8c3f7SMatthew G. Knepley @*/
457090b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4571c6b900c6SMatthew G. Knepley {
4572c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4573c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4574c6b900c6SMatthew G. Knepley 
4575c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4576c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
457790b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
457890b157c4SStefano Zampini   if (maxCell) {
457990b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
458090b157c4SStefano Zampini     PetscValidPointer(L,4);
458190b157c4SStefano Zampini     PetscValidPointer(bd,5);
458290b157c4SStefano Zampini   }
45835dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
45845dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
458590b157c4SStefano Zampini   if (maxCell) {
45865dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
45875dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
458890b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
458990b157c4SStefano Zampini   } else {
459090b157c4SStefano Zampini     dm->periodic = per;
459190b157c4SStefano Zampini   }
4592c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4593c6b900c6SMatthew G. Knepley }
4594c6b900c6SMatthew G. Knepley 
45952e17dfb7SMatthew G. Knepley /*@
45962e17dfb7SMatthew 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.
45972e17dfb7SMatthew G. Knepley 
45982e17dfb7SMatthew G. Knepley   Input Parameters:
45992e17dfb7SMatthew G. Knepley + dm     - The DM
460065da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
460165da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
46022e17dfb7SMatthew G. Knepley 
46032e17dfb7SMatthew G. Knepley   Output Parameter:
46042e17dfb7SMatthew G. Knepley . out - The localized coordinate point
46052e17dfb7SMatthew G. Knepley 
46062e17dfb7SMatthew G. Knepley   Level: developer
46072e17dfb7SMatthew G. Knepley 
46082e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46092e17dfb7SMatthew G. Knepley @*/
461065da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
46112e17dfb7SMatthew G. Knepley {
46122e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
46132e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
46142e17dfb7SMatthew G. Knepley 
46152e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46162e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
46172e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46182e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46192e17dfb7SMatthew G. Knepley   } else {
462065da65dcSMatthew G. Knepley     if (endpoint) {
462165da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4622da3333bfSMatthew 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)) {
4623da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
462465da65dcSMatthew G. Knepley         } else {
4625da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
462665da65dcSMatthew G. Knepley         }
462765da65dcSMatthew G. Knepley       }
462865da65dcSMatthew G. Knepley     } else {
46292e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
46301118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
46312e17dfb7SMatthew G. Knepley       }
46322e17dfb7SMatthew G. Knepley     }
463365da65dcSMatthew G. Knepley   }
46342e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46352e17dfb7SMatthew G. Knepley }
46362e17dfb7SMatthew G. Knepley 
46372e17dfb7SMatthew G. Knepley /*
46382e17dfb7SMatthew 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.
46392e17dfb7SMatthew G. Knepley 
46402e17dfb7SMatthew G. Knepley   Input Parameters:
46412e17dfb7SMatthew G. Knepley + dm     - The DM
46422e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
46432e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
46442e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
46452e17dfb7SMatthew G. Knepley 
46462e17dfb7SMatthew G. Knepley   Output Parameter:
46472e17dfb7SMatthew G. Knepley . out - The localized coordinate point
46482e17dfb7SMatthew G. Knepley 
46492e17dfb7SMatthew G. Knepley   Level: developer
46502e17dfb7SMatthew G. Knepley 
46512e17dfb7SMatthew 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
46522e17dfb7SMatthew G. Knepley 
46532e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46542e17dfb7SMatthew G. Knepley */
46552e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46562e17dfb7SMatthew G. Knepley {
46572e17dfb7SMatthew G. Knepley   PetscInt d;
46582e17dfb7SMatthew G. Knepley 
46592e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46602e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46612e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46622e17dfb7SMatthew G. Knepley   } else {
46632e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4664908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
46652e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46662e17dfb7SMatthew G. Knepley       } else {
46672e17dfb7SMatthew G. Knepley         out[d] = in[d];
46682e17dfb7SMatthew G. Knepley       }
46692e17dfb7SMatthew G. Knepley     }
46702e17dfb7SMatthew G. Knepley   }
46712e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46722e17dfb7SMatthew G. Knepley }
46732e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
46742e17dfb7SMatthew G. Knepley {
46752e17dfb7SMatthew G. Knepley   PetscInt d;
46762e17dfb7SMatthew G. Knepley 
46772e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46782e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46792e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46802e17dfb7SMatthew G. Knepley   } else {
46812e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4682908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
46832e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
46842e17dfb7SMatthew G. Knepley       } else {
46852e17dfb7SMatthew G. Knepley         out[d] = in[d];
46862e17dfb7SMatthew G. Knepley       }
46872e17dfb7SMatthew G. Knepley     }
46882e17dfb7SMatthew G. Knepley   }
46892e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46902e17dfb7SMatthew G. Knepley }
46912e17dfb7SMatthew G. Knepley 
46922e17dfb7SMatthew G. Knepley /*
46932e17dfb7SMatthew 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.
46942e17dfb7SMatthew G. Knepley 
46952e17dfb7SMatthew G. Knepley   Input Parameters:
46962e17dfb7SMatthew G. Knepley + dm     - The DM
46972e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
46982e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
46992e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
47002e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
47012e17dfb7SMatthew G. Knepley 
47022e17dfb7SMatthew G. Knepley   Output Parameter:
47032e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
47042e17dfb7SMatthew G. Knepley 
47052e17dfb7SMatthew G. Knepley   Level: developer
47062e17dfb7SMatthew G. Knepley 
47072e17dfb7SMatthew 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
47082e17dfb7SMatthew G. Knepley 
47092e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
47102e17dfb7SMatthew G. Knepley */
47112e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
47122e17dfb7SMatthew G. Knepley {
47132e17dfb7SMatthew G. Knepley   PetscInt d;
47142e17dfb7SMatthew G. Knepley 
47152e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47162e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47172e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
47182e17dfb7SMatthew G. Knepley   } else {
47192e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4720908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
47212e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
47222e17dfb7SMatthew G. Knepley       } else {
47232e17dfb7SMatthew G. Knepley         out[d] += in[d];
47242e17dfb7SMatthew G. Knepley       }
47252e17dfb7SMatthew G. Knepley     }
47262e17dfb7SMatthew G. Knepley   }
47272e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47282e17dfb7SMatthew G. Knepley }
47292e17dfb7SMatthew G. Knepley 
473036447a5eSToby Isaac /*@
473136447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
473236447a5eSToby Isaac 
473336447a5eSToby Isaac   Input Parameter:
473436447a5eSToby Isaac . dm - The DM
473536447a5eSToby Isaac 
473636447a5eSToby Isaac   Output Parameter:
473736447a5eSToby Isaac   areLocalized - True if localized
473836447a5eSToby Isaac 
473936447a5eSToby Isaac   Level: developer
474036447a5eSToby Isaac 
474136447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
474236447a5eSToby Isaac @*/
474336447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
474436447a5eSToby Isaac {
474536447a5eSToby Isaac   DM             cdm;
474636447a5eSToby Isaac   PetscSection   coordSection;
474736447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
474836447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
474936447a5eSToby Isaac   PetscErrorCode ierr;
475036447a5eSToby Isaac 
475136447a5eSToby Isaac   PetscFunctionBegin;
475236447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
475392c9c85fSStefano Zampini   if (!dm->periodic) {
47548b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
47558b09590cSToby Isaac     PetscFunctionReturn(0);
47568b09590cSToby Isaac   }
475736447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
475836447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
475936447a5eSToby Isaac   {
476036447a5eSToby Isaac     PetscBool isplex;
476136447a5eSToby Isaac 
476236447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
476336447a5eSToby Isaac     if (isplex) {
476436447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
476536447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
476636447a5eSToby Isaac   }
476736447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
476836447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47695a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
477036447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
477136447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
477236447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
477336447a5eSToby Isaac       break;
477436447a5eSToby Isaac     }
477536447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
47765a42012cSToby Isaac     if (dof) {
47775a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
477836447a5eSToby Isaac       break;
477936447a5eSToby Isaac     }
478036447a5eSToby Isaac   }
47815a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
478236447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
478336447a5eSToby Isaac   PetscFunctionReturn(0);
478436447a5eSToby Isaac }
478536447a5eSToby Isaac 
478636447a5eSToby Isaac 
47872e17dfb7SMatthew G. Knepley /*@
4788492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
47892e17dfb7SMatthew G. Knepley 
47902e17dfb7SMatthew G. Knepley   Input Parameter:
47912e17dfb7SMatthew G. Knepley . dm - The DM
47922e17dfb7SMatthew G. Knepley 
47932e17dfb7SMatthew G. Knepley   Level: developer
47942e17dfb7SMatthew G. Knepley 
47952e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
47962e17dfb7SMatthew G. Knepley @*/
47972e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
47982e17dfb7SMatthew G. Knepley {
47992e17dfb7SMatthew G. Knepley   DM             cdm;
48002e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
48012e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
48023e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
48033e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4804e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
48053e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
48063e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
48072e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
48082e17dfb7SMatthew G. Knepley 
48092e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48102e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
481192c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
4812f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
4813f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
4814f7cbd40bSStefano Zampini 
48152e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
48162e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
48172e17dfb7SMatthew G. Knepley   {
48182e17dfb7SMatthew G. Knepley     PetscBool isplex;
48192e17dfb7SMatthew G. Knepley 
48202e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
48212e17dfb7SMatthew G. Knepley     if (isplex) {
48222e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
48233e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
482469291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
48253e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
48263e922f36SToby Isaac       newStart = vStart;
48273e922f36SToby Isaac       newEnd   = vEnd;
48283e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
48293e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
48303e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
48313e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
48323e922f36SToby Isaac       }
48332e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
48342e17dfb7SMatthew G. Knepley   }
48352e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
483643eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
48372e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
48383e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4839e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
48403e922f36SToby Isaac 
48412e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
48422e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
48432e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
48442e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
48453e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
48463e922f36SToby Isaac 
484769291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
48483e922f36SToby Isaac   localized = &anchor[bs];
48493e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
48503e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
48513e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
48523e922f36SToby Isaac 
48533e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
48543e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
48553e922f36SToby Isaac       PetscInt     b;
48563e922f36SToby Isaac 
48573e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
48583e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48593e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48603e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
48613e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
48623e922f36SToby Isaac         for (b = 0; b < bs; b++) {
48633e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
48643e922f36SToby Isaac         }
48653e922f36SToby Isaac         if (b < bs) break;
48663e922f36SToby Isaac       }
48673e922f36SToby Isaac       if (d < dof/bs) {
48683e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
48693e922f36SToby Isaac           PetscInt cdof;
48703e922f36SToby Isaac 
48713e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
48723e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
48733e922f36SToby Isaac         }
48743e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
48753e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
48763e922f36SToby Isaac       }
48773e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48783e922f36SToby Isaac     }
48793e922f36SToby Isaac   }
48803e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
48813e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
488269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
48833e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
488469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
48853e922f36SToby Isaac     PetscFunctionReturn(0);
48863e922f36SToby Isaac   }
48872e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
48882e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
48892e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
48902e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
48912e17dfb7SMatthew G. Knepley   }
48922e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
48932e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4894c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
48952e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
48962e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
48972e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
48982e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4899c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49002e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
49012e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
49022e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
49032e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
49042e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
49052e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
49062e17dfb7SMatthew G. Knepley   }
49073e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
49083e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
49093e922f36SToby Isaac 
49102e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
49112e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
49123e922f36SToby Isaac       PetscInt     b, cdof;
49132e17dfb7SMatthew G. Knepley 
49143e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
49153e922f36SToby Isaac       if (!cdof) continue;
49162e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49172e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
49182e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
49192e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
49202e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49212e17dfb7SMatthew G. Knepley     }
49223e922f36SToby Isaac   }
492369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
492469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
4925c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49262e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
49272e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
49282e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
49292e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
49302e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
49312e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
49322e17dfb7SMatthew G. Knepley }
49332e17dfb7SMatthew G. Knepley 
4934e87bb0d3SMatthew G Knepley /*@
49353a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4936e87bb0d3SMatthew G Knepley 
49373a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4938e87bb0d3SMatthew G Knepley 
4939e87bb0d3SMatthew G Knepley   Input Parameters:
4940e87bb0d3SMatthew G Knepley + dm - The DM
49413a93e3b7SToby Isaac . v - The Vec of points
494262a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
49433a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4944e87bb0d3SMatthew G Knepley 
494561e3bb9bSMatthew G Knepley   Output Parameter:
494662a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
494762a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
49483a93e3b7SToby Isaac 
4949e87bb0d3SMatthew G Knepley 
4950e87bb0d3SMatthew G Knepley   Level: developer
495161e3bb9bSMatthew G Knepley 
495262a38674SMatthew G. Knepley   Notes:
49533a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
495462a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
49553a93e3b7SToby Isaac 
49563a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
495762a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
49583a93e3b7SToby Isaac 
49593a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
49603a93e3b7SToby Isaac 
496162a38674SMatthew G. Knepley $    const PetscSFNode *cells;
496262a38674SMatthew G. Knepley $    PetscInt           nFound;
496362a38674SMatthew G. Knepley $    const PetscSFNode *found;
496462a38674SMatthew G. Knepley $
496562a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
49663a93e3b7SToby Isaac 
49673a93e3b7SToby 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
49683a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
49693a93e3b7SToby Isaac 
497061e3bb9bSMatthew G Knepley .keywords: point location, mesh
497162a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
497261e3bb9bSMatthew G Knepley @*/
497362a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4974e87bb0d3SMatthew G Knepley {
4975735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4976735aa83eSMatthew G Knepley 
4977e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4978e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4979e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4980e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
49813a93e3b7SToby Isaac   if (*cellSF) {
49823a93e3b7SToby Isaac     PetscMPIInt result;
49833a93e3b7SToby Isaac 
4984e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
4985a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
49863a93e3b7SToby 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");
4987e0fc9d1bSMatthew G. Knepley   } else {
49883a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
49893a93e3b7SToby Isaac   }
499047a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4991735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
499262a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
499382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
499447a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4995e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4996e87bb0d3SMatthew G Knepley }
499714f150ffSMatthew G. Knepley 
4998f4d763aaSMatthew G. Knepley /*@
4999f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5000f4d763aaSMatthew G. Knepley 
5001f4d763aaSMatthew G. Knepley   Input Parameter:
5002f4d763aaSMatthew G. Knepley . dm - The original DM
5003f4d763aaSMatthew G. Knepley 
5004f4d763aaSMatthew G. Knepley   Output Parameter:
5005f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5006f4d763aaSMatthew G. Knepley 
5007f4d763aaSMatthew G. Knepley   Level: intermediate
5008f4d763aaSMatthew G. Knepley 
5009f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
5010f4d763aaSMatthew G. Knepley @*/
501114f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
501214f150ffSMatthew G. Knepley {
5013c26acbdeSMatthew G. Knepley   PetscSection   section;
50142d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
501514f150ffSMatthew G. Knepley   PetscErrorCode ierr;
501614f150ffSMatthew G. Knepley 
501714f150ffSMatthew G. Knepley   PetscFunctionBegin;
501814f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
501914f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5020c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
5021c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5022127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
50232d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5024c26acbdeSMatthew G. Knepley     *odm = dm;
5025c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5026c26acbdeSMatthew G. Knepley   }
502714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
50280305aff6SMatthew G. Knepley     PetscDS      ds;
5029c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
503014f150ffSMatthew G. Knepley     PetscSF      sf;
503114f150ffSMatthew G. Knepley 
503214f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
50330305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
50340305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
503514f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
503614f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
503714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
503814f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
503915b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
504014f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
504114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
504214f150ffSMatthew G. Knepley   }
504314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
504414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
504514f150ffSMatthew G. Knepley }
5046f4d763aaSMatthew G. Knepley 
5047f4d763aaSMatthew G. Knepley /*@
5048cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5049f4d763aaSMatthew G. Knepley 
5050f4d763aaSMatthew G. Knepley   Input Parameter:
5051f4d763aaSMatthew G. Knepley . dm - The original DM
5052f4d763aaSMatthew G. Knepley 
5053cdb7a50dSMatthew G. Knepley   Output Parameters:
5054cdb7a50dSMatthew G. Knepley + num - The output sequence number
5055cdb7a50dSMatthew G. Knepley - val - The output sequence value
5056f4d763aaSMatthew G. Knepley 
5057f4d763aaSMatthew G. Knepley   Level: intermediate
5058f4d763aaSMatthew G. Knepley 
5059f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5060f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5061f4d763aaSMatthew G. Knepley 
5062f4d763aaSMatthew G. Knepley .seealso: VecView()
5063f4d763aaSMatthew G. Knepley @*/
5064cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5065f4d763aaSMatthew G. Knepley {
5066f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5067f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5068cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5069cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5070f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5071f4d763aaSMatthew G. Knepley }
5072f4d763aaSMatthew G. Knepley 
5073f4d763aaSMatthew G. Knepley /*@
5074cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5075f4d763aaSMatthew G. Knepley 
5076f4d763aaSMatthew G. Knepley   Input Parameters:
5077f4d763aaSMatthew G. Knepley + dm - The original DM
5078cdb7a50dSMatthew G. Knepley . num - The output sequence number
5079cdb7a50dSMatthew G. Knepley - val - The output sequence value
5080f4d763aaSMatthew G. Knepley 
5081f4d763aaSMatthew G. Knepley   Level: intermediate
5082f4d763aaSMatthew G. Knepley 
5083f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5084f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5085f4d763aaSMatthew G. Knepley 
5086f4d763aaSMatthew G. Knepley .seealso: VecView()
5087f4d763aaSMatthew G. Knepley @*/
5088cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5089f4d763aaSMatthew G. Knepley {
5090f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5091f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5092f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5093cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5094cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5095cdb7a50dSMatthew G. Knepley }
5096cdb7a50dSMatthew G. Knepley 
5097cdb7a50dSMatthew G. Knepley /*@C
5098cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5099cdb7a50dSMatthew G. Knepley 
5100cdb7a50dSMatthew G. Knepley   Input Parameters:
5101cdb7a50dSMatthew G. Knepley + dm   - The original DM
5102cdb7a50dSMatthew G. Knepley . name - The sequence name
5103cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5104cdb7a50dSMatthew G. Knepley 
5105cdb7a50dSMatthew G. Knepley   Output Parameter:
5106cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5107cdb7a50dSMatthew G. Knepley 
5108cdb7a50dSMatthew G. Knepley   Level: intermediate
5109cdb7a50dSMatthew G. Knepley 
5110cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5111cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5112cdb7a50dSMatthew G. Knepley 
5113cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5114cdb7a50dSMatthew G. Knepley @*/
5115cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5116cdb7a50dSMatthew G. Knepley {
5117cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5118cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5119cdb7a50dSMatthew G. Knepley 
5120cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5121cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5122cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5123cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5124cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5125cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5126cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5127cdb7a50dSMatthew G. Knepley     PetscScalar value;
5128cdb7a50dSMatthew G. Knepley 
512939d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
51304aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5131cdb7a50dSMatthew G. Knepley #endif
5132cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5133f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5134f4d763aaSMatthew G. Knepley }
51358e4ac7eaSMatthew G. Knepley 
51368e4ac7eaSMatthew G. Knepley /*@
51378e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
51388e4ac7eaSMatthew G. Knepley 
51398e4ac7eaSMatthew G. Knepley   Not collective
51408e4ac7eaSMatthew G. Knepley 
51418e4ac7eaSMatthew G. Knepley   Input Parameter:
51428e4ac7eaSMatthew G. Knepley . dm - The DM
51438e4ac7eaSMatthew G. Knepley 
51448e4ac7eaSMatthew G. Knepley   Output Parameter:
51458e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
51468e4ac7eaSMatthew G. Knepley 
51478e4ac7eaSMatthew G. Knepley   Level: beginner
51488e4ac7eaSMatthew G. Knepley 
51498e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
51508e4ac7eaSMatthew G. Knepley @*/
51518e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
51528e4ac7eaSMatthew G. Knepley {
51538e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51548e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51558e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
51568e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
51578e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51588e4ac7eaSMatthew G. Knepley }
51598e4ac7eaSMatthew G. Knepley 
51608e4ac7eaSMatthew G. Knepley /*@
51618e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
51628e4ac7eaSMatthew G. Knepley 
51638e4ac7eaSMatthew G. Knepley   Collective on dm
51648e4ac7eaSMatthew G. Knepley 
51658e4ac7eaSMatthew G. Knepley   Input Parameters:
51668e4ac7eaSMatthew G. Knepley + dm - The DM
51678e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
51688e4ac7eaSMatthew G. Knepley 
51698e4ac7eaSMatthew G. Knepley   Level: beginner
51708e4ac7eaSMatthew G. Knepley 
51718e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
51728e4ac7eaSMatthew G. Knepley @*/
51738e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
51748e4ac7eaSMatthew G. Knepley {
51758e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51768e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51778e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
51788e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
51798e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51808e4ac7eaSMatthew G. Knepley }
5181c58f1c22SToby Isaac 
5182c58f1c22SToby Isaac 
5183c58f1c22SToby Isaac /*@C
5184c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5185c58f1c22SToby Isaac 
5186c58f1c22SToby Isaac   Not Collective
5187c58f1c22SToby Isaac 
5188c58f1c22SToby Isaac   Input Parameters:
5189c58f1c22SToby Isaac + dm   - The DM object
5190c58f1c22SToby Isaac - name - The label name
5191c58f1c22SToby Isaac 
5192c58f1c22SToby Isaac   Level: intermediate
5193c58f1c22SToby Isaac 
5194c58f1c22SToby Isaac .keywords: mesh
5195c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5196c58f1c22SToby Isaac @*/
5197c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5198c58f1c22SToby Isaac {
5199c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5200c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5201c58f1c22SToby Isaac   PetscErrorCode ierr;
5202c58f1c22SToby Isaac 
5203c58f1c22SToby Isaac   PetscFunctionBegin;
5204c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5205c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5206c58f1c22SToby Isaac   while (next) {
5207c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5208c58f1c22SToby Isaac     if (flg) break;
5209c58f1c22SToby Isaac     next = next->next;
5210c58f1c22SToby Isaac   }
5211c58f1c22SToby Isaac   if (!flg) {
5212c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5213c58f1c22SToby Isaac 
5214c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5215c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5216c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5217c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5218c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5219c58f1c22SToby Isaac   }
5220c58f1c22SToby Isaac   PetscFunctionReturn(0);
5221c58f1c22SToby Isaac }
5222c58f1c22SToby Isaac 
5223c58f1c22SToby Isaac /*@C
5224c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5225c58f1c22SToby Isaac 
5226c58f1c22SToby Isaac   Not Collective
5227c58f1c22SToby Isaac 
5228c58f1c22SToby Isaac   Input Parameters:
5229c58f1c22SToby Isaac + dm   - The DM object
5230c58f1c22SToby Isaac . name - The label name
5231c58f1c22SToby Isaac - point - The mesh point
5232c58f1c22SToby Isaac 
5233c58f1c22SToby Isaac   Output Parameter:
5234c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5235c58f1c22SToby Isaac 
5236c58f1c22SToby Isaac   Level: beginner
5237c58f1c22SToby Isaac 
5238c58f1c22SToby Isaac .keywords: mesh
5239c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5240c58f1c22SToby Isaac @*/
5241c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5242c58f1c22SToby Isaac {
5243c58f1c22SToby Isaac   DMLabel        label;
5244c58f1c22SToby Isaac   PetscErrorCode ierr;
5245c58f1c22SToby Isaac 
5246c58f1c22SToby Isaac   PetscFunctionBegin;
5247c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5248c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5249c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
525013903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5251c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5252c58f1c22SToby Isaac   PetscFunctionReturn(0);
5253c58f1c22SToby Isaac }
5254c58f1c22SToby Isaac 
5255c58f1c22SToby Isaac /*@C
5256c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5257c58f1c22SToby Isaac 
5258c58f1c22SToby Isaac   Not Collective
5259c58f1c22SToby Isaac 
5260c58f1c22SToby Isaac   Input Parameters:
5261c58f1c22SToby Isaac + dm   - The DM object
5262c58f1c22SToby Isaac . name - The label name
5263c58f1c22SToby Isaac . point - The mesh point
5264c58f1c22SToby Isaac - value - The label value for this point
5265c58f1c22SToby Isaac 
5266c58f1c22SToby Isaac   Output Parameter:
5267c58f1c22SToby Isaac 
5268c58f1c22SToby Isaac   Level: beginner
5269c58f1c22SToby Isaac 
5270c58f1c22SToby Isaac .keywords: mesh
5271c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5272c58f1c22SToby Isaac @*/
5273c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5274c58f1c22SToby Isaac {
5275c58f1c22SToby Isaac   DMLabel        label;
5276c58f1c22SToby Isaac   PetscErrorCode ierr;
5277c58f1c22SToby Isaac 
5278c58f1c22SToby Isaac   PetscFunctionBegin;
5279c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5280c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5281c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5282c58f1c22SToby Isaac   if (!label) {
5283c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5284c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5285c58f1c22SToby Isaac   }
5286c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5287c58f1c22SToby Isaac   PetscFunctionReturn(0);
5288c58f1c22SToby Isaac }
5289c58f1c22SToby Isaac 
5290c58f1c22SToby Isaac /*@C
5291c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5292c58f1c22SToby Isaac 
5293c58f1c22SToby Isaac   Not Collective
5294c58f1c22SToby Isaac 
5295c58f1c22SToby Isaac   Input Parameters:
5296c58f1c22SToby Isaac + dm   - The DM object
5297c58f1c22SToby Isaac . name - The label name
5298c58f1c22SToby Isaac . point - The mesh point
5299c58f1c22SToby Isaac - value - The label value for this point
5300c58f1c22SToby Isaac 
5301c58f1c22SToby Isaac   Output Parameter:
5302c58f1c22SToby Isaac 
5303c58f1c22SToby Isaac   Level: beginner
5304c58f1c22SToby Isaac 
5305c58f1c22SToby Isaac .keywords: mesh
5306c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5307c58f1c22SToby Isaac @*/
5308c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5309c58f1c22SToby Isaac {
5310c58f1c22SToby Isaac   DMLabel        label;
5311c58f1c22SToby Isaac   PetscErrorCode ierr;
5312c58f1c22SToby Isaac 
5313c58f1c22SToby Isaac   PetscFunctionBegin;
5314c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5315c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5316c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5317c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5318c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5319c58f1c22SToby Isaac   PetscFunctionReturn(0);
5320c58f1c22SToby Isaac }
5321c58f1c22SToby Isaac 
5322c58f1c22SToby Isaac /*@C
5323c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5324c58f1c22SToby Isaac 
5325c58f1c22SToby Isaac   Not Collective
5326c58f1c22SToby Isaac 
5327c58f1c22SToby Isaac   Input Parameters:
5328c58f1c22SToby Isaac + dm   - The DM object
5329c58f1c22SToby Isaac - name - The label name
5330c58f1c22SToby Isaac 
5331c58f1c22SToby Isaac   Output Parameter:
5332c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5333c58f1c22SToby Isaac 
5334c58f1c22SToby Isaac   Level: beginner
5335c58f1c22SToby Isaac 
5336c58f1c22SToby Isaac .keywords: mesh
5337c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5338c58f1c22SToby Isaac @*/
5339c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5340c58f1c22SToby Isaac {
5341c58f1c22SToby Isaac   DMLabel        label;
5342c58f1c22SToby Isaac   PetscErrorCode ierr;
5343c58f1c22SToby Isaac 
5344c58f1c22SToby Isaac   PetscFunctionBegin;
5345c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5346c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5347c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5348c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5349c58f1c22SToby Isaac   *size = 0;
5350c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5351c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5352c58f1c22SToby Isaac   PetscFunctionReturn(0);
5353c58f1c22SToby Isaac }
5354c58f1c22SToby Isaac 
5355c58f1c22SToby Isaac /*@C
5356c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5357c58f1c22SToby Isaac 
5358c58f1c22SToby Isaac   Not Collective
5359c58f1c22SToby Isaac 
5360c58f1c22SToby Isaac   Input Parameters:
5361c58f1c22SToby Isaac + mesh - The DM object
5362c58f1c22SToby Isaac - name - The label name
5363c58f1c22SToby Isaac 
5364c58f1c22SToby Isaac   Output Parameter:
5365c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5366c58f1c22SToby Isaac 
5367c58f1c22SToby Isaac   Level: beginner
5368c58f1c22SToby Isaac 
5369c58f1c22SToby Isaac .keywords: mesh
5370c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5371c58f1c22SToby Isaac @*/
5372c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5373c58f1c22SToby Isaac {
5374c58f1c22SToby Isaac   DMLabel        label;
5375c58f1c22SToby Isaac   PetscErrorCode ierr;
5376c58f1c22SToby Isaac 
5377c58f1c22SToby Isaac   PetscFunctionBegin;
5378c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5379c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5380c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5381c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5382c58f1c22SToby Isaac   *ids = NULL;
5383c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5384c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5385c58f1c22SToby Isaac   PetscFunctionReturn(0);
5386c58f1c22SToby Isaac }
5387c58f1c22SToby Isaac 
5388c58f1c22SToby Isaac /*@C
5389c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5390c58f1c22SToby Isaac 
5391c58f1c22SToby Isaac   Not Collective
5392c58f1c22SToby Isaac 
5393c58f1c22SToby Isaac   Input Parameters:
5394c58f1c22SToby Isaac + dm - The DM object
5395c58f1c22SToby Isaac . name - The label name
5396c58f1c22SToby Isaac - value - The stratum value
5397c58f1c22SToby Isaac 
5398c58f1c22SToby Isaac   Output Parameter:
5399c58f1c22SToby Isaac . size - The stratum size
5400c58f1c22SToby Isaac 
5401c58f1c22SToby Isaac   Level: beginner
5402c58f1c22SToby Isaac 
5403c58f1c22SToby Isaac .keywords: mesh
5404c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5405c58f1c22SToby Isaac @*/
5406c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5407c58f1c22SToby Isaac {
5408c58f1c22SToby Isaac   DMLabel        label;
5409c58f1c22SToby Isaac   PetscErrorCode ierr;
5410c58f1c22SToby Isaac 
5411c58f1c22SToby Isaac   PetscFunctionBegin;
5412c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5413c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5414c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5415c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5416c58f1c22SToby Isaac   *size = 0;
5417c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5418c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5419c58f1c22SToby Isaac   PetscFunctionReturn(0);
5420c58f1c22SToby Isaac }
5421c58f1c22SToby Isaac 
5422c58f1c22SToby Isaac /*@C
5423c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5424c58f1c22SToby Isaac 
5425c58f1c22SToby Isaac   Not Collective
5426c58f1c22SToby Isaac 
5427c58f1c22SToby Isaac   Input Parameters:
5428c58f1c22SToby Isaac + dm - The DM object
5429c58f1c22SToby Isaac . name - The label name
5430c58f1c22SToby Isaac - value - The stratum value
5431c58f1c22SToby Isaac 
5432c58f1c22SToby Isaac   Output Parameter:
5433c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5434c58f1c22SToby Isaac 
5435c58f1c22SToby Isaac   Level: beginner
5436c58f1c22SToby Isaac 
5437c58f1c22SToby Isaac .keywords: mesh
5438c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5439c58f1c22SToby Isaac @*/
5440c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5441c58f1c22SToby Isaac {
5442c58f1c22SToby Isaac   DMLabel        label;
5443c58f1c22SToby Isaac   PetscErrorCode ierr;
5444c58f1c22SToby Isaac 
5445c58f1c22SToby Isaac   PetscFunctionBegin;
5446c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5447c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5448c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5449c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5450c58f1c22SToby Isaac   *points = NULL;
5451c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5452c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5453c58f1c22SToby Isaac   PetscFunctionReturn(0);
5454c58f1c22SToby Isaac }
5455c58f1c22SToby Isaac 
54564de306b1SToby Isaac /*@C
54574de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
54584de306b1SToby Isaac 
54594de306b1SToby Isaac   Not Collective
54604de306b1SToby Isaac 
54614de306b1SToby Isaac   Input Parameters:
54624de306b1SToby Isaac + dm - The DM object
54634de306b1SToby Isaac . name - The label name
54644de306b1SToby Isaac . value - The stratum value
54654de306b1SToby Isaac - points - The stratum points
54664de306b1SToby Isaac 
54674de306b1SToby Isaac   Level: beginner
54684de306b1SToby Isaac 
54694de306b1SToby Isaac .keywords: mesh
54704de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
54714de306b1SToby Isaac @*/
54724de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
54734de306b1SToby Isaac {
54744de306b1SToby Isaac   DMLabel        label;
54754de306b1SToby Isaac   PetscErrorCode ierr;
54764de306b1SToby Isaac 
54774de306b1SToby Isaac   PetscFunctionBegin;
54784de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54794de306b1SToby Isaac   PetscValidCharPointer(name, 2);
54804de306b1SToby Isaac   PetscValidPointer(points, 4);
54814de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
54824de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
54834de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
54844de306b1SToby Isaac   PetscFunctionReturn(0);
54854de306b1SToby Isaac }
54864de306b1SToby Isaac 
5487c58f1c22SToby Isaac /*@C
5488c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5489c58f1c22SToby Isaac 
5490c58f1c22SToby Isaac   Not Collective
5491c58f1c22SToby Isaac 
5492c58f1c22SToby Isaac   Input Parameters:
5493c58f1c22SToby Isaac + dm   - The DM object
5494c58f1c22SToby Isaac . name - The label name
5495c58f1c22SToby Isaac - value - The label value for this point
5496c58f1c22SToby Isaac 
5497c58f1c22SToby Isaac   Output Parameter:
5498c58f1c22SToby Isaac 
5499c58f1c22SToby Isaac   Level: beginner
5500c58f1c22SToby Isaac 
5501c58f1c22SToby Isaac .keywords: mesh
5502c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5503c58f1c22SToby Isaac @*/
5504c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5505c58f1c22SToby Isaac {
5506c58f1c22SToby Isaac   DMLabel        label;
5507c58f1c22SToby Isaac   PetscErrorCode ierr;
5508c58f1c22SToby Isaac 
5509c58f1c22SToby Isaac   PetscFunctionBegin;
5510c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5511c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5512c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5513c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5514c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5515c58f1c22SToby Isaac   PetscFunctionReturn(0);
5516c58f1c22SToby Isaac }
5517c58f1c22SToby Isaac 
5518c58f1c22SToby Isaac /*@
5519c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5520c58f1c22SToby Isaac 
5521c58f1c22SToby Isaac   Not Collective
5522c58f1c22SToby Isaac 
5523c58f1c22SToby Isaac   Input Parameter:
5524c58f1c22SToby Isaac . dm   - The DM object
5525c58f1c22SToby Isaac 
5526c58f1c22SToby Isaac   Output Parameter:
5527c58f1c22SToby Isaac . numLabels - the number of Labels
5528c58f1c22SToby Isaac 
5529c58f1c22SToby Isaac   Level: intermediate
5530c58f1c22SToby Isaac 
5531c58f1c22SToby Isaac .keywords: mesh
5532c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5533c58f1c22SToby Isaac @*/
5534c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5535c58f1c22SToby Isaac {
5536c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5537c58f1c22SToby Isaac   PetscInt  n    = 0;
5538c58f1c22SToby Isaac 
5539c58f1c22SToby Isaac   PetscFunctionBegin;
5540c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5541c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5542c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5543c58f1c22SToby Isaac   *numLabels = n;
5544c58f1c22SToby Isaac   PetscFunctionReturn(0);
5545c58f1c22SToby Isaac }
5546c58f1c22SToby Isaac 
5547c58f1c22SToby Isaac /*@C
5548c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5549c58f1c22SToby Isaac 
5550c58f1c22SToby Isaac   Not Collective
5551c58f1c22SToby Isaac 
5552c58f1c22SToby Isaac   Input Parameters:
5553c58f1c22SToby Isaac + dm - The DM object
5554c58f1c22SToby Isaac - n  - the label number
5555c58f1c22SToby Isaac 
5556c58f1c22SToby Isaac   Output Parameter:
5557c58f1c22SToby Isaac . name - the label name
5558c58f1c22SToby Isaac 
5559c58f1c22SToby Isaac   Level: intermediate
5560c58f1c22SToby Isaac 
5561c58f1c22SToby Isaac .keywords: mesh
5562c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5563c58f1c22SToby Isaac @*/
5564c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5565c58f1c22SToby Isaac {
5566c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5567c58f1c22SToby Isaac   PetscInt  l    = 0;
5568c58f1c22SToby Isaac 
5569c58f1c22SToby Isaac   PetscFunctionBegin;
5570c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5571c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5572c58f1c22SToby Isaac   while (next) {
5573c58f1c22SToby Isaac     if (l == n) {
5574c58f1c22SToby Isaac       *name = next->label->name;
5575c58f1c22SToby Isaac       PetscFunctionReturn(0);
5576c58f1c22SToby Isaac     }
5577c58f1c22SToby Isaac     ++l;
5578c58f1c22SToby Isaac     next = next->next;
5579c58f1c22SToby Isaac   }
5580c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5581c58f1c22SToby Isaac }
5582c58f1c22SToby Isaac 
5583c58f1c22SToby Isaac /*@C
5584c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5585c58f1c22SToby Isaac 
5586c58f1c22SToby Isaac   Not Collective
5587c58f1c22SToby Isaac 
5588c58f1c22SToby Isaac   Input Parameters:
5589c58f1c22SToby Isaac + dm   - The DM object
5590c58f1c22SToby Isaac - name - The label name
5591c58f1c22SToby Isaac 
5592c58f1c22SToby Isaac   Output Parameter:
5593c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5594c58f1c22SToby Isaac 
5595c58f1c22SToby Isaac   Level: intermediate
5596c58f1c22SToby Isaac 
5597c58f1c22SToby Isaac .keywords: mesh
5598c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5599c58f1c22SToby Isaac @*/
5600c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5601c58f1c22SToby Isaac {
5602c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5603c58f1c22SToby Isaac   PetscErrorCode ierr;
5604c58f1c22SToby Isaac 
5605c58f1c22SToby Isaac   PetscFunctionBegin;
5606c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5607c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5608c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5609c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5610c58f1c22SToby Isaac   while (next) {
5611c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5612c58f1c22SToby Isaac     if (*hasLabel) break;
5613c58f1c22SToby Isaac     next = next->next;
5614c58f1c22SToby Isaac   }
5615c58f1c22SToby Isaac   PetscFunctionReturn(0);
5616c58f1c22SToby Isaac }
5617c58f1c22SToby Isaac 
5618c58f1c22SToby Isaac /*@C
5619c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5620c58f1c22SToby Isaac 
5621c58f1c22SToby Isaac   Not Collective
5622c58f1c22SToby Isaac 
5623c58f1c22SToby Isaac   Input Parameters:
5624c58f1c22SToby Isaac + dm   - The DM object
5625c58f1c22SToby Isaac - name - The label name
5626c58f1c22SToby Isaac 
5627c58f1c22SToby Isaac   Output Parameter:
5628c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5629c58f1c22SToby Isaac 
5630c58f1c22SToby Isaac   Level: intermediate
5631c58f1c22SToby Isaac 
5632c58f1c22SToby Isaac .keywords: mesh
5633c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5634c58f1c22SToby Isaac @*/
5635c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5636c58f1c22SToby Isaac {
5637c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5638c58f1c22SToby Isaac   PetscBool      hasLabel;
5639c58f1c22SToby Isaac   PetscErrorCode ierr;
5640c58f1c22SToby Isaac 
5641c58f1c22SToby Isaac   PetscFunctionBegin;
5642c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5643c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5644c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5645c58f1c22SToby Isaac   *label = NULL;
5646c58f1c22SToby Isaac   while (next) {
5647c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5648c58f1c22SToby Isaac     if (hasLabel) {
5649c58f1c22SToby Isaac       *label = next->label;
5650c58f1c22SToby Isaac       break;
5651c58f1c22SToby Isaac     }
5652c58f1c22SToby Isaac     next = next->next;
5653c58f1c22SToby Isaac   }
5654c58f1c22SToby Isaac   PetscFunctionReturn(0);
5655c58f1c22SToby Isaac }
5656c58f1c22SToby Isaac 
5657c58f1c22SToby Isaac /*@C
5658c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5659c58f1c22SToby Isaac 
5660c58f1c22SToby Isaac   Not Collective
5661c58f1c22SToby Isaac 
5662c58f1c22SToby Isaac   Input Parameters:
5663c58f1c22SToby Isaac + dm - The DM object
5664c58f1c22SToby Isaac - n  - the label number
5665c58f1c22SToby Isaac 
5666c58f1c22SToby Isaac   Output Parameter:
5667c58f1c22SToby Isaac . label - the label
5668c58f1c22SToby Isaac 
5669c58f1c22SToby Isaac   Level: intermediate
5670c58f1c22SToby Isaac 
5671c58f1c22SToby Isaac .keywords: mesh
5672c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5673c58f1c22SToby Isaac @*/
5674c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5675c58f1c22SToby Isaac {
5676c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5677c58f1c22SToby Isaac   PetscInt    l    = 0;
5678c58f1c22SToby Isaac 
5679c58f1c22SToby Isaac   PetscFunctionBegin;
5680c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5681c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5682c58f1c22SToby Isaac   while (next) {
5683c58f1c22SToby Isaac     if (l == n) {
5684c58f1c22SToby Isaac       *label = next->label;
5685c58f1c22SToby Isaac       PetscFunctionReturn(0);
5686c58f1c22SToby Isaac     }
5687c58f1c22SToby Isaac     ++l;
5688c58f1c22SToby Isaac     next = next->next;
5689c58f1c22SToby Isaac   }
5690c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5691c58f1c22SToby Isaac }
5692c58f1c22SToby Isaac 
5693c58f1c22SToby Isaac /*@C
5694c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5695c58f1c22SToby Isaac 
5696c58f1c22SToby Isaac   Not Collective
5697c58f1c22SToby Isaac 
5698c58f1c22SToby Isaac   Input Parameters:
5699c58f1c22SToby Isaac + dm   - The DM object
5700c58f1c22SToby Isaac - label - The DMLabel
5701c58f1c22SToby Isaac 
5702c58f1c22SToby Isaac   Level: developer
5703c58f1c22SToby Isaac 
5704c58f1c22SToby Isaac .keywords: mesh
5705c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5706c58f1c22SToby Isaac @*/
5707c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5708c58f1c22SToby Isaac {
5709c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5710c58f1c22SToby Isaac   PetscBool      hasLabel;
5711c58f1c22SToby Isaac   PetscErrorCode ierr;
5712c58f1c22SToby Isaac 
5713c58f1c22SToby Isaac   PetscFunctionBegin;
5714c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5715c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5716c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5717c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5718c58f1c22SToby Isaac   tmpLabel->label  = label;
5719c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5720c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5721c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5722c58f1c22SToby Isaac   PetscFunctionReturn(0);
5723c58f1c22SToby Isaac }
5724c58f1c22SToby Isaac 
5725c58f1c22SToby Isaac /*@C
5726c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5727c58f1c22SToby Isaac 
5728c58f1c22SToby Isaac   Not Collective
5729c58f1c22SToby Isaac 
5730c58f1c22SToby Isaac   Input Parameters:
5731c58f1c22SToby Isaac + dm   - The DM object
5732c58f1c22SToby Isaac - name - The label name
5733c58f1c22SToby Isaac 
5734c58f1c22SToby Isaac   Output Parameter:
5735c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5736c58f1c22SToby Isaac 
5737c58f1c22SToby Isaac   Level: developer
5738c58f1c22SToby Isaac 
5739c58f1c22SToby Isaac .keywords: mesh
5740c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5741c58f1c22SToby Isaac @*/
5742c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5743c58f1c22SToby Isaac {
5744c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5745c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5746c58f1c22SToby Isaac   PetscBool      hasLabel;
5747c58f1c22SToby Isaac   PetscErrorCode ierr;
5748c58f1c22SToby Isaac 
5749c58f1c22SToby Isaac   PetscFunctionBegin;
5750c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5751c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5752c58f1c22SToby Isaac   *label = NULL;
5753c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5754c58f1c22SToby Isaac   while (next) {
5755c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5756c58f1c22SToby Isaac     if (hasLabel) {
5757c58f1c22SToby Isaac       if (last) last->next       = next->next;
5758c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5759c58f1c22SToby Isaac       next->next = NULL;
5760c58f1c22SToby Isaac       *label     = next->label;
5761c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5762c58f1c22SToby Isaac       if (hasLabel) {
5763c58f1c22SToby Isaac         dm->depthLabel = NULL;
5764c58f1c22SToby Isaac       }
5765c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5766c58f1c22SToby Isaac       break;
5767c58f1c22SToby Isaac     }
5768c58f1c22SToby Isaac     last = next;
5769c58f1c22SToby Isaac     next = next->next;
5770c58f1c22SToby Isaac   }
5771c58f1c22SToby Isaac   PetscFunctionReturn(0);
5772c58f1c22SToby Isaac }
5773c58f1c22SToby Isaac 
5774c58f1c22SToby Isaac /*@C
5775c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5776c58f1c22SToby Isaac 
5777c58f1c22SToby Isaac   Not Collective
5778c58f1c22SToby Isaac 
5779c58f1c22SToby Isaac   Input Parameters:
5780c58f1c22SToby Isaac + dm   - The DM object
5781c58f1c22SToby Isaac - name - The label name
5782c58f1c22SToby Isaac 
5783c58f1c22SToby Isaac   Output Parameter:
5784c58f1c22SToby Isaac . output - The flag for output
5785c58f1c22SToby Isaac 
5786c58f1c22SToby Isaac   Level: developer
5787c58f1c22SToby Isaac 
5788c58f1c22SToby Isaac .keywords: mesh
5789c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5790c58f1c22SToby Isaac @*/
5791c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5792c58f1c22SToby Isaac {
5793c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5794c58f1c22SToby Isaac   PetscErrorCode ierr;
5795c58f1c22SToby Isaac 
5796c58f1c22SToby Isaac   PetscFunctionBegin;
5797c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5798c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5799c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5800c58f1c22SToby Isaac   while (next) {
5801c58f1c22SToby Isaac     PetscBool flg;
5802c58f1c22SToby Isaac 
5803c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5804c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5805c58f1c22SToby Isaac     next = next->next;
5806c58f1c22SToby Isaac   }
5807c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5808c58f1c22SToby Isaac }
5809c58f1c22SToby Isaac 
5810c58f1c22SToby Isaac /*@C
5811c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5812c58f1c22SToby Isaac 
5813c58f1c22SToby Isaac   Not Collective
5814c58f1c22SToby Isaac 
5815c58f1c22SToby Isaac   Input Parameters:
5816c58f1c22SToby Isaac + dm     - The DM object
5817c58f1c22SToby Isaac . name   - The label name
5818c58f1c22SToby Isaac - output - The flag for output
5819c58f1c22SToby Isaac 
5820c58f1c22SToby Isaac   Level: developer
5821c58f1c22SToby Isaac 
5822c58f1c22SToby Isaac .keywords: mesh
5823c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5824c58f1c22SToby Isaac @*/
5825c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5826c58f1c22SToby Isaac {
5827c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5828c58f1c22SToby Isaac   PetscErrorCode ierr;
5829c58f1c22SToby Isaac 
5830c58f1c22SToby Isaac   PetscFunctionBegin;
5831c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5832c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5833c58f1c22SToby Isaac   while (next) {
5834c58f1c22SToby Isaac     PetscBool flg;
5835c58f1c22SToby Isaac 
5836c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5837c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5838c58f1c22SToby Isaac     next = next->next;
5839c58f1c22SToby Isaac   }
5840c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5841c58f1c22SToby Isaac }
5842c58f1c22SToby Isaac 
5843c58f1c22SToby Isaac 
5844c58f1c22SToby Isaac /*@
5845c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5846c58f1c22SToby Isaac 
5847c58f1c22SToby Isaac   Collective on DM
5848c58f1c22SToby Isaac 
5849c58f1c22SToby Isaac   Input Parameter:
58502e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5851c58f1c22SToby Isaac 
5852c58f1c22SToby Isaac   Output Parameter:
58532e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5854c58f1c22SToby Isaac 
5855c58f1c22SToby Isaac   Level: intermediate
5856c58f1c22SToby Isaac 
5857c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5858c58f1c22SToby Isaac 
5859c58f1c22SToby Isaac .keywords: mesh
5860c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5861c58f1c22SToby Isaac @*/
5862c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5863c58f1c22SToby Isaac {
5864c58f1c22SToby Isaac   PetscInt       numLabels, l;
5865c58f1c22SToby Isaac   PetscErrorCode ierr;
5866c58f1c22SToby Isaac 
5867c58f1c22SToby Isaac   PetscFunctionBegin;
5868c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5869c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5870c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5871c58f1c22SToby Isaac     DMLabel     label, labelNew;
5872c58f1c22SToby Isaac     const char *name;
5873c58f1c22SToby Isaac     PetscBool   flg;
5874c58f1c22SToby Isaac 
5875c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5876c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5877c58f1c22SToby Isaac     if (flg) continue;
5878c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5879c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5880c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5881c58f1c22SToby Isaac   }
5882c58f1c22SToby Isaac   PetscFunctionReturn(0);
5883c58f1c22SToby Isaac }
5884a8fb8f29SToby Isaac 
5885a8fb8f29SToby Isaac /*@
5886a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5887a8fb8f29SToby Isaac 
5888a8fb8f29SToby Isaac   Input Parameter:
5889a8fb8f29SToby Isaac . dm - The DM object
5890a8fb8f29SToby Isaac 
5891a8fb8f29SToby Isaac   Output Parameter:
5892a8fb8f29SToby Isaac . cdm - The coarse DM
5893a8fb8f29SToby Isaac 
5894a8fb8f29SToby Isaac   Level: intermediate
5895a8fb8f29SToby Isaac 
5896a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5897a8fb8f29SToby Isaac @*/
5898a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5899a8fb8f29SToby Isaac {
5900a8fb8f29SToby Isaac   PetscFunctionBegin;
5901a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5902a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5903a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5904a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5905a8fb8f29SToby Isaac }
5906a8fb8f29SToby Isaac 
5907a8fb8f29SToby Isaac /*@
5908a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5909a8fb8f29SToby Isaac 
5910a8fb8f29SToby Isaac   Input Parameters:
5911a8fb8f29SToby Isaac + dm - The DM object
5912a8fb8f29SToby Isaac - cdm - The coarse DM
5913a8fb8f29SToby Isaac 
5914a8fb8f29SToby Isaac   Level: intermediate
5915a8fb8f29SToby Isaac 
5916a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5917a8fb8f29SToby Isaac @*/
5918a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5919a8fb8f29SToby Isaac {
5920a8fb8f29SToby Isaac   PetscErrorCode ierr;
5921a8fb8f29SToby Isaac 
5922a8fb8f29SToby Isaac   PetscFunctionBegin;
5923a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5924a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5925a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5926a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5927a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5928a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5929a8fb8f29SToby Isaac }
5930a8fb8f29SToby Isaac 
593188bdff64SToby Isaac /*@
593288bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
593388bdff64SToby Isaac 
593488bdff64SToby Isaac   Input Parameter:
593588bdff64SToby Isaac . dm - The DM object
593688bdff64SToby Isaac 
593788bdff64SToby Isaac   Output Parameter:
593888bdff64SToby Isaac . fdm - The fine DM
593988bdff64SToby Isaac 
594088bdff64SToby Isaac   Level: intermediate
594188bdff64SToby Isaac 
594288bdff64SToby Isaac .seealso: DMSetFineDM()
594388bdff64SToby Isaac @*/
594488bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
594588bdff64SToby Isaac {
594688bdff64SToby Isaac   PetscFunctionBegin;
594788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
594888bdff64SToby Isaac   PetscValidPointer(fdm, 2);
594988bdff64SToby Isaac   *fdm = dm->fineMesh;
595088bdff64SToby Isaac   PetscFunctionReturn(0);
595188bdff64SToby Isaac }
595288bdff64SToby Isaac 
595388bdff64SToby Isaac /*@
595488bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
595588bdff64SToby Isaac 
595688bdff64SToby Isaac   Input Parameters:
595788bdff64SToby Isaac + dm - The DM object
595888bdff64SToby Isaac - fdm - The fine DM
595988bdff64SToby Isaac 
596088bdff64SToby Isaac   Level: intermediate
596188bdff64SToby Isaac 
596288bdff64SToby Isaac .seealso: DMGetFineDM()
596388bdff64SToby Isaac @*/
596488bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
596588bdff64SToby Isaac {
596688bdff64SToby Isaac   PetscErrorCode ierr;
596788bdff64SToby Isaac 
596888bdff64SToby Isaac   PetscFunctionBegin;
596988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
597088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
597188bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
597288bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
597388bdff64SToby Isaac   dm->fineMesh = fdm;
597488bdff64SToby Isaac   PetscFunctionReturn(0);
597588bdff64SToby Isaac }
597688bdff64SToby Isaac 
5977a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5978a6ba4734SToby Isaac 
5979a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5980a6ba4734SToby Isaac {
5981a6ba4734SToby Isaac   PetscErrorCode ierr;
5982a6ba4734SToby Isaac 
5983a6ba4734SToby Isaac   PetscFunctionBegin;
5984e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
5985a6ba4734SToby Isaac   PetscFunctionReturn(0);
5986a6ba4734SToby Isaac }
5987a6ba4734SToby Isaac 
5988a6ba4734SToby Isaac /*@C
5989a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5990a6ba4734SToby Isaac 
5991a6ba4734SToby Isaac   Input Parameters:
59924c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
5993f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5994a6ba4734SToby Isaac . name        - The BC name
5995a6ba4734SToby Isaac . labelname   - The label defining constrained points
5996a6ba4734SToby Isaac . field       - The field to constrain
5997a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5998a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5999a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6000a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6001a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6002a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6003a6ba4734SToby Isaac 
6004a6ba4734SToby Isaac   Options Database Keys:
6005a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6006a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6007a6ba4734SToby Isaac 
6008a6ba4734SToby Isaac   Level: developer
6009a6ba4734SToby Isaac 
6010a6ba4734SToby Isaac .seealso: DMGetBoundary()
6011a6ba4734SToby Isaac @*/
6012a30ec4eaSSatish 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)
6013a6ba4734SToby Isaac {
6014a6ba4734SToby Isaac   PetscErrorCode ierr;
6015a6ba4734SToby Isaac 
6016a6ba4734SToby Isaac   PetscFunctionBegin;
6017a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6018f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6019a6ba4734SToby Isaac   PetscFunctionReturn(0);
6020a6ba4734SToby Isaac }
6021a6ba4734SToby Isaac 
6022a6ba4734SToby Isaac /*@
6023a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6024a6ba4734SToby Isaac 
6025a6ba4734SToby Isaac   Input Parameters:
6026a6ba4734SToby Isaac . dm - The mesh object
6027a6ba4734SToby Isaac 
6028a6ba4734SToby Isaac   Output Parameters:
6029a6ba4734SToby Isaac . numBd - The number of BC
6030a6ba4734SToby Isaac 
6031a6ba4734SToby Isaac   Level: intermediate
6032a6ba4734SToby Isaac 
6033a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6034a6ba4734SToby Isaac @*/
6035a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6036a6ba4734SToby Isaac {
603758ebd649SToby Isaac   PetscErrorCode ierr;
6038a6ba4734SToby Isaac 
6039a6ba4734SToby Isaac   PetscFunctionBegin;
6040a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
604158ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6042a6ba4734SToby Isaac   PetscFunctionReturn(0);
6043a6ba4734SToby Isaac }
6044a6ba4734SToby Isaac 
6045a6ba4734SToby Isaac /*@C
60461c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6047a6ba4734SToby Isaac 
6048a6ba4734SToby Isaac   Input Parameters:
6049a6ba4734SToby Isaac + dm          - The mesh object
6050a6ba4734SToby Isaac - bd          - The BC number
6051a6ba4734SToby Isaac 
6052a6ba4734SToby Isaac   Output Parameters:
6053f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6054a6ba4734SToby Isaac . name        - The BC name
6055a6ba4734SToby Isaac . labelname   - The label defining constrained points
6056a6ba4734SToby Isaac . field       - The field to constrain
6057a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6058a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6059a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6060a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6061a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6062a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6063a6ba4734SToby Isaac 
6064a6ba4734SToby Isaac   Options Database Keys:
6065a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6066a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6067a6ba4734SToby Isaac 
6068a6ba4734SToby Isaac   Level: developer
6069a6ba4734SToby Isaac 
6070a6ba4734SToby Isaac .seealso: DMAddBoundary()
6071a6ba4734SToby Isaac @*/
6072a30ec4eaSSatish 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)
6073a6ba4734SToby Isaac {
607458ebd649SToby Isaac   PetscErrorCode ierr;
6075a6ba4734SToby Isaac 
6076a6ba4734SToby Isaac   PetscFunctionBegin;
6077a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6078f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6079a6ba4734SToby Isaac   PetscFunctionReturn(0);
6080a6ba4734SToby Isaac }
6081a6ba4734SToby Isaac 
6082e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6083e6f8dbb6SToby Isaac {
6084dff059c6SToby Isaac   DMBoundary *lastnext;
6085e6f8dbb6SToby Isaac   DSBoundary dsbound;
6086e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6087e6f8dbb6SToby Isaac 
6088e6f8dbb6SToby Isaac   PetscFunctionBegin;
6089e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
609047a1f5adSToby Isaac   if (dm->boundary) {
609147a1f5adSToby Isaac     DMBoundary next = dm->boundary;
609247a1f5adSToby Isaac 
609347a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
609447a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
609547a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
609647a1f5adSToby Isaac     while (next) {
609747a1f5adSToby Isaac       DMBoundary b = next;
609847a1f5adSToby Isaac 
609947a1f5adSToby Isaac       next = b->next;
610047a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6101a6ba4734SToby Isaac     }
610247a1f5adSToby Isaac     dm->boundary = NULL;
6103a6ba4734SToby Isaac   }
610447a1f5adSToby Isaac 
6105dff059c6SToby Isaac   lastnext = &(dm->boundary);
6106e6f8dbb6SToby Isaac   while (dsbound) {
6107e6f8dbb6SToby Isaac     DMBoundary dmbound;
6108e6f8dbb6SToby Isaac 
6109e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6110e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6111e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6112e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
611347a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6114dff059c6SToby Isaac     *lastnext = dmbound;
6115dff059c6SToby Isaac     lastnext = &(dmbound->next);
6116dff059c6SToby Isaac     dsbound = dsbound->next;
6117a6ba4734SToby Isaac   }
6118a6ba4734SToby Isaac   PetscFunctionReturn(0);
6119a6ba4734SToby Isaac }
6120a6ba4734SToby Isaac 
6121a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6122a6ba4734SToby Isaac {
6123b95f2879SToby Isaac   DMBoundary     b;
6124a6ba4734SToby Isaac   PetscErrorCode ierr;
6125a6ba4734SToby Isaac 
6126a6ba4734SToby Isaac   PetscFunctionBegin;
6127a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6128a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6129a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6130e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6131b95f2879SToby Isaac   b = dm->boundary;
6132a6ba4734SToby Isaac   while (b && !(*isBd)) {
6133e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6134e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
61353424c85cSToby Isaac 
61363424c85cSToby Isaac     if (label) {
6137a6ba4734SToby Isaac       PetscInt i;
6138a6ba4734SToby Isaac 
6139e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6140e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6141a6ba4734SToby Isaac       }
6142a6ba4734SToby Isaac     }
6143a6ba4734SToby Isaac     b = b->next;
6144a6ba4734SToby Isaac   }
6145a6ba4734SToby Isaac   PetscFunctionReturn(0);
6146a6ba4734SToby Isaac }
61474d6f44ffSToby Isaac 
61484d6f44ffSToby Isaac /*@C
61494d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
61504d6f44ffSToby Isaac 
61514d6f44ffSToby Isaac   Input Parameters:
61524d6f44ffSToby Isaac + dm      - The DM
61530709b2feSToby Isaac . time    - The time
61544d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
61554d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
61564d6f44ffSToby Isaac - mode    - The insertion mode for values
61574d6f44ffSToby Isaac 
61584d6f44ffSToby Isaac   Output Parameter:
61594d6f44ffSToby Isaac . X - vector
61604d6f44ffSToby Isaac 
61614d6f44ffSToby Isaac    Calling sequence of func:
61620709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
61634d6f44ffSToby Isaac 
61644d6f44ffSToby Isaac +  dim - The spatial dimension
61654d6f44ffSToby Isaac .  x   - The coordinates
61664d6f44ffSToby Isaac .  Nf  - The number of fields
61674d6f44ffSToby Isaac .  u   - The output field values
61684d6f44ffSToby Isaac -  ctx - optional user-defined function context
61694d6f44ffSToby Isaac 
61704d6f44ffSToby Isaac   Level: developer
61714d6f44ffSToby Isaac 
61722716604bSToby Isaac .seealso: DMComputeL2Diff()
61734d6f44ffSToby Isaac @*/
61740709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
61754d6f44ffSToby Isaac {
61764d6f44ffSToby Isaac   Vec            localX;
61774d6f44ffSToby Isaac   PetscErrorCode ierr;
61784d6f44ffSToby Isaac 
61794d6f44ffSToby Isaac   PetscFunctionBegin;
61804d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61814d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
61820709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61834d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
61844d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
61854d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
61864d6f44ffSToby Isaac   PetscFunctionReturn(0);
61874d6f44ffSToby Isaac }
61884d6f44ffSToby Isaac 
61890709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
61904d6f44ffSToby Isaac {
61914d6f44ffSToby Isaac   PetscErrorCode ierr;
61924d6f44ffSToby Isaac 
61934d6f44ffSToby Isaac   PetscFunctionBegin;
61944d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61954d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
61960918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
61970709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61984d6f44ffSToby Isaac   PetscFunctionReturn(0);
61994d6f44ffSToby Isaac }
62004d6f44ffSToby Isaac 
62012c53366bSMatthew 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)
62022c53366bSMatthew G. Knepley {
62032c53366bSMatthew G. Knepley   Vec            localX;
62042c53366bSMatthew G. Knepley   PetscErrorCode ierr;
62052c53366bSMatthew G. Knepley 
62062c53366bSMatthew G. Knepley   PetscFunctionBegin;
62072c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62082c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62092c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62102c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62112c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62122c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62132c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
62142c53366bSMatthew G. Knepley }
62152c53366bSMatthew G. Knepley 
62161c531cf8SMatthew 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)
62174d6f44ffSToby Isaac {
62184d6f44ffSToby Isaac   PetscErrorCode ierr;
62194d6f44ffSToby Isaac 
62204d6f44ffSToby Isaac   PetscFunctionBegin;
62214d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62224d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62230918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
62241c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62254d6f44ffSToby Isaac   PetscFunctionReturn(0);
62264d6f44ffSToby Isaac }
62272716604bSToby Isaac 
62288c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
62298c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
62308c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62318c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6232191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
62338c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
62348c6c5593SMatthew G. Knepley {
62358c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62368c6c5593SMatthew G. Knepley 
62378c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62388c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62398c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
62408c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
62410918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
62428c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
62438c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62448c6c5593SMatthew G. Knepley }
62458c6c5593SMatthew G. Knepley 
62461c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
62478c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
62488c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62498c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6250191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
62518c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
62528c6c5593SMatthew G. Knepley {
62538c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62548c6c5593SMatthew G. Knepley 
62558c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62568c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
62588c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
62590918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
62601c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
62618c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62628c6c5593SMatthew G. Knepley }
62638c6c5593SMatthew G. Knepley 
62642716604bSToby Isaac /*@C
62652716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
62662716604bSToby Isaac 
62672716604bSToby Isaac   Input Parameters:
62682716604bSToby Isaac + dm    - The DM
62690709b2feSToby Isaac . time  - The time
62702716604bSToby Isaac . funcs - The functions to evaluate for each field component
62712716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6272574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
62732716604bSToby Isaac 
62742716604bSToby Isaac   Output Parameter:
62752716604bSToby Isaac . diff - The diff ||u - u_h||_2
62762716604bSToby Isaac 
62772716604bSToby Isaac   Level: developer
62782716604bSToby Isaac 
62791189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62802716604bSToby Isaac @*/
62810709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
62822716604bSToby Isaac {
62832716604bSToby Isaac   PetscErrorCode ierr;
62842716604bSToby Isaac 
62852716604bSToby Isaac   PetscFunctionBegin;
62862716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6287b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62880918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
62890709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
62902716604bSToby Isaac   PetscFunctionReturn(0);
62912716604bSToby Isaac }
6292b698f381SToby Isaac 
6293b698f381SToby Isaac /*@C
6294b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6295b698f381SToby Isaac 
6296b698f381SToby Isaac   Input Parameters:
6297b698f381SToby Isaac + dm    - The DM
6298b698f381SToby Isaac , time  - The time
6299b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6300b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6301574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6302b698f381SToby Isaac - n     - The vector to project along
6303b698f381SToby Isaac 
6304b698f381SToby Isaac   Output Parameter:
6305b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6306b698f381SToby Isaac 
6307b698f381SToby Isaac   Level: developer
6308b698f381SToby Isaac 
6309b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6310b698f381SToby Isaac @*/
6311b698f381SToby 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)
6312b698f381SToby Isaac {
6313b698f381SToby Isaac   PetscErrorCode ierr;
6314b698f381SToby Isaac 
6315b698f381SToby Isaac   PetscFunctionBegin;
6316b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6317b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6318b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6319b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6320b698f381SToby Isaac   PetscFunctionReturn(0);
6321b698f381SToby Isaac }
6322b698f381SToby Isaac 
63232a16baeaSToby Isaac /*@C
63242a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
63252a16baeaSToby Isaac 
63262a16baeaSToby Isaac   Input Parameters:
63272a16baeaSToby Isaac + dm    - The DM
63282a16baeaSToby Isaac . time  - The time
63292a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
63302a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6331574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
63322a16baeaSToby Isaac 
63332a16baeaSToby Isaac   Output Parameter:
63342a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
63352a16baeaSToby Isaac 
63362a16baeaSToby Isaac   Level: developer
63372a16baeaSToby Isaac 
63381189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
63392a16baeaSToby Isaac @*/
63401189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
63412a16baeaSToby Isaac {
63422a16baeaSToby Isaac   PetscErrorCode ierr;
63432a16baeaSToby Isaac 
63442a16baeaSToby Isaac   PetscFunctionBegin;
63452a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63462a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
63470918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
63482a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63492a16baeaSToby Isaac   PetscFunctionReturn(0);
63502a16baeaSToby Isaac }
63512a16baeaSToby Isaac 
6352df0b854cSToby Isaac /*@C
6353df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6354cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6355df0b854cSToby Isaac 
6356df0b854cSToby Isaac   Collective on dm
6357df0b854cSToby Isaac 
6358df0b854cSToby Isaac   Input parameters:
6359df0b854cSToby Isaac + dm - the pre-adaptation DM object
6360a1b0c543SToby Isaac - label - label with the flags
6361df0b854cSToby Isaac 
6362df0b854cSToby Isaac   Output parameters:
63630d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6364df0b854cSToby Isaac 
6365df0b854cSToby Isaac   Level: intermediate
63660d1cd5e0SMatthew G. Knepley 
63670d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6368df0b854cSToby Isaac @*/
63690d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6370df0b854cSToby Isaac {
6371df0b854cSToby Isaac   PetscErrorCode ierr;
6372df0b854cSToby Isaac 
6373df0b854cSToby Isaac   PetscFunctionBegin;
6374df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6375a1b0c543SToby Isaac   PetscValidPointer(label,2);
63760d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
63770d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
63786f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
63790d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
63800d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
63810d1cd5e0SMatthew G. Knepley }
63820d1cd5e0SMatthew G. Knepley 
63830d1cd5e0SMatthew G. Knepley /*@C
63840d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
63850d1cd5e0SMatthew G. Knepley 
63860d1cd5e0SMatthew G. Knepley   Input Parameters:
63870d1cd5e0SMatthew G. Knepley + dm - The DM object
63880d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
63896f25b0d8SLisandro 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_".
63900d1cd5e0SMatthew G. Knepley 
63910d1cd5e0SMatthew G. Knepley   Output Parameter:
63920d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
63930d1cd5e0SMatthew G. Knepley 
63940d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
63950d1cd5e0SMatthew G. Knepley 
63960d1cd5e0SMatthew G. Knepley   Level: advanced
63970d1cd5e0SMatthew G. Knepley 
63980d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
63990d1cd5e0SMatthew G. Knepley @*/
64000d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
64010d1cd5e0SMatthew G. Knepley {
64020d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
64030d1cd5e0SMatthew G. Knepley 
64040d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
64050d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64060d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
64070d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
64080d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
64096f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
64106f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
64110d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6412df0b854cSToby Isaac   PetscFunctionReturn(0);
6413df0b854cSToby Isaac }
6414c4088d22SMatthew G. Knepley 
6415502a2867SDave May /*@C
6416502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6417502a2867SDave May 
6418502a2867SDave May  Not Collective
6419502a2867SDave May 
6420502a2867SDave May  Input Parameter:
6421502a2867SDave May  . dm    - The DM
6422502a2867SDave May 
6423502a2867SDave May  Output Parameter:
6424502a2867SDave May  . nranks - the number of neighbours
6425502a2867SDave May  . ranks - the neighbors ranks
6426502a2867SDave May 
6427502a2867SDave May  Notes:
6428502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6429502a2867SDave May 
6430502a2867SDave May  Level: beginner
6431502a2867SDave May 
6432dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6433502a2867SDave May @*/
6434502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6435502a2867SDave May {
6436502a2867SDave May   PetscErrorCode ierr;
6437502a2867SDave May 
6438502a2867SDave May   PetscFunctionBegin;
6439502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64400918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6441502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6442502a2867SDave May   PetscFunctionReturn(0);
6443502a2867SDave May }
6444502a2867SDave May 
6445531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6446531c7667SBarry Smith 
6447531c7667SBarry Smith /*
6448531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6449531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6450531c7667SBarry Smith */
6451531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6452531c7667SBarry Smith {
6453531c7667SBarry Smith   PetscErrorCode ierr;
6454531c7667SBarry Smith 
6455531c7667SBarry Smith   PetscFunctionBegin;
6456531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6457531c7667SBarry Smith     Vec x1local;
6458531c7667SBarry Smith     DM  dm;
6459531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6460531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6461531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6462531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6463531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6464531c7667SBarry Smith     x1   = x1local;
6465531c7667SBarry Smith   }
6466531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6467531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6468531c7667SBarry Smith     DM  dm;
6469531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6470531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6471531c7667SBarry Smith   }
6472531c7667SBarry Smith   PetscFunctionReturn(0);
6473531c7667SBarry Smith }
6474531c7667SBarry Smith 
6475531c7667SBarry Smith /*@
6476531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6477531c7667SBarry Smith 
6478531c7667SBarry Smith     Input Parameter:
6479531c7667SBarry Smith .    coloring - the MatFDColoring object
6480531c7667SBarry Smith 
6481531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6482531c7667SBarry Smith 
64831b266c99SBarry Smith     Level: advanced
64841b266c99SBarry Smith 
6485531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6486531c7667SBarry Smith @*/
6487531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6488531c7667SBarry Smith {
6489531c7667SBarry Smith   PetscFunctionBegin;
6490531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6491531c7667SBarry Smith   PetscFunctionReturn(0);
6492531c7667SBarry Smith }
6493