xref: /petsc/src/dm/interface/dm.c (revision 0be3e97a89e5c9bd605be1541df92e78df8b4e36)
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>
50c312b8eSJed Brown #include <petscsf.h>
62764a2aaSMatthew G. Knepley #include <petscds.h>
747c6ae99SBarry Smith 
8732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
91ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1067a56275SMatthew G Knepley 
11bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
12bff4a2f0SMatthew G. Knepley 
13a4121054SBarry Smith /*@
14de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
15a4121054SBarry Smith 
16a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
17a4121054SBarry Smith    error when you try to use the vector.
18a4121054SBarry Smith 
19a4121054SBarry Smith   Collective on MPI_Comm
20a4121054SBarry Smith 
21a4121054SBarry Smith   Input Parameter:
22a4121054SBarry Smith . comm - The communicator for the DM object
23a4121054SBarry Smith 
24a4121054SBarry Smith   Output Parameter:
25a4121054SBarry Smith . dm - The DM object
26a4121054SBarry Smith 
27a4121054SBarry Smith   Level: beginner
28a4121054SBarry Smith 
298472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
30a4121054SBarry Smith @*/
317087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
32a4121054SBarry Smith {
33a4121054SBarry Smith   DM             v;
34a4121054SBarry Smith   PetscErrorCode ierr;
35a4121054SBarry Smith 
36a4121054SBarry Smith   PetscFunctionBegin;
371411c6eeSJed Brown   PetscValidPointer(dm,2);
380298fd71SBarry Smith   *dm = NULL;
398b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
40607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
42607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
43a4121054SBarry Smith 
4473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
45e7c4fc90SDmitry Karpeev 
460298fd71SBarry Smith   v->ltogmap                  = NULL;
471411c6eeSJed Brown   v->bs                       = 1;
48171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
4988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
51c58f1c22SToby Isaac   v->labels                   = NULL;
52c58f1c22SToby Isaac   v->depthLabel               = NULL;
530298fd71SBarry Smith   v->defaultSection           = NULL;
540298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
55fba222abSToby Isaac   v->defaultConstraintSection = NULL;
56fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
57c6b900c6SMatthew G. Knepley   v->L                        = NULL;
58c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
595dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
609a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
61435a35e8SMatthew G Knepley   {
62435a35e8SMatthew G Knepley     PetscInt i;
63435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
640298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
65435a35e8SMatthew G Knepley     }
66435a35e8SMatthew G Knepley   }
672764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6814f150ffSMatthew G. Knepley   v->dmBC = NULL;
69a8fb8f29SToby Isaac   v->coarseMesh = NULL;
70f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
71cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
72c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
73b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
74bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
75c58f1c22SToby Isaac   v->labels->refct = 1;
761411c6eeSJed Brown   *dm = v;
77a4121054SBarry Smith   PetscFunctionReturn(0);
78a4121054SBarry Smith }
79a4121054SBarry Smith 
8038221697SMatthew G. Knepley /*@
8138221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
8238221697SMatthew G. Knepley 
8338221697SMatthew G. Knepley   Collective on MPI_Comm
8438221697SMatthew G. Knepley 
8538221697SMatthew G. Knepley   Input Parameter:
8638221697SMatthew G. Knepley . dm - The original DM object
8738221697SMatthew G. Knepley 
8838221697SMatthew G. Knepley   Output Parameter:
8938221697SMatthew G. Knepley . newdm  - The new DM object
9038221697SMatthew G. Knepley 
9138221697SMatthew G. Knepley   Level: beginner
9238221697SMatthew G. Knepley 
9338221697SMatthew G. Knepley .keywords: DM, topology, create
9438221697SMatthew G. Knepley @*/
9538221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9638221697SMatthew G. Knepley {
9738221697SMatthew G. Knepley   PetscSF        sf;
9838221697SMatthew G. Knepley   Vec            coords;
9938221697SMatthew G. Knepley   void          *ctx;
100a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
10138221697SMatthew G. Knepley   PetscErrorCode ierr;
10238221697SMatthew G. Knepley 
10338221697SMatthew G. Knepley   PetscFunctionBegin;
10438221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10538221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10638221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
107c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
108c58f1c22SToby Isaac   dm->labels->refct++;
109c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
110c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1111de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1121de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
11338221697SMatthew G. Knepley   if (dm->ops->clone) {
11438221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
11538221697SMatthew G. Knepley   }
1163f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
11738221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
11838221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
11938221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
12038221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
121be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
122be4c1c3eSMatthew G. Knepley     DM           ncdm;
123be4c1c3eSMatthew G. Knepley     PetscSection cs;
1245a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
125be4c1c3eSMatthew G. Knepley 
126be4c1c3eSMatthew G. Knepley     ierr = DMGetDefaultSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
127be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1285a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1295a0206caSToby Isaac     if (pEndMax >= 0) {
130be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
13113bffc4cSMatthew G. Knepley       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
132a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
133be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
134be4c1c3eSMatthew G. Knepley     }
135be4c1c3eSMatthew G. Knepley   }
136a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
137a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
13838221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   if (coords) {
14038221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   } else {
14238221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
14338221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
14438221697SMatthew G. Knepley   }
14590b157c4SStefano Zampini   {
14690b157c4SStefano Zampini     PetscBool             isper;
147c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1485dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
14990b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
15090b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
151c6b900c6SMatthew G. Knepley   }
15238221697SMatthew G. Knepley   PetscFunctionReturn(0);
15338221697SMatthew G. Knepley }
15438221697SMatthew G. Knepley 
1559a42bb27SBarry Smith /*@C
156564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1579a42bb27SBarry Smith 
1588472ad0fSDave May    Logically Collective on DM
1599a42bb27SBarry Smith 
1609a42bb27SBarry Smith    Input Parameter:
1619a42bb27SBarry Smith +  da - initial distributed array
1628154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1639a42bb27SBarry Smith 
1649a42bb27SBarry Smith    Options Database:
165dd85299cSBarry Smith .   -dm_vec_type ctype
1669a42bb27SBarry Smith 
1679a42bb27SBarry Smith    Level: intermediate
1689a42bb27SBarry Smith 
1698472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1709a42bb27SBarry Smith @*/
17119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1729a42bb27SBarry Smith {
1739a42bb27SBarry Smith   PetscErrorCode ierr;
1749a42bb27SBarry Smith 
1759a42bb27SBarry Smith   PetscFunctionBegin;
1769a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1779a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
17819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1799a42bb27SBarry Smith   PetscFunctionReturn(0);
1809a42bb27SBarry Smith }
1819a42bb27SBarry Smith 
182c0dedaeaSBarry Smith /*@C
183c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
184c0dedaeaSBarry Smith 
1858472ad0fSDave May    Logically Collective on DM
186c0dedaeaSBarry Smith 
187c0dedaeaSBarry Smith    Input Parameter:
188c0dedaeaSBarry Smith .  da - initial distributed array
189c0dedaeaSBarry Smith 
190c0dedaeaSBarry Smith    Output Parameter:
191c0dedaeaSBarry Smith .  ctype - the vector type
192c0dedaeaSBarry Smith 
193c0dedaeaSBarry Smith    Level: intermediate
194c0dedaeaSBarry Smith 
1958472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
196c0dedaeaSBarry Smith @*/
197c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
198c0dedaeaSBarry Smith {
199c0dedaeaSBarry Smith   PetscFunctionBegin;
200c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
201c0dedaeaSBarry Smith   *ctype = da->vectype;
202c0dedaeaSBarry Smith   PetscFunctionReturn(0);
203c0dedaeaSBarry Smith }
204c0dedaeaSBarry Smith 
2055f1ad066SMatthew G Knepley /*@
20634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2075f1ad066SMatthew G Knepley 
2085f1ad066SMatthew G Knepley   Not collective
2095f1ad066SMatthew G Knepley 
2105f1ad066SMatthew G Knepley   Input Parameter:
2115f1ad066SMatthew G Knepley . v - The Vec
2125f1ad066SMatthew G Knepley 
2135f1ad066SMatthew G Knepley   Output Parameter:
2145f1ad066SMatthew G Knepley . dm - The DM
2155f1ad066SMatthew G Knepley 
2165f1ad066SMatthew G Knepley   Level: intermediate
2175f1ad066SMatthew G Knepley 
2185f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2195f1ad066SMatthew G Knepley @*/
2205f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2215f1ad066SMatthew G Knepley {
2225f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2235f1ad066SMatthew G Knepley 
2245f1ad066SMatthew G Knepley   PetscFunctionBegin;
2255f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2265f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2275f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2285f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2295f1ad066SMatthew G Knepley }
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley /*@
232d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2335f1ad066SMatthew G Knepley 
2345f1ad066SMatthew G Knepley   Not collective
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   Input Parameters:
2375f1ad066SMatthew G Knepley + v - The Vec
2385f1ad066SMatthew G Knepley - dm - The DM
2395f1ad066SMatthew G Knepley 
240d9805387SMatthew 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.
241d9805387SMatthew G. Knepley 
2425f1ad066SMatthew G Knepley   Level: intermediate
2435f1ad066SMatthew G Knepley 
2445f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2455f1ad066SMatthew G Knepley @*/
2465f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2475f1ad066SMatthew G Knepley {
2485f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2495f1ad066SMatthew G Knepley 
2505f1ad066SMatthew G Knepley   PetscFunctionBegin;
2515f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
252d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2535f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2545f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2555f1ad066SMatthew G Knepley }
2565f1ad066SMatthew G Knepley 
257521d9a4cSLisandro Dalcin /*@C
258521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
259521d9a4cSLisandro Dalcin 
260521d9a4cSLisandro Dalcin    Logically Collective on DM
261521d9a4cSLisandro Dalcin 
262521d9a4cSLisandro Dalcin    Input Parameter:
263521d9a4cSLisandro Dalcin +  dm - the DM context
264a2b5a043SBarry Smith -  ctype - the matrix type
265521d9a4cSLisandro Dalcin 
266521d9a4cSLisandro Dalcin    Options Database:
267521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
268521d9a4cSLisandro Dalcin 
269521d9a4cSLisandro Dalcin    Level: intermediate
270521d9a4cSLisandro Dalcin 
271c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
272521d9a4cSLisandro Dalcin @*/
27319fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
274521d9a4cSLisandro Dalcin {
275521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
27688f0584fSBarry Smith 
277521d9a4cSLisandro Dalcin   PetscFunctionBegin;
278521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
279521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
28019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
281521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
282521d9a4cSLisandro Dalcin }
283521d9a4cSLisandro Dalcin 
284c0dedaeaSBarry Smith /*@C
285c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
286c0dedaeaSBarry Smith 
287c0dedaeaSBarry Smith    Logically Collective on DM
288c0dedaeaSBarry Smith 
289c0dedaeaSBarry Smith    Input Parameter:
290c0dedaeaSBarry Smith .  dm - the DM context
291c0dedaeaSBarry Smith 
292c0dedaeaSBarry Smith    Output Parameter:
293c0dedaeaSBarry Smith .  ctype - the matrix type
294c0dedaeaSBarry Smith 
295c0dedaeaSBarry Smith    Options Database:
296c0dedaeaSBarry Smith .   -dm_mat_type ctype
297c0dedaeaSBarry Smith 
298c0dedaeaSBarry Smith    Level: intermediate
299c0dedaeaSBarry Smith 
300c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
301c0dedaeaSBarry Smith @*/
302c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
303c0dedaeaSBarry Smith {
304c0dedaeaSBarry Smith   PetscFunctionBegin;
305c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
306c0dedaeaSBarry Smith   *ctype = dm->mattype;
307c0dedaeaSBarry Smith   PetscFunctionReturn(0);
308c0dedaeaSBarry Smith }
309c0dedaeaSBarry Smith 
310c688c046SMatthew G Knepley /*@
31134f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
312c688c046SMatthew G Knepley 
313c688c046SMatthew G Knepley   Not collective
314c688c046SMatthew G Knepley 
315c688c046SMatthew G Knepley   Input Parameter:
316c688c046SMatthew G Knepley . A - The Mat
317c688c046SMatthew G Knepley 
318c688c046SMatthew G Knepley   Output Parameter:
319c688c046SMatthew G Knepley . dm - The DM
320c688c046SMatthew G Knepley 
321c688c046SMatthew G Knepley   Level: intermediate
322c688c046SMatthew G Knepley 
323c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
324c688c046SMatthew G Knepley @*/
325c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
326c688c046SMatthew G Knepley {
327c688c046SMatthew G Knepley   PetscErrorCode ierr;
328c688c046SMatthew G Knepley 
329c688c046SMatthew G Knepley   PetscFunctionBegin;
330c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
331c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
332c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
333c688c046SMatthew G Knepley   PetscFunctionReturn(0);
334c688c046SMatthew G Knepley }
335c688c046SMatthew G Knepley 
336c688c046SMatthew G Knepley /*@
337c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
338c688c046SMatthew G Knepley 
339c688c046SMatthew G Knepley   Not collective
340c688c046SMatthew G Knepley 
341c688c046SMatthew G Knepley   Input Parameters:
342c688c046SMatthew G Knepley + A - The Mat
343c688c046SMatthew G Knepley - dm - The DM
344c688c046SMatthew G Knepley 
345c688c046SMatthew G Knepley   Level: intermediate
346c688c046SMatthew G Knepley 
347c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
348c688c046SMatthew G Knepley @*/
349c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
350c688c046SMatthew G Knepley {
351c688c046SMatthew G Knepley   PetscErrorCode ierr;
352c688c046SMatthew G Knepley 
353c688c046SMatthew G Knepley   PetscFunctionBegin;
354c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3558865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
356c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
357c688c046SMatthew G Knepley   PetscFunctionReturn(0);
358c688c046SMatthew G Knepley }
359c688c046SMatthew G Knepley 
3609a42bb27SBarry Smith /*@C
3619a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3626757b960SDave May    DM options in the database.
3639a42bb27SBarry Smith 
3648353ddbbSDave May    Logically Collective on DM
3659a42bb27SBarry Smith 
3669a42bb27SBarry Smith    Input Parameter:
3678353ddbbSDave May +  da - the DM context
3689a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3699a42bb27SBarry Smith 
3709a42bb27SBarry Smith    Notes:
3719a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3729a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3739a42bb27SBarry Smith 
3749a42bb27SBarry Smith    Level: advanced
3759a42bb27SBarry Smith 
3768353ddbbSDave May .keywords: DM, set, options, prefix, database
3779a42bb27SBarry Smith 
3789a42bb27SBarry Smith .seealso: DMSetFromOptions()
3799a42bb27SBarry Smith @*/
3807087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3819a42bb27SBarry Smith {
3829a42bb27SBarry Smith   PetscErrorCode ierr;
3839a42bb27SBarry Smith 
3849a42bb27SBarry Smith   PetscFunctionBegin;
3859a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3869a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
387691be533SLawrence Mitchell   if (dm->sf) {
388691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
389691be533SLawrence Mitchell   }
390691be533SLawrence Mitchell   if (dm->defaultSF) {
391691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
392691be533SLawrence Mitchell   }
3939a42bb27SBarry Smith   PetscFunctionReturn(0);
3949a42bb27SBarry Smith }
3959a42bb27SBarry Smith 
39631697293SDave May /*@C
39731697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
39831697293SDave May    DM options in the database.
39931697293SDave May 
40031697293SDave May    Logically Collective on DM
40131697293SDave May 
40231697293SDave May    Input Parameters:
40331697293SDave May +  dm - the DM context
40431697293SDave May -  prefix - the prefix string to prepend to all DM option requests
40531697293SDave May 
40631697293SDave May    Notes:
40731697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
40831697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
40931697293SDave May 
41031697293SDave May    Level: advanced
41131697293SDave May 
41231697293SDave May .keywords: DM, append, options, prefix, database
41331697293SDave May 
41431697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
41531697293SDave May @*/
41631697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
41731697293SDave May {
41831697293SDave May   PetscErrorCode ierr;
41931697293SDave May 
42031697293SDave May   PetscFunctionBegin;
42131697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42231697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
42331697293SDave May   PetscFunctionReturn(0);
42431697293SDave May }
42531697293SDave May 
42631697293SDave May /*@C
42731697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
42831697293SDave May    DM options in the database.
42931697293SDave May 
43031697293SDave May    Not Collective
43131697293SDave May 
43231697293SDave May    Input Parameters:
43331697293SDave May .  dm - the DM context
43431697293SDave May 
43531697293SDave May    Output Parameters:
43631697293SDave May .  prefix - pointer to the prefix string used is returned
43731697293SDave May 
43831697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
43931697293SDave May    sufficient length to hold the prefix.
44031697293SDave May 
44131697293SDave May    Level: advanced
44231697293SDave May 
44331697293SDave May .keywords: DM, set, options, prefix, database
44431697293SDave May 
44531697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
44631697293SDave May @*/
44731697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
44831697293SDave May {
44931697293SDave May   PetscErrorCode ierr;
45031697293SDave May 
45131697293SDave May   PetscFunctionBegin;
45231697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45331697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
45431697293SDave May   PetscFunctionReturn(0);
45531697293SDave May }
45631697293SDave May 
45788bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
45888bdff64SToby Isaac {
45988bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
46088bdff64SToby Isaac   DMNamedVecLink nlink;
46188bdff64SToby Isaac   PetscErrorCode ierr;
46288bdff64SToby Isaac 
46388bdff64SToby Isaac   PetscFunctionBegin;
464aab5bcd8SJed Brown   *ncrefct = 0;
46588bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
46688bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
46788bdff64SToby Isaac     if (dm->localin[i])  refct--;
46888bdff64SToby Isaac     if (dm->globalin[i]) refct--;
46988bdff64SToby Isaac   }
47088bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
47188bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
47288bdff64SToby Isaac   if (dm->x) {
47388bdff64SToby Isaac     DM obj;
47488bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
47588bdff64SToby Isaac     if (obj == dm) refct--;
47688bdff64SToby Isaac   }
47788bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
47888bdff64SToby Isaac     refct--;
47988bdff64SToby Isaac     if (recurseCoarse) {
48088bdff64SToby Isaac       PetscInt coarseCount;
48188bdff64SToby Isaac 
48288bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
48388bdff64SToby Isaac       refct += coarseCount;
48488bdff64SToby Isaac     }
48588bdff64SToby Isaac   }
48688bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
48788bdff64SToby Isaac     refct--;
48888bdff64SToby Isaac     if (recurseFine) {
48988bdff64SToby Isaac       PetscInt fineCount;
49088bdff64SToby Isaac 
49188bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
49288bdff64SToby Isaac       refct += fineCount;
49388bdff64SToby Isaac     }
49488bdff64SToby Isaac   }
49588bdff64SToby Isaac   *ncrefct = refct;
49688bdff64SToby Isaac   PetscFunctionReturn(0);
49788bdff64SToby Isaac }
49888bdff64SToby Isaac 
499354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
500354557abSToby Isaac {
501354557abSToby Isaac   PetscErrorCode ierr;
502354557abSToby Isaac 
503354557abSToby Isaac   PetscFunctionBegin;
504354557abSToby Isaac   if (!--(dm->labels->refct)) {
505354557abSToby Isaac     DMLabelLink next = dm->labels->next;
506354557abSToby Isaac 
507354557abSToby Isaac     /* destroy the labels */
508354557abSToby Isaac     while (next) {
509354557abSToby Isaac       DMLabelLink tmp = next->next;
510354557abSToby Isaac 
511354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
512354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
513354557abSToby Isaac       next = tmp;
514354557abSToby Isaac     }
515354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
516354557abSToby Isaac   }
517354557abSToby Isaac   PetscFunctionReturn(0);
518354557abSToby Isaac }
519354557abSToby Isaac 
52047c6ae99SBarry Smith /*@
5218472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
52247c6ae99SBarry Smith 
52347c6ae99SBarry Smith     Collective on DM
52447c6ae99SBarry Smith 
52547c6ae99SBarry Smith     Input Parameter:
52647c6ae99SBarry Smith .   dm - the DM object to destroy
52747c6ae99SBarry Smith 
52847c6ae99SBarry Smith     Level: developer
52947c6ae99SBarry Smith 
530e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
53147c6ae99SBarry Smith 
53247c6ae99SBarry Smith @*/
533fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
53447c6ae99SBarry Smith {
53588bdff64SToby Isaac   PetscInt       i, cnt;
536dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
53747c6ae99SBarry Smith   PetscErrorCode ierr;
53847c6ae99SBarry Smith 
53947c6ae99SBarry Smith   PetscFunctionBegin;
5406bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5416bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
54287e657c6SBarry Smith 
54388bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
54488bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
54588bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
54688bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
547732e2eb9SMatthew G Knepley   /*
548732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
549732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
550732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
551732e2eb9SMatthew G Knepley   */
5526bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5536bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
554732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5556bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5566bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
557732e2eb9SMatthew G Knepley   }
558f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5590298fd71SBarry Smith   (*dm)->namedglobal = NULL;
560f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5612348bcf4SPeter Brune     nnext = nlink->next;
5622348bcf4SPeter 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);
5632348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5642348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5652348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5662348bcf4SPeter Brune   }
567f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5680298fd71SBarry Smith   (*dm)->namedlocal = NULL;
569f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
570f490541aSPeter Brune     nnext = nlink->next;
571f490541aSPeter 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);
572f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
573f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
574f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
575f490541aSPeter Brune   }
5762348bcf4SPeter Brune 
577b17ce1afSJed Brown   /* Destroy the list of hooks */
578c833c3b5SJed Brown   {
579c833c3b5SJed Brown     DMCoarsenHookLink link,next;
580b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
581b17ce1afSJed Brown       next = link->next;
582b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
583b17ce1afSJed Brown     }
5840298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
585c833c3b5SJed Brown   }
586c833c3b5SJed Brown   {
587c833c3b5SJed Brown     DMRefineHookLink link,next;
588c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
589c833c3b5SJed Brown       next = link->next;
590c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
591c833c3b5SJed Brown     }
5920298fd71SBarry Smith     (*dm)->refinehook = NULL;
593c833c3b5SJed Brown   }
594be081cd6SPeter Brune   {
595be081cd6SPeter Brune     DMSubDomainHookLink link,next;
596be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
597be081cd6SPeter Brune       next = link->next;
598be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
599be081cd6SPeter Brune     }
6000298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
601be081cd6SPeter Brune   }
602baf369e7SPeter Brune   {
603baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
604baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
605baf369e7SPeter Brune       next = link->next;
606baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
607baf369e7SPeter Brune     }
6080298fd71SBarry Smith     (*dm)->gtolhook = NULL;
609baf369e7SPeter Brune   }
610d4d07f1eSToby Isaac   {
611d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
612d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
613d4d07f1eSToby Isaac       next = link->next;
614d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
615d4d07f1eSToby Isaac     }
616d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
617d4d07f1eSToby Isaac   }
618aa1993deSMatthew G Knepley   /* Destroy the work arrays */
619aa1993deSMatthew G Knepley   {
620aa1993deSMatthew G Knepley     DMWorkLink link,next;
621aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
622aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
623aa1993deSMatthew G Knepley       next = link->next;
624aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
625aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
626aa1993deSMatthew G Knepley     }
6270298fd71SBarry Smith     (*dm)->workin = NULL;
628aa1993deSMatthew G Knepley   }
629c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
630c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
631c58f1c22SToby Isaac 
632c58f1c22SToby Isaac     /* destroy the labels */
633c58f1c22SToby Isaac     while (next) {
634c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
635c58f1c22SToby Isaac 
636c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
637c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
638c58f1c22SToby Isaac       next = tmp;
639c58f1c22SToby Isaac     }
640c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
641c58f1c22SToby Isaac   }
642e6f8dbb6SToby Isaac   {
643e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
644e6f8dbb6SToby Isaac     while (next) {
645e6f8dbb6SToby Isaac       DMBoundary b = next;
646e6f8dbb6SToby Isaac 
647e6f8dbb6SToby Isaac       next = b->next;
648e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
649e6f8dbb6SToby Isaac     }
650e6f8dbb6SToby Isaac   }
651b17ce1afSJed Brown 
65252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
65352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
65452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
65552536dc3SBarry Smith 
6561a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6571a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6581a266240SBarry Smith   }
65987e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
66071cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6614dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6626bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6636bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
664073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
66588ed4aceSMatthew G Knepley 
66688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
66788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6688b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
669fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
670fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
67188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
67288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6738e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
674af122d2aSMatthew G Knepley 
67588bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
67688bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
67788bdff64SToby Isaac   }
678a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
67988bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
68088bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
68188bdff64SToby Isaac   }
68288bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
6836636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6846636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6856636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6865dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6876636e97aSMatthew G Knepley 
6882764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
68914f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
690e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
691e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
692732e2eb9SMatthew G Knepley 
6936bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
694435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
695732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
69647c6ae99SBarry Smith   PetscFunctionReturn(0);
69747c6ae99SBarry Smith }
69847c6ae99SBarry Smith 
699d7bf68aeSBarry Smith /*@
700d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
701d7bf68aeSBarry Smith 
702d7bf68aeSBarry Smith     Collective on DM
703d7bf68aeSBarry Smith 
704d7bf68aeSBarry Smith     Input Parameter:
705d7bf68aeSBarry Smith .   dm - the DM object to setup
706d7bf68aeSBarry Smith 
707d7bf68aeSBarry Smith     Level: developer
708d7bf68aeSBarry Smith 
709e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
710d7bf68aeSBarry Smith 
711d7bf68aeSBarry Smith @*/
7127087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
713d7bf68aeSBarry Smith {
714d7bf68aeSBarry Smith   PetscErrorCode ierr;
715d7bf68aeSBarry Smith 
716d7bf68aeSBarry Smith   PetscFunctionBegin;
717171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7188387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
719d7bf68aeSBarry Smith   if (dm->ops->setup) {
720d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
721d7bf68aeSBarry Smith   }
7228387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
723d7bf68aeSBarry Smith   PetscFunctionReturn(0);
724d7bf68aeSBarry Smith }
725d7bf68aeSBarry Smith 
726d7bf68aeSBarry Smith /*@
727d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
728d7bf68aeSBarry Smith 
729d7bf68aeSBarry Smith     Collective on DM
730d7bf68aeSBarry Smith 
731d7bf68aeSBarry Smith     Input Parameter:
732d7bf68aeSBarry Smith .   dm - the DM object to set options for
733d7bf68aeSBarry Smith 
734732e2eb9SMatthew G Knepley     Options Database:
7354164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7364164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7374164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7384164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
739732e2eb9SMatthew G Knepley 
740d7bf68aeSBarry Smith     Level: developer
741d7bf68aeSBarry Smith 
742e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
743d7bf68aeSBarry Smith 
744d7bf68aeSBarry Smith @*/
7457087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
746d7bf68aeSBarry Smith {
7477781c08eSBarry Smith   char           typeName[256];
748ca266f36SBarry Smith   PetscBool      flg;
749d7bf68aeSBarry Smith   PetscErrorCode ierr;
750d7bf68aeSBarry Smith 
751d7bf68aeSBarry Smith   PetscFunctionBegin;
752171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
753f1fd5e65SToby Isaac   if (dm->prob) {
754f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
755f1fd5e65SToby Isaac   }
756691be533SLawrence Mitchell   if (dm->sf) {
757691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
758691be533SLawrence Mitchell   }
759691be533SLawrence Mitchell   if (dm->defaultSF) {
760691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
761691be533SLawrence Mitchell   }
7623194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7630298fd71SBarry 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);
764a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
765f9ba7244SBarry Smith   if (flg) {
766f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
767f9ba7244SBarry Smith   }
768a264d7a6SBarry 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);
769073dac72SJed Brown   if (flg) {
770521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
771073dac72SJed Brown   }
7720298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
773f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
774e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
775f9ba7244SBarry Smith   }
776f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
7770633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
77882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
779d7bf68aeSBarry Smith   PetscFunctionReturn(0);
780d7bf68aeSBarry Smith }
781d7bf68aeSBarry Smith 
782fc9bc008SSatish Balay /*@C
783224748a4SBarry Smith     DMView - Views a DM
78447c6ae99SBarry Smith 
78547c6ae99SBarry Smith     Collective on DM
78647c6ae99SBarry Smith 
78747c6ae99SBarry Smith     Input Parameter:
78847c6ae99SBarry Smith +   dm - the DM object to view
78947c6ae99SBarry Smith -   v - the viewer
79047c6ae99SBarry Smith 
791224748a4SBarry Smith     Level: beginner
79247c6ae99SBarry Smith 
793e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
79447c6ae99SBarry Smith 
79547c6ae99SBarry Smith @*/
7967087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
79747c6ae99SBarry Smith {
79847c6ae99SBarry Smith   PetscErrorCode ierr;
79932c0f0efSBarry Smith   PetscBool      isbinary;
80047c6ae99SBarry Smith 
80147c6ae99SBarry Smith   PetscFunctionBegin;
802171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8033014e516SBarry Smith   if (!v) {
804ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8053014e516SBarry Smith   }
80698c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
80732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
80832c0f0efSBarry Smith   if (isbinary) {
80955849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
81032c0f0efSBarry Smith     char     type[256];
81132c0f0efSBarry Smith 
81232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
81332c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
81432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
81532c0f0efSBarry Smith   }
8160c010503SBarry Smith   if (dm->ops->view) {
8170c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
81847c6ae99SBarry Smith   }
81947c6ae99SBarry Smith   PetscFunctionReturn(0);
82047c6ae99SBarry Smith }
82147c6ae99SBarry Smith 
82247c6ae99SBarry Smith /*@
8238472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
82447c6ae99SBarry Smith 
82547c6ae99SBarry Smith     Collective on DM
82647c6ae99SBarry Smith 
82747c6ae99SBarry Smith     Input Parameter:
82847c6ae99SBarry Smith .   dm - the DM object
82947c6ae99SBarry Smith 
83047c6ae99SBarry Smith     Output Parameter:
83147c6ae99SBarry Smith .   vec - the global vector
83247c6ae99SBarry Smith 
833073dac72SJed Brown     Level: beginner
83447c6ae99SBarry Smith 
835e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith @*/
8387087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
83947c6ae99SBarry Smith {
84047c6ae99SBarry Smith   PetscErrorCode ierr;
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith   PetscFunctionBegin;
843171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
84547c6ae99SBarry Smith   PetscFunctionReturn(0);
84647c6ae99SBarry Smith }
84747c6ae99SBarry Smith 
84847c6ae99SBarry Smith /*@
8498472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
85047c6ae99SBarry Smith 
85147c6ae99SBarry Smith     Not Collective
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith     Input Parameter:
85447c6ae99SBarry Smith .   dm - the DM object
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith     Output Parameter:
85747c6ae99SBarry Smith .   vec - the local vector
85847c6ae99SBarry Smith 
859073dac72SJed Brown     Level: beginner
86047c6ae99SBarry Smith 
861e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith @*/
8647087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
86547c6ae99SBarry Smith {
86647c6ae99SBarry Smith   PetscErrorCode ierr;
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith   PetscFunctionBegin;
869171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87047c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
87147c6ae99SBarry Smith   PetscFunctionReturn(0);
87247c6ae99SBarry Smith }
87347c6ae99SBarry Smith 
8741411c6eeSJed Brown /*@
8751411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8761411c6eeSJed Brown 
8771411c6eeSJed Brown    Collective on DM
8781411c6eeSJed Brown 
8791411c6eeSJed Brown    Input Parameter:
8801411c6eeSJed Brown .  dm - the DM that provides the mapping
8811411c6eeSJed Brown 
8821411c6eeSJed Brown    Output Parameter:
8831411c6eeSJed Brown .  ltog - the mapping
8841411c6eeSJed Brown 
8851411c6eeSJed Brown    Level: intermediate
8861411c6eeSJed Brown 
8871411c6eeSJed Brown    Notes:
8881411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8891411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8901411c6eeSJed Brown 
891fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8921411c6eeSJed Brown @*/
8937087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8941411c6eeSJed Brown {
895*0be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
8961411c6eeSJed Brown   PetscErrorCode ierr;
8971411c6eeSJed Brown 
8981411c6eeSJed Brown   PetscFunctionBegin;
8991411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9001411c6eeSJed Brown   PetscValidPointer(ltog,2);
9011411c6eeSJed Brown   if (!dm->ltogmap) {
90237d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
90337d0c07bSMatthew G Knepley 
90437d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
90537d0c07bSMatthew G Knepley     if (section) {
906a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
90737d0c07bSMatthew G Knepley       PetscInt       *ltog;
908ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
90937d0c07bSMatthew G Knepley 
91037d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
91137d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
912ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
913ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
91437d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
915a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
91637d0c07bSMatthew G Knepley 
91737d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
91837d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
919a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
920a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
92137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
9221a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
9231a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
9241a7dc684SMatthew G. Knepley         if (dof) {
925b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
926b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
9271a7dc684SMatthew G. Knepley         }
92837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
929a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
930a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
93137d0c07bSMatthew G Knepley         }
93237d0c07bSMatthew G Knepley       }
933bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
934*0be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
935*0be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
936*0be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
937*0be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
9387591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
9397591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
940ccf3bd66SMatthew G. Knepley       if (bs > 1) {
941ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
942ccf3bd66SMatthew G. Knepley         n /= bs;
943ccf3bd66SMatthew G. Knepley       }
944ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9453bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
94637d0c07bSMatthew G Knepley     } else {
947184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
948184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9491411c6eeSJed Brown     }
95037d0c07bSMatthew G Knepley   }
9511411c6eeSJed Brown   *ltog = dm->ltogmap;
9521411c6eeSJed Brown   PetscFunctionReturn(0);
9531411c6eeSJed Brown }
9541411c6eeSJed Brown 
9551411c6eeSJed Brown /*@
9561411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9571411c6eeSJed Brown 
9581411c6eeSJed Brown    Not Collective
9591411c6eeSJed Brown 
9601411c6eeSJed Brown    Input Parameter:
9611411c6eeSJed Brown .  dm - the DM with block structure
9621411c6eeSJed Brown 
9631411c6eeSJed Brown    Output Parameter:
9641411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9651411c6eeSJed Brown 
9661411c6eeSJed Brown    Level: intermediate
9671411c6eeSJed Brown 
968fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9691411c6eeSJed Brown @*/
9707087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9711411c6eeSJed Brown {
9721411c6eeSJed Brown   PetscFunctionBegin;
9731411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9741411c6eeSJed Brown   PetscValidPointer(bs,2);
9751411c6eeSJed 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");
9761411c6eeSJed Brown   *bs = dm->bs;
9771411c6eeSJed Brown   PetscFunctionReturn(0);
9781411c6eeSJed Brown }
9791411c6eeSJed Brown 
98047c6ae99SBarry Smith /*@
9818472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
98247c6ae99SBarry Smith 
98347c6ae99SBarry Smith     Collective on DM
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith     Input Parameter:
98647c6ae99SBarry Smith +   dm1 - the DM object
98747c6ae99SBarry Smith -   dm2 - the second, finer DM object
98847c6ae99SBarry Smith 
98947c6ae99SBarry Smith     Output Parameter:
99047c6ae99SBarry Smith +  mat - the interpolation
99147c6ae99SBarry Smith -  vec - the scaling (optional)
99247c6ae99SBarry Smith 
99347c6ae99SBarry Smith     Level: developer
99447c6ae99SBarry Smith 
99585afcc9aSBarry 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
99685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
997d52bd9f3SBarry Smith 
9981f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
999d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
100085afcc9aSBarry Smith 
100185afcc9aSBarry Smith 
10023ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith @*/
1005e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
100647c6ae99SBarry Smith {
100747c6ae99SBarry Smith   PetscErrorCode ierr;
100847c6ae99SBarry Smith 
100947c6ae99SBarry Smith   PetscFunctionBegin;
1010171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1011171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1012cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
101325296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1014cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
101547c6ae99SBarry Smith   PetscFunctionReturn(0);
101647c6ae99SBarry Smith }
101747c6ae99SBarry Smith 
10183ad4599aSBarry Smith /*@
10193ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10203ad4599aSBarry Smith 
10213ad4599aSBarry Smith     Collective on DM
10223ad4599aSBarry Smith 
10233ad4599aSBarry Smith     Input Parameter:
10243ad4599aSBarry Smith +   dm1 - the DM object
10253ad4599aSBarry Smith -   dm2 - the second, finer DM object
10263ad4599aSBarry Smith 
10273ad4599aSBarry Smith     Output Parameter:
10283ad4599aSBarry Smith .  mat - the restriction
10293ad4599aSBarry Smith 
10303ad4599aSBarry Smith 
10313ad4599aSBarry Smith     Level: developer
10323ad4599aSBarry Smith 
10333ad4599aSBarry 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
10343ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10353ad4599aSBarry Smith 
10363ad4599aSBarry Smith 
10373ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10383ad4599aSBarry Smith 
10393ad4599aSBarry Smith @*/
10403ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10413ad4599aSBarry Smith {
10423ad4599aSBarry Smith   PetscErrorCode ierr;
10433ad4599aSBarry Smith 
10443ad4599aSBarry Smith   PetscFunctionBegin;
10453ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10463ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10473ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10487294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
10493ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10503ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10513ad4599aSBarry Smith   PetscFunctionReturn(0);
10523ad4599aSBarry Smith }
10533ad4599aSBarry Smith 
105447c6ae99SBarry Smith /*@
10558472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
105647c6ae99SBarry Smith 
105747c6ae99SBarry Smith     Collective on DM
105847c6ae99SBarry Smith 
105947c6ae99SBarry Smith     Input Parameter:
106047c6ae99SBarry Smith +   dm1 - the DM object
106147c6ae99SBarry Smith -   dm2 - the second, finer DM object
106247c6ae99SBarry Smith 
106347c6ae99SBarry Smith     Output Parameter:
10646dbf9973SLawrence Mitchell .   mat - the injection
106547c6ae99SBarry Smith 
106647c6ae99SBarry Smith     Level: developer
106747c6ae99SBarry Smith 
106885afcc9aSBarry 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
106985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
107085afcc9aSBarry Smith 
1071e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
107247c6ae99SBarry Smith 
107347c6ae99SBarry Smith @*/
10746dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
107547c6ae99SBarry Smith {
107647c6ae99SBarry Smith   PetscErrorCode ierr;
107747c6ae99SBarry Smith 
107847c6ae99SBarry Smith   PetscFunctionBegin;
1079171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1080171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10810c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
10826dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
108347c6ae99SBarry Smith   PetscFunctionReturn(0);
108447c6ae99SBarry Smith }
108547c6ae99SBarry Smith 
1086b412c318SBarry Smith /*@
1087bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1088bd041c0cSMatthew G. Knepley 
1089bd041c0cSMatthew G. Knepley   Collective on DM
1090bd041c0cSMatthew G. Knepley 
1091bd041c0cSMatthew G. Knepley   Input Parameter:
1092bd041c0cSMatthew G. Knepley + dm1 - the DM object
1093bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1094bd041c0cSMatthew G. Knepley 
1095bd041c0cSMatthew G. Knepley   Output Parameter:
1096bd041c0cSMatthew G. Knepley . mat - the interpolation
1097bd041c0cSMatthew G. Knepley 
1098bd041c0cSMatthew G. Knepley   Level: developer
1099bd041c0cSMatthew G. Knepley 
1100bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1101bd041c0cSMatthew G. Knepley @*/
1102bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1103bd041c0cSMatthew G. Knepley {
1104bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1105bd041c0cSMatthew G. Knepley 
1106bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1107bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1108bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1109bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1110bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1111bd041c0cSMatthew G. Knepley }
1112bd041c0cSMatthew G. Knepley 
1113bd041c0cSMatthew G. Knepley /*@
1114b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
111547c6ae99SBarry Smith 
111647c6ae99SBarry Smith     Collective on DM
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith     Input Parameter:
111947c6ae99SBarry Smith +   dm - the DM object
11205bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
112147c6ae99SBarry Smith 
112247c6ae99SBarry Smith     Output Parameter:
112347c6ae99SBarry Smith .   coloring - the coloring
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith     Level: developer
112647c6ae99SBarry Smith 
1127b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
112847c6ae99SBarry Smith 
1129aab9d709SJed Brown @*/
1130b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
113147c6ae99SBarry Smith {
113247c6ae99SBarry Smith   PetscErrorCode ierr;
113347c6ae99SBarry Smith 
113447c6ae99SBarry Smith   PetscFunctionBegin;
1135171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1136ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1137b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
113847c6ae99SBarry Smith   PetscFunctionReturn(0);
113947c6ae99SBarry Smith }
114047c6ae99SBarry Smith 
1141b412c318SBarry Smith /*@
11428472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
114347c6ae99SBarry Smith 
114447c6ae99SBarry Smith     Collective on DM
114547c6ae99SBarry Smith 
114647c6ae99SBarry Smith     Input Parameter:
1147b412c318SBarry Smith .   dm - the DM object
114847c6ae99SBarry Smith 
114947c6ae99SBarry Smith     Output Parameter:
115047c6ae99SBarry Smith .   mat - the empty Jacobian
115147c6ae99SBarry Smith 
1152073dac72SJed Brown     Level: beginner
115347c6ae99SBarry Smith 
115494013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
115594013140SBarry Smith        do not need to do it yourself.
115694013140SBarry Smith 
115794013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
11587889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
115994013140SBarry Smith 
116094013140SBarry 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
116194013140SBarry Smith        internally by PETSc.
116294013140SBarry Smith 
116394013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1164aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
116594013140SBarry Smith 
1166b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
116747c6ae99SBarry Smith 
1168aab9d709SJed Brown @*/
1169b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
117047c6ae99SBarry Smith {
117147c6ae99SBarry Smith   PetscErrorCode ierr;
117247c6ae99SBarry Smith 
117347c6ae99SBarry Smith   PetscFunctionBegin;
1174171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1175607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1176c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1177c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1178b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
117947c6ae99SBarry Smith   PetscFunctionReturn(0);
118047c6ae99SBarry Smith }
118147c6ae99SBarry Smith 
1182732e2eb9SMatthew G Knepley /*@
1183950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1184732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1185732e2eb9SMatthew G Knepley 
11868472ad0fSDave May   Logically Collective on DM
1187732e2eb9SMatthew G Knepley 
1188732e2eb9SMatthew G Knepley   Input Parameter:
1189732e2eb9SMatthew G Knepley + dm - the DM
1190732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1191732e2eb9SMatthew G Knepley 
1192732e2eb9SMatthew G Knepley   Level: developer
1193b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1194732e2eb9SMatthew G Knepley @*/
1195732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1196732e2eb9SMatthew G Knepley {
1197732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1198732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1199732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1200732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1201732e2eb9SMatthew G Knepley }
1202732e2eb9SMatthew G Knepley 
1203b06ff27eSHong Zhang /*@
1204b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1205b06ff27eSHong Zhang     but the array for values will not be allocated.
1206b06ff27eSHong Zhang 
1207b06ff27eSHong Zhang   Logically Collective on DM
1208b06ff27eSHong Zhang 
1209b06ff27eSHong Zhang   Input Parameter:
1210b06ff27eSHong Zhang + dm - the DM
1211b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1212b06ff27eSHong Zhang 
1213b06ff27eSHong Zhang   Level: developer
1214b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1215b06ff27eSHong Zhang @*/
1216b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1217b06ff27eSHong Zhang {
1218b06ff27eSHong Zhang   PetscFunctionBegin;
1219b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1220b06ff27eSHong Zhang   dm->structure_only = only;
1221b06ff27eSHong Zhang   PetscFunctionReturn(0);
1222b06ff27eSHong Zhang }
1223b06ff27eSHong Zhang 
1224a89ea682SMatthew G Knepley /*@C
1225aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1226a89ea682SMatthew G Knepley 
1227a89ea682SMatthew G Knepley   Not Collective
1228a89ea682SMatthew G Knepley 
1229a89ea682SMatthew G Knepley   Input Parameters:
1230a89ea682SMatthew G Knepley + dm - the DM object
1231aa1993deSMatthew G Knepley . count - The minium size
1232aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1233a89ea682SMatthew G Knepley 
1234a89ea682SMatthew G Knepley   Output Parameter:
1235a89ea682SMatthew G Knepley . array - the work array
1236a89ea682SMatthew G Knepley 
1237a89ea682SMatthew G Knepley   Level: developer
1238a89ea682SMatthew G Knepley 
1239a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1240a89ea682SMatthew G Knepley @*/
1241aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1242a89ea682SMatthew G Knepley {
1243a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1244aa1993deSMatthew G Knepley   DMWorkLink     link;
1245854ce69bSBarry Smith   size_t         dsize;
1246a89ea682SMatthew G Knepley 
1247a89ea682SMatthew G Knepley   PetscFunctionBegin;
1248a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1249aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1250aa1993deSMatthew G Knepley   if (dm->workin) {
1251aa1993deSMatthew G Knepley     link       = dm->workin;
1252aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1253aa1993deSMatthew G Knepley   } else {
1254b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1255a89ea682SMatthew G Knepley   }
1256854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1257854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1258aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1259854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1260854ce69bSBarry Smith     link->bytes = dsize*count;
1261aa1993deSMatthew G Knepley   }
1262aa1993deSMatthew G Knepley   link->next   = dm->workout;
1263aa1993deSMatthew G Knepley   dm->workout  = link;
1264aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1265a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1266a89ea682SMatthew G Knepley }
1267a89ea682SMatthew G Knepley 
1268aa1993deSMatthew G Knepley /*@C
1269aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1270aa1993deSMatthew G Knepley 
1271aa1993deSMatthew G Knepley   Not Collective
1272aa1993deSMatthew G Knepley 
1273aa1993deSMatthew G Knepley   Input Parameters:
1274aa1993deSMatthew G Knepley + dm - the DM object
1275aa1993deSMatthew G Knepley . count - The minium size
1276aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1277aa1993deSMatthew G Knepley 
1278aa1993deSMatthew G Knepley   Output Parameter:
1279aa1993deSMatthew G Knepley . array - the work array
1280aa1993deSMatthew G Knepley 
1281aa1993deSMatthew G Knepley   Level: developer
1282aa1993deSMatthew G Knepley 
1283aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1284aa1993deSMatthew G Knepley @*/
1285aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1286aa1993deSMatthew G Knepley {
1287aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1288aa1993deSMatthew G Knepley 
1289aa1993deSMatthew G Knepley   PetscFunctionBegin;
1290aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1291aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1292aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1293aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1294aa1993deSMatthew G Knepley       *p           = link->next;
1295aa1993deSMatthew G Knepley       link->next   = dm->workin;
1296aa1993deSMatthew G Knepley       dm->workin   = link;
12970298fd71SBarry Smith       *(void**)mem = NULL;
1298aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1299aa1993deSMatthew G Knepley     }
1300aa1993deSMatthew G Knepley   }
1301aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1302aa1993deSMatthew G Knepley }
1303e7c4fc90SDmitry Karpeev 
1304435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1305435a35e8SMatthew G Knepley {
1306435a35e8SMatthew G Knepley   PetscFunctionBegin;
1307435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
130882f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1309435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1310435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1311435a35e8SMatthew G Knepley }
1312435a35e8SMatthew G Knepley 
13134f3b5142SJed Brown /*@C
13144d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
13154d343eeaSMatthew G Knepley 
13164d343eeaSMatthew G Knepley   Not collective
13174d343eeaSMatthew G Knepley 
13184d343eeaSMatthew G Knepley   Input Parameter:
13194d343eeaSMatthew G Knepley . dm - the DM object
13204d343eeaSMatthew G Knepley 
13214d343eeaSMatthew G Knepley   Output Parameters:
13220298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13230298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13240298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13254d343eeaSMatthew G Knepley 
13264d343eeaSMatthew G Knepley   Level: intermediate
13274d343eeaSMatthew G Knepley 
132821c9b008SJed Brown   Notes:
132921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
133021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
133121c9b008SJed Brown   PetscFree().
133221c9b008SJed Brown 
13334d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13344d343eeaSMatthew G Knepley @*/
133537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13364d343eeaSMatthew G Knepley {
133737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13384d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13394d343eeaSMatthew G Knepley 
13404d343eeaSMatthew G Knepley   PetscFunctionBegin;
13414d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134269ca1f37SDmitry Karpeev   if (numFields) {
134369ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
134469ca1f37SDmitry Karpeev     *numFields = 0;
134569ca1f37SDmitry Karpeev   }
134637d0c07bSMatthew G Knepley   if (fieldNames) {
134737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13480298fd71SBarry Smith     *fieldNames = NULL;
134969ca1f37SDmitry Karpeev   }
135069ca1f37SDmitry Karpeev   if (fields) {
135169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13520298fd71SBarry Smith     *fields = NULL;
135369ca1f37SDmitry Karpeev   }
135437d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
135537d0c07bSMatthew G Knepley   if (section) {
135637d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
135737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
135837d0c07bSMatthew G Knepley 
135937d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
136037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1361dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
136237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
136337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
136437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
136537d0c07bSMatthew G Knepley     }
136637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
136737d0c07bSMatthew G Knepley       PetscInt gdof;
136837d0c07bSMatthew G Knepley 
136937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
137037d0c07bSMatthew G Knepley       if (gdof > 0) {
137137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
137237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
137337d0c07bSMatthew G Knepley 
137437d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
137537d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
137637d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
137737d0c07bSMatthew G Knepley         }
137837d0c07bSMatthew G Knepley       }
137937d0c07bSMatthew G Knepley     }
138037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1381785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
138237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
138337d0c07bSMatthew G Knepley     }
138437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
138537d0c07bSMatthew G Knepley       PetscInt gdof, goff;
138637d0c07bSMatthew G Knepley 
138737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
138837d0c07bSMatthew G Knepley       if (gdof > 0) {
138937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
139037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
139137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
139237d0c07bSMatthew G Knepley 
139337d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
139437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
139537d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
139637d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
139737d0c07bSMatthew G Knepley           }
139837d0c07bSMatthew G Knepley         }
139937d0c07bSMatthew G Knepley       }
140037d0c07bSMatthew G Knepley     }
14018865f1eaSKarl Rupp     if (numFields) *numFields = nF;
140237d0c07bSMatthew G Knepley     if (fieldNames) {
1403785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
140437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
140537d0c07bSMatthew G Knepley         const char *fieldName;
140637d0c07bSMatthew G Knepley 
140737d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
140837d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
140937d0c07bSMatthew G Knepley       }
141037d0c07bSMatthew G Knepley     }
141137d0c07bSMatthew G Knepley     if (fields) {
1412785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
141337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
141482f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
141537d0c07bSMatthew G Knepley       }
141637d0c07bSMatthew G Knepley     }
141737d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
14188865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
14198865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
142069ca1f37SDmitry Karpeev   }
14214d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14224d343eeaSMatthew G Knepley }
14234d343eeaSMatthew G Knepley 
142416621825SDmitry Karpeev 
142516621825SDmitry Karpeev /*@C
142616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
142716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
142816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1429e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1430e7c4fc90SDmitry Karpeev 
1431e7c4fc90SDmitry Karpeev   Not collective
1432e7c4fc90SDmitry Karpeev 
1433e7c4fc90SDmitry Karpeev   Input Parameter:
1434e7c4fc90SDmitry Karpeev . dm - the DM object
1435e7c4fc90SDmitry Karpeev 
1436e7c4fc90SDmitry Karpeev   Output Parameters:
14370298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14380298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14390298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14400298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1441e7c4fc90SDmitry Karpeev 
1442e7c4fc90SDmitry Karpeev   Level: intermediate
1443e7c4fc90SDmitry Karpeev 
1444e7c4fc90SDmitry Karpeev   Notes:
1445e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1446e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1447e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1448e7c4fc90SDmitry Karpeev 
1449e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1450e7c4fc90SDmitry Karpeev @*/
145116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1452e7c4fc90SDmitry Karpeev {
1453e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1454e7c4fc90SDmitry Karpeev 
1455e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1456e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14578865f1eaSKarl Rupp   if (len) {
14588865f1eaSKarl Rupp     PetscValidPointer(len,2);
14598865f1eaSKarl Rupp     *len = 0;
14608865f1eaSKarl Rupp   }
14618865f1eaSKarl Rupp   if (namelist) {
14628865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14638865f1eaSKarl Rupp     *namelist = 0;
14648865f1eaSKarl Rupp   }
14658865f1eaSKarl Rupp   if (islist) {
14668865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14678865f1eaSKarl Rupp     *islist = 0;
14688865f1eaSKarl Rupp   }
14698865f1eaSKarl Rupp   if (dmlist) {
14708865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14718865f1eaSKarl Rupp     *dmlist = 0;
14728865f1eaSKarl Rupp   }
1473f3f0edfdSDmitry Karpeev   /*
1474f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1475f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1476f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1477f3f0edfdSDmitry Karpeev    */
1478ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
147916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1480435a35e8SMatthew G Knepley     PetscSection section;
1481435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1482435a35e8SMatthew G Knepley 
1483435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1484435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1485435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1486f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
148703dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
148803dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
148903dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1490435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1491435a35e8SMatthew G Knepley         const char *fieldName;
1492435a35e8SMatthew G Knepley 
149303dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
149403dc3394SMatthew G. Knepley         if (namelist) {
1495435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1496435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1497435a35e8SMatthew G Knepley         }
149803dc3394SMatthew G. Knepley       }
1499435a35e8SMatthew G Knepley     } else {
150069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1501e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
15020298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1503e7c4fc90SDmitry Karpeev     }
15048865f1eaSKarl Rupp   } else {
150516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
150616621825SDmitry Karpeev   }
150716621825SDmitry Karpeev   PetscFunctionReturn(0);
150816621825SDmitry Karpeev }
150916621825SDmitry Karpeev 
1510564cec59SMatthew G. Knepley /*@
1511435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1512435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1513435a35e8SMatthew G Knepley 
1514435a35e8SMatthew G Knepley   Not collective
1515435a35e8SMatthew G Knepley 
1516435a35e8SMatthew G Knepley   Input Parameters:
15172adcc780SMatthew G. Knepley + dm        - The DM object
15182adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
15192adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1520435a35e8SMatthew G Knepley 
1521435a35e8SMatthew G Knepley   Output Parameters:
15222adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15232adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1524435a35e8SMatthew G Knepley 
1525435a35e8SMatthew G Knepley   Level: intermediate
1526435a35e8SMatthew G Knepley 
1527435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1528435a35e8SMatthew G Knepley @*/
1529435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1530435a35e8SMatthew G Knepley {
1531435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1532435a35e8SMatthew G Knepley 
1533435a35e8SMatthew G Knepley   PetscFunctionBegin;
1534435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1535435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15368865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15378865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1538435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1539435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
154082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1541435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1542435a35e8SMatthew G Knepley }
1543435a35e8SMatthew G Knepley 
15442adcc780SMatthew G. Knepley /*@C
15452adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
15462adcc780SMatthew G. Knepley 
15472adcc780SMatthew G. Knepley   Not collective
15482adcc780SMatthew G. Knepley 
15492adcc780SMatthew G. Knepley   Input Parameter:
15502adcc780SMatthew G. Knepley + dms - The DM objects
15512adcc780SMatthew G. Knepley - len - The number of DMs
15522adcc780SMatthew G. Knepley 
15532adcc780SMatthew G. Knepley   Output Parameters:
15542adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15552adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
15562adcc780SMatthew G. Knepley 
15572adcc780SMatthew G. Knepley   Level: intermediate
15582adcc780SMatthew G. Knepley 
15592adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
15602adcc780SMatthew G. Knepley @*/
15612adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
15622adcc780SMatthew G. Knepley {
15632adcc780SMatthew G. Knepley   PetscInt       i;
15642adcc780SMatthew G. Knepley   PetscErrorCode ierr;
15652adcc780SMatthew G. Knepley 
15662adcc780SMatthew G. Knepley   PetscFunctionBegin;
15672adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
15682adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
15692adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
15702adcc780SMatthew G. Knepley   if (superdm) PetscValidPointer(superdm,4);
15712adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
15722adcc780SMatthew G. Knepley   if (len) {
15732adcc780SMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {
15742adcc780SMatthew G. Knepley       ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
15752adcc780SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
15762adcc780SMatthew G. Knepley   }
15772adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
15782adcc780SMatthew G. Knepley }
15792adcc780SMatthew G. Knepley 
158016621825SDmitry Karpeev 
158116621825SDmitry Karpeev /*@C
15828d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
15838d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
15848d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
15858d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
15868d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
158716621825SDmitry Karpeev 
158816621825SDmitry Karpeev   Not collective
158916621825SDmitry Karpeev 
159016621825SDmitry Karpeev   Input Parameter:
159116621825SDmitry Karpeev . dm - the DM object
159216621825SDmitry Karpeev 
159316621825SDmitry Karpeev   Output Parameters:
15940298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
15950298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
15960298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
15970298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
15980298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
159916621825SDmitry Karpeev 
160016621825SDmitry Karpeev   Level: intermediate
160116621825SDmitry Karpeev 
160216621825SDmitry Karpeev   Notes:
160316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
160416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
160516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
160616621825SDmitry Karpeev 
16078d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
160816621825SDmitry Karpeev @*/
16098d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
161016621825SDmitry Karpeev {
161116621825SDmitry Karpeev   PetscErrorCode      ierr;
1612be081cd6SPeter Brune   DMSubDomainHookLink link;
1613be081cd6SPeter Brune   PetscInt            i,l;
161416621825SDmitry Karpeev 
161516621825SDmitry Karpeev   PetscFunctionBegin;
161616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
161714a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
16180298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
16190298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
16200298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
16210298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1622f3f0edfdSDmitry Karpeev   /*
1623f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1624f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1625f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1626f3f0edfdSDmitry Karpeev    */
1627ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
162816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1629be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
163014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1631f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1632be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1633be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1634be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1635be081cd6SPeter Brune         }
1636648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1637e7c4fc90SDmitry Karpeev       }
163814a18fd3SPeter Brune     }
163914a18fd3SPeter Brune     if (len) *len = l;
164014a18fd3SPeter Brune   }
1641e30e807fSPeter Brune   PetscFunctionReturn(0);
1642e30e807fSPeter Brune }
1643e30e807fSPeter Brune 
1644e30e807fSPeter Brune 
1645e30e807fSPeter Brune /*@C
1646e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1647e30e807fSPeter Brune 
1648e30e807fSPeter Brune   Not collective
1649e30e807fSPeter Brune 
1650e30e807fSPeter Brune   Input Parameters:
1651e30e807fSPeter Brune + dm - the DM object
1652e30e807fSPeter Brune . n  - the number of subdomain scatters
1653e30e807fSPeter Brune - subdms - the local subdomains
1654e30e807fSPeter Brune 
1655e30e807fSPeter Brune   Output Parameters:
1656e30e807fSPeter Brune + n     - the number of scatters returned
1657e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1658e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1659e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1660e30e807fSPeter Brune 
1661e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1662e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1663e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1664e30e807fSPeter Brune   solution and residual data.
1665e30e807fSPeter Brune 
1666e30e807fSPeter Brune   Level: developer
1667e30e807fSPeter Brune 
1668e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1669e30e807fSPeter Brune @*/
1670e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1671e30e807fSPeter Brune {
1672e30e807fSPeter Brune   PetscErrorCode ierr;
1673e30e807fSPeter Brune 
1674e30e807fSPeter Brune   PetscFunctionBegin;
1675e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1676e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1677e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1678e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
16796668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1680e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1681e7c4fc90SDmitry Karpeev }
1682e7c4fc90SDmitry Karpeev 
168347c6ae99SBarry Smith /*@
168447c6ae99SBarry Smith   DMRefine - Refines a DM object
168547c6ae99SBarry Smith 
168647c6ae99SBarry Smith   Collective on DM
168747c6ae99SBarry Smith 
168847c6ae99SBarry Smith   Input Parameter:
168947c6ae99SBarry Smith + dm   - the DM object
169091d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
169147c6ae99SBarry Smith 
169247c6ae99SBarry Smith   Output Parameter:
16930298fd71SBarry Smith . dmf - the refined DM, or NULL
1694ae0a1c52SMatthew G Knepley 
16950298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
169647c6ae99SBarry Smith 
169747c6ae99SBarry Smith   Level: developer
169847c6ae99SBarry Smith 
1699e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
170047c6ae99SBarry Smith @*/
17017087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
170247c6ae99SBarry Smith {
170347c6ae99SBarry Smith   PetscErrorCode   ierr;
1704c833c3b5SJed Brown   DMRefineHookLink link;
170547c6ae99SBarry Smith 
170647c6ae99SBarry Smith   PetscFunctionBegin;
1707732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17081ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1709fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
171047c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
17114057135bSMatthew G Knepley   if (*dmf) {
171243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
17138865f1eaSKarl Rupp 
17148cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
17158865f1eaSKarl Rupp 
1716644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
17170598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1718656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
17198865f1eaSKarl Rupp 
1720e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1721c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
17228865f1eaSKarl Rupp       if (link->refinehook) {
17238865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
17248865f1eaSKarl Rupp       }
1725c833c3b5SJed Brown     }
1726c833c3b5SJed Brown   }
17271ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1728c833c3b5SJed Brown   PetscFunctionReturn(0);
1729c833c3b5SJed Brown }
1730c833c3b5SJed Brown 
1731bb9467b5SJed Brown /*@C
1732c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1733c833c3b5SJed Brown 
1734c833c3b5SJed Brown    Logically Collective
1735c833c3b5SJed Brown 
1736c833c3b5SJed Brown    Input Arguments:
1737c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1738c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1739c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17400298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1741c833c3b5SJed Brown 
1742c833c3b5SJed Brown    Calling sequence of refinehook:
1743c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1744c833c3b5SJed Brown 
1745c833c3b5SJed Brown +  coarse - coarse level DM
1746c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1747c833c3b5SJed Brown -  ctx - optional user-defined function context
1748c833c3b5SJed Brown 
1749c833c3b5SJed Brown    Calling sequence for interphook:
1750c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1751c833c3b5SJed Brown 
1752c833c3b5SJed Brown +  coarse - coarse level DM
1753c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1754c833c3b5SJed Brown .  fine - fine level DM to update
1755c833c3b5SJed Brown -  ctx - optional user-defined function context
1756c833c3b5SJed Brown 
1757c833c3b5SJed Brown    Level: advanced
1758c833c3b5SJed Brown 
1759c833c3b5SJed Brown    Notes:
1760c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1761c833c3b5SJed Brown 
1762c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1763c833c3b5SJed Brown 
1764bb9467b5SJed Brown    This function is currently not available from Fortran.
1765bb9467b5SJed Brown 
1766c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1767c833c3b5SJed Brown @*/
1768c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1769c833c3b5SJed Brown {
1770c833c3b5SJed Brown   PetscErrorCode   ierr;
1771c833c3b5SJed Brown   DMRefineHookLink link,*p;
1772c833c3b5SJed Brown 
1773c833c3b5SJed Brown   PetscFunctionBegin;
1774c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
17753d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
17763d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
17773d8e3701SJed Brown   }
177895dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1779c833c3b5SJed Brown   link->refinehook = refinehook;
1780c833c3b5SJed Brown   link->interphook = interphook;
1781c833c3b5SJed Brown   link->ctx        = ctx;
17820298fd71SBarry Smith   link->next       = NULL;
1783c833c3b5SJed Brown   *p               = link;
1784c833c3b5SJed Brown   PetscFunctionReturn(0);
1785c833c3b5SJed Brown }
1786c833c3b5SJed Brown 
17873d8e3701SJed Brown /*@C
17883d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
17893d8e3701SJed Brown 
17903d8e3701SJed Brown    Logically Collective
17913d8e3701SJed Brown 
17923d8e3701SJed Brown    Input Arguments:
17933d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
17943d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
17953d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17963d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
17973d8e3701SJed Brown 
17983d8e3701SJed Brown    Level: advanced
17993d8e3701SJed Brown 
18003d8e3701SJed Brown    Notes:
18013d8e3701SJed Brown    This function does nothing if the hook is not in the list.
18023d8e3701SJed Brown 
18033d8e3701SJed Brown    This function is currently not available from Fortran.
18043d8e3701SJed Brown 
18053d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
18063d8e3701SJed Brown @*/
18073d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
18083d8e3701SJed Brown {
18093d8e3701SJed Brown   PetscErrorCode   ierr;
18103d8e3701SJed Brown   DMRefineHookLink link,*p;
18113d8e3701SJed Brown 
18123d8e3701SJed Brown   PetscFunctionBegin;
18133d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
18143d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
18153d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
18163d8e3701SJed Brown       link = *p;
18173d8e3701SJed Brown       *p = link->next;
18183d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
18193d8e3701SJed Brown       break;
18203d8e3701SJed Brown     }
18213d8e3701SJed Brown   }
18223d8e3701SJed Brown   PetscFunctionReturn(0);
18233d8e3701SJed Brown }
18243d8e3701SJed Brown 
1825c833c3b5SJed Brown /*@
1826c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1827c833c3b5SJed Brown 
1828c833c3b5SJed Brown    Collective if any hooks are
1829c833c3b5SJed Brown 
1830c833c3b5SJed Brown    Input Arguments:
1831c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1832c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1833c833c3b5SJed Brown -  fine - finer DM to update
1834c833c3b5SJed Brown 
1835c833c3b5SJed Brown    Level: developer
1836c833c3b5SJed Brown 
1837c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1838c833c3b5SJed Brown @*/
1839c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1840c833c3b5SJed Brown {
1841c833c3b5SJed Brown   PetscErrorCode   ierr;
1842c833c3b5SJed Brown   DMRefineHookLink link;
1843c833c3b5SJed Brown 
1844c833c3b5SJed Brown   PetscFunctionBegin;
1845c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
18468865f1eaSKarl Rupp     if (link->interphook) {
18478865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
18488865f1eaSKarl Rupp     }
18494057135bSMatthew G Knepley   }
185047c6ae99SBarry Smith   PetscFunctionReturn(0);
185147c6ae99SBarry Smith }
185247c6ae99SBarry Smith 
1853eb3f98d2SBarry Smith /*@
1854eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1855eb3f98d2SBarry Smith 
1856eb3f98d2SBarry Smith     Not Collective
1857eb3f98d2SBarry Smith 
1858eb3f98d2SBarry Smith     Input Parameter:
1859eb3f98d2SBarry Smith .   dm - the DM object
1860eb3f98d2SBarry Smith 
1861eb3f98d2SBarry Smith     Output Parameter:
1862eb3f98d2SBarry Smith .   level - number of refinements
1863eb3f98d2SBarry Smith 
1864eb3f98d2SBarry Smith     Level: developer
1865eb3f98d2SBarry Smith 
18666a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1867eb3f98d2SBarry Smith 
1868eb3f98d2SBarry Smith @*/
1869eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1870eb3f98d2SBarry Smith {
1871eb3f98d2SBarry Smith   PetscFunctionBegin;
1872eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1873eb3f98d2SBarry Smith   *level = dm->levelup;
1874eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1875eb3f98d2SBarry Smith }
1876eb3f98d2SBarry Smith 
1877fef3a512SBarry Smith /*@
1878fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1879fef3a512SBarry Smith 
1880fef3a512SBarry Smith     Not Collective
1881fef3a512SBarry Smith 
1882fef3a512SBarry Smith     Input Parameter:
1883fef3a512SBarry Smith +   dm - the DM object
1884fef3a512SBarry Smith -   level - number of refinements
1885fef3a512SBarry Smith 
1886fef3a512SBarry Smith     Level: advanced
1887fef3a512SBarry Smith 
1888fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1889fef3a512SBarry Smith 
1890fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1891fef3a512SBarry Smith 
1892fef3a512SBarry Smith @*/
1893fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1894fef3a512SBarry Smith {
1895fef3a512SBarry Smith   PetscFunctionBegin;
1896fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1897fef3a512SBarry Smith   dm->levelup = level;
1898fef3a512SBarry Smith   PetscFunctionReturn(0);
1899fef3a512SBarry Smith }
1900fef3a512SBarry Smith 
1901bb9467b5SJed Brown /*@C
1902baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1903baf369e7SPeter Brune 
1904baf369e7SPeter Brune    Logically Collective
1905baf369e7SPeter Brune 
1906baf369e7SPeter Brune    Input Arguments:
1907baf369e7SPeter Brune +  dm - the DM
1908baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1909baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
19100298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1911baf369e7SPeter Brune 
1912baf369e7SPeter Brune    Calling sequence for beginhook:
1913baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1914baf369e7SPeter Brune 
1915baf369e7SPeter Brune +  dm - global DM
1916baf369e7SPeter Brune .  g - global vector
1917baf369e7SPeter Brune .  mode - mode
1918baf369e7SPeter Brune .  l - local vector
1919baf369e7SPeter Brune -  ctx - optional user-defined function context
1920baf369e7SPeter Brune 
1921baf369e7SPeter Brune 
1922baf369e7SPeter Brune    Calling sequence for endhook:
1923ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1924baf369e7SPeter Brune 
1925baf369e7SPeter Brune +  global - global DM
1926baf369e7SPeter Brune -  ctx - optional user-defined function context
1927baf369e7SPeter Brune 
1928baf369e7SPeter Brune    Level: advanced
1929baf369e7SPeter Brune 
1930baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1931baf369e7SPeter Brune @*/
1932baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1933baf369e7SPeter Brune {
1934baf369e7SPeter Brune   PetscErrorCode          ierr;
1935baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1936baf369e7SPeter Brune 
1937baf369e7SPeter Brune   PetscFunctionBegin;
1938baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1939baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
194095dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1941baf369e7SPeter Brune   link->beginhook = beginhook;
1942baf369e7SPeter Brune   link->endhook   = endhook;
1943baf369e7SPeter Brune   link->ctx       = ctx;
19440298fd71SBarry Smith   link->next      = NULL;
1945baf369e7SPeter Brune   *p              = link;
1946baf369e7SPeter Brune   PetscFunctionReturn(0);
1947baf369e7SPeter Brune }
1948baf369e7SPeter Brune 
19494c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
19504c274da1SToby Isaac {
19514c274da1SToby Isaac   Mat cMat;
19524c274da1SToby Isaac   Vec cVec;
19534c274da1SToby Isaac   PetscSection section, cSec;
19544c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19554c274da1SToby Isaac   PetscErrorCode ierr;
19564c274da1SToby Isaac 
19574c274da1SToby Isaac   PetscFunctionBegin;
19584c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19594c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19604c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
19615db9a05bSToby Isaac     PetscInt nRows;
19625db9a05bSToby Isaac 
19635db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
19645db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
19654c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19667711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19674c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
19684c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19694c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19704c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19714c274da1SToby Isaac       if (dof) {
19724c274da1SToby Isaac         PetscScalar *vals;
19734c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
19744c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
19754c274da1SToby Isaac       }
19764c274da1SToby Isaac     }
19774c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19784c274da1SToby Isaac   }
19794c274da1SToby Isaac   PetscFunctionReturn(0);
19804c274da1SToby Isaac }
19814c274da1SToby Isaac 
198247c6ae99SBarry Smith /*@
198347c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
198447c6ae99SBarry Smith 
198547c6ae99SBarry Smith     Neighbor-wise Collective on DM
198647c6ae99SBarry Smith 
198747c6ae99SBarry Smith     Input Parameters:
198847c6ae99SBarry Smith +   dm - the DM object
198947c6ae99SBarry Smith .   g - the global vector
199047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
199147c6ae99SBarry Smith -   l - the local vector
199247c6ae99SBarry Smith 
199347c6ae99SBarry Smith 
199447c6ae99SBarry Smith     Level: beginner
199547c6ae99SBarry Smith 
1996e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
199747c6ae99SBarry Smith 
199847c6ae99SBarry Smith @*/
19997087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
200047c6ae99SBarry Smith {
20017128ae9fSMatthew G Knepley   PetscSF                 sf;
200247c6ae99SBarry Smith   PetscErrorCode          ierr;
2003baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
200447c6ae99SBarry Smith 
200547c6ae99SBarry Smith   PetscFunctionBegin;
2006171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2007baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
20088865f1eaSKarl Rupp     if (link->beginhook) {
20098865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
20108865f1eaSKarl Rupp     }
2011baf369e7SPeter Brune   }
20127128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20137128ae9fSMatthew G Knepley   if (sf) {
2014ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2015ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
20167128ae9fSMatthew G Knepley 
201782f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20187128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2019ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20207128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20217128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2022ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20237128ae9fSMatthew G Knepley   } else {
2024843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20257128ae9fSMatthew G Knepley   }
202647c6ae99SBarry Smith   PetscFunctionReturn(0);
202747c6ae99SBarry Smith }
202847c6ae99SBarry Smith 
202947c6ae99SBarry Smith /*@
203047c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
203147c6ae99SBarry Smith 
203247c6ae99SBarry Smith     Neighbor-wise Collective on DM
203347c6ae99SBarry Smith 
203447c6ae99SBarry Smith     Input Parameters:
203547c6ae99SBarry Smith +   dm - the DM object
203647c6ae99SBarry Smith .   g - the global vector
203747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
203847c6ae99SBarry Smith -   l - the local vector
203947c6ae99SBarry Smith 
204047c6ae99SBarry Smith 
204147c6ae99SBarry Smith     Level: beginner
204247c6ae99SBarry Smith 
2043e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
204447c6ae99SBarry Smith 
204547c6ae99SBarry Smith @*/
20467087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
204747c6ae99SBarry Smith {
20487128ae9fSMatthew G Knepley   PetscSF                 sf;
204947c6ae99SBarry Smith   PetscErrorCode          ierr;
2050ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2051ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2052baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
205347c6ae99SBarry Smith 
205447c6ae99SBarry Smith   PetscFunctionBegin;
2055171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20567128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20577128ae9fSMatthew G Knepley   if (sf) {
205882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20597128ae9fSMatthew G Knepley 
20607128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2061ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20627128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20637128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2064ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20657128ae9fSMatthew G Knepley   } else {
2066843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20677128ae9fSMatthew G Knepley   }
20684c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2069baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2070baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2071baf369e7SPeter Brune   }
207247c6ae99SBarry Smith   PetscFunctionReturn(0);
207347c6ae99SBarry Smith }
207447c6ae99SBarry Smith 
2075d4d07f1eSToby Isaac /*@C
2076d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2077d4d07f1eSToby Isaac 
2078d4d07f1eSToby Isaac    Logically Collective
2079d4d07f1eSToby Isaac 
2080d4d07f1eSToby Isaac    Input Arguments:
2081d4d07f1eSToby Isaac +  dm - the DM
2082d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2083d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2084d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2085d4d07f1eSToby Isaac 
2086d4d07f1eSToby Isaac    Calling sequence for beginhook:
2087d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2088d4d07f1eSToby Isaac 
2089d4d07f1eSToby Isaac +  dm - global DM
2090d4d07f1eSToby Isaac .  l - local vector
2091d4d07f1eSToby Isaac .  mode - mode
2092d4d07f1eSToby Isaac .  g - global vector
2093d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2094d4d07f1eSToby Isaac 
2095d4d07f1eSToby Isaac 
2096d4d07f1eSToby Isaac    Calling sequence for endhook:
2097d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2098d4d07f1eSToby Isaac 
2099d4d07f1eSToby Isaac +  global - global DM
2100d4d07f1eSToby Isaac .  l - local vector
2101d4d07f1eSToby Isaac .  mode - mode
2102d4d07f1eSToby Isaac .  g - global vector
2103d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2104d4d07f1eSToby Isaac 
2105d4d07f1eSToby Isaac    Level: advanced
2106d4d07f1eSToby Isaac 
2107d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2108d4d07f1eSToby Isaac @*/
2109d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2110d4d07f1eSToby Isaac {
2111d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2112d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2113d4d07f1eSToby Isaac 
2114d4d07f1eSToby Isaac   PetscFunctionBegin;
2115d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2116d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
211795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2118d4d07f1eSToby Isaac   link->beginhook = beginhook;
2119d4d07f1eSToby Isaac   link->endhook   = endhook;
2120d4d07f1eSToby Isaac   link->ctx       = ctx;
2121d4d07f1eSToby Isaac   link->next      = NULL;
2122d4d07f1eSToby Isaac   *p              = link;
2123d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2124d4d07f1eSToby Isaac }
2125d4d07f1eSToby Isaac 
21264c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
21274c274da1SToby Isaac {
21284c274da1SToby Isaac   Mat cMat;
21294c274da1SToby Isaac   Vec cVec;
21304c274da1SToby Isaac   PetscSection section, cSec;
21314c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21324c274da1SToby Isaac   PetscErrorCode ierr;
21334c274da1SToby Isaac 
21344c274da1SToby Isaac   PetscFunctionBegin;
21354c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21364c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21374c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
21385db9a05bSToby Isaac     PetscInt nRows;
21395db9a05bSToby Isaac 
21405db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21415db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
21424c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
21437711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21444c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21454c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21464c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21474c274da1SToby Isaac       if (dof) {
21484c274da1SToby Isaac         PetscInt d;
21494c274da1SToby Isaac         PetscScalar *vals;
21504c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
21514c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
21524c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
21534c274da1SToby Isaac          * we just extracted */
21544c274da1SToby Isaac         for (d = 0; d < dof; d++) {
21554c274da1SToby Isaac           vals[d] = 0.;
21564c274da1SToby Isaac         }
21574c274da1SToby Isaac       }
21584c274da1SToby Isaac     }
21594c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
21604c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21614c274da1SToby Isaac   }
21624c274da1SToby Isaac   PetscFunctionReturn(0);
21634c274da1SToby Isaac }
21644c274da1SToby Isaac 
216547c6ae99SBarry Smith /*@
21669a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21679a42bb27SBarry Smith 
21689a42bb27SBarry Smith     Neighbor-wise Collective on DM
21699a42bb27SBarry Smith 
21709a42bb27SBarry Smith     Input Parameters:
21719a42bb27SBarry Smith +   dm - the DM object
2172f6813fd5SJed Brown .   l - the local vector
21731eb28f2eSBarry 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.
21741eb28f2eSBarry Smith -   g - the global vector
21759a42bb27SBarry Smith 
2176d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
217784330215SMatthew 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.
21789a42bb27SBarry Smith 
21799a42bb27SBarry Smith     Level: beginner
21809a42bb27SBarry Smith 
2181e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
21829a42bb27SBarry Smith 
21839a42bb27SBarry Smith @*/
21847087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
21859a42bb27SBarry Smith {
21867128ae9fSMatthew G Knepley   PetscSF                 sf;
218784330215SMatthew G. Knepley   PetscSection            s, gs;
2188d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2189ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2190ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
219184330215SMatthew G. Knepley   PetscBool               isInsert;
219284330215SMatthew G. Knepley   PetscErrorCode          ierr;
21939a42bb27SBarry Smith 
21949a42bb27SBarry Smith   PetscFunctionBegin;
2195171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2196d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2197d4d07f1eSToby Isaac     if (link->beginhook) {
2198d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2199d4d07f1eSToby Isaac     }
2200d4d07f1eSToby Isaac   }
22014c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
22027128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
220384330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22047128ae9fSMatthew G Knepley   switch (mode) {
22057128ae9fSMatthew G Knepley   case INSERT_VALUES:
22067128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2207304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
220884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22097128ae9fSMatthew G Knepley   case ADD_VALUES:
22107128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2211304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
221284330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22137128ae9fSMatthew G Knepley   default:
221482f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22157128ae9fSMatthew G Knepley   }
221684330215SMatthew G. Knepley   if (sf && !isInsert) {
2217ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22187128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2219a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2220ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
222184330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
222284330215SMatthew G. Knepley   } else if (s && isInsert) {
222384330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
222484330215SMatthew G. Knepley 
222584330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
222684330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
222784330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2228ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
222984330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
223084330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2231b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
223284330215SMatthew G. Knepley 
223384330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
223403442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
223584330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2236b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
223784330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
223884330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2239b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
224003442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2241b3b16f48SMatthew 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);
2242b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2243b3b16f48SMatthew G. Knepley       if (!gcdof) {
224484330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2245b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2246b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
224784330215SMatthew G. Knepley         const PetscInt *cdofs;
224884330215SMatthew G. Knepley         PetscInt        cind = 0;
224984330215SMatthew G. Knepley 
225084330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2251b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
225284330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2253b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
225484330215SMatthew G. Knepley         }
2255b3b16f48SMatthew 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);
225684330215SMatthew G. Knepley     }
2257ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22587128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
22597128ae9fSMatthew G Knepley   } else {
2260843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22617128ae9fSMatthew G Knepley   }
22629a42bb27SBarry Smith   PetscFunctionReturn(0);
22639a42bb27SBarry Smith }
22649a42bb27SBarry Smith 
22659a42bb27SBarry Smith /*@
22669a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
226747c6ae99SBarry Smith 
226847c6ae99SBarry Smith     Neighbor-wise Collective on DM
226947c6ae99SBarry Smith 
227047c6ae99SBarry Smith     Input Parameters:
227147c6ae99SBarry Smith +   dm - the DM object
2272f6813fd5SJed Brown .   l - the local vector
227347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2274f6813fd5SJed Brown -   g - the global vector
227547c6ae99SBarry Smith 
227647c6ae99SBarry Smith 
227747c6ae99SBarry Smith     Level: beginner
227847c6ae99SBarry Smith 
2279e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
228047c6ae99SBarry Smith 
228147c6ae99SBarry Smith @*/
22827087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
228347c6ae99SBarry Smith {
22847128ae9fSMatthew G Knepley   PetscSF                 sf;
228584330215SMatthew G. Knepley   PetscSection            s;
2286d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
228784330215SMatthew G. Knepley   PetscBool               isInsert;
228884330215SMatthew G. Knepley   PetscErrorCode          ierr;
228947c6ae99SBarry Smith 
229047c6ae99SBarry Smith   PetscFunctionBegin;
2291171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22927128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
229384330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22947128ae9fSMatthew G Knepley   switch (mode) {
22957128ae9fSMatthew G Knepley   case INSERT_VALUES:
22967128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
229784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22987128ae9fSMatthew G Knepley   case ADD_VALUES:
22997128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
230084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23017128ae9fSMatthew G Knepley   default:
230282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23037128ae9fSMatthew G Knepley   }
230484330215SMatthew G. Knepley   if (sf && !isInsert) {
2305ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2306ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
230784330215SMatthew G. Knepley 
2308ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23097128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2310a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2311ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
23127128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
231384330215SMatthew G. Knepley   } else if (s && isInsert) {
23147128ae9fSMatthew G Knepley   } else {
2315843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
23167128ae9fSMatthew G Knepley   }
2317d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2318d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2319d4d07f1eSToby Isaac   }
232047c6ae99SBarry Smith   PetscFunctionReturn(0);
232147c6ae99SBarry Smith }
232247c6ae99SBarry Smith 
2323f089877aSRichard Tran Mills /*@
2324bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2325bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2326d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2327f089877aSRichard Tran Mills 
2328bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2329f089877aSRichard Tran Mills 
2330f089877aSRichard Tran Mills    Input Parameters:
2331f089877aSRichard Tran Mills +  dm - the DM object
2332bc0a1609SRichard Tran Mills .  g - the original local vector
2333bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2334f089877aSRichard Tran Mills 
2335bc0a1609SRichard Tran Mills    Output Parameter:
2336bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2337f089877aSRichard Tran Mills 
2338f089877aSRichard Tran Mills    Level: intermediate
2339f089877aSRichard Tran Mills 
2340bc0a1609SRichard Tran Mills    Notes:
2341bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2342bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2343bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2344bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2345bc0a1609SRichard Tran Mills 
2346bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2347bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2348f089877aSRichard Tran Mills 
2349f089877aSRichard Tran Mills @*/
2350f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2351f089877aSRichard Tran Mills {
2352f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2353f089877aSRichard Tran Mills 
2354f089877aSRichard Tran Mills   PetscFunctionBegin;
2355f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2356f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2357f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2358f089877aSRichard Tran Mills }
2359f089877aSRichard Tran Mills 
2360f089877aSRichard Tran Mills /*@
2361bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2362bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2363d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2364f089877aSRichard Tran Mills 
2365bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2366f089877aSRichard Tran Mills 
2367f089877aSRichard Tran Mills    Input Parameters:
2368bc0a1609SRichard Tran Mills +  da - the DM object
2369bc0a1609SRichard Tran Mills .  g - the original local vector
2370bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2371f089877aSRichard Tran Mills 
2372bc0a1609SRichard Tran Mills    Output Parameter:
2373bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2374f089877aSRichard Tran Mills 
2375f089877aSRichard Tran Mills    Level: intermediate
2376f089877aSRichard Tran Mills 
2377bc0a1609SRichard Tran Mills    Notes:
2378bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2379bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2380bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2381bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2382bc0a1609SRichard Tran Mills 
2383bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2384bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2385f089877aSRichard Tran Mills 
2386f089877aSRichard Tran Mills @*/
2387f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2388f089877aSRichard Tran Mills {
2389f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2390f089877aSRichard Tran Mills 
2391f089877aSRichard Tran Mills   PetscFunctionBegin;
2392f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2393f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2394f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2395f089877aSRichard Tran Mills }
2396f089877aSRichard Tran Mills 
2397f089877aSRichard Tran Mills 
239847c6ae99SBarry Smith /*@
239947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
240047c6ae99SBarry Smith 
240147c6ae99SBarry Smith     Collective on DM
240247c6ae99SBarry Smith 
240347c6ae99SBarry Smith     Input Parameter:
240447c6ae99SBarry Smith +   dm - the DM object
240591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
240647c6ae99SBarry Smith 
240747c6ae99SBarry Smith     Output Parameter:
240847c6ae99SBarry Smith .   dmc - the coarsened DM
240947c6ae99SBarry Smith 
241047c6ae99SBarry Smith     Level: developer
241147c6ae99SBarry Smith 
2412e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
241347c6ae99SBarry Smith 
241447c6ae99SBarry Smith @*/
24157087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
241647c6ae99SBarry Smith {
241747c6ae99SBarry Smith   PetscErrorCode    ierr;
2418b17ce1afSJed Brown   DMCoarsenHookLink link;
241947c6ae99SBarry Smith 
242047c6ae99SBarry Smith   PetscFunctionBegin;
2421171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2422fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
242347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
242447c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
24258892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2426a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
242743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
24288cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2429644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
24300598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2431656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2432e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2433b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2434b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2435b17ce1afSJed Brown   }
243647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2437b17ce1afSJed Brown   PetscFunctionReturn(0);
2438b17ce1afSJed Brown }
2439b17ce1afSJed Brown 
2440bb9467b5SJed Brown /*@C
2441b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2442b17ce1afSJed Brown 
2443b17ce1afSJed Brown    Logically Collective
2444b17ce1afSJed Brown 
2445b17ce1afSJed Brown    Input Arguments:
2446b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2447b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2448b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
24490298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2450b17ce1afSJed Brown 
2451b17ce1afSJed Brown    Calling sequence of coarsenhook:
2452b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2453b17ce1afSJed Brown 
2454b17ce1afSJed Brown +  fine - fine level DM
2455b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2456b17ce1afSJed Brown -  ctx - optional user-defined function context
2457b17ce1afSJed Brown 
2458b17ce1afSJed Brown    Calling sequence for restricthook:
2459c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2460b17ce1afSJed Brown 
2461b17ce1afSJed Brown +  fine - fine level DM
2462b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2463c833c3b5SJed Brown .  rscale - scaling vector for restriction
2464c833c3b5SJed Brown .  inject - matrix restricting by injection
2465b17ce1afSJed Brown .  coarse - coarse level DM to update
2466b17ce1afSJed Brown -  ctx - optional user-defined function context
2467b17ce1afSJed Brown 
2468b17ce1afSJed Brown    Level: advanced
2469b17ce1afSJed Brown 
2470b17ce1afSJed Brown    Notes:
2471b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2472b17ce1afSJed Brown 
2473b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2474b17ce1afSJed Brown 
2475b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2476b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2477b17ce1afSJed Brown 
2478bb9467b5SJed Brown    This function is currently not available from Fortran.
2479bb9467b5SJed Brown 
2480dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2481b17ce1afSJed Brown @*/
2482b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2483b17ce1afSJed Brown {
2484b17ce1afSJed Brown   PetscErrorCode    ierr;
2485b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2486b17ce1afSJed Brown 
2487b17ce1afSJed Brown   PetscFunctionBegin;
2488b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
24891e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
24901e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
24911e3d8eccSJed Brown   }
249295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2493b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2494b17ce1afSJed Brown   link->restricthook = restricthook;
2495b17ce1afSJed Brown   link->ctx          = ctx;
24960298fd71SBarry Smith   link->next         = NULL;
2497b17ce1afSJed Brown   *p                 = link;
2498b17ce1afSJed Brown   PetscFunctionReturn(0);
2499b17ce1afSJed Brown }
2500b17ce1afSJed Brown 
2501dc822a44SJed Brown /*@C
2502dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2503dc822a44SJed Brown 
2504dc822a44SJed Brown    Logically Collective
2505dc822a44SJed Brown 
2506dc822a44SJed Brown    Input Arguments:
2507dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2508dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2509dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2510dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2511dc822a44SJed Brown 
2512dc822a44SJed Brown    Level: advanced
2513dc822a44SJed Brown 
2514dc822a44SJed Brown    Notes:
2515dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2516dc822a44SJed Brown 
2517dc822a44SJed Brown    This function is currently not available from Fortran.
2518dc822a44SJed Brown 
2519dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2520dc822a44SJed Brown @*/
2521dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2522dc822a44SJed Brown {
2523dc822a44SJed Brown   PetscErrorCode    ierr;
2524dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2525dc822a44SJed Brown 
2526dc822a44SJed Brown   PetscFunctionBegin;
2527dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2528dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2529dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2530dc822a44SJed Brown       link = *p;
2531dc822a44SJed Brown       *p = link->next;
2532dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2533dc822a44SJed Brown       break;
2534dc822a44SJed Brown     }
2535dc822a44SJed Brown   }
2536dc822a44SJed Brown   PetscFunctionReturn(0);
2537dc822a44SJed Brown }
2538dc822a44SJed Brown 
2539dc822a44SJed Brown 
2540b17ce1afSJed Brown /*@
2541b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2542b17ce1afSJed Brown 
2543b17ce1afSJed Brown    Collective if any hooks are
2544b17ce1afSJed Brown 
2545b17ce1afSJed Brown    Input Arguments:
2546b17ce1afSJed Brown +  fine - finer DM to use as a base
2547b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2548b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2549b17ce1afSJed Brown -  coarse - coarer DM to update
2550b17ce1afSJed Brown 
2551b17ce1afSJed Brown    Level: developer
2552b17ce1afSJed Brown 
2553b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2554b17ce1afSJed Brown @*/
2555b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2556b17ce1afSJed Brown {
2557b17ce1afSJed Brown   PetscErrorCode    ierr;
2558b17ce1afSJed Brown   DMCoarsenHookLink link;
2559b17ce1afSJed Brown 
2560b17ce1afSJed Brown   PetscFunctionBegin;
2561b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
25628865f1eaSKarl Rupp     if (link->restricthook) {
25638865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
25648865f1eaSKarl Rupp     }
2565b17ce1afSJed Brown   }
256647c6ae99SBarry Smith   PetscFunctionReturn(0);
256747c6ae99SBarry Smith }
256847c6ae99SBarry Smith 
2569bb9467b5SJed Brown /*@C
2570be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
25715dbd56e3SPeter Brune 
25725dbd56e3SPeter Brune    Logically Collective
25735dbd56e3SPeter Brune 
25745dbd56e3SPeter Brune    Input Arguments:
25755dbd56e3SPeter Brune +  global - global DM
2576ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
25775dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
25780298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
25795dbd56e3SPeter Brune 
2580ec4806b8SPeter Brune 
2581ec4806b8SPeter Brune    Calling sequence for ddhook:
2582ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2583ec4806b8SPeter Brune 
2584ec4806b8SPeter Brune +  global - global DM
2585ec4806b8SPeter Brune .  block  - block DM
2586ec4806b8SPeter Brune -  ctx - optional user-defined function context
2587ec4806b8SPeter Brune 
25885dbd56e3SPeter Brune    Calling sequence for restricthook:
2589ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
25905dbd56e3SPeter Brune 
25915dbd56e3SPeter Brune +  global - global DM
25925dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
25935dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2594ec4806b8SPeter Brune .  block  - block DM
25955dbd56e3SPeter Brune -  ctx - optional user-defined function context
25965dbd56e3SPeter Brune 
25975dbd56e3SPeter Brune    Level: advanced
25985dbd56e3SPeter Brune 
25995dbd56e3SPeter Brune    Notes:
2600ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
26015dbd56e3SPeter Brune 
26025dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
26035dbd56e3SPeter Brune 
26045dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2605ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
26065dbd56e3SPeter Brune 
2607bb9467b5SJed Brown    This function is currently not available from Fortran.
2608bb9467b5SJed Brown 
26095dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
26105dbd56e3SPeter Brune @*/
2611be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
26125dbd56e3SPeter Brune {
26135dbd56e3SPeter Brune   PetscErrorCode      ierr;
2614be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
26155dbd56e3SPeter Brune 
26165dbd56e3SPeter Brune   PetscFunctionBegin;
26175dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2618b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2619b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2620b3a6b972SJed Brown   }
262195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
26225dbd56e3SPeter Brune   link->restricthook = restricthook;
2623be081cd6SPeter Brune   link->ddhook       = ddhook;
26245dbd56e3SPeter Brune   link->ctx          = ctx;
26250298fd71SBarry Smith   link->next         = NULL;
26265dbd56e3SPeter Brune   *p                 = link;
26275dbd56e3SPeter Brune   PetscFunctionReturn(0);
26285dbd56e3SPeter Brune }
26295dbd56e3SPeter Brune 
2630b3a6b972SJed Brown /*@C
2631b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2632b3a6b972SJed Brown 
2633b3a6b972SJed Brown    Logically Collective
2634b3a6b972SJed Brown 
2635b3a6b972SJed Brown    Input Arguments:
2636b3a6b972SJed Brown +  global - global DM
2637b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2638b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2639b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2640b3a6b972SJed Brown 
2641b3a6b972SJed Brown    Level: advanced
2642b3a6b972SJed Brown 
2643b3a6b972SJed Brown    Notes:
2644b3a6b972SJed Brown 
2645b3a6b972SJed Brown    This function is currently not available from Fortran.
2646b3a6b972SJed Brown 
2647b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2648b3a6b972SJed Brown @*/
2649b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2650b3a6b972SJed Brown {
2651b3a6b972SJed Brown   PetscErrorCode      ierr;
2652b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2653b3a6b972SJed Brown 
2654b3a6b972SJed Brown   PetscFunctionBegin;
2655b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2656b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2657b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2658b3a6b972SJed Brown       link = *p;
2659b3a6b972SJed Brown       *p = link->next;
2660b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2661b3a6b972SJed Brown       break;
2662b3a6b972SJed Brown     }
2663b3a6b972SJed Brown   }
2664b3a6b972SJed Brown   PetscFunctionReturn(0);
2665b3a6b972SJed Brown }
2666b3a6b972SJed Brown 
26675dbd56e3SPeter Brune /*@
2668be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
26695dbd56e3SPeter Brune 
26705dbd56e3SPeter Brune    Collective if any hooks are
26715dbd56e3SPeter Brune 
26725dbd56e3SPeter Brune    Input Arguments:
26735dbd56e3SPeter Brune +  fine - finer DM to use as a base
2674be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2675be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
26765dbd56e3SPeter Brune -  coarse - coarer DM to update
26775dbd56e3SPeter Brune 
26785dbd56e3SPeter Brune    Level: developer
26795dbd56e3SPeter Brune 
26805dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
26815dbd56e3SPeter Brune @*/
2682be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
26835dbd56e3SPeter Brune {
26845dbd56e3SPeter Brune   PetscErrorCode      ierr;
2685be081cd6SPeter Brune   DMSubDomainHookLink link;
26865dbd56e3SPeter Brune 
26875dbd56e3SPeter Brune   PetscFunctionBegin;
2688be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
26898865f1eaSKarl Rupp     if (link->restricthook) {
26908865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
26918865f1eaSKarl Rupp     }
26925dbd56e3SPeter Brune   }
26935dbd56e3SPeter Brune   PetscFunctionReturn(0);
26945dbd56e3SPeter Brune }
26955dbd56e3SPeter Brune 
26965fe1f584SPeter Brune /*@
26976a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
26985fe1f584SPeter Brune 
26995fe1f584SPeter Brune     Not Collective
27005fe1f584SPeter Brune 
27015fe1f584SPeter Brune     Input Parameter:
27025fe1f584SPeter Brune .   dm - the DM object
27035fe1f584SPeter Brune 
27045fe1f584SPeter Brune     Output Parameter:
27056a7d9d85SPeter Brune .   level - number of coarsenings
27065fe1f584SPeter Brune 
27075fe1f584SPeter Brune     Level: developer
27085fe1f584SPeter Brune 
27096a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
27105fe1f584SPeter Brune 
27115fe1f584SPeter Brune @*/
27125fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
27135fe1f584SPeter Brune {
27145fe1f584SPeter Brune   PetscFunctionBegin;
27155fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27165fe1f584SPeter Brune   *level = dm->leveldown;
27175fe1f584SPeter Brune   PetscFunctionReturn(0);
27185fe1f584SPeter Brune }
27195fe1f584SPeter Brune 
27205fe1f584SPeter Brune 
27215fe1f584SPeter Brune 
272247c6ae99SBarry Smith /*@C
272347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
272447c6ae99SBarry Smith 
272547c6ae99SBarry Smith     Collective on DM
272647c6ae99SBarry Smith 
272747c6ae99SBarry Smith     Input Parameter:
272847c6ae99SBarry Smith +   dm - the DM object
272947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
273047c6ae99SBarry Smith 
273147c6ae99SBarry Smith     Output Parameter:
273247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
273347c6ae99SBarry Smith 
273447c6ae99SBarry Smith     Level: developer
273547c6ae99SBarry Smith 
2736e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
273747c6ae99SBarry Smith 
273847c6ae99SBarry Smith @*/
27397087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
274047c6ae99SBarry Smith {
274147c6ae99SBarry Smith   PetscErrorCode ierr;
274247c6ae99SBarry Smith 
274347c6ae99SBarry Smith   PetscFunctionBegin;
2744171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2745ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
274647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
274747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
274847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
274947c6ae99SBarry Smith   } else if (dm->ops->refine) {
275047c6ae99SBarry Smith     PetscInt i;
275147c6ae99SBarry Smith 
2752ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
275347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2754ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
275547c6ae99SBarry Smith     }
2756ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
275747c6ae99SBarry Smith   PetscFunctionReturn(0);
275847c6ae99SBarry Smith }
275947c6ae99SBarry Smith 
276047c6ae99SBarry Smith /*@C
276147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
276247c6ae99SBarry Smith 
276347c6ae99SBarry Smith     Collective on DM
276447c6ae99SBarry Smith 
276547c6ae99SBarry Smith     Input Parameter:
276647c6ae99SBarry Smith +   dm - the DM object
276747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
276847c6ae99SBarry Smith 
276947c6ae99SBarry Smith     Output Parameter:
277047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
277147c6ae99SBarry Smith 
277247c6ae99SBarry Smith     Level: developer
277347c6ae99SBarry Smith 
2774e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
277547c6ae99SBarry Smith 
277647c6ae99SBarry Smith @*/
27777087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
277847c6ae99SBarry Smith {
277947c6ae99SBarry Smith   PetscErrorCode ierr;
278047c6ae99SBarry Smith 
278147c6ae99SBarry Smith   PetscFunctionBegin;
2782171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2783ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
278447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
278547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
278647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
278747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
278847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
278947c6ae99SBarry Smith     PetscInt i;
279047c6ae99SBarry Smith 
2791ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
279247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2793ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
279447c6ae99SBarry Smith     }
2795ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
279647c6ae99SBarry Smith   PetscFunctionReturn(0);
279747c6ae99SBarry Smith }
279847c6ae99SBarry Smith 
279947c6ae99SBarry Smith /*@
2800e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
280147c6ae99SBarry Smith    grids associated with two DMs.
280247c6ae99SBarry Smith 
280347c6ae99SBarry Smith    Collective on DM
280447c6ae99SBarry Smith 
280547c6ae99SBarry Smith    Input Parameters:
280647c6ae99SBarry Smith +  dmc - the coarse grid DM
280747c6ae99SBarry Smith -  dmf - the fine grid DM
280847c6ae99SBarry Smith 
280947c6ae99SBarry Smith    Output Parameters:
281047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
281147c6ae99SBarry Smith 
281247c6ae99SBarry Smith    Level: intermediate
281347c6ae99SBarry Smith 
281447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
281547c6ae99SBarry Smith 
2816e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
281747c6ae99SBarry Smith @*/
2818e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
281947c6ae99SBarry Smith {
282047c6ae99SBarry Smith   PetscErrorCode ierr;
282147c6ae99SBarry Smith 
282247c6ae99SBarry Smith   PetscFunctionBegin;
2823171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2824171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
282547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
282647c6ae99SBarry Smith   PetscFunctionReturn(0);
282747c6ae99SBarry Smith }
282847c6ae99SBarry Smith 
28291a266240SBarry Smith /*@C
28301a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
28311a266240SBarry Smith 
28321a266240SBarry Smith     Not Collective
28331a266240SBarry Smith 
28341a266240SBarry Smith     Input Parameters:
28351a266240SBarry Smith +   dm - the DM object
28361a266240SBarry Smith -   destroy - the destroy function
28371a266240SBarry Smith 
28381a266240SBarry Smith     Level: intermediate
28391a266240SBarry Smith 
2840e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
28411a266240SBarry Smith 
2842f07f9ceaSJed Brown @*/
28431a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
28441a266240SBarry Smith {
28451a266240SBarry Smith   PetscFunctionBegin;
2846171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28471a266240SBarry Smith   dm->ctxdestroy = destroy;
28481a266240SBarry Smith   PetscFunctionReturn(0);
28491a266240SBarry Smith }
28501a266240SBarry Smith 
2851b07ff414SBarry Smith /*@
28521b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
285347c6ae99SBarry Smith 
285447c6ae99SBarry Smith     Not Collective
285547c6ae99SBarry Smith 
285647c6ae99SBarry Smith     Input Parameters:
285747c6ae99SBarry Smith +   dm - the DM object
285847c6ae99SBarry Smith -   ctx - the user context
285947c6ae99SBarry Smith 
286047c6ae99SBarry Smith     Level: intermediate
286147c6ae99SBarry Smith 
2862e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
286347c6ae99SBarry Smith 
286447c6ae99SBarry Smith @*/
28651b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
286647c6ae99SBarry Smith {
286747c6ae99SBarry Smith   PetscFunctionBegin;
2868171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
286947c6ae99SBarry Smith   dm->ctx = ctx;
287047c6ae99SBarry Smith   PetscFunctionReturn(0);
287147c6ae99SBarry Smith }
287247c6ae99SBarry Smith 
287347c6ae99SBarry Smith /*@
28741b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
287547c6ae99SBarry Smith 
287647c6ae99SBarry Smith     Not Collective
287747c6ae99SBarry Smith 
287847c6ae99SBarry Smith     Input Parameter:
287947c6ae99SBarry Smith .   dm - the DM object
288047c6ae99SBarry Smith 
288147c6ae99SBarry Smith     Output Parameter:
288247c6ae99SBarry Smith .   ctx - the user context
288347c6ae99SBarry Smith 
288447c6ae99SBarry Smith     Level: intermediate
288547c6ae99SBarry Smith 
2886e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
288747c6ae99SBarry Smith 
288847c6ae99SBarry Smith @*/
28891b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
289047c6ae99SBarry Smith {
289147c6ae99SBarry Smith   PetscFunctionBegin;
2892171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28931b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
289447c6ae99SBarry Smith   PetscFunctionReturn(0);
289547c6ae99SBarry Smith }
289647c6ae99SBarry Smith 
289708da532bSDmitry Karpeev /*@C
2898df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
289908da532bSDmitry Karpeev 
290008da532bSDmitry Karpeev     Logically Collective on DM
290108da532bSDmitry Karpeev 
290208da532bSDmitry Karpeev     Input Parameter:
290308da532bSDmitry Karpeev +   dm - the DM object
29040298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
290508da532bSDmitry Karpeev 
290608da532bSDmitry Karpeev     Level: intermediate
290708da532bSDmitry Karpeev 
2908835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
290908da532bSDmitry Karpeev          DMSetJacobian()
291008da532bSDmitry Karpeev 
291108da532bSDmitry Karpeev @*/
291208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
291308da532bSDmitry Karpeev {
291408da532bSDmitry Karpeev   PetscFunctionBegin;
291508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
291608da532bSDmitry Karpeev   PetscFunctionReturn(0);
291708da532bSDmitry Karpeev }
291808da532bSDmitry Karpeev 
291908da532bSDmitry Karpeev /*@
292008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
292108da532bSDmitry Karpeev 
292208da532bSDmitry Karpeev     Not Collective
292308da532bSDmitry Karpeev 
292408da532bSDmitry Karpeev     Input Parameter:
292508da532bSDmitry Karpeev .   dm - the DM object to destroy
292608da532bSDmitry Karpeev 
292708da532bSDmitry Karpeev     Output Parameter:
292808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
292908da532bSDmitry Karpeev 
293008da532bSDmitry Karpeev     Level: developer
293108da532bSDmitry Karpeev 
293274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
293308da532bSDmitry Karpeev 
293408da532bSDmitry Karpeev @*/
293508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
293608da532bSDmitry Karpeev {
293708da532bSDmitry Karpeev   PetscFunctionBegin;
293808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
293908da532bSDmitry Karpeev   PetscFunctionReturn(0);
294008da532bSDmitry Karpeev }
294108da532bSDmitry Karpeev 
294208da532bSDmitry Karpeev /*@C
294308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
294408da532bSDmitry Karpeev 
294508da532bSDmitry Karpeev     Logically Collective on DM
294608da532bSDmitry Karpeev 
294708da532bSDmitry Karpeev     Input Parameters:
2948907376e6SBarry Smith .   dm - the DM object
294908da532bSDmitry Karpeev 
295008da532bSDmitry Karpeev     Output parameters:
295108da532bSDmitry Karpeev +   xl - lower bound
295208da532bSDmitry Karpeev -   xu - upper bound
295308da532bSDmitry Karpeev 
2954907376e6SBarry Smith     Level: advanced
2955907376e6SBarry Smith 
2956907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
295708da532bSDmitry Karpeev 
295874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
295908da532bSDmitry Karpeev 
296008da532bSDmitry Karpeev @*/
296108da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
296208da532bSDmitry Karpeev {
296308da532bSDmitry Karpeev   PetscErrorCode ierr;
29645fd66863SKarl Rupp 
296508da532bSDmitry Karpeev   PetscFunctionBegin;
296608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
296708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
296808da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
296908da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
29708865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
297108da532bSDmitry Karpeev   PetscFunctionReturn(0);
297208da532bSDmitry Karpeev }
297308da532bSDmitry Karpeev 
2974b0ae01b7SPeter Brune /*@
2975b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2976b0ae01b7SPeter Brune 
2977b0ae01b7SPeter Brune     Not Collective
2978b0ae01b7SPeter Brune 
2979b0ae01b7SPeter Brune     Input Parameter:
2980b0ae01b7SPeter Brune .   dm - the DM object
2981b0ae01b7SPeter Brune 
2982b0ae01b7SPeter Brune     Output Parameter:
2983b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2984b0ae01b7SPeter Brune 
2985b0ae01b7SPeter Brune     Level: developer
2986b0ae01b7SPeter Brune 
2987b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2988b0ae01b7SPeter Brune 
2989b0ae01b7SPeter Brune @*/
2990b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2991b0ae01b7SPeter Brune {
2992b0ae01b7SPeter Brune   PetscFunctionBegin;
2993b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2994b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2995b0ae01b7SPeter Brune }
2996b0ae01b7SPeter Brune 
29973ad4599aSBarry Smith /*@
29983ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
29993ad4599aSBarry Smith 
30003ad4599aSBarry Smith     Not Collective
30013ad4599aSBarry Smith 
30023ad4599aSBarry Smith     Input Parameter:
30033ad4599aSBarry Smith .   dm - the DM object
30043ad4599aSBarry Smith 
30053ad4599aSBarry Smith     Output Parameter:
30063ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
30073ad4599aSBarry Smith 
30083ad4599aSBarry Smith     Level: developer
30093ad4599aSBarry Smith 
30103ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
30113ad4599aSBarry Smith 
30123ad4599aSBarry Smith @*/
30133ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
30143ad4599aSBarry Smith {
30153ad4599aSBarry Smith   PetscFunctionBegin;
30163ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
30173ad4599aSBarry Smith   PetscFunctionReturn(0);
30183ad4599aSBarry Smith }
30193ad4599aSBarry Smith 
3020748fac09SDmitry Karpeev /*@C
302108da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
302208da532bSDmitry Karpeev 
302308da532bSDmitry Karpeev     Collective on DM
302408da532bSDmitry Karpeev 
302508da532bSDmitry Karpeev     Input Parameter:
302608da532bSDmitry Karpeev +   dm - the DM object
30270298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
302808da532bSDmitry Karpeev 
302908da532bSDmitry Karpeev     Level: developer
303008da532bSDmitry Karpeev 
303174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
303208da532bSDmitry Karpeev 
303308da532bSDmitry Karpeev @*/
303408da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
303508da532bSDmitry Karpeev {
303608da532bSDmitry Karpeev   PetscErrorCode ierr;
30375fd66863SKarl Rupp 
303808da532bSDmitry Karpeev   PetscFunctionBegin;
303908da532bSDmitry Karpeev   if (x) {
304008da532bSDmitry Karpeev     if (!dm->x) {
304108da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
304208da532bSDmitry Karpeev     }
304308da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
30448865f1eaSKarl Rupp   } else if (dm->x) {
304508da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
304608da532bSDmitry Karpeev   }
304708da532bSDmitry Karpeev   PetscFunctionReturn(0);
304808da532bSDmitry Karpeev }
304908da532bSDmitry Karpeev 
30500298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3051264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3052264ace61SBarry Smith 
3053264ace61SBarry Smith /*@C
3054264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3055264ace61SBarry Smith 
3056264ace61SBarry Smith   Collective on DM
3057264ace61SBarry Smith 
3058264ace61SBarry Smith   Input Parameters:
3059264ace61SBarry Smith + dm     - The DM object
3060264ace61SBarry Smith - method - The name of the DM type
3061264ace61SBarry Smith 
3062264ace61SBarry Smith   Options Database Key:
3063264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3064264ace61SBarry Smith 
3065264ace61SBarry Smith   Notes:
3066e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3067264ace61SBarry Smith 
3068264ace61SBarry Smith   Level: intermediate
3069264ace61SBarry Smith 
3070264ace61SBarry Smith .keywords: DM, set, type
3071264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3072264ace61SBarry Smith @*/
307319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3074264ace61SBarry Smith {
3075264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3076264ace61SBarry Smith   PetscBool      match;
3077264ace61SBarry Smith   PetscErrorCode ierr;
3078264ace61SBarry Smith 
3079264ace61SBarry Smith   PetscFunctionBegin;
3080264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3081251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3082264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3083264ace61SBarry Smith 
30840f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
30851c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3086ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3087264ace61SBarry Smith 
3088264ace61SBarry Smith   if (dm->ops->destroy) {
3089264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
30900298fd71SBarry Smith     dm->ops->destroy = NULL;
3091264ace61SBarry Smith   }
3092264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3093264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3094264ace61SBarry Smith   PetscFunctionReturn(0);
3095264ace61SBarry Smith }
3096264ace61SBarry Smith 
3097264ace61SBarry Smith /*@C
3098264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3099264ace61SBarry Smith 
3100264ace61SBarry Smith   Not Collective
3101264ace61SBarry Smith 
3102264ace61SBarry Smith   Input Parameter:
3103264ace61SBarry Smith . dm  - The DM
3104264ace61SBarry Smith 
3105264ace61SBarry Smith   Output Parameter:
3106264ace61SBarry Smith . type - The DM type name
3107264ace61SBarry Smith 
3108264ace61SBarry Smith   Level: intermediate
3109264ace61SBarry Smith 
3110264ace61SBarry Smith .keywords: DM, get, type, name
3111264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3112264ace61SBarry Smith @*/
311319fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3114264ace61SBarry Smith {
3115264ace61SBarry Smith   PetscErrorCode ierr;
3116264ace61SBarry Smith 
3117264ace61SBarry Smith   PetscFunctionBegin;
3118264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3119c959eef4SJed Brown   PetscValidPointer(type,2);
3120607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3121264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3122264ace61SBarry Smith   PetscFunctionReturn(0);
3123264ace61SBarry Smith }
3124264ace61SBarry Smith 
312567a56275SMatthew G Knepley /*@C
312667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
312767a56275SMatthew G Knepley 
312867a56275SMatthew G Knepley   Collective on DM
312967a56275SMatthew G Knepley 
313067a56275SMatthew G Knepley   Input Parameters:
313167a56275SMatthew G Knepley + dm - the DM
313267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
313367a56275SMatthew G Knepley 
313467a56275SMatthew G Knepley   Output Parameter:
313567a56275SMatthew G Knepley . M - pointer to new DM
313667a56275SMatthew G Knepley 
313767a56275SMatthew G Knepley   Notes:
313867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
313967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
314067a56275SMatthew G Knepley   of the input DM.
314167a56275SMatthew G Knepley 
314267a56275SMatthew G Knepley   Level: intermediate
314367a56275SMatthew G Knepley 
314467a56275SMatthew G Knepley .seealso: DMCreate()
314567a56275SMatthew G Knepley @*/
314619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
314767a56275SMatthew G Knepley {
314867a56275SMatthew G Knepley   DM             B;
314967a56275SMatthew G Knepley   char           convname[256];
3150c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
315167a56275SMatthew G Knepley   PetscErrorCode ierr;
315267a56275SMatthew G Knepley 
315367a56275SMatthew G Knepley   PetscFunctionBegin;
315467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
315567a56275SMatthew G Knepley   PetscValidType(dm,1);
315667a56275SMatthew G Knepley   PetscValidPointer(M,3);
3157251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3158c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3159c067b6caSMatthew G. Knepley   if (sametype) {
3160c067b6caSMatthew G. Knepley     *M   = dm;
3161c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3162c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3163c067b6caSMatthew G. Knepley   } else {
31640298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
316567a56275SMatthew G Knepley 
316667a56275SMatthew G Knepley     /*
316767a56275SMatthew G Knepley        Order of precedence:
316867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
316967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
317067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
317167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
317267a56275SMatthew G Knepley        5) Use a really basic converter.
317367a56275SMatthew G Knepley     */
317467a56275SMatthew G Knepley 
317567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
317667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
317767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
317867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
317967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
318067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
31810005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
318267a56275SMatthew G Knepley     if (conv) goto foundconv;
318367a56275SMatthew G Knepley 
318467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
318582f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
318667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
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)B,convname,&conv);CHKERRQ(ierr);
319367a56275SMatthew G Knepley     if (conv) {
3194fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
319567a56275SMatthew G Knepley       goto foundconv;
319667a56275SMatthew G Knepley     }
319767a56275SMatthew G Knepley 
319867a56275SMatthew G Knepley #if 0
319967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
320067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3201fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
320267a56275SMatthew G Knepley     if (conv) goto foundconv;
320367a56275SMatthew G Knepley 
320467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
320567a56275SMatthew G Knepley     if (dm->ops->convert) {
320667a56275SMatthew G Knepley       conv = dm->ops->convert;
320767a56275SMatthew G Knepley     }
320867a56275SMatthew G Knepley     if (conv) goto foundconv;
320967a56275SMatthew G Knepley #endif
321067a56275SMatthew G Knepley 
321167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
321282f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
321367a56275SMatthew G Knepley 
321467a56275SMatthew G Knepley foundconv:
321567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
321667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
321712fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
321890b157c4SStefano Zampini     {
321990b157c4SStefano Zampini       PetscBool             isper;
322012fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
322112fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
322290b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
322390b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
322412fa691eSMatthew G. Knepley     }
322567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
322667a56275SMatthew G Knepley   }
322767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
322867a56275SMatthew G Knepley   PetscFunctionReturn(0);
322967a56275SMatthew G Knepley }
3230264ace61SBarry Smith 
3231264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3232264ace61SBarry Smith 
3233264ace61SBarry Smith /*@C
32341c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
32351c84c290SBarry Smith 
32361c84c290SBarry Smith   Not Collective
32371c84c290SBarry Smith 
32381c84c290SBarry Smith   Input Parameters:
32391c84c290SBarry Smith + name        - The name of a new user-defined creation routine
32401c84c290SBarry Smith - create_func - The creation routine itself
32411c84c290SBarry Smith 
32421c84c290SBarry Smith   Notes:
32431c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
32441c84c290SBarry Smith 
32451c84c290SBarry Smith 
32461c84c290SBarry Smith   Sample usage:
32471c84c290SBarry Smith .vb
3248bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
32491c84c290SBarry Smith .ve
32501c84c290SBarry Smith 
32511c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
32521c84c290SBarry Smith .vb
32531c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
32541c84c290SBarry Smith     DMSetType(DM,"my_da");
32551c84c290SBarry Smith .ve
32561c84c290SBarry Smith    or at runtime via the option
32571c84c290SBarry Smith .vb
32581c84c290SBarry Smith     -da_type my_da
32591c84c290SBarry Smith .ve
3260264ace61SBarry Smith 
3261264ace61SBarry Smith   Level: advanced
32621c84c290SBarry Smith 
32631c84c290SBarry Smith .keywords: DM, register
3264bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
32651c84c290SBarry Smith 
3266264ace61SBarry Smith @*/
3267bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3268264ace61SBarry Smith {
3269264ace61SBarry Smith   PetscErrorCode ierr;
3270264ace61SBarry Smith 
3271264ace61SBarry Smith   PetscFunctionBegin;
3272a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3273264ace61SBarry Smith   PetscFunctionReturn(0);
3274264ace61SBarry Smith }
3275264ace61SBarry Smith 
3276b859378eSBarry Smith /*@C
327755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3278b859378eSBarry Smith 
3279b859378eSBarry Smith   Collective on PetscViewer
3280b859378eSBarry Smith 
3281b859378eSBarry Smith   Input Parameters:
3282b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3283b859378eSBarry Smith            some related function before a call to DMLoad().
3284b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3285b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3286b859378eSBarry Smith 
3287b859378eSBarry Smith    Level: intermediate
3288b859378eSBarry Smith 
3289b859378eSBarry Smith   Notes:
329055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3291b859378eSBarry Smith 
3292b859378eSBarry Smith   Notes for advanced users:
3293b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3294b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3295b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3296b859378eSBarry Smith   format is
3297b859378eSBarry Smith .vb
3298b859378eSBarry Smith      has not yet been determined
3299b859378eSBarry Smith .ve
3300b859378eSBarry Smith 
3301b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3302b859378eSBarry Smith @*/
3303b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3304b859378eSBarry Smith {
33059331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3306b859378eSBarry Smith   PetscErrorCode ierr;
3307b859378eSBarry Smith 
3308b859378eSBarry Smith   PetscFunctionBegin;
3309b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3310b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
331132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
33129331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
33139331c7a4SMatthew G. Knepley   if (isbinary) {
33149331c7a4SMatthew G. Knepley     PetscInt classid;
33159331c7a4SMatthew G. Knepley     char     type[256];
3316b859378eSBarry Smith 
3317060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
33189200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3319060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
332032c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
33219331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33229331c7a4SMatthew G. Knepley   } else if (ishdf5) {
33239331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33249331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3325b859378eSBarry Smith   PetscFunctionReturn(0);
3326b859378eSBarry Smith }
3327b859378eSBarry Smith 
33287da65231SMatthew G Knepley /******************************** FEM Support **********************************/
33297da65231SMatthew G Knepley 
3330a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3331a6dfd86eSKarl Rupp {
33321d47ebbbSSatish Balay   PetscInt       f;
33331b30c384SMatthew G Knepley   PetscErrorCode ierr;
33341b30c384SMatthew G Knepley 
33357da65231SMatthew G Knepley   PetscFunctionBegin;
333674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33371d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
333857622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
33397da65231SMatthew G Knepley   }
33407da65231SMatthew G Knepley   PetscFunctionReturn(0);
33417da65231SMatthew G Knepley }
33427da65231SMatthew G Knepley 
3343a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3344a6dfd86eSKarl Rupp {
33451b30c384SMatthew G Knepley   PetscInt       f, g;
33467da65231SMatthew G Knepley   PetscErrorCode ierr;
33477da65231SMatthew G Knepley 
33487da65231SMatthew G Knepley   PetscFunctionBegin;
334974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33501d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
335174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
33521d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3353e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
33547da65231SMatthew G Knepley     }
335574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
33567da65231SMatthew G Knepley   }
33577da65231SMatthew G Knepley   PetscFunctionReturn(0);
33587da65231SMatthew G Knepley }
3359e7c4fc90SDmitry Karpeev 
33606113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3361e759306cSMatthew G. Knepley {
33620c5b8624SToby Isaac   PetscInt          localSize, bs;
33630c5b8624SToby Isaac   PetscMPIInt       size;
33640c5b8624SToby Isaac   Vec               x, xglob;
33650c5b8624SToby Isaac   const PetscScalar *xarray;
3366e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3367e759306cSMatthew G. Knepley 
3368e759306cSMatthew G. Knepley   PetscFunctionBegin;
33699852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3370e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3371e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
33726113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
33730c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
33740c5b8624SToby Isaac   if (size > 1) {
33750c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
33760c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
33770c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
33780c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
33790c5b8624SToby Isaac   } else {
33800c5b8624SToby Isaac     xglob = x;
33810c5b8624SToby Isaac   }
33820c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
33830c5b8624SToby Isaac   if (size > 1) {
33840c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
33850c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
33860c5b8624SToby Isaac   }
3387e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3388e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3389e759306cSMatthew G. Knepley }
3390e759306cSMatthew G. Knepley 
339188ed4aceSMatthew G Knepley /*@
339288ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
339388ed4aceSMatthew G Knepley 
339488ed4aceSMatthew G Knepley   Input Parameter:
339588ed4aceSMatthew G Knepley . dm - The DM
339688ed4aceSMatthew G Knepley 
339788ed4aceSMatthew G Knepley   Output Parameter:
339888ed4aceSMatthew G Knepley . section - The PetscSection
339988ed4aceSMatthew G Knepley 
340088ed4aceSMatthew G Knepley   Level: intermediate
340188ed4aceSMatthew G Knepley 
340288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
340388ed4aceSMatthew G Knepley 
340488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
340588ed4aceSMatthew G Knepley @*/
34060adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
34070adebc6cSBarry Smith {
3408fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3409fd59a867SMatthew G. Knepley 
341088ed4aceSMatthew G Knepley   PetscFunctionBegin;
341188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
341288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
34132f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
34142f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3415ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
34162f0f8703SMatthew G. Knepley   }
341788ed4aceSMatthew G Knepley   *section = dm->defaultSection;
341888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
341988ed4aceSMatthew G Knepley }
342088ed4aceSMatthew G Knepley 
342188ed4aceSMatthew G Knepley /*@
342288ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
342388ed4aceSMatthew G Knepley 
342488ed4aceSMatthew G Knepley   Input Parameters:
342588ed4aceSMatthew G Knepley + dm - The DM
342688ed4aceSMatthew G Knepley - section - The PetscSection
342788ed4aceSMatthew G Knepley 
342888ed4aceSMatthew G Knepley   Level: intermediate
342988ed4aceSMatthew G Knepley 
343088ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
343188ed4aceSMatthew G Knepley 
343288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
343388ed4aceSMatthew G Knepley @*/
34340adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
34350adebc6cSBarry Smith {
3436c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3437af122d2aSMatthew G Knepley   PetscInt       f;
343888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
343988ed4aceSMatthew G Knepley 
344088ed4aceSMatthew G Knepley   PetscFunctionBegin;
344188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3442c473ab19SMatthew G. Knepley   if (section) {
34431d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34441d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3445c473ab19SMatthew G. Knepley   }
344688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
344788ed4aceSMatthew G Knepley   dm->defaultSection = section;
3448c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3449af122d2aSMatthew G Knepley   if (numFields) {
3450af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3451af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
34520f21e855SMatthew G. Knepley       PetscObject disc;
3453af122d2aSMatthew G Knepley       const char *name;
3454af122d2aSMatthew G Knepley 
3455af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
34560f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
34570f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3458af122d2aSMatthew G Knepley     }
3459af122d2aSMatthew G Knepley   }
34601d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
34611d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
346288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
346388ed4aceSMatthew G Knepley }
346488ed4aceSMatthew G Knepley 
34659435951eSToby Isaac /*@
34669435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
34679435951eSToby Isaac 
3468e228b242SToby Isaac   not collective
3469e228b242SToby Isaac 
34709435951eSToby Isaac   Input Parameter:
34719435951eSToby Isaac . dm - The DM
34729435951eSToby Isaac 
34739435951eSToby Isaac   Output Parameter:
34749435951eSToby 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.
34759435951eSToby 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.
34769435951eSToby Isaac 
34779435951eSToby Isaac   Level: advanced
34789435951eSToby Isaac 
34799435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
34809435951eSToby Isaac 
34819435951eSToby Isaac .seealso: DMSetDefaultConstraints()
34829435951eSToby Isaac @*/
34839435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
34849435951eSToby Isaac {
34859435951eSToby Isaac   PetscErrorCode ierr;
34869435951eSToby Isaac 
34879435951eSToby Isaac   PetscFunctionBegin;
34889435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34899435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
349045a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
349145a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
34929435951eSToby Isaac   PetscFunctionReturn(0);
34939435951eSToby Isaac }
34949435951eSToby Isaac 
34959435951eSToby Isaac /*@
34969435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
34979435951eSToby Isaac 
34989435951eSToby 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().
34999435951eSToby Isaac 
35009435951eSToby 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.
35019435951eSToby Isaac 
3502e228b242SToby Isaac   collective on dm
3503e228b242SToby Isaac 
35049435951eSToby Isaac   Input Parameters:
35059435951eSToby Isaac + dm - The DM
3506e228b242SToby 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).
3507e228b242SToby 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).
35089435951eSToby Isaac 
35099435951eSToby Isaac   Level: advanced
35109435951eSToby Isaac 
35119435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
35129435951eSToby Isaac 
35139435951eSToby Isaac .seealso: DMGetDefaultConstraints()
35149435951eSToby Isaac @*/
35159435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
35169435951eSToby Isaac {
3517e228b242SToby Isaac   PetscMPIInt result;
35189435951eSToby Isaac   PetscErrorCode ierr;
35199435951eSToby Isaac 
35209435951eSToby Isaac   PetscFunctionBegin;
35219435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3522e228b242SToby Isaac   if (section) {
3523e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3524e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3525f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3526e228b242SToby Isaac   }
3527e228b242SToby Isaac   if (mat) {
3528e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3529e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3530f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3531e228b242SToby Isaac   }
35329435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
35339435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
35349435951eSToby Isaac   dm->defaultConstraintSection = section;
35359435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
35369435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
35379435951eSToby Isaac   dm->defaultConstraintMat = mat;
35389435951eSToby Isaac   PetscFunctionReturn(0);
35399435951eSToby Isaac }
35409435951eSToby Isaac 
3541f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3542507e4973SMatthew G. Knepley /*
3543507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3544507e4973SMatthew G. Knepley 
3545507e4973SMatthew G. Knepley   Input Parameters:
3546507e4973SMatthew G. Knepley + dm - The DM
3547507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3548507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3549507e4973SMatthew G. Knepley 
3550507e4973SMatthew G. Knepley   Level: intermediate
3551507e4973SMatthew G. Knepley 
3552507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3553507e4973SMatthew G. Knepley */
3554f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3555507e4973SMatthew G. Knepley {
3556507e4973SMatthew G. Knepley   MPI_Comm        comm;
3557507e4973SMatthew G. Knepley   PetscLayout     layout;
3558507e4973SMatthew G. Knepley   const PetscInt *ranges;
3559507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3560507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3561507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3562507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3563507e4973SMatthew G. Knepley 
3564507e4973SMatthew G. Knepley   PetscFunctionBegin;
3565507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3566507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3567507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3568507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3569507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3570507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3571507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3572507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3573507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3574507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3575507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3576507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3577f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3578507e4973SMatthew G. Knepley 
3579507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3580507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3581507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3582507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3583507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3584507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3585507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3586507e4973SMatthew 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;}
3587507e4973SMatthew 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;}
3588507e4973SMatthew G. Knepley     if (gdof < 0) {
3589507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3590507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3591507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3592507e4973SMatthew G. Knepley 
3593507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3594507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3595507e4973SMatthew 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;}
3596507e4973SMatthew G. Knepley       }
3597507e4973SMatthew G. Knepley     }
3598507e4973SMatthew G. Knepley   }
3599507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3600507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3601b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3602507e4973SMatthew G. Knepley   if (!gvalid) {
3603507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3604507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3605507e4973SMatthew G. Knepley   }
3606507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3607507e4973SMatthew G. Knepley }
3608f741bcd2SMatthew G. Knepley #endif
3609507e4973SMatthew G. Knepley 
361088ed4aceSMatthew G Knepley /*@
361188ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
361288ed4aceSMatthew G Knepley 
36138b1ab98fSJed Brown   Collective on DM
36148b1ab98fSJed Brown 
361588ed4aceSMatthew G Knepley   Input Parameter:
361688ed4aceSMatthew G Knepley . dm - The DM
361788ed4aceSMatthew G Knepley 
361888ed4aceSMatthew G Knepley   Output Parameter:
361988ed4aceSMatthew G Knepley . section - The PetscSection
362088ed4aceSMatthew G Knepley 
362188ed4aceSMatthew G Knepley   Level: intermediate
362288ed4aceSMatthew G Knepley 
362388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
362488ed4aceSMatthew G Knepley 
362588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
362688ed4aceSMatthew G Knepley @*/
36270adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
36280adebc6cSBarry Smith {
362988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
363088ed4aceSMatthew G Knepley 
363188ed4aceSMatthew G Knepley   PetscFunctionBegin;
363288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
363388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
363488ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3635fd59a867SMatthew G. Knepley     PetscSection s;
3636fd59a867SMatthew G. Knepley 
3637fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3638fd59a867SMatthew 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");
3639fd59a867SMatthew 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");
364015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3641cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3642ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3643685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
364488ed4aceSMatthew G Knepley   }
364588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
364688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
364788ed4aceSMatthew G Knepley }
364888ed4aceSMatthew G Knepley 
3649b21d0597SMatthew G Knepley /*@
3650b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3651b21d0597SMatthew G Knepley 
3652b21d0597SMatthew G Knepley   Input Parameters:
3653b21d0597SMatthew G Knepley + dm - The DM
36545080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3655b21d0597SMatthew G Knepley 
3656b21d0597SMatthew G Knepley   Level: intermediate
3657b21d0597SMatthew G Knepley 
3658b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3659b21d0597SMatthew G Knepley 
3660b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3661b21d0597SMatthew G Knepley @*/
36620adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
36630adebc6cSBarry Smith {
3664b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3665b21d0597SMatthew G Knepley 
3666b21d0597SMatthew G Knepley   PetscFunctionBegin;
3667b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36685080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36691d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3670b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3671b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3672507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3673f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3674507e4973SMatthew G. Knepley #endif
3675b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3676b21d0597SMatthew G Knepley }
3677b21d0597SMatthew G Knepley 
367888ed4aceSMatthew G Knepley /*@
367988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
368088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
368188ed4aceSMatthew G Knepley 
368288ed4aceSMatthew G Knepley   Input Parameter:
368388ed4aceSMatthew G Knepley . dm - The DM
368488ed4aceSMatthew G Knepley 
368588ed4aceSMatthew G Knepley   Output Parameter:
368688ed4aceSMatthew G Knepley . sf - The PetscSF
368788ed4aceSMatthew G Knepley 
368888ed4aceSMatthew G Knepley   Level: intermediate
368988ed4aceSMatthew G Knepley 
369088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
369188ed4aceSMatthew G Knepley 
369288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
369388ed4aceSMatthew G Knepley @*/
36940adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
36950adebc6cSBarry Smith {
369688ed4aceSMatthew G Knepley   PetscInt       nroots;
369788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
369888ed4aceSMatthew G Knepley 
369988ed4aceSMatthew G Knepley   PetscFunctionBegin;
370088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
370188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
37020298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
370388ed4aceSMatthew G Knepley   if (nroots < 0) {
370488ed4aceSMatthew G Knepley     PetscSection section, gSection;
370588ed4aceSMatthew G Knepley 
370688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
370731ea6d37SMatthew G Knepley     if (section) {
370888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
370988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
371031ea6d37SMatthew G Knepley     } else {
37110298fd71SBarry Smith       *sf = NULL;
371231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
371331ea6d37SMatthew G Knepley     }
371488ed4aceSMatthew G Knepley   }
371588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
371688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
371788ed4aceSMatthew G Knepley }
371888ed4aceSMatthew G Knepley 
371988ed4aceSMatthew G Knepley /*@
372088ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
372188ed4aceSMatthew G Knepley 
372288ed4aceSMatthew G Knepley   Input Parameters:
372388ed4aceSMatthew G Knepley + dm - The DM
372488ed4aceSMatthew G Knepley - sf - The PetscSF
372588ed4aceSMatthew G Knepley 
372688ed4aceSMatthew G Knepley   Level: intermediate
372788ed4aceSMatthew G Knepley 
372888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
372988ed4aceSMatthew G Knepley 
373088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
373188ed4aceSMatthew G Knepley @*/
37320adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
37330adebc6cSBarry Smith {
373488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
373588ed4aceSMatthew G Knepley 
373688ed4aceSMatthew G Knepley   PetscFunctionBegin;
373788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
373888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
373988ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
374088ed4aceSMatthew G Knepley   dm->defaultSF = sf;
374188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
374288ed4aceSMatthew G Knepley }
374388ed4aceSMatthew G Knepley 
374488ed4aceSMatthew G Knepley /*@C
374588ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
374688ed4aceSMatthew G Knepley   describing the data layout.
374788ed4aceSMatthew G Knepley 
374888ed4aceSMatthew G Knepley   Input Parameters:
374988ed4aceSMatthew G Knepley + dm - The DM
375088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
375188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
375288ed4aceSMatthew G Knepley 
375388ed4aceSMatthew G Knepley   Level: intermediate
375488ed4aceSMatthew G Knepley 
375588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
375688ed4aceSMatthew G Knepley @*/
375788ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
375888ed4aceSMatthew G Knepley {
375982f516ccSBarry Smith   MPI_Comm       comm;
376088ed4aceSMatthew G Knepley   PetscLayout    layout;
376188ed4aceSMatthew G Knepley   const PetscInt *ranges;
376288ed4aceSMatthew G Knepley   PetscInt       *local;
376388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3764ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
376588ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
376688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
376788ed4aceSMatthew G Knepley 
376888ed4aceSMatthew G Knepley   PetscFunctionBegin;
376982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
377088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
377188ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
377288ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
377388ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
377488ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
377588ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
377688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
377788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
377888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
377988ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3780ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
37816636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
378288ed4aceSMatthew G Knepley 
37836636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37846636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3785235fbf56SMatthew 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));
37866636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
378788ed4aceSMatthew G Knepley   }
3788785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3789785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
379088ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
37911f588964SMatthew G Knepley     const PetscInt *cind;
37926636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
379388ed4aceSMatthew G Knepley 
379488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
379588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
379688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
379788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
379888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37996636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
380088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
38016636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
38026636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
38036636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3804057b4bcdSMatthew 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);
38056636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
38066636e97aSMatthew G Knepley     }
380788ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
380888ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
380988ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
381088ed4aceSMatthew G Knepley     }
381188ed4aceSMatthew G Knepley     if (gdof < 0) {
38126636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
381388ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
381488ed4aceSMatthew G Knepley 
381505376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
381631d3f06eSJed Brown         if (r < 0) r = -(r+2);
381705376888SMatthew 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);
381888ed4aceSMatthew G Knepley         remote[l].rank  = r;
381988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
382088ed4aceSMatthew G Knepley       }
382188ed4aceSMatthew G Knepley     } else {
38226636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
382388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
382488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
382588ed4aceSMatthew G Knepley       }
382688ed4aceSMatthew G Knepley     }
382788ed4aceSMatthew G Knepley   }
38286636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
382988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
383088ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
383188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
383288ed4aceSMatthew G Knepley }
3833af122d2aSMatthew G Knepley 
3834b21d0597SMatthew G Knepley /*@
3835b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3836b21d0597SMatthew G Knepley 
3837b21d0597SMatthew G Knepley   Input Parameter:
3838b21d0597SMatthew G Knepley . dm - The DM
3839b21d0597SMatthew G Knepley 
3840b21d0597SMatthew G Knepley   Output Parameter:
3841b21d0597SMatthew G Knepley . sf - The PetscSF
3842b21d0597SMatthew G Knepley 
3843b21d0597SMatthew G Knepley   Level: intermediate
3844b21d0597SMatthew G Knepley 
3845b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3846b21d0597SMatthew G Knepley 
3847057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3848b21d0597SMatthew G Knepley @*/
38490adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
38500adebc6cSBarry Smith {
3851b21d0597SMatthew G Knepley   PetscFunctionBegin;
3852b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3853b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3854b21d0597SMatthew G Knepley   *sf = dm->sf;
3855b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3856b21d0597SMatthew G Knepley }
3857b21d0597SMatthew G Knepley 
3858057b4bcdSMatthew G Knepley /*@
3859057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3860057b4bcdSMatthew G Knepley 
3861057b4bcdSMatthew G Knepley   Input Parameters:
3862057b4bcdSMatthew G Knepley + dm - The DM
3863057b4bcdSMatthew G Knepley - sf - The PetscSF
3864057b4bcdSMatthew G Knepley 
3865057b4bcdSMatthew G Knepley   Level: intermediate
3866057b4bcdSMatthew G Knepley 
3867057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3868057b4bcdSMatthew G Knepley @*/
38690adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
38700adebc6cSBarry Smith {
3871057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3872057b4bcdSMatthew G Knepley 
3873057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3874057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3875057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3876057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3877057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3878057b4bcdSMatthew G Knepley   dm->sf = sf;
3879057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3880057b4bcdSMatthew G Knepley }
3881057b4bcdSMatthew G Knepley 
38822764a2aaSMatthew G. Knepley /*@
38832764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
38842764a2aaSMatthew G. Knepley 
38852764a2aaSMatthew G. Knepley   Input Parameter:
38862764a2aaSMatthew G. Knepley . dm - The DM
38872764a2aaSMatthew G. Knepley 
38882764a2aaSMatthew G. Knepley   Output Parameter:
38892764a2aaSMatthew G. Knepley . prob - The PetscDS
38902764a2aaSMatthew G. Knepley 
38912764a2aaSMatthew G. Knepley   Level: developer
38922764a2aaSMatthew G. Knepley 
38932764a2aaSMatthew G. Knepley .seealso: DMSetDS()
38942764a2aaSMatthew G. Knepley @*/
38952764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3896af122d2aSMatthew G Knepley {
3897af122d2aSMatthew G Knepley   PetscFunctionBegin;
3898af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38990f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
39000f21e855SMatthew G. Knepley   *prob = dm->prob;
39010f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39020f21e855SMatthew G. Knepley }
39030f21e855SMatthew G. Knepley 
39042764a2aaSMatthew G. Knepley /*@
39052764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
39062764a2aaSMatthew G. Knepley 
39072764a2aaSMatthew G. Knepley   Input Parameters:
39082764a2aaSMatthew G. Knepley + dm - The DM
39092764a2aaSMatthew G. Knepley - prob - The PetscDS
39102764a2aaSMatthew G. Knepley 
39112764a2aaSMatthew G. Knepley   Level: developer
39122764a2aaSMatthew G. Knepley 
39132764a2aaSMatthew G. Knepley .seealso: DMGetDS()
39142764a2aaSMatthew G. Knepley @*/
39152764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
39160f21e855SMatthew G. Knepley {
39170f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39180f21e855SMatthew G. Knepley 
39190f21e855SMatthew G. Knepley   PetscFunctionBegin;
39200f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39212764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
392237316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
39232764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
39240f21e855SMatthew G. Knepley   dm->prob = prob;
39250f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39260f21e855SMatthew G. Knepley }
39270f21e855SMatthew G. Knepley 
39280f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
39290f21e855SMatthew G. Knepley {
39300f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39310f21e855SMatthew G. Knepley 
39320f21e855SMatthew G. Knepley   PetscFunctionBegin;
39330f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39342764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3935af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3936af122d2aSMatthew G Knepley }
3937af122d2aSMatthew G Knepley 
3938af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3939af122d2aSMatthew G Knepley {
39400f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3941af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3942af122d2aSMatthew G Knepley 
3943af122d2aSMatthew G Knepley   PetscFunctionBegin;
3944af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39452764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
39460f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
39470f21e855SMatthew G. Knepley     PetscContainer obj;
39480f21e855SMatthew G. Knepley 
39490f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
39502764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
39510f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3952af122d2aSMatthew G Knepley   }
3953af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3954af122d2aSMatthew G Knepley }
3955af122d2aSMatthew G Knepley 
3956c1929be8SMatthew G. Knepley /*@
3957c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3958c1929be8SMatthew G. Knepley 
3959c1929be8SMatthew G. Knepley   Not collective
3960c1929be8SMatthew G. Knepley 
3961c1929be8SMatthew G. Knepley   Input Parameters:
3962c1929be8SMatthew G. Knepley + dm - The DM
3963c1929be8SMatthew G. Knepley - f  - The field number
3964c1929be8SMatthew G. Knepley 
3965c1929be8SMatthew G. Knepley   Output Parameter:
3966c1929be8SMatthew G. Knepley . field - The discretization object
3967c1929be8SMatthew G. Knepley 
3968c1929be8SMatthew G. Knepley   Level: developer
3969c1929be8SMatthew G. Knepley 
3970c1929be8SMatthew G. Knepley .seealso: DMSetField()
3971c1929be8SMatthew G. Knepley @*/
3972af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3973af122d2aSMatthew G Knepley {
39740f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39750f21e855SMatthew G. Knepley 
3976af122d2aSMatthew G Knepley   PetscFunctionBegin;
3977af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39782764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3979decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3980decb47aaSMatthew G. Knepley }
3981decb47aaSMatthew G. Knepley 
3982c1929be8SMatthew G. Knepley /*@
3983c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3984c1929be8SMatthew G. Knepley 
3985c1929be8SMatthew G. Knepley   Logically collective on DM
3986c1929be8SMatthew G. Knepley 
3987c1929be8SMatthew G. Knepley   Input Parameters:
3988c1929be8SMatthew G. Knepley + dm - The DM
3989c1929be8SMatthew G. Knepley . f  - The field number
3990c1929be8SMatthew G. Knepley - field - The discretization object
3991c1929be8SMatthew G. Knepley 
3992c1929be8SMatthew G. Knepley   Level: developer
3993c1929be8SMatthew G. Knepley 
3994c1929be8SMatthew G. Knepley .seealso: DMGetField()
3995c1929be8SMatthew G. Knepley @*/
3996decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3997decb47aaSMatthew G. Knepley {
3998decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3999decb47aaSMatthew G. Knepley 
4000decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4001decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40022764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4003af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4004af122d2aSMatthew G Knepley }
40056636e97aSMatthew G Knepley 
4006b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4007b64e0483SPeter Brune {
4008b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4009b64e0483SPeter Brune   PetscErrorCode ierr;
4010b64e0483SPeter Brune   Vec coords,ccoords;
40116dbf9973SLawrence Mitchell   Mat inject;
4012b64e0483SPeter Brune   PetscFunctionBegin;
4013b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4014b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4015b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4016b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4017b64e0483SPeter Brune   if (coords && !ccoords) {
4018b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
40196668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
40206dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
40212adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
40226dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4023b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4024b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4025b64e0483SPeter Brune   }
4026b64e0483SPeter Brune   PetscFunctionReturn(0);
4027b64e0483SPeter Brune }
4028b64e0483SPeter Brune 
402903dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
403003dadc2fSPeter Brune {
403103dadc2fSPeter Brune   DM dm_coord,subdm_coord;
403203dadc2fSPeter Brune   PetscErrorCode ierr;
403303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
403403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
403503dadc2fSPeter Brune   PetscFunctionBegin;
403603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
403703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
403803dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
403903dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
404003dadc2fSPeter Brune   if (coords && !ccoords) {
404103dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
40426668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
404303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
404424640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
404503dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
404603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
404703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
404803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
404903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
405003dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
405103dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
405203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
405303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
405403dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
405503dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
405603dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
405703dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
405803dadc2fSPeter Brune   }
405903dadc2fSPeter Brune   PetscFunctionReturn(0);
406003dadc2fSPeter Brune }
406103dadc2fSPeter Brune 
4062c73cfb54SMatthew G. Knepley /*@
4063c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4064c73cfb54SMatthew G. Knepley 
4065c73cfb54SMatthew G. Knepley   Not collective
4066c73cfb54SMatthew G. Knepley 
4067c73cfb54SMatthew G. Knepley   Input Parameter:
4068c73cfb54SMatthew G. Knepley . dm - The DM
4069c73cfb54SMatthew G. Knepley 
4070c73cfb54SMatthew G. Knepley   Output Parameter:
4071c73cfb54SMatthew G. Knepley . dim - The topological dimension
4072c73cfb54SMatthew G. Knepley 
4073c73cfb54SMatthew G. Knepley   Level: beginner
4074c73cfb54SMatthew G. Knepley 
4075c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4076c73cfb54SMatthew G. Knepley @*/
4077c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4078c73cfb54SMatthew G. Knepley {
4079c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4080c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4081c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4082c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4083c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4084c73cfb54SMatthew G. Knepley }
4085c73cfb54SMatthew G. Knepley 
4086c73cfb54SMatthew G. Knepley /*@
4087c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4088c73cfb54SMatthew G. Knepley 
4089c73cfb54SMatthew G. Knepley   Collective on dm
4090c73cfb54SMatthew G. Knepley 
4091c73cfb54SMatthew G. Knepley   Input Parameters:
4092c73cfb54SMatthew G. Knepley + dm - The DM
4093c73cfb54SMatthew G. Knepley - dim - The topological dimension
4094c73cfb54SMatthew G. Knepley 
4095c73cfb54SMatthew G. Knepley   Level: beginner
4096c73cfb54SMatthew G. Knepley 
4097c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4098c73cfb54SMatthew G. Knepley @*/
4099c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4100c73cfb54SMatthew G. Knepley {
4101c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4102c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4103c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4104c73cfb54SMatthew G. Knepley   dm->dim = dim;
4105c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4106c73cfb54SMatthew G. Knepley }
4107c73cfb54SMatthew G. Knepley 
4108793f3fe5SMatthew G. Knepley /*@
4109793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4110793f3fe5SMatthew G. Knepley 
4111793f3fe5SMatthew G. Knepley   Collective on DM
4112793f3fe5SMatthew G. Knepley 
4113793f3fe5SMatthew G. Knepley   Input Parameters:
4114793f3fe5SMatthew G. Knepley + dm - the DM
4115793f3fe5SMatthew G. Knepley - dim - the dimension
4116793f3fe5SMatthew G. Knepley 
4117793f3fe5SMatthew G. Knepley   Output Parameters:
4118793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4119793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4120793f3fe5SMatthew G. Knepley 
4121793f3fe5SMatthew G. Knepley   Note:
4122793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4123793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4124793f3fe5SMatthew G. Knepley   then the interval is empty.
4125793f3fe5SMatthew G. Knepley 
4126793f3fe5SMatthew G. Knepley   Level: intermediate
4127793f3fe5SMatthew G. Knepley 
4128793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4129793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4130793f3fe5SMatthew G. Knepley @*/
4131793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4132793f3fe5SMatthew G. Knepley {
4133793f3fe5SMatthew G. Knepley   PetscInt       d;
4134793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4135793f3fe5SMatthew G. Knepley 
4136793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4137793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4138793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4139793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4140793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4141793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4142793f3fe5SMatthew G. Knepley }
4143793f3fe5SMatthew G. Knepley 
41446636e97aSMatthew G Knepley /*@
41456636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
41466636e97aSMatthew G Knepley 
41476636e97aSMatthew G Knepley   Collective on DM
41486636e97aSMatthew G Knepley 
41496636e97aSMatthew G Knepley   Input Parameters:
41506636e97aSMatthew G Knepley + dm - the DM
41516636e97aSMatthew G Knepley - c - coordinate vector
41526636e97aSMatthew G Knepley 
41536636e97aSMatthew G Knepley   Note:
41546636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
41556636e97aSMatthew G Knepley 
41566636e97aSMatthew G Knepley   Level: intermediate
41576636e97aSMatthew G Knepley 
41586636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41596636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
41606636e97aSMatthew G Knepley @*/
41616636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
41626636e97aSMatthew G Knepley {
41636636e97aSMatthew G Knepley   PetscErrorCode ierr;
41646636e97aSMatthew G Knepley 
41656636e97aSMatthew G Knepley   PetscFunctionBegin;
41666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41676636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41686636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41696636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41706636e97aSMatthew G Knepley   dm->coordinates = c;
41716636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4172b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
417303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
41746636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41756636e97aSMatthew G Knepley }
41766636e97aSMatthew G Knepley 
41776636e97aSMatthew G Knepley /*@
41786636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
41796636e97aSMatthew G Knepley 
41806636e97aSMatthew G Knepley   Collective on DM
41816636e97aSMatthew G Knepley 
41826636e97aSMatthew G Knepley    Input Parameters:
41836636e97aSMatthew G Knepley +  dm - the DM
41846636e97aSMatthew G Knepley -  c - coordinate vector
41856636e97aSMatthew G Knepley 
41866636e97aSMatthew G Knepley   Note:
41876636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
41886636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
41896636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
41906636e97aSMatthew G Knepley 
41916636e97aSMatthew G Knepley   Level: intermediate
41926636e97aSMatthew G Knepley 
41936636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41946636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
41956636e97aSMatthew G Knepley @*/
41966636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
41976636e97aSMatthew G Knepley {
41986636e97aSMatthew G Knepley   PetscErrorCode ierr;
41996636e97aSMatthew G Knepley 
42006636e97aSMatthew G Knepley   PetscFunctionBegin;
42016636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42026636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
42036636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
42046636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
42058865f1eaSKarl Rupp 
42066636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
42078865f1eaSKarl Rupp 
42086636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42096636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42106636e97aSMatthew G Knepley }
42116636e97aSMatthew G Knepley 
42126636e97aSMatthew G Knepley /*@
42136636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
42146636e97aSMatthew G Knepley 
42156636e97aSMatthew G Knepley   Not Collective
42166636e97aSMatthew G Knepley 
42176636e97aSMatthew G Knepley   Input Parameter:
42186636e97aSMatthew G Knepley . dm - the DM
42196636e97aSMatthew G Knepley 
42206636e97aSMatthew G Knepley   Output Parameter:
42216636e97aSMatthew G Knepley . c - global coordinate vector
42226636e97aSMatthew G Knepley 
42236636e97aSMatthew G Knepley   Note:
42246636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42256636e97aSMatthew G Knepley 
42266636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
42276636e97aSMatthew G Knepley 
42286636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42296636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42306636e97aSMatthew G Knepley 
42316636e97aSMatthew G Knepley   Level: intermediate
42326636e97aSMatthew G Knepley 
42336636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42346636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
42356636e97aSMatthew G Knepley @*/
42366636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
42376636e97aSMatthew G Knepley {
42386636e97aSMatthew G Knepley   PetscErrorCode ierr;
42396636e97aSMatthew G Knepley 
42406636e97aSMatthew G Knepley   PetscFunctionBegin;
42416636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42426636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42431f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
42440298fd71SBarry Smith     DM cdm = NULL;
42456636e97aSMatthew G Knepley 
42466636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42476636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
42486636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
42496636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42506636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42516636e97aSMatthew G Knepley   }
42526636e97aSMatthew G Knepley   *c = dm->coordinates;
42536636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42546636e97aSMatthew G Knepley }
42556636e97aSMatthew G Knepley 
42566636e97aSMatthew G Knepley /*@
42576636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
42586636e97aSMatthew G Knepley 
42596636e97aSMatthew G Knepley   Collective on DM
42606636e97aSMatthew G Knepley 
42616636e97aSMatthew G Knepley   Input Parameter:
42626636e97aSMatthew G Knepley . dm - the DM
42636636e97aSMatthew G Knepley 
42646636e97aSMatthew G Knepley   Output Parameter:
42656636e97aSMatthew G Knepley . c - coordinate vector
42666636e97aSMatthew G Knepley 
42676636e97aSMatthew G Knepley   Note:
42686636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42696636e97aSMatthew G Knepley 
42706636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
42716636e97aSMatthew G Knepley 
42726636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42736636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42746636e97aSMatthew G Knepley 
42756636e97aSMatthew G Knepley   Level: intermediate
42766636e97aSMatthew G Knepley 
42776636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42786636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
42796636e97aSMatthew G Knepley @*/
42806636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
42816636e97aSMatthew G Knepley {
42826636e97aSMatthew G Knepley   PetscErrorCode ierr;
42836636e97aSMatthew G Knepley 
42846636e97aSMatthew G Knepley   PetscFunctionBegin;
42856636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42866636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42871f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
42880298fd71SBarry Smith     DM cdm = NULL;
42896636e97aSMatthew G Knepley 
42906636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42916636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
42926636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
42936636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42946636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42956636e97aSMatthew G Knepley   }
42966636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
42976636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42986636e97aSMatthew G Knepley }
42996636e97aSMatthew G Knepley 
43006636e97aSMatthew G Knepley /*@
43011cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
43026636e97aSMatthew G Knepley 
43036636e97aSMatthew G Knepley   Collective on DM
43046636e97aSMatthew G Knepley 
43056636e97aSMatthew G Knepley   Input Parameter:
43066636e97aSMatthew G Knepley . dm - the DM
43076636e97aSMatthew G Knepley 
43086636e97aSMatthew G Knepley   Output Parameter:
43096636e97aSMatthew G Knepley . cdm - coordinate DM
43106636e97aSMatthew G Knepley 
43116636e97aSMatthew G Knepley   Level: intermediate
43126636e97aSMatthew G Knepley 
43136636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43141cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43156636e97aSMatthew G Knepley @*/
43166636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
43176636e97aSMatthew G Knepley {
43186636e97aSMatthew G Knepley   PetscErrorCode ierr;
43196636e97aSMatthew G Knepley 
43206636e97aSMatthew G Knepley   PetscFunctionBegin;
43216636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43226636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
43236636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
432482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
43256636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
43266636e97aSMatthew G Knepley   }
43276636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
43286636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43296636e97aSMatthew G Knepley }
4330e87bb0d3SMatthew G Knepley 
43311cfe2091SMatthew G. Knepley /*@
43321cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
43331cfe2091SMatthew G. Knepley 
43341cfe2091SMatthew G. Knepley   Logically Collective on DM
43351cfe2091SMatthew G. Knepley 
43361cfe2091SMatthew G. Knepley   Input Parameters:
43371cfe2091SMatthew G. Knepley + dm - the DM
43381cfe2091SMatthew G. Knepley - cdm - coordinate DM
43391cfe2091SMatthew G. Knepley 
43401cfe2091SMatthew G. Knepley   Level: intermediate
43411cfe2091SMatthew G. Knepley 
43421cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43431cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43441cfe2091SMatthew G. Knepley @*/
43451cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
43461cfe2091SMatthew G. Knepley {
43471cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
43481cfe2091SMatthew G. Knepley 
43491cfe2091SMatthew G. Knepley   PetscFunctionBegin;
43501cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43511cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4352f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
43531cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
43541cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
43551cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
43561cfe2091SMatthew G. Knepley }
43571cfe2091SMatthew G. Knepley 
435846e270d4SMatthew G. Knepley /*@
435946e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
436046e270d4SMatthew G. Knepley 
436146e270d4SMatthew G. Knepley   Not Collective
436246e270d4SMatthew G. Knepley 
436346e270d4SMatthew G. Knepley   Input Parameter:
436446e270d4SMatthew G. Knepley . dm - The DM object
436546e270d4SMatthew G. Knepley 
436646e270d4SMatthew G. Knepley   Output Parameter:
436746e270d4SMatthew G. Knepley . dim - The embedding dimension
436846e270d4SMatthew G. Knepley 
436946e270d4SMatthew G. Knepley   Level: intermediate
437046e270d4SMatthew G. Knepley 
437146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
437246e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
437346e270d4SMatthew G. Knepley @*/
437446e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
437546e270d4SMatthew G. Knepley {
437646e270d4SMatthew G. Knepley   PetscFunctionBegin;
437746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
437846e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
43799a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
43809a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
43819a9a41abSToby Isaac   }
438246e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
438346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
438446e270d4SMatthew G. Knepley }
438546e270d4SMatthew G. Knepley 
438646e270d4SMatthew G. Knepley /*@
438746e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
438846e270d4SMatthew G. Knepley 
438946e270d4SMatthew G. Knepley   Not Collective
439046e270d4SMatthew G. Knepley 
439146e270d4SMatthew G. Knepley   Input Parameters:
439246e270d4SMatthew G. Knepley + dm  - The DM object
439346e270d4SMatthew G. Knepley - dim - The embedding dimension
439446e270d4SMatthew G. Knepley 
439546e270d4SMatthew G. Knepley   Level: intermediate
439646e270d4SMatthew G. Knepley 
439746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
439846e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
439946e270d4SMatthew G. Knepley @*/
440046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
440146e270d4SMatthew G. Knepley {
440246e270d4SMatthew G. Knepley   PetscFunctionBegin;
440346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
440446e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
440546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
440646e270d4SMatthew G. Knepley }
440746e270d4SMatthew G. Knepley 
4408e8abe2deSMatthew G. Knepley /*@
4409e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4410e8abe2deSMatthew G. Knepley 
4411e8abe2deSMatthew G. Knepley   Not Collective
4412e8abe2deSMatthew G. Knepley 
4413e8abe2deSMatthew G. Knepley   Input Parameter:
4414e8abe2deSMatthew G. Knepley . dm - The DM object
4415e8abe2deSMatthew G. Knepley 
4416e8abe2deSMatthew G. Knepley   Output Parameter:
4417e8abe2deSMatthew G. Knepley . section - The PetscSection object
4418e8abe2deSMatthew G. Knepley 
4419e8abe2deSMatthew G. Knepley   Level: intermediate
4420e8abe2deSMatthew G. Knepley 
4421e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4422e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4423e8abe2deSMatthew G. Knepley @*/
4424e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4425e8abe2deSMatthew G. Knepley {
4426e8abe2deSMatthew G. Knepley   DM             cdm;
4427e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4428e8abe2deSMatthew G. Knepley 
4429e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4430e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4431e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4432e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4433e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4434e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4435e8abe2deSMatthew G. Knepley }
4436e8abe2deSMatthew G. Knepley 
4437e8abe2deSMatthew G. Knepley /*@
4438e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4439e8abe2deSMatthew G. Knepley 
4440e8abe2deSMatthew G. Knepley   Not Collective
4441e8abe2deSMatthew G. Knepley 
4442e8abe2deSMatthew G. Knepley   Input Parameters:
4443e8abe2deSMatthew G. Knepley + dm      - The DM object
444446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4445e8abe2deSMatthew G. Knepley - section - The PetscSection object
4446e8abe2deSMatthew G. Knepley 
4447e8abe2deSMatthew G. Knepley   Level: intermediate
4448e8abe2deSMatthew G. Knepley 
4449e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4450e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4451e8abe2deSMatthew G. Knepley @*/
445246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4453e8abe2deSMatthew G. Knepley {
4454e8abe2deSMatthew G. Knepley   DM             cdm;
4455e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4456e8abe2deSMatthew G. Knepley 
4457e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4458e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
445946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4460e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4461e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
446246e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
44634c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
446446e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
446546e270d4SMatthew G. Knepley 
446646e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
446746e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
446846e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
446946e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
447046e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
447146e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
447246e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
447346e270d4SMatthew G. Knepley     }
44746be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
447546e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
447646e270d4SMatthew G. Knepley   }
4477e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4478e8abe2deSMatthew G. Knepley }
4479e8abe2deSMatthew G. Knepley 
44805dc8c3f7SMatthew G. Knepley /*@C
448190b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
44825dc8c3f7SMatthew G. Knepley 
44835dc8c3f7SMatthew G. Knepley   Input Parameters:
448490b157c4SStefano Zampini . dm      - The DM object
448590b157c4SStefano Zampini 
448690b157c4SStefano Zampini   Output Parameters:
448790b157c4SStefano Zampini + per     - Whether the DM is periodic or not
44885dc8c3f7SMatthew 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
44895dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44905dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44915dc8c3f7SMatthew G. Knepley 
44925dc8c3f7SMatthew G. Knepley   Level: developer
44935dc8c3f7SMatthew G. Knepley 
44945dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44955dc8c3f7SMatthew G. Knepley @*/
449690b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4497c6b900c6SMatthew G. Knepley {
4498c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4499c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
450090b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4501c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4502c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
45035dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4504c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4505c6b900c6SMatthew G. Knepley }
4506c6b900c6SMatthew G. Knepley 
45075dc8c3f7SMatthew G. Knepley /*@C
45085dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
45095dc8c3f7SMatthew G. Knepley 
45105dc8c3f7SMatthew G. Knepley   Input Parameters:
45115dc8c3f7SMatthew G. Knepley + dm      - The DM object
451290b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
45135dc8c3f7SMatthew 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
45145dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45155dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45165dc8c3f7SMatthew G. Knepley 
45175dc8c3f7SMatthew G. Knepley   Level: developer
45185dc8c3f7SMatthew G. Knepley 
45195dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45205dc8c3f7SMatthew G. Knepley @*/
452190b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4522c6b900c6SMatthew G. Knepley {
4523c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4524c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4525c6b900c6SMatthew G. Knepley 
4526c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4527c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
452890b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
452990b157c4SStefano Zampini   if (maxCell) {
453090b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
453190b157c4SStefano Zampini     PetscValidPointer(L,4);
453290b157c4SStefano Zampini     PetscValidPointer(bd,5);
453390b157c4SStefano Zampini   }
45345dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
45355dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
453690b157c4SStefano Zampini   if (maxCell) {
45375dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
45385dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
453990b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
454090b157c4SStefano Zampini   } else {
454190b157c4SStefano Zampini     dm->periodic = per;
454290b157c4SStefano Zampini   }
4543c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4544c6b900c6SMatthew G. Knepley }
4545c6b900c6SMatthew G. Knepley 
45462e17dfb7SMatthew G. Knepley /*@
45472e17dfb7SMatthew 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.
45482e17dfb7SMatthew G. Knepley 
45492e17dfb7SMatthew G. Knepley   Input Parameters:
45502e17dfb7SMatthew G. Knepley + dm     - The DM
455165da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
455265da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
45532e17dfb7SMatthew G. Knepley 
45542e17dfb7SMatthew G. Knepley   Output Parameter:
45552e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45562e17dfb7SMatthew G. Knepley 
45572e17dfb7SMatthew G. Knepley   Level: developer
45582e17dfb7SMatthew G. Knepley 
45592e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45602e17dfb7SMatthew G. Knepley @*/
456165da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
45622e17dfb7SMatthew G. Knepley {
45632e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
45642e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
45652e17dfb7SMatthew G. Knepley 
45662e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45672e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
45682e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45692e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45702e17dfb7SMatthew G. Knepley   } else {
457165da65dcSMatthew G. Knepley     if (endpoint) {
457265da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4573da3333bfSMatthew 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)) {
4574da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
457565da65dcSMatthew G. Knepley         } else {
4576da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
457765da65dcSMatthew G. Knepley         }
457865da65dcSMatthew G. Knepley       }
457965da65dcSMatthew G. Knepley     } else {
45802e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
45811118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
45822e17dfb7SMatthew G. Knepley       }
45832e17dfb7SMatthew G. Knepley     }
458465da65dcSMatthew G. Knepley   }
45852e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45862e17dfb7SMatthew G. Knepley }
45872e17dfb7SMatthew G. Knepley 
45882e17dfb7SMatthew G. Knepley /*
45892e17dfb7SMatthew 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.
45902e17dfb7SMatthew G. Knepley 
45912e17dfb7SMatthew G. Knepley   Input Parameters:
45922e17dfb7SMatthew G. Knepley + dm     - The DM
45932e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
45942e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
45952e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
45962e17dfb7SMatthew G. Knepley 
45972e17dfb7SMatthew G. Knepley   Output Parameter:
45982e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45992e17dfb7SMatthew G. Knepley 
46002e17dfb7SMatthew G. Knepley   Level: developer
46012e17dfb7SMatthew G. Knepley 
46022e17dfb7SMatthew 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
46032e17dfb7SMatthew G. Knepley 
46042e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46052e17dfb7SMatthew G. Knepley */
46062e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46072e17dfb7SMatthew G. Knepley {
46082e17dfb7SMatthew G. Knepley   PetscInt d;
46092e17dfb7SMatthew G. Knepley 
46102e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46112e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46122e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46132e17dfb7SMatthew G. Knepley   } else {
46142e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4615908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
46162e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46172e17dfb7SMatthew G. Knepley       } else {
46182e17dfb7SMatthew G. Knepley         out[d] = in[d];
46192e17dfb7SMatthew G. Knepley       }
46202e17dfb7SMatthew G. Knepley     }
46212e17dfb7SMatthew G. Knepley   }
46222e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46232e17dfb7SMatthew G. Knepley }
46242e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
46252e17dfb7SMatthew G. Knepley {
46262e17dfb7SMatthew G. Knepley   PetscInt d;
46272e17dfb7SMatthew G. Knepley 
46282e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46292e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46302e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46312e17dfb7SMatthew G. Knepley   } else {
46322e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4633908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
46342e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
46352e17dfb7SMatthew G. Knepley       } else {
46362e17dfb7SMatthew G. Knepley         out[d] = in[d];
46372e17dfb7SMatthew G. Knepley       }
46382e17dfb7SMatthew G. Knepley     }
46392e17dfb7SMatthew G. Knepley   }
46402e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46412e17dfb7SMatthew G. Knepley }
46422e17dfb7SMatthew G. Knepley 
46432e17dfb7SMatthew G. Knepley /*
46442e17dfb7SMatthew 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.
46452e17dfb7SMatthew G. Knepley 
46462e17dfb7SMatthew G. Knepley   Input Parameters:
46472e17dfb7SMatthew G. Knepley + dm     - The DM
46482e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
46492e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
46502e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
46512e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
46522e17dfb7SMatthew G. Knepley 
46532e17dfb7SMatthew G. Knepley   Output Parameter:
46542e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
46552e17dfb7SMatthew G. Knepley 
46562e17dfb7SMatthew G. Knepley   Level: developer
46572e17dfb7SMatthew G. Knepley 
46582e17dfb7SMatthew 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
46592e17dfb7SMatthew G. Knepley 
46602e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
46612e17dfb7SMatthew G. Knepley */
46622e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46632e17dfb7SMatthew G. Knepley {
46642e17dfb7SMatthew G. Knepley   PetscInt d;
46652e17dfb7SMatthew G. Knepley 
46662e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46672e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46682e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
46692e17dfb7SMatthew G. Knepley   } else {
46702e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4671908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
46722e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46732e17dfb7SMatthew G. Knepley       } else {
46742e17dfb7SMatthew G. Knepley         out[d] += in[d];
46752e17dfb7SMatthew G. Knepley       }
46762e17dfb7SMatthew G. Knepley     }
46772e17dfb7SMatthew G. Knepley   }
46782e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46792e17dfb7SMatthew G. Knepley }
46802e17dfb7SMatthew G. Knepley 
468136447a5eSToby Isaac /*@
468236447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
468336447a5eSToby Isaac 
468436447a5eSToby Isaac   Input Parameter:
468536447a5eSToby Isaac . dm - The DM
468636447a5eSToby Isaac 
468736447a5eSToby Isaac   Output Parameter:
468836447a5eSToby Isaac   areLocalized - True if localized
468936447a5eSToby Isaac 
469036447a5eSToby Isaac   Level: developer
469136447a5eSToby Isaac 
469236447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
469336447a5eSToby Isaac @*/
469436447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
469536447a5eSToby Isaac {
469636447a5eSToby Isaac   DM             cdm;
469736447a5eSToby Isaac   PetscSection   coordSection;
469836447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
469936447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
470036447a5eSToby Isaac   PetscErrorCode ierr;
470136447a5eSToby Isaac 
470236447a5eSToby Isaac   PetscFunctionBegin;
470336447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
470492c9c85fSStefano Zampini   if (!dm->periodic) {
47058b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
47068b09590cSToby Isaac     PetscFunctionReturn(0);
47078b09590cSToby Isaac   }
470836447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
470936447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
471036447a5eSToby Isaac   {
471136447a5eSToby Isaac     PetscBool isplex;
471236447a5eSToby Isaac 
471336447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
471436447a5eSToby Isaac     if (isplex) {
471536447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
471636447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
471736447a5eSToby Isaac   }
471836447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
471936447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47205a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
472136447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
472236447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
472336447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
472436447a5eSToby Isaac       break;
472536447a5eSToby Isaac     }
472636447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
47275a42012cSToby Isaac     if (dof) {
47285a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
472936447a5eSToby Isaac       break;
473036447a5eSToby Isaac     }
473136447a5eSToby Isaac   }
47325a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
473336447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
473436447a5eSToby Isaac   PetscFunctionReturn(0);
473536447a5eSToby Isaac }
473636447a5eSToby Isaac 
473736447a5eSToby Isaac 
47382e17dfb7SMatthew G. Knepley /*@
47392e17dfb7SMatthew G. Knepley   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for each cell
47402e17dfb7SMatthew G. Knepley 
47412e17dfb7SMatthew G. Knepley   Input Parameter:
47422e17dfb7SMatthew G. Knepley . dm - The DM
47432e17dfb7SMatthew G. Knepley 
47442e17dfb7SMatthew G. Knepley   Level: developer
47452e17dfb7SMatthew G. Knepley 
47462e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
47472e17dfb7SMatthew G. Knepley @*/
47482e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
47492e17dfb7SMatthew G. Knepley {
47502e17dfb7SMatthew G. Knepley   DM             cdm;
47512e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
47522e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
47533e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
47543e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4755e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
47563e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
47573e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
47582e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
47592e17dfb7SMatthew G. Knepley 
47602e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47612e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
476292c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
47632e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
47642e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
47652e17dfb7SMatthew G. Knepley   {
47662e17dfb7SMatthew G. Knepley     PetscBool isplex;
47672e17dfb7SMatthew G. Knepley 
47682e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
47692e17dfb7SMatthew G. Knepley     if (isplex) {
47702e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
47713e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
47723e922f36SToby Isaac       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
47733e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
47743e922f36SToby Isaac       newStart = vStart;
47753e922f36SToby Isaac       newEnd   = vEnd;
47763e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
47773e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
47783e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
47793e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
47803e922f36SToby Isaac       }
47812e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
47822e17dfb7SMatthew G. Knepley   }
47832e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
47842e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
47853e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4786e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47873e922f36SToby Isaac 
47882e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
47892e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
47902e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
47912e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
47923e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
47933e922f36SToby Isaac 
47943e922f36SToby Isaac   ierr = DMGetWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47953e922f36SToby Isaac   localized = &anchor[bs];
47963e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
47973e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
47983e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
47993e922f36SToby Isaac 
48003e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
48013e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
48023e922f36SToby Isaac       PetscInt     b;
48033e922f36SToby Isaac 
48043e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
48053e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48063e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48073e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
48083e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
48093e922f36SToby Isaac         for (b = 0; b < bs; b++) {
48103e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
48113e922f36SToby Isaac         }
48123e922f36SToby Isaac         if (b < bs) break;
48133e922f36SToby Isaac       }
48143e922f36SToby Isaac       if (d < dof/bs) {
48153e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
48163e922f36SToby Isaac           PetscInt cdof;
48173e922f36SToby Isaac 
48183e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
48193e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
48203e922f36SToby Isaac         }
48213e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
48223e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
48233e922f36SToby Isaac       }
48243e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48253e922f36SToby Isaac     }
48263e922f36SToby Isaac   }
48273e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
48283e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
48293e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
48303e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
48313e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
48323e922f36SToby Isaac     PetscFunctionReturn(0);
48333e922f36SToby Isaac   }
48342e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
48352e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
48362e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
48372e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
48382e17dfb7SMatthew G. Knepley   }
48392e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
48402e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4841c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
48422e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
48432e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
48442e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
48452e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4846c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
48472e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
48482e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
48492e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
48502e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
48512e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
48522e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
48532e17dfb7SMatthew G. Knepley   }
48543e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
48553e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
48563e922f36SToby Isaac 
48572e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
48582e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
48593e922f36SToby Isaac       PetscInt     b, cdof;
48602e17dfb7SMatthew G. Knepley 
48613e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
48623e922f36SToby Isaac       if (!cdof) continue;
48632e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48642e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
48652e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48662e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
48672e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48682e17dfb7SMatthew G. Knepley     }
48693e922f36SToby Isaac   }
48703e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
48713e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
4872c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
48732e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
48742e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
48752e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
48762e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
48772e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
48782e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48792e17dfb7SMatthew G. Knepley }
48802e17dfb7SMatthew G. Knepley 
4881e87bb0d3SMatthew G Knepley /*@
48823a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4883e87bb0d3SMatthew G Knepley 
48843a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4885e87bb0d3SMatthew G Knepley 
4886e87bb0d3SMatthew G Knepley   Input Parameters:
4887e87bb0d3SMatthew G Knepley + dm - The DM
48883a93e3b7SToby Isaac . v - The Vec of points
488962a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
48903a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4891e87bb0d3SMatthew G Knepley 
489261e3bb9bSMatthew G Knepley   Output Parameter:
489362a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
489462a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
48953a93e3b7SToby Isaac 
4896e87bb0d3SMatthew G Knepley 
4897e87bb0d3SMatthew G Knepley   Level: developer
489861e3bb9bSMatthew G Knepley 
489962a38674SMatthew G. Knepley   Notes:
49003a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
490162a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
49023a93e3b7SToby Isaac 
49033a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
490462a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
49053a93e3b7SToby Isaac 
49063a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
49073a93e3b7SToby Isaac 
490862a38674SMatthew G. Knepley $    const PetscSFNode *cells;
490962a38674SMatthew G. Knepley $    PetscInt           nFound;
491062a38674SMatthew G. Knepley $    const PetscSFNode *found;
491162a38674SMatthew G. Knepley $
491262a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
49133a93e3b7SToby Isaac 
49143a93e3b7SToby 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
49153a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
49163a93e3b7SToby Isaac 
491761e3bb9bSMatthew G Knepley .keywords: point location, mesh
491862a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
491961e3bb9bSMatthew G Knepley @*/
492062a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4921e87bb0d3SMatthew G Knepley {
4922735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4923735aa83eSMatthew G Knepley 
4924e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4925e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4926e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4927e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
49283a93e3b7SToby Isaac   if (*cellSF) {
49293a93e3b7SToby Isaac     PetscMPIInt result;
49303a93e3b7SToby Isaac 
4931e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
4932a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
49333a93e3b7SToby 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");
4934e0fc9d1bSMatthew G. Knepley   } else {
49353a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
49363a93e3b7SToby Isaac   }
493747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4938735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
493962a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
494082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
494147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4942e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4943e87bb0d3SMatthew G Knepley }
494414f150ffSMatthew G. Knepley 
4945f4d763aaSMatthew G. Knepley /*@
4946f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4947f4d763aaSMatthew G. Knepley 
4948f4d763aaSMatthew G. Knepley   Input Parameter:
4949f4d763aaSMatthew G. Knepley . dm - The original DM
4950f4d763aaSMatthew G. Knepley 
4951f4d763aaSMatthew G. Knepley   Output Parameter:
4952f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4953f4d763aaSMatthew G. Knepley 
4954f4d763aaSMatthew G. Knepley   Level: intermediate
4955f4d763aaSMatthew G. Knepley 
4956f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4957f4d763aaSMatthew G. Knepley @*/
495814f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
495914f150ffSMatthew G. Knepley {
4960c26acbdeSMatthew G. Knepley   PetscSection   section;
49612d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
496214f150ffSMatthew G. Knepley   PetscErrorCode ierr;
496314f150ffSMatthew G. Knepley 
496414f150ffSMatthew G. Knepley   PetscFunctionBegin;
496514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
496614f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4967c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4968c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4969127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
49702d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
4971c26acbdeSMatthew G. Knepley     *odm = dm;
4972c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4973c26acbdeSMatthew G. Knepley   }
497414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
49750305aff6SMatthew G. Knepley     PetscDS      ds;
4976c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
497714f150ffSMatthew G. Knepley     PetscSF      sf;
497814f150ffSMatthew G. Knepley 
497914f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
49800305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
49810305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
498214f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
498314f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
498414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
498514f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
498615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
498714f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
498814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
498914f150ffSMatthew G. Knepley   }
499014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
499114f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
499214f150ffSMatthew G. Knepley }
4993f4d763aaSMatthew G. Knepley 
4994f4d763aaSMatthew G. Knepley /*@
4995cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4996f4d763aaSMatthew G. Knepley 
4997f4d763aaSMatthew G. Knepley   Input Parameter:
4998f4d763aaSMatthew G. Knepley . dm - The original DM
4999f4d763aaSMatthew G. Knepley 
5000cdb7a50dSMatthew G. Knepley   Output Parameters:
5001cdb7a50dSMatthew G. Knepley + num - The output sequence number
5002cdb7a50dSMatthew G. Knepley - val - The output sequence value
5003f4d763aaSMatthew G. Knepley 
5004f4d763aaSMatthew G. Knepley   Level: intermediate
5005f4d763aaSMatthew G. Knepley 
5006f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5007f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5008f4d763aaSMatthew G. Knepley 
5009f4d763aaSMatthew G. Knepley .seealso: VecView()
5010f4d763aaSMatthew G. Knepley @*/
5011cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5012f4d763aaSMatthew G. Knepley {
5013f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5014f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5015cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5016cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5017f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5018f4d763aaSMatthew G. Knepley }
5019f4d763aaSMatthew G. Knepley 
5020f4d763aaSMatthew G. Knepley /*@
5021cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5022f4d763aaSMatthew G. Knepley 
5023f4d763aaSMatthew G. Knepley   Input Parameters:
5024f4d763aaSMatthew G. Knepley + dm - The original DM
5025cdb7a50dSMatthew G. Knepley . num - The output sequence number
5026cdb7a50dSMatthew G. Knepley - val - The output sequence value
5027f4d763aaSMatthew G. Knepley 
5028f4d763aaSMatthew G. Knepley   Level: intermediate
5029f4d763aaSMatthew G. Knepley 
5030f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5031f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5032f4d763aaSMatthew G. Knepley 
5033f4d763aaSMatthew G. Knepley .seealso: VecView()
5034f4d763aaSMatthew G. Knepley @*/
5035cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5036f4d763aaSMatthew G. Knepley {
5037f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5038f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5039f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5040cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5041cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5042cdb7a50dSMatthew G. Knepley }
5043cdb7a50dSMatthew G. Knepley 
5044cdb7a50dSMatthew G. Knepley /*@C
5045cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5046cdb7a50dSMatthew G. Knepley 
5047cdb7a50dSMatthew G. Knepley   Input Parameters:
5048cdb7a50dSMatthew G. Knepley + dm   - The original DM
5049cdb7a50dSMatthew G. Knepley . name - The sequence name
5050cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5051cdb7a50dSMatthew G. Knepley 
5052cdb7a50dSMatthew G. Knepley   Output Parameter:
5053cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5054cdb7a50dSMatthew G. Knepley 
5055cdb7a50dSMatthew G. Knepley   Level: intermediate
5056cdb7a50dSMatthew G. Knepley 
5057cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5058cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5059cdb7a50dSMatthew G. Knepley 
5060cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5061cdb7a50dSMatthew G. Knepley @*/
5062cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5063cdb7a50dSMatthew G. Knepley {
5064cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5065cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5066cdb7a50dSMatthew G. Knepley 
5067cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5068cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5069cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5070cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5071cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5072cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5073cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5074cdb7a50dSMatthew G. Knepley     PetscScalar value;
5075cdb7a50dSMatthew G. Knepley 
507639d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
50774aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5078cdb7a50dSMatthew G. Knepley #endif
5079cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5080f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5081f4d763aaSMatthew G. Knepley }
50828e4ac7eaSMatthew G. Knepley 
50838e4ac7eaSMatthew G. Knepley /*@
50848e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
50858e4ac7eaSMatthew G. Knepley 
50868e4ac7eaSMatthew G. Knepley   Not collective
50878e4ac7eaSMatthew G. Knepley 
50888e4ac7eaSMatthew G. Knepley   Input Parameter:
50898e4ac7eaSMatthew G. Knepley . dm - The DM
50908e4ac7eaSMatthew G. Knepley 
50918e4ac7eaSMatthew G. Knepley   Output Parameter:
50928e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
50938e4ac7eaSMatthew G. Knepley 
50948e4ac7eaSMatthew G. Knepley   Level: beginner
50958e4ac7eaSMatthew G. Knepley 
50968e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
50978e4ac7eaSMatthew G. Knepley @*/
50988e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
50998e4ac7eaSMatthew G. Knepley {
51008e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51018e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51028e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
51038e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
51048e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51058e4ac7eaSMatthew G. Knepley }
51068e4ac7eaSMatthew G. Knepley 
51078e4ac7eaSMatthew G. Knepley /*@
51088e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
51098e4ac7eaSMatthew G. Knepley 
51108e4ac7eaSMatthew G. Knepley   Collective on dm
51118e4ac7eaSMatthew G. Knepley 
51128e4ac7eaSMatthew G. Knepley   Input Parameters:
51138e4ac7eaSMatthew G. Knepley + dm - The DM
51148e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
51158e4ac7eaSMatthew G. Knepley 
51168e4ac7eaSMatthew G. Knepley   Level: beginner
51178e4ac7eaSMatthew G. Knepley 
51188e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
51198e4ac7eaSMatthew G. Knepley @*/
51208e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
51218e4ac7eaSMatthew G. Knepley {
51228e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51238e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51248e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
51258e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
51268e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51278e4ac7eaSMatthew G. Knepley }
5128c58f1c22SToby Isaac 
5129c58f1c22SToby Isaac 
5130c58f1c22SToby Isaac /*@C
5131c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5132c58f1c22SToby Isaac 
5133c58f1c22SToby Isaac   Not Collective
5134c58f1c22SToby Isaac 
5135c58f1c22SToby Isaac   Input Parameters:
5136c58f1c22SToby Isaac + dm   - The DM object
5137c58f1c22SToby Isaac - name - The label name
5138c58f1c22SToby Isaac 
5139c58f1c22SToby Isaac   Level: intermediate
5140c58f1c22SToby Isaac 
5141c58f1c22SToby Isaac .keywords: mesh
5142c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5143c58f1c22SToby Isaac @*/
5144c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5145c58f1c22SToby Isaac {
5146c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5147c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5148c58f1c22SToby Isaac   PetscErrorCode ierr;
5149c58f1c22SToby Isaac 
5150c58f1c22SToby Isaac   PetscFunctionBegin;
5151c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5152c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5153c58f1c22SToby Isaac   while (next) {
5154c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5155c58f1c22SToby Isaac     if (flg) break;
5156c58f1c22SToby Isaac     next = next->next;
5157c58f1c22SToby Isaac   }
5158c58f1c22SToby Isaac   if (!flg) {
5159c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5160c58f1c22SToby Isaac 
5161c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5162c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5163c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5164c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5165c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5166c58f1c22SToby Isaac   }
5167c58f1c22SToby Isaac   PetscFunctionReturn(0);
5168c58f1c22SToby Isaac }
5169c58f1c22SToby Isaac 
5170c58f1c22SToby Isaac /*@C
5171c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5172c58f1c22SToby Isaac 
5173c58f1c22SToby Isaac   Not Collective
5174c58f1c22SToby Isaac 
5175c58f1c22SToby Isaac   Input Parameters:
5176c58f1c22SToby Isaac + dm   - The DM object
5177c58f1c22SToby Isaac . name - The label name
5178c58f1c22SToby Isaac - point - The mesh point
5179c58f1c22SToby Isaac 
5180c58f1c22SToby Isaac   Output Parameter:
5181c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5182c58f1c22SToby Isaac 
5183c58f1c22SToby Isaac   Level: beginner
5184c58f1c22SToby Isaac 
5185c58f1c22SToby Isaac .keywords: mesh
5186c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5187c58f1c22SToby Isaac @*/
5188c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5189c58f1c22SToby Isaac {
5190c58f1c22SToby Isaac   DMLabel        label;
5191c58f1c22SToby Isaac   PetscErrorCode ierr;
5192c58f1c22SToby Isaac 
5193c58f1c22SToby Isaac   PetscFunctionBegin;
5194c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5195c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5196c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
519713903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5198c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5199c58f1c22SToby Isaac   PetscFunctionReturn(0);
5200c58f1c22SToby Isaac }
5201c58f1c22SToby Isaac 
5202c58f1c22SToby Isaac /*@C
5203c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5204c58f1c22SToby Isaac 
5205c58f1c22SToby Isaac   Not Collective
5206c58f1c22SToby Isaac 
5207c58f1c22SToby Isaac   Input Parameters:
5208c58f1c22SToby Isaac + dm   - The DM object
5209c58f1c22SToby Isaac . name - The label name
5210c58f1c22SToby Isaac . point - The mesh point
5211c58f1c22SToby Isaac - value - The label value for this point
5212c58f1c22SToby Isaac 
5213c58f1c22SToby Isaac   Output Parameter:
5214c58f1c22SToby Isaac 
5215c58f1c22SToby Isaac   Level: beginner
5216c58f1c22SToby Isaac 
5217c58f1c22SToby Isaac .keywords: mesh
5218c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5219c58f1c22SToby Isaac @*/
5220c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5221c58f1c22SToby Isaac {
5222c58f1c22SToby Isaac   DMLabel        label;
5223c58f1c22SToby Isaac   PetscErrorCode ierr;
5224c58f1c22SToby Isaac 
5225c58f1c22SToby Isaac   PetscFunctionBegin;
5226c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5227c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5228c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5229c58f1c22SToby Isaac   if (!label) {
5230c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5231c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5232c58f1c22SToby Isaac   }
5233c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5234c58f1c22SToby Isaac   PetscFunctionReturn(0);
5235c58f1c22SToby Isaac }
5236c58f1c22SToby Isaac 
5237c58f1c22SToby Isaac /*@C
5238c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5239c58f1c22SToby Isaac 
5240c58f1c22SToby Isaac   Not Collective
5241c58f1c22SToby Isaac 
5242c58f1c22SToby Isaac   Input Parameters:
5243c58f1c22SToby Isaac + dm   - The DM object
5244c58f1c22SToby Isaac . name - The label name
5245c58f1c22SToby Isaac . point - The mesh point
5246c58f1c22SToby Isaac - value - The label value for this point
5247c58f1c22SToby Isaac 
5248c58f1c22SToby Isaac   Output Parameter:
5249c58f1c22SToby Isaac 
5250c58f1c22SToby Isaac   Level: beginner
5251c58f1c22SToby Isaac 
5252c58f1c22SToby Isaac .keywords: mesh
5253c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5254c58f1c22SToby Isaac @*/
5255c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5256c58f1c22SToby Isaac {
5257c58f1c22SToby Isaac   DMLabel        label;
5258c58f1c22SToby Isaac   PetscErrorCode ierr;
5259c58f1c22SToby Isaac 
5260c58f1c22SToby Isaac   PetscFunctionBegin;
5261c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5262c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5263c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5264c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5265c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5266c58f1c22SToby Isaac   PetscFunctionReturn(0);
5267c58f1c22SToby Isaac }
5268c58f1c22SToby Isaac 
5269c58f1c22SToby Isaac /*@C
5270c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5271c58f1c22SToby Isaac 
5272c58f1c22SToby Isaac   Not Collective
5273c58f1c22SToby Isaac 
5274c58f1c22SToby Isaac   Input Parameters:
5275c58f1c22SToby Isaac + dm   - The DM object
5276c58f1c22SToby Isaac - name - The label name
5277c58f1c22SToby Isaac 
5278c58f1c22SToby Isaac   Output Parameter:
5279c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5280c58f1c22SToby Isaac 
5281c58f1c22SToby Isaac   Level: beginner
5282c58f1c22SToby Isaac 
5283c58f1c22SToby Isaac .keywords: mesh
5284c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5285c58f1c22SToby Isaac @*/
5286c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5287c58f1c22SToby Isaac {
5288c58f1c22SToby Isaac   DMLabel        label;
5289c58f1c22SToby Isaac   PetscErrorCode ierr;
5290c58f1c22SToby Isaac 
5291c58f1c22SToby Isaac   PetscFunctionBegin;
5292c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5293c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5294c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5295c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5296c58f1c22SToby Isaac   *size = 0;
5297c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5298c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5299c58f1c22SToby Isaac   PetscFunctionReturn(0);
5300c58f1c22SToby Isaac }
5301c58f1c22SToby Isaac 
5302c58f1c22SToby Isaac /*@C
5303c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5304c58f1c22SToby Isaac 
5305c58f1c22SToby Isaac   Not Collective
5306c58f1c22SToby Isaac 
5307c58f1c22SToby Isaac   Input Parameters:
5308c58f1c22SToby Isaac + mesh - The DM object
5309c58f1c22SToby Isaac - name - The label name
5310c58f1c22SToby Isaac 
5311c58f1c22SToby Isaac   Output Parameter:
5312c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5313c58f1c22SToby Isaac 
5314c58f1c22SToby Isaac   Level: beginner
5315c58f1c22SToby Isaac 
5316c58f1c22SToby Isaac .keywords: mesh
5317c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5318c58f1c22SToby Isaac @*/
5319c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5320c58f1c22SToby Isaac {
5321c58f1c22SToby Isaac   DMLabel        label;
5322c58f1c22SToby Isaac   PetscErrorCode ierr;
5323c58f1c22SToby Isaac 
5324c58f1c22SToby Isaac   PetscFunctionBegin;
5325c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5326c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5327c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5328c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5329c58f1c22SToby Isaac   *ids = NULL;
5330c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5331c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5332c58f1c22SToby Isaac   PetscFunctionReturn(0);
5333c58f1c22SToby Isaac }
5334c58f1c22SToby Isaac 
5335c58f1c22SToby Isaac /*@C
5336c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5337c58f1c22SToby Isaac 
5338c58f1c22SToby Isaac   Not Collective
5339c58f1c22SToby Isaac 
5340c58f1c22SToby Isaac   Input Parameters:
5341c58f1c22SToby Isaac + dm - The DM object
5342c58f1c22SToby Isaac . name - The label name
5343c58f1c22SToby Isaac - value - The stratum value
5344c58f1c22SToby Isaac 
5345c58f1c22SToby Isaac   Output Parameter:
5346c58f1c22SToby Isaac . size - The stratum size
5347c58f1c22SToby Isaac 
5348c58f1c22SToby Isaac   Level: beginner
5349c58f1c22SToby Isaac 
5350c58f1c22SToby Isaac .keywords: mesh
5351c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5352c58f1c22SToby Isaac @*/
5353c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5354c58f1c22SToby Isaac {
5355c58f1c22SToby Isaac   DMLabel        label;
5356c58f1c22SToby Isaac   PetscErrorCode ierr;
5357c58f1c22SToby Isaac 
5358c58f1c22SToby Isaac   PetscFunctionBegin;
5359c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5360c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5361c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5362c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5363c58f1c22SToby Isaac   *size = 0;
5364c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5365c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5366c58f1c22SToby Isaac   PetscFunctionReturn(0);
5367c58f1c22SToby Isaac }
5368c58f1c22SToby Isaac 
5369c58f1c22SToby Isaac /*@C
5370c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5371c58f1c22SToby Isaac 
5372c58f1c22SToby Isaac   Not Collective
5373c58f1c22SToby Isaac 
5374c58f1c22SToby Isaac   Input Parameters:
5375c58f1c22SToby Isaac + dm - The DM object
5376c58f1c22SToby Isaac . name - The label name
5377c58f1c22SToby Isaac - value - The stratum value
5378c58f1c22SToby Isaac 
5379c58f1c22SToby Isaac   Output Parameter:
5380c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5381c58f1c22SToby Isaac 
5382c58f1c22SToby Isaac   Level: beginner
5383c58f1c22SToby Isaac 
5384c58f1c22SToby Isaac .keywords: mesh
5385c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5386c58f1c22SToby Isaac @*/
5387c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5388c58f1c22SToby Isaac {
5389c58f1c22SToby Isaac   DMLabel        label;
5390c58f1c22SToby Isaac   PetscErrorCode ierr;
5391c58f1c22SToby Isaac 
5392c58f1c22SToby Isaac   PetscFunctionBegin;
5393c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5394c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5395c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5396c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5397c58f1c22SToby Isaac   *points = NULL;
5398c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5399c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5400c58f1c22SToby Isaac   PetscFunctionReturn(0);
5401c58f1c22SToby Isaac }
5402c58f1c22SToby Isaac 
54034de306b1SToby Isaac /*@C
54044de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
54054de306b1SToby Isaac 
54064de306b1SToby Isaac   Not Collective
54074de306b1SToby Isaac 
54084de306b1SToby Isaac   Input Parameters:
54094de306b1SToby Isaac + dm - The DM object
54104de306b1SToby Isaac . name - The label name
54114de306b1SToby Isaac . value - The stratum value
54124de306b1SToby Isaac - points - The stratum points
54134de306b1SToby Isaac 
54144de306b1SToby Isaac   Level: beginner
54154de306b1SToby Isaac 
54164de306b1SToby Isaac .keywords: mesh
54174de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
54184de306b1SToby Isaac @*/
54194de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
54204de306b1SToby Isaac {
54214de306b1SToby Isaac   DMLabel        label;
54224de306b1SToby Isaac   PetscErrorCode ierr;
54234de306b1SToby Isaac 
54244de306b1SToby Isaac   PetscFunctionBegin;
54254de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54264de306b1SToby Isaac   PetscValidCharPointer(name, 2);
54274de306b1SToby Isaac   PetscValidPointer(points, 4);
54284de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
54294de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
54304de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
54314de306b1SToby Isaac   PetscFunctionReturn(0);
54324de306b1SToby Isaac }
54334de306b1SToby Isaac 
5434c58f1c22SToby Isaac /*@C
5435c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5436c58f1c22SToby Isaac 
5437c58f1c22SToby Isaac   Not Collective
5438c58f1c22SToby Isaac 
5439c58f1c22SToby Isaac   Input Parameters:
5440c58f1c22SToby Isaac + dm   - The DM object
5441c58f1c22SToby Isaac . name - The label name
5442c58f1c22SToby Isaac - value - The label value for this point
5443c58f1c22SToby Isaac 
5444c58f1c22SToby Isaac   Output Parameter:
5445c58f1c22SToby Isaac 
5446c58f1c22SToby Isaac   Level: beginner
5447c58f1c22SToby Isaac 
5448c58f1c22SToby Isaac .keywords: mesh
5449c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5450c58f1c22SToby Isaac @*/
5451c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5452c58f1c22SToby Isaac {
5453c58f1c22SToby Isaac   DMLabel        label;
5454c58f1c22SToby Isaac   PetscErrorCode ierr;
5455c58f1c22SToby Isaac 
5456c58f1c22SToby Isaac   PetscFunctionBegin;
5457c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5458c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5459c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5460c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5461c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5462c58f1c22SToby Isaac   PetscFunctionReturn(0);
5463c58f1c22SToby Isaac }
5464c58f1c22SToby Isaac 
5465c58f1c22SToby Isaac /*@
5466c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5467c58f1c22SToby Isaac 
5468c58f1c22SToby Isaac   Not Collective
5469c58f1c22SToby Isaac 
5470c58f1c22SToby Isaac   Input Parameter:
5471c58f1c22SToby Isaac . dm   - The DM object
5472c58f1c22SToby Isaac 
5473c58f1c22SToby Isaac   Output Parameter:
5474c58f1c22SToby Isaac . numLabels - the number of Labels
5475c58f1c22SToby Isaac 
5476c58f1c22SToby Isaac   Level: intermediate
5477c58f1c22SToby Isaac 
5478c58f1c22SToby Isaac .keywords: mesh
5479c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5480c58f1c22SToby Isaac @*/
5481c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5482c58f1c22SToby Isaac {
5483c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5484c58f1c22SToby Isaac   PetscInt  n    = 0;
5485c58f1c22SToby Isaac 
5486c58f1c22SToby Isaac   PetscFunctionBegin;
5487c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5488c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5489c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5490c58f1c22SToby Isaac   *numLabels = n;
5491c58f1c22SToby Isaac   PetscFunctionReturn(0);
5492c58f1c22SToby Isaac }
5493c58f1c22SToby Isaac 
5494c58f1c22SToby Isaac /*@C
5495c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5496c58f1c22SToby Isaac 
5497c58f1c22SToby Isaac   Not Collective
5498c58f1c22SToby Isaac 
5499c58f1c22SToby Isaac   Input Parameters:
5500c58f1c22SToby Isaac + dm - The DM object
5501c58f1c22SToby Isaac - n  - the label number
5502c58f1c22SToby Isaac 
5503c58f1c22SToby Isaac   Output Parameter:
5504c58f1c22SToby Isaac . name - the label name
5505c58f1c22SToby Isaac 
5506c58f1c22SToby Isaac   Level: intermediate
5507c58f1c22SToby Isaac 
5508c58f1c22SToby Isaac .keywords: mesh
5509c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5510c58f1c22SToby Isaac @*/
5511c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5512c58f1c22SToby Isaac {
5513c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5514c58f1c22SToby Isaac   PetscInt  l    = 0;
5515c58f1c22SToby Isaac 
5516c58f1c22SToby Isaac   PetscFunctionBegin;
5517c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5518c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5519c58f1c22SToby Isaac   while (next) {
5520c58f1c22SToby Isaac     if (l == n) {
5521c58f1c22SToby Isaac       *name = next->label->name;
5522c58f1c22SToby Isaac       PetscFunctionReturn(0);
5523c58f1c22SToby Isaac     }
5524c58f1c22SToby Isaac     ++l;
5525c58f1c22SToby Isaac     next = next->next;
5526c58f1c22SToby Isaac   }
5527c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5528c58f1c22SToby Isaac }
5529c58f1c22SToby Isaac 
5530c58f1c22SToby Isaac /*@C
5531c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5532c58f1c22SToby Isaac 
5533c58f1c22SToby Isaac   Not Collective
5534c58f1c22SToby Isaac 
5535c58f1c22SToby Isaac   Input Parameters:
5536c58f1c22SToby Isaac + dm   - The DM object
5537c58f1c22SToby Isaac - name - The label name
5538c58f1c22SToby Isaac 
5539c58f1c22SToby Isaac   Output Parameter:
5540c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5541c58f1c22SToby Isaac 
5542c58f1c22SToby Isaac   Level: intermediate
5543c58f1c22SToby Isaac 
5544c58f1c22SToby Isaac .keywords: mesh
5545c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5546c58f1c22SToby Isaac @*/
5547c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5548c58f1c22SToby Isaac {
5549c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5550c58f1c22SToby Isaac   PetscErrorCode ierr;
5551c58f1c22SToby Isaac 
5552c58f1c22SToby Isaac   PetscFunctionBegin;
5553c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5554c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5555c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5556c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5557c58f1c22SToby Isaac   while (next) {
5558c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5559c58f1c22SToby Isaac     if (*hasLabel) break;
5560c58f1c22SToby Isaac     next = next->next;
5561c58f1c22SToby Isaac   }
5562c58f1c22SToby Isaac   PetscFunctionReturn(0);
5563c58f1c22SToby Isaac }
5564c58f1c22SToby Isaac 
5565c58f1c22SToby Isaac /*@C
5566c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5567c58f1c22SToby Isaac 
5568c58f1c22SToby Isaac   Not Collective
5569c58f1c22SToby Isaac 
5570c58f1c22SToby Isaac   Input Parameters:
5571c58f1c22SToby Isaac + dm   - The DM object
5572c58f1c22SToby Isaac - name - The label name
5573c58f1c22SToby Isaac 
5574c58f1c22SToby Isaac   Output Parameter:
5575c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5576c58f1c22SToby Isaac 
5577c58f1c22SToby Isaac   Level: intermediate
5578c58f1c22SToby Isaac 
5579c58f1c22SToby Isaac .keywords: mesh
5580c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5581c58f1c22SToby Isaac @*/
5582c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5583c58f1c22SToby Isaac {
5584c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5585c58f1c22SToby Isaac   PetscBool      hasLabel;
5586c58f1c22SToby Isaac   PetscErrorCode ierr;
5587c58f1c22SToby Isaac 
5588c58f1c22SToby Isaac   PetscFunctionBegin;
5589c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5590c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5591c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5592c58f1c22SToby Isaac   *label = NULL;
5593c58f1c22SToby Isaac   while (next) {
5594c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5595c58f1c22SToby Isaac     if (hasLabel) {
5596c58f1c22SToby Isaac       *label = next->label;
5597c58f1c22SToby Isaac       break;
5598c58f1c22SToby Isaac     }
5599c58f1c22SToby Isaac     next = next->next;
5600c58f1c22SToby Isaac   }
5601c58f1c22SToby Isaac   PetscFunctionReturn(0);
5602c58f1c22SToby Isaac }
5603c58f1c22SToby Isaac 
5604c58f1c22SToby Isaac /*@C
5605c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5606c58f1c22SToby Isaac 
5607c58f1c22SToby Isaac   Not Collective
5608c58f1c22SToby Isaac 
5609c58f1c22SToby Isaac   Input Parameters:
5610c58f1c22SToby Isaac + dm - The DM object
5611c58f1c22SToby Isaac - n  - the label number
5612c58f1c22SToby Isaac 
5613c58f1c22SToby Isaac   Output Parameter:
5614c58f1c22SToby Isaac . label - the label
5615c58f1c22SToby Isaac 
5616c58f1c22SToby Isaac   Level: intermediate
5617c58f1c22SToby Isaac 
5618c58f1c22SToby Isaac .keywords: mesh
5619c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5620c58f1c22SToby Isaac @*/
5621c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5622c58f1c22SToby Isaac {
5623c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5624c58f1c22SToby Isaac   PetscInt    l    = 0;
5625c58f1c22SToby Isaac 
5626c58f1c22SToby Isaac   PetscFunctionBegin;
5627c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5628c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5629c58f1c22SToby Isaac   while (next) {
5630c58f1c22SToby Isaac     if (l == n) {
5631c58f1c22SToby Isaac       *label = next->label;
5632c58f1c22SToby Isaac       PetscFunctionReturn(0);
5633c58f1c22SToby Isaac     }
5634c58f1c22SToby Isaac     ++l;
5635c58f1c22SToby Isaac     next = next->next;
5636c58f1c22SToby Isaac   }
5637c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5638c58f1c22SToby Isaac }
5639c58f1c22SToby Isaac 
5640c58f1c22SToby Isaac /*@C
5641c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5642c58f1c22SToby Isaac 
5643c58f1c22SToby Isaac   Not Collective
5644c58f1c22SToby Isaac 
5645c58f1c22SToby Isaac   Input Parameters:
5646c58f1c22SToby Isaac + dm   - The DM object
5647c58f1c22SToby Isaac - label - The DMLabel
5648c58f1c22SToby Isaac 
5649c58f1c22SToby Isaac   Level: developer
5650c58f1c22SToby Isaac 
5651c58f1c22SToby Isaac .keywords: mesh
5652c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5653c58f1c22SToby Isaac @*/
5654c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5655c58f1c22SToby Isaac {
5656c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5657c58f1c22SToby Isaac   PetscBool      hasLabel;
5658c58f1c22SToby Isaac   PetscErrorCode ierr;
5659c58f1c22SToby Isaac 
5660c58f1c22SToby Isaac   PetscFunctionBegin;
5661c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5662c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5663c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5664c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5665c58f1c22SToby Isaac   tmpLabel->label  = label;
5666c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5667c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5668c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5669c58f1c22SToby Isaac   PetscFunctionReturn(0);
5670c58f1c22SToby Isaac }
5671c58f1c22SToby Isaac 
5672c58f1c22SToby Isaac /*@C
5673c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5674c58f1c22SToby Isaac 
5675c58f1c22SToby Isaac   Not Collective
5676c58f1c22SToby Isaac 
5677c58f1c22SToby Isaac   Input Parameters:
5678c58f1c22SToby Isaac + dm   - The DM object
5679c58f1c22SToby Isaac - name - The label name
5680c58f1c22SToby Isaac 
5681c58f1c22SToby Isaac   Output Parameter:
5682c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5683c58f1c22SToby Isaac 
5684c58f1c22SToby Isaac   Level: developer
5685c58f1c22SToby Isaac 
5686c58f1c22SToby Isaac .keywords: mesh
5687c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5688c58f1c22SToby Isaac @*/
5689c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5690c58f1c22SToby Isaac {
5691c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5692c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5693c58f1c22SToby Isaac   PetscBool      hasLabel;
5694c58f1c22SToby Isaac   PetscErrorCode ierr;
5695c58f1c22SToby Isaac 
5696c58f1c22SToby Isaac   PetscFunctionBegin;
5697c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5698c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5699c58f1c22SToby Isaac   *label = NULL;
5700c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5701c58f1c22SToby Isaac   while (next) {
5702c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5703c58f1c22SToby Isaac     if (hasLabel) {
5704c58f1c22SToby Isaac       if (last) last->next       = next->next;
5705c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5706c58f1c22SToby Isaac       next->next = NULL;
5707c58f1c22SToby Isaac       *label     = next->label;
5708c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5709c58f1c22SToby Isaac       if (hasLabel) {
5710c58f1c22SToby Isaac         dm->depthLabel = NULL;
5711c58f1c22SToby Isaac       }
5712c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5713c58f1c22SToby Isaac       break;
5714c58f1c22SToby Isaac     }
5715c58f1c22SToby Isaac     last = next;
5716c58f1c22SToby Isaac     next = next->next;
5717c58f1c22SToby Isaac   }
5718c58f1c22SToby Isaac   PetscFunctionReturn(0);
5719c58f1c22SToby Isaac }
5720c58f1c22SToby Isaac 
5721c58f1c22SToby Isaac /*@C
5722c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5723c58f1c22SToby Isaac 
5724c58f1c22SToby Isaac   Not Collective
5725c58f1c22SToby Isaac 
5726c58f1c22SToby Isaac   Input Parameters:
5727c58f1c22SToby Isaac + dm   - The DM object
5728c58f1c22SToby Isaac - name - The label name
5729c58f1c22SToby Isaac 
5730c58f1c22SToby Isaac   Output Parameter:
5731c58f1c22SToby Isaac . output - The flag for output
5732c58f1c22SToby Isaac 
5733c58f1c22SToby Isaac   Level: developer
5734c58f1c22SToby Isaac 
5735c58f1c22SToby Isaac .keywords: mesh
5736c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5737c58f1c22SToby Isaac @*/
5738c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5739c58f1c22SToby Isaac {
5740c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5741c58f1c22SToby Isaac   PetscErrorCode ierr;
5742c58f1c22SToby Isaac 
5743c58f1c22SToby Isaac   PetscFunctionBegin;
5744c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5745c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5746c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5747c58f1c22SToby Isaac   while (next) {
5748c58f1c22SToby Isaac     PetscBool flg;
5749c58f1c22SToby Isaac 
5750c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5751c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5752c58f1c22SToby Isaac     next = next->next;
5753c58f1c22SToby Isaac   }
5754c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5755c58f1c22SToby Isaac }
5756c58f1c22SToby Isaac 
5757c58f1c22SToby Isaac /*@C
5758c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5759c58f1c22SToby Isaac 
5760c58f1c22SToby Isaac   Not Collective
5761c58f1c22SToby Isaac 
5762c58f1c22SToby Isaac   Input Parameters:
5763c58f1c22SToby Isaac + dm     - The DM object
5764c58f1c22SToby Isaac . name   - The label name
5765c58f1c22SToby Isaac - output - The flag for output
5766c58f1c22SToby Isaac 
5767c58f1c22SToby Isaac   Level: developer
5768c58f1c22SToby Isaac 
5769c58f1c22SToby Isaac .keywords: mesh
5770c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5771c58f1c22SToby Isaac @*/
5772c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5773c58f1c22SToby Isaac {
5774c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5775c58f1c22SToby Isaac   PetscErrorCode ierr;
5776c58f1c22SToby Isaac 
5777c58f1c22SToby Isaac   PetscFunctionBegin;
5778c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5779c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5780c58f1c22SToby Isaac   while (next) {
5781c58f1c22SToby Isaac     PetscBool flg;
5782c58f1c22SToby Isaac 
5783c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5784c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5785c58f1c22SToby Isaac     next = next->next;
5786c58f1c22SToby Isaac   }
5787c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5788c58f1c22SToby Isaac }
5789c58f1c22SToby Isaac 
5790c58f1c22SToby Isaac 
5791c58f1c22SToby Isaac /*@
5792c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5793c58f1c22SToby Isaac 
5794c58f1c22SToby Isaac   Collective on DM
5795c58f1c22SToby Isaac 
5796c58f1c22SToby Isaac   Input Parameter:
57972e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5798c58f1c22SToby Isaac 
5799c58f1c22SToby Isaac   Output Parameter:
58002e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5801c58f1c22SToby Isaac 
5802c58f1c22SToby Isaac   Level: intermediate
5803c58f1c22SToby Isaac 
5804c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5805c58f1c22SToby Isaac 
5806c58f1c22SToby Isaac .keywords: mesh
5807c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5808c58f1c22SToby Isaac @*/
5809c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5810c58f1c22SToby Isaac {
5811c58f1c22SToby Isaac   PetscInt       numLabels, l;
5812c58f1c22SToby Isaac   PetscErrorCode ierr;
5813c58f1c22SToby Isaac 
5814c58f1c22SToby Isaac   PetscFunctionBegin;
5815c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5816c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5817c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5818c58f1c22SToby Isaac     DMLabel     label, labelNew;
5819c58f1c22SToby Isaac     const char *name;
5820c58f1c22SToby Isaac     PetscBool   flg;
5821c58f1c22SToby Isaac 
5822c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5823c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5824c58f1c22SToby Isaac     if (flg) continue;
5825c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5826c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5827c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5828c58f1c22SToby Isaac   }
5829c58f1c22SToby Isaac   PetscFunctionReturn(0);
5830c58f1c22SToby Isaac }
5831a8fb8f29SToby Isaac 
5832a8fb8f29SToby Isaac /*@
5833a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5834a8fb8f29SToby Isaac 
5835a8fb8f29SToby Isaac   Input Parameter:
5836a8fb8f29SToby Isaac . dm - The DM object
5837a8fb8f29SToby Isaac 
5838a8fb8f29SToby Isaac   Output Parameter:
5839a8fb8f29SToby Isaac . cdm - The coarse DM
5840a8fb8f29SToby Isaac 
5841a8fb8f29SToby Isaac   Level: intermediate
5842a8fb8f29SToby Isaac 
5843a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5844a8fb8f29SToby Isaac @*/
5845a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5846a8fb8f29SToby Isaac {
5847a8fb8f29SToby Isaac   PetscFunctionBegin;
5848a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5849a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5850a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5851a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5852a8fb8f29SToby Isaac }
5853a8fb8f29SToby Isaac 
5854a8fb8f29SToby Isaac /*@
5855a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5856a8fb8f29SToby Isaac 
5857a8fb8f29SToby Isaac   Input Parameters:
5858a8fb8f29SToby Isaac + dm - The DM object
5859a8fb8f29SToby Isaac - cdm - The coarse DM
5860a8fb8f29SToby Isaac 
5861a8fb8f29SToby Isaac   Level: intermediate
5862a8fb8f29SToby Isaac 
5863a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5864a8fb8f29SToby Isaac @*/
5865a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5866a8fb8f29SToby Isaac {
5867a8fb8f29SToby Isaac   PetscErrorCode ierr;
5868a8fb8f29SToby Isaac 
5869a8fb8f29SToby Isaac   PetscFunctionBegin;
5870a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5871a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5872a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5873a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5874a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5875a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5876a8fb8f29SToby Isaac }
5877a8fb8f29SToby Isaac 
587888bdff64SToby Isaac /*@
587988bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
588088bdff64SToby Isaac 
588188bdff64SToby Isaac   Input Parameter:
588288bdff64SToby Isaac . dm - The DM object
588388bdff64SToby Isaac 
588488bdff64SToby Isaac   Output Parameter:
588588bdff64SToby Isaac . fdm - The fine DM
588688bdff64SToby Isaac 
588788bdff64SToby Isaac   Level: intermediate
588888bdff64SToby Isaac 
588988bdff64SToby Isaac .seealso: DMSetFineDM()
589088bdff64SToby Isaac @*/
589188bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
589288bdff64SToby Isaac {
589388bdff64SToby Isaac   PetscFunctionBegin;
589488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
589588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
589688bdff64SToby Isaac   *fdm = dm->fineMesh;
589788bdff64SToby Isaac   PetscFunctionReturn(0);
589888bdff64SToby Isaac }
589988bdff64SToby Isaac 
590088bdff64SToby Isaac /*@
590188bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
590288bdff64SToby Isaac 
590388bdff64SToby Isaac   Input Parameters:
590488bdff64SToby Isaac + dm - The DM object
590588bdff64SToby Isaac - fdm - The fine DM
590688bdff64SToby Isaac 
590788bdff64SToby Isaac   Level: intermediate
590888bdff64SToby Isaac 
590988bdff64SToby Isaac .seealso: DMGetFineDM()
591088bdff64SToby Isaac @*/
591188bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
591288bdff64SToby Isaac {
591388bdff64SToby Isaac   PetscErrorCode ierr;
591488bdff64SToby Isaac 
591588bdff64SToby Isaac   PetscFunctionBegin;
591688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
591788bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
591888bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
591988bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
592088bdff64SToby Isaac   dm->fineMesh = fdm;
592188bdff64SToby Isaac   PetscFunctionReturn(0);
592288bdff64SToby Isaac }
592388bdff64SToby Isaac 
5924a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5925a6ba4734SToby Isaac 
5926a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5927a6ba4734SToby Isaac {
5928a6ba4734SToby Isaac   PetscErrorCode ierr;
5929a6ba4734SToby Isaac 
5930a6ba4734SToby Isaac   PetscFunctionBegin;
5931e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
5932a6ba4734SToby Isaac   PetscFunctionReturn(0);
5933a6ba4734SToby Isaac }
5934a6ba4734SToby Isaac 
5935a6ba4734SToby Isaac /*@C
5936a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5937a6ba4734SToby Isaac 
5938a6ba4734SToby Isaac   Input Parameters:
59394c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
5940f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5941a6ba4734SToby Isaac . name        - The BC name
5942a6ba4734SToby Isaac . labelname   - The label defining constrained points
5943a6ba4734SToby Isaac . field       - The field to constrain
5944a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5945a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5946a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5947a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5948a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5949a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5950a6ba4734SToby Isaac 
5951a6ba4734SToby Isaac   Options Database Keys:
5952a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5953a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5954a6ba4734SToby Isaac 
5955a6ba4734SToby Isaac   Level: developer
5956a6ba4734SToby Isaac 
5957a6ba4734SToby Isaac .seealso: DMGetBoundary()
5958a6ba4734SToby Isaac @*/
5959a30ec4eaSSatish 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)
5960a6ba4734SToby Isaac {
5961a6ba4734SToby Isaac   PetscErrorCode ierr;
5962a6ba4734SToby Isaac 
5963a6ba4734SToby Isaac   PetscFunctionBegin;
5964a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5965f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
5966a6ba4734SToby Isaac   PetscFunctionReturn(0);
5967a6ba4734SToby Isaac }
5968a6ba4734SToby Isaac 
5969a6ba4734SToby Isaac /*@
5970a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5971a6ba4734SToby Isaac 
5972a6ba4734SToby Isaac   Input Parameters:
5973a6ba4734SToby Isaac . dm - The mesh object
5974a6ba4734SToby Isaac 
5975a6ba4734SToby Isaac   Output Parameters:
5976a6ba4734SToby Isaac . numBd - The number of BC
5977a6ba4734SToby Isaac 
5978a6ba4734SToby Isaac   Level: intermediate
5979a6ba4734SToby Isaac 
5980a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5981a6ba4734SToby Isaac @*/
5982a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5983a6ba4734SToby Isaac {
598458ebd649SToby Isaac   PetscErrorCode ierr;
5985a6ba4734SToby Isaac 
5986a6ba4734SToby Isaac   PetscFunctionBegin;
5987a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
598858ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
5989a6ba4734SToby Isaac   PetscFunctionReturn(0);
5990a6ba4734SToby Isaac }
5991a6ba4734SToby Isaac 
5992a6ba4734SToby Isaac /*@C
59931c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
5994a6ba4734SToby Isaac 
5995a6ba4734SToby Isaac   Input Parameters:
5996a6ba4734SToby Isaac + dm          - The mesh object
5997a6ba4734SToby Isaac - bd          - The BC number
5998a6ba4734SToby Isaac 
5999a6ba4734SToby Isaac   Output Parameters:
6000f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6001a6ba4734SToby Isaac . name        - The BC name
6002a6ba4734SToby Isaac . labelname   - The label defining constrained points
6003a6ba4734SToby Isaac . field       - The field to constrain
6004a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6005a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6006a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6007a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6008a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6009a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6010a6ba4734SToby Isaac 
6011a6ba4734SToby Isaac   Options Database Keys:
6012a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6013a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6014a6ba4734SToby Isaac 
6015a6ba4734SToby Isaac   Level: developer
6016a6ba4734SToby Isaac 
6017a6ba4734SToby Isaac .seealso: DMAddBoundary()
6018a6ba4734SToby Isaac @*/
6019a30ec4eaSSatish 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)
6020a6ba4734SToby Isaac {
602158ebd649SToby Isaac   PetscErrorCode ierr;
6022a6ba4734SToby Isaac 
6023a6ba4734SToby Isaac   PetscFunctionBegin;
6024a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6025f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6026a6ba4734SToby Isaac   PetscFunctionReturn(0);
6027a6ba4734SToby Isaac }
6028a6ba4734SToby Isaac 
6029e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6030e6f8dbb6SToby Isaac {
6031dff059c6SToby Isaac   DMBoundary *lastnext;
6032e6f8dbb6SToby Isaac   DSBoundary dsbound;
6033e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6034e6f8dbb6SToby Isaac 
6035e6f8dbb6SToby Isaac   PetscFunctionBegin;
6036e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
603747a1f5adSToby Isaac   if (dm->boundary) {
603847a1f5adSToby Isaac     DMBoundary next = dm->boundary;
603947a1f5adSToby Isaac 
604047a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
604147a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
604247a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
604347a1f5adSToby Isaac     while (next) {
604447a1f5adSToby Isaac       DMBoundary b = next;
604547a1f5adSToby Isaac 
604647a1f5adSToby Isaac       next = b->next;
604747a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6048a6ba4734SToby Isaac     }
604947a1f5adSToby Isaac     dm->boundary = NULL;
6050a6ba4734SToby Isaac   }
605147a1f5adSToby Isaac 
6052dff059c6SToby Isaac   lastnext = &(dm->boundary);
6053e6f8dbb6SToby Isaac   while (dsbound) {
6054e6f8dbb6SToby Isaac     DMBoundary dmbound;
6055e6f8dbb6SToby Isaac 
6056e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6057e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6058e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6059e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
606047a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6061dff059c6SToby Isaac     *lastnext = dmbound;
6062dff059c6SToby Isaac     lastnext = &(dmbound->next);
6063dff059c6SToby Isaac     dsbound = dsbound->next;
6064a6ba4734SToby Isaac   }
6065a6ba4734SToby Isaac   PetscFunctionReturn(0);
6066a6ba4734SToby Isaac }
6067a6ba4734SToby Isaac 
6068a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6069a6ba4734SToby Isaac {
6070b95f2879SToby Isaac   DMBoundary     b;
6071a6ba4734SToby Isaac   PetscErrorCode ierr;
6072a6ba4734SToby Isaac 
6073a6ba4734SToby Isaac   PetscFunctionBegin;
6074a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6075a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6076a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6077e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6078b95f2879SToby Isaac   b = dm->boundary;
6079a6ba4734SToby Isaac   while (b && !(*isBd)) {
6080e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6081e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
60823424c85cSToby Isaac 
60833424c85cSToby Isaac     if (label) {
6084a6ba4734SToby Isaac       PetscInt i;
6085a6ba4734SToby Isaac 
6086e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6087e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6088a6ba4734SToby Isaac       }
6089a6ba4734SToby Isaac     }
6090a6ba4734SToby Isaac     b = b->next;
6091a6ba4734SToby Isaac   }
6092a6ba4734SToby Isaac   PetscFunctionReturn(0);
6093a6ba4734SToby Isaac }
60944d6f44ffSToby Isaac 
60954d6f44ffSToby Isaac /*@C
60964d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
60974d6f44ffSToby Isaac 
60984d6f44ffSToby Isaac   Input Parameters:
60994d6f44ffSToby Isaac + dm      - The DM
61000709b2feSToby Isaac . time    - The time
61014d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
61024d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
61034d6f44ffSToby Isaac - mode    - The insertion mode for values
61044d6f44ffSToby Isaac 
61054d6f44ffSToby Isaac   Output Parameter:
61064d6f44ffSToby Isaac . X - vector
61074d6f44ffSToby Isaac 
61084d6f44ffSToby Isaac    Calling sequence of func:
61090709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
61104d6f44ffSToby Isaac 
61114d6f44ffSToby Isaac +  dim - The spatial dimension
61124d6f44ffSToby Isaac .  x   - The coordinates
61134d6f44ffSToby Isaac .  Nf  - The number of fields
61144d6f44ffSToby Isaac .  u   - The output field values
61154d6f44ffSToby Isaac -  ctx - optional user-defined function context
61164d6f44ffSToby Isaac 
61174d6f44ffSToby Isaac   Level: developer
61184d6f44ffSToby Isaac 
61192716604bSToby Isaac .seealso: DMComputeL2Diff()
61204d6f44ffSToby Isaac @*/
61210709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
61224d6f44ffSToby Isaac {
61234d6f44ffSToby Isaac   Vec            localX;
61244d6f44ffSToby Isaac   PetscErrorCode ierr;
61254d6f44ffSToby Isaac 
61264d6f44ffSToby Isaac   PetscFunctionBegin;
61274d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61284d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
61290709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61304d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
61314d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
61324d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
61334d6f44ffSToby Isaac   PetscFunctionReturn(0);
61344d6f44ffSToby Isaac }
61354d6f44ffSToby Isaac 
61360709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
61374d6f44ffSToby Isaac {
61384d6f44ffSToby Isaac   PetscErrorCode ierr;
61394d6f44ffSToby Isaac 
61404d6f44ffSToby Isaac   PetscFunctionBegin;
61414d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61424d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
61434d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
61440709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61454d6f44ffSToby Isaac   PetscFunctionReturn(0);
61464d6f44ffSToby Isaac }
61474d6f44ffSToby Isaac 
61481c531cf8SMatthew 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)
61494d6f44ffSToby Isaac {
61504d6f44ffSToby Isaac   PetscErrorCode ierr;
61514d6f44ffSToby Isaac 
61524d6f44ffSToby Isaac   PetscFunctionBegin;
61534d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61544d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
61554d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
61561c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61574d6f44ffSToby Isaac   PetscFunctionReturn(0);
61584d6f44ffSToby Isaac }
61592716604bSToby Isaac 
61608c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
61618c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
61628c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
61638c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6164191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
61658c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
61668c6c5593SMatthew G. Knepley {
61678c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
61688c6c5593SMatthew G. Knepley 
61698c6c5593SMatthew G. Knepley   PetscFunctionBegin;
61708c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61718c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
61728c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
61738c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
61748c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
61758c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
61768c6c5593SMatthew G. Knepley }
61778c6c5593SMatthew G. Knepley 
61781c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
61798c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
61808c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
61818c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6182191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
61838c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
61848c6c5593SMatthew G. Knepley {
61858c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
61868c6c5593SMatthew G. Knepley 
61878c6c5593SMatthew G. Knepley   PetscFunctionBegin;
61888c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61898c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
61908c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
61918c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
61921c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
61938c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
61948c6c5593SMatthew G. Knepley }
61958c6c5593SMatthew G. Knepley 
61962716604bSToby Isaac /*@C
61972716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
61982716604bSToby Isaac 
61992716604bSToby Isaac   Input Parameters:
62002716604bSToby Isaac + dm    - The DM
62010709b2feSToby Isaac . time  - The time
62022716604bSToby Isaac . funcs - The functions to evaluate for each field component
62032716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
62042716604bSToby Isaac - X     - The coefficient vector u_h
62052716604bSToby Isaac 
62062716604bSToby Isaac   Output Parameter:
62072716604bSToby Isaac . diff - The diff ||u - u_h||_2
62082716604bSToby Isaac 
62092716604bSToby Isaac   Level: developer
62102716604bSToby Isaac 
62111189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62122716604bSToby Isaac @*/
62130709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
62142716604bSToby Isaac {
62152716604bSToby Isaac   PetscErrorCode ierr;
62162716604bSToby Isaac 
62172716604bSToby Isaac   PetscFunctionBegin;
62182716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6219b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62202716604bSToby Isaac   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2Diff",((PetscObject)dm)->type_name);
62210709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
62222716604bSToby Isaac   PetscFunctionReturn(0);
62232716604bSToby Isaac }
6224b698f381SToby Isaac 
6225b698f381SToby Isaac /*@C
6226b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6227b698f381SToby Isaac 
6228b698f381SToby Isaac   Input Parameters:
6229b698f381SToby Isaac + dm    - The DM
6230b698f381SToby Isaac , time  - The time
6231b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6232b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6233b698f381SToby Isaac . X     - The coefficient vector u_h
6234b698f381SToby Isaac - n     - The vector to project along
6235b698f381SToby Isaac 
6236b698f381SToby Isaac   Output Parameter:
6237b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6238b698f381SToby Isaac 
6239b698f381SToby Isaac   Level: developer
6240b698f381SToby Isaac 
6241b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6242b698f381SToby Isaac @*/
6243b698f381SToby 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)
6244b698f381SToby Isaac {
6245b698f381SToby Isaac   PetscErrorCode ierr;
6246b698f381SToby Isaac 
6247b698f381SToby Isaac   PetscFunctionBegin;
6248b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6249b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6250b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6251b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6252b698f381SToby Isaac   PetscFunctionReturn(0);
6253b698f381SToby Isaac }
6254b698f381SToby Isaac 
62552a16baeaSToby Isaac /*@C
62562a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
62572a16baeaSToby Isaac 
62582a16baeaSToby Isaac   Input Parameters:
62592a16baeaSToby Isaac + dm    - The DM
62602a16baeaSToby Isaac . time  - The time
62612a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
62622a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
62632a16baeaSToby Isaac - X     - The coefficient vector u_h
62642a16baeaSToby Isaac 
62652a16baeaSToby Isaac   Output Parameter:
62662a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
62672a16baeaSToby Isaac 
62682a16baeaSToby Isaac   Level: developer
62692a16baeaSToby Isaac 
62701189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62712a16baeaSToby Isaac @*/
62721189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
62732a16baeaSToby Isaac {
62742a16baeaSToby Isaac   PetscErrorCode ierr;
62752a16baeaSToby Isaac 
62762a16baeaSToby Isaac   PetscFunctionBegin;
62772a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62782a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62792a16baeaSToby Isaac   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
62802a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
62812a16baeaSToby Isaac   PetscFunctionReturn(0);
62822a16baeaSToby Isaac }
62832a16baeaSToby Isaac 
6284df0b854cSToby Isaac /*@C
6285df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6286cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6287df0b854cSToby Isaac 
6288df0b854cSToby Isaac   Collective on dm
6289df0b854cSToby Isaac 
6290df0b854cSToby Isaac   Input parameters:
6291df0b854cSToby Isaac + dm - the pre-adaptation DM object
6292a1b0c543SToby Isaac - label - label with the flags
6293df0b854cSToby Isaac 
6294df0b854cSToby Isaac   Output parameters:
62950d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6296df0b854cSToby Isaac 
6297df0b854cSToby Isaac   Level: intermediate
62980d1cd5e0SMatthew G. Knepley 
62990d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6300df0b854cSToby Isaac @*/
63010d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6302df0b854cSToby Isaac {
6303df0b854cSToby Isaac   PetscErrorCode ierr;
6304df0b854cSToby Isaac 
6305df0b854cSToby Isaac   PetscFunctionBegin;
6306df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6307a1b0c543SToby Isaac   PetscValidPointer(label,2);
63080d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
63090d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
63106f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
63110d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
63120d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
63130d1cd5e0SMatthew G. Knepley }
63140d1cd5e0SMatthew G. Knepley 
63150d1cd5e0SMatthew G. Knepley /*@C
63160d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
63170d1cd5e0SMatthew G. Knepley 
63180d1cd5e0SMatthew G. Knepley   Input Parameters:
63190d1cd5e0SMatthew G. Knepley + dm - The DM object
63200d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
63216f25b0d8SLisandro 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_".
63220d1cd5e0SMatthew G. Knepley 
63230d1cd5e0SMatthew G. Knepley   Output Parameter:
63240d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
63250d1cd5e0SMatthew G. Knepley 
63260d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
63270d1cd5e0SMatthew G. Knepley 
63280d1cd5e0SMatthew G. Knepley   Level: advanced
63290d1cd5e0SMatthew G. Knepley 
63300d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
63310d1cd5e0SMatthew G. Knepley @*/
63320d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
63330d1cd5e0SMatthew G. Knepley {
63340d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
63350d1cd5e0SMatthew G. Knepley 
63360d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
63370d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63380d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
63390d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
63400d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
63416f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
63426f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
63430d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6344df0b854cSToby Isaac   PetscFunctionReturn(0);
6345df0b854cSToby Isaac }
6346c4088d22SMatthew G. Knepley 
6347502a2867SDave May /*@C
6348502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6349502a2867SDave May 
6350502a2867SDave May  Not Collective
6351502a2867SDave May 
6352502a2867SDave May  Input Parameter:
6353502a2867SDave May  . dm    - The DM
6354502a2867SDave May 
6355502a2867SDave May  Output Parameter:
6356502a2867SDave May  . nranks - the number of neighbours
6357502a2867SDave May  . ranks - the neighbors ranks
6358502a2867SDave May 
6359502a2867SDave May  Notes:
6360502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6361502a2867SDave May 
6362502a2867SDave May  Level: beginner
6363502a2867SDave May 
6364dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6365502a2867SDave May @*/
6366502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6367502a2867SDave May {
6368502a2867SDave May   PetscErrorCode ierr;
6369502a2867SDave May 
6370502a2867SDave May   PetscFunctionBegin;
6371502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6372502a2867SDave May   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMGetNeighbors",((PetscObject)dm)->type_name);
6373502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6374502a2867SDave May   PetscFunctionReturn(0);
6375502a2867SDave May }
6376502a2867SDave May 
6377531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6378531c7667SBarry Smith 
6379531c7667SBarry Smith /*
6380531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6381531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6382531c7667SBarry Smith */
6383531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6384531c7667SBarry Smith {
6385531c7667SBarry Smith   PetscErrorCode ierr;
6386531c7667SBarry Smith 
6387531c7667SBarry Smith   PetscFunctionBegin;
6388531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6389531c7667SBarry Smith     Vec x1local;
6390531c7667SBarry Smith     DM  dm;
6391531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6392531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6393531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6394531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6395531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6396531c7667SBarry Smith     x1   = x1local;
6397531c7667SBarry Smith   }
6398531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6399531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6400531c7667SBarry Smith     DM  dm;
6401531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6402531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6403531c7667SBarry Smith   }
6404531c7667SBarry Smith   PetscFunctionReturn(0);
6405531c7667SBarry Smith }
6406531c7667SBarry Smith 
6407531c7667SBarry Smith /*@
6408531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6409531c7667SBarry Smith 
6410531c7667SBarry Smith     Input Parameter:
6411531c7667SBarry Smith .    coloring - the MatFDColoring object
6412531c7667SBarry Smith 
6413531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6414531c7667SBarry Smith 
64151b266c99SBarry Smith     Level: advanced
64161b266c99SBarry Smith 
6417531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6418531c7667SBarry Smith @*/
6419531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6420531c7667SBarry Smith {
6421531c7667SBarry Smith   PetscFunctionBegin;
6422531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6423531c7667SBarry Smith   PetscFunctionReturn(0);
6424531c7667SBarry Smith }
6425