xref: /petsc/src/dm/interface/dm.c (revision 37bc7515e0ae993f57122f44cf53af97ca6a070d)
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 {
895bff27382SMatthew G. Knepley   PetscInt       bs = -1, bsLocal, bsMin, bsMax;
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) */
934bff27382SMatthew G. Knepley       bsLocal = bs;
935bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
936bff27382SMatthew G. Knepley       bsLocal = bs < 0 ? bsMax : bs;
937bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
938bff27382SMatthew G. Knepley       if (bsMin != bsMax) {bs = 1;}
939bff27382SMatthew G. Knepley       else                {bs = bsMax;}
9407591dbb2SMatthew G. Knepley       bs   = bs < 0 ? 1 : bs;
9417591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
942ccf3bd66SMatthew G. Knepley       if (bs > 1) {
943ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
944ccf3bd66SMatthew G. Knepley         n /= bs;
945ccf3bd66SMatthew G. Knepley       }
946ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9473bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
94837d0c07bSMatthew G Knepley     } else {
949184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
950184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9511411c6eeSJed Brown     }
95237d0c07bSMatthew G Knepley   }
9531411c6eeSJed Brown   *ltog = dm->ltogmap;
9541411c6eeSJed Brown   PetscFunctionReturn(0);
9551411c6eeSJed Brown }
9561411c6eeSJed Brown 
9571411c6eeSJed Brown /*@
9581411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9591411c6eeSJed Brown 
9601411c6eeSJed Brown    Not Collective
9611411c6eeSJed Brown 
9621411c6eeSJed Brown    Input Parameter:
9631411c6eeSJed Brown .  dm - the DM with block structure
9641411c6eeSJed Brown 
9651411c6eeSJed Brown    Output Parameter:
9661411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9671411c6eeSJed Brown 
9681411c6eeSJed Brown    Level: intermediate
9691411c6eeSJed Brown 
970fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9711411c6eeSJed Brown @*/
9727087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9731411c6eeSJed Brown {
9741411c6eeSJed Brown   PetscFunctionBegin;
9751411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9761411c6eeSJed Brown   PetscValidPointer(bs,2);
9771411c6eeSJed 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");
9781411c6eeSJed Brown   *bs = dm->bs;
9791411c6eeSJed Brown   PetscFunctionReturn(0);
9801411c6eeSJed Brown }
9811411c6eeSJed Brown 
98247c6ae99SBarry Smith /*@
9838472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith     Collective on DM
98647c6ae99SBarry Smith 
98747c6ae99SBarry Smith     Input Parameter:
98847c6ae99SBarry Smith +   dm1 - the DM object
98947c6ae99SBarry Smith -   dm2 - the second, finer DM object
99047c6ae99SBarry Smith 
99147c6ae99SBarry Smith     Output Parameter:
99247c6ae99SBarry Smith +  mat - the interpolation
99347c6ae99SBarry Smith -  vec - the scaling (optional)
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith     Level: developer
99647c6ae99SBarry Smith 
99785afcc9aSBarry 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
99885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
999d52bd9f3SBarry Smith 
10001f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1001d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
100285afcc9aSBarry Smith 
100385afcc9aSBarry Smith 
10043ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith @*/
1007e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
100847c6ae99SBarry Smith {
100947c6ae99SBarry Smith   PetscErrorCode ierr;
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith   PetscFunctionBegin;
1012171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1013171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1014cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
101525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1016cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
101747c6ae99SBarry Smith   PetscFunctionReturn(0);
101847c6ae99SBarry Smith }
101947c6ae99SBarry Smith 
10203ad4599aSBarry Smith /*@
10213ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10223ad4599aSBarry Smith 
10233ad4599aSBarry Smith     Collective on DM
10243ad4599aSBarry Smith 
10253ad4599aSBarry Smith     Input Parameter:
10263ad4599aSBarry Smith +   dm1 - the DM object
10273ad4599aSBarry Smith -   dm2 - the second, finer DM object
10283ad4599aSBarry Smith 
10293ad4599aSBarry Smith     Output Parameter:
10303ad4599aSBarry Smith .  mat - the restriction
10313ad4599aSBarry Smith 
10323ad4599aSBarry Smith 
10333ad4599aSBarry Smith     Level: developer
10343ad4599aSBarry Smith 
10353ad4599aSBarry 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
10363ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10373ad4599aSBarry Smith 
10383ad4599aSBarry Smith 
10393ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10403ad4599aSBarry Smith 
10413ad4599aSBarry Smith @*/
10423ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10433ad4599aSBarry Smith {
10443ad4599aSBarry Smith   PetscErrorCode ierr;
10453ad4599aSBarry Smith 
10463ad4599aSBarry Smith   PetscFunctionBegin;
10473ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10483ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10493ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10507294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
10513ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10523ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10533ad4599aSBarry Smith   PetscFunctionReturn(0);
10543ad4599aSBarry Smith }
10553ad4599aSBarry Smith 
105647c6ae99SBarry Smith /*@
10578472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
105847c6ae99SBarry Smith 
105947c6ae99SBarry Smith     Collective on DM
106047c6ae99SBarry Smith 
106147c6ae99SBarry Smith     Input Parameter:
106247c6ae99SBarry Smith +   dm1 - the DM object
106347c6ae99SBarry Smith -   dm2 - the second, finer DM object
106447c6ae99SBarry Smith 
106547c6ae99SBarry Smith     Output Parameter:
10666dbf9973SLawrence Mitchell .   mat - the injection
106747c6ae99SBarry Smith 
106847c6ae99SBarry Smith     Level: developer
106947c6ae99SBarry Smith 
107085afcc9aSBarry 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
107185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
107285afcc9aSBarry Smith 
1073e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
107447c6ae99SBarry Smith 
107547c6ae99SBarry Smith @*/
10766dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
107747c6ae99SBarry Smith {
107847c6ae99SBarry Smith   PetscErrorCode ierr;
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith   PetscFunctionBegin;
1081171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1082171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10830c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
10846dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
108547c6ae99SBarry Smith   PetscFunctionReturn(0);
108647c6ae99SBarry Smith }
108747c6ae99SBarry Smith 
1088b412c318SBarry Smith /*@
1089bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1090bd041c0cSMatthew G. Knepley 
1091bd041c0cSMatthew G. Knepley   Collective on DM
1092bd041c0cSMatthew G. Knepley 
1093bd041c0cSMatthew G. Knepley   Input Parameter:
1094bd041c0cSMatthew G. Knepley + dm1 - the DM object
1095bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1096bd041c0cSMatthew G. Knepley 
1097bd041c0cSMatthew G. Knepley   Output Parameter:
1098bd041c0cSMatthew G. Knepley . mat - the interpolation
1099bd041c0cSMatthew G. Knepley 
1100bd041c0cSMatthew G. Knepley   Level: developer
1101bd041c0cSMatthew G. Knepley 
1102bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1103bd041c0cSMatthew G. Knepley @*/
1104bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1105bd041c0cSMatthew G. Knepley {
1106bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1107bd041c0cSMatthew G. Knepley 
1108bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1109bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1110bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1111bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1112bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1113bd041c0cSMatthew G. Knepley }
1114bd041c0cSMatthew G. Knepley 
1115bd041c0cSMatthew G. Knepley /*@
1116b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith     Collective on DM
111947c6ae99SBarry Smith 
112047c6ae99SBarry Smith     Input Parameter:
112147c6ae99SBarry Smith +   dm - the DM object
11225bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
112347c6ae99SBarry Smith 
112447c6ae99SBarry Smith     Output Parameter:
112547c6ae99SBarry Smith .   coloring - the coloring
112647c6ae99SBarry Smith 
112747c6ae99SBarry Smith     Level: developer
112847c6ae99SBarry Smith 
1129b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
113047c6ae99SBarry Smith 
1131aab9d709SJed Brown @*/
1132b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
113347c6ae99SBarry Smith {
113447c6ae99SBarry Smith   PetscErrorCode ierr;
113547c6ae99SBarry Smith 
113647c6ae99SBarry Smith   PetscFunctionBegin;
1137171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1138ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1139b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
114047c6ae99SBarry Smith   PetscFunctionReturn(0);
114147c6ae99SBarry Smith }
114247c6ae99SBarry Smith 
1143b412c318SBarry Smith /*@
11448472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
114547c6ae99SBarry Smith 
114647c6ae99SBarry Smith     Collective on DM
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith     Input Parameter:
1149b412c318SBarry Smith .   dm - the DM object
115047c6ae99SBarry Smith 
115147c6ae99SBarry Smith     Output Parameter:
115247c6ae99SBarry Smith .   mat - the empty Jacobian
115347c6ae99SBarry Smith 
1154073dac72SJed Brown     Level: beginner
115547c6ae99SBarry Smith 
115694013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
115794013140SBarry Smith        do not need to do it yourself.
115894013140SBarry Smith 
115994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
11607889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
116194013140SBarry Smith 
116294013140SBarry 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
116394013140SBarry Smith        internally by PETSc.
116494013140SBarry Smith 
116594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1166aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
116794013140SBarry Smith 
1168b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
116947c6ae99SBarry Smith 
1170aab9d709SJed Brown @*/
1171b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
117247c6ae99SBarry Smith {
117347c6ae99SBarry Smith   PetscErrorCode ierr;
117447c6ae99SBarry Smith 
117547c6ae99SBarry Smith   PetscFunctionBegin;
1176171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1177607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1178c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1179c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1180b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
118147c6ae99SBarry Smith   PetscFunctionReturn(0);
118247c6ae99SBarry Smith }
118347c6ae99SBarry Smith 
1184732e2eb9SMatthew G Knepley /*@
1185950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1186732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1187732e2eb9SMatthew G Knepley 
11888472ad0fSDave May   Logically Collective on DM
1189732e2eb9SMatthew G Knepley 
1190732e2eb9SMatthew G Knepley   Input Parameter:
1191732e2eb9SMatthew G Knepley + dm - the DM
1192732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1193732e2eb9SMatthew G Knepley 
1194732e2eb9SMatthew G Knepley   Level: developer
1195b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1196732e2eb9SMatthew G Knepley @*/
1197732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1198732e2eb9SMatthew G Knepley {
1199732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1200732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1201732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1202732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1203732e2eb9SMatthew G Knepley }
1204732e2eb9SMatthew G Knepley 
1205b06ff27eSHong Zhang /*@
1206b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1207b06ff27eSHong Zhang     but the array for values will not be allocated.
1208b06ff27eSHong Zhang 
1209b06ff27eSHong Zhang   Logically Collective on DM
1210b06ff27eSHong Zhang 
1211b06ff27eSHong Zhang   Input Parameter:
1212b06ff27eSHong Zhang + dm - the DM
1213b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1214b06ff27eSHong Zhang 
1215b06ff27eSHong Zhang   Level: developer
1216b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1217b06ff27eSHong Zhang @*/
1218b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1219b06ff27eSHong Zhang {
1220b06ff27eSHong Zhang   PetscFunctionBegin;
1221b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1222b06ff27eSHong Zhang   dm->structure_only = only;
1223b06ff27eSHong Zhang   PetscFunctionReturn(0);
1224b06ff27eSHong Zhang }
1225b06ff27eSHong Zhang 
1226a89ea682SMatthew G Knepley /*@C
1227aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1228a89ea682SMatthew G Knepley 
1229a89ea682SMatthew G Knepley   Not Collective
1230a89ea682SMatthew G Knepley 
1231a89ea682SMatthew G Knepley   Input Parameters:
1232a89ea682SMatthew G Knepley + dm - the DM object
1233aa1993deSMatthew G Knepley . count - The minium size
123469291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1235a89ea682SMatthew G Knepley 
1236a89ea682SMatthew G Knepley   Output Parameter:
1237a89ea682SMatthew G Knepley . array - the work array
1238a89ea682SMatthew G Knepley 
1239a89ea682SMatthew G Knepley   Level: developer
1240a89ea682SMatthew G Knepley 
1241a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1242a89ea682SMatthew G Knepley @*/
124369291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1244a89ea682SMatthew G Knepley {
1245a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1246aa1993deSMatthew G Knepley   DMWorkLink     link;
124769291d52SBarry Smith   PetscMPIInt    dsize;
1248a89ea682SMatthew G Knepley 
1249a89ea682SMatthew G Knepley   PetscFunctionBegin;
1250a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1251aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1252aa1993deSMatthew G Knepley   if (dm->workin) {
1253aa1993deSMatthew G Knepley     link       = dm->workin;
1254aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1255aa1993deSMatthew G Knepley   } else {
1256b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1257a89ea682SMatthew G Knepley   }
125869291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
12595056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1260aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1261854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1262854ce69bSBarry Smith     link->bytes = dsize*count;
1263aa1993deSMatthew G Knepley   }
1264aa1993deSMatthew G Knepley   link->next   = dm->workout;
1265aa1993deSMatthew G Knepley   dm->workout  = link;
1266aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1267a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1268a89ea682SMatthew G Knepley }
1269a89ea682SMatthew G Knepley 
1270aa1993deSMatthew G Knepley /*@C
1271aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1272aa1993deSMatthew G Knepley 
1273aa1993deSMatthew G Knepley   Not Collective
1274aa1993deSMatthew G Knepley 
1275aa1993deSMatthew G Knepley   Input Parameters:
1276aa1993deSMatthew G Knepley + dm - the DM object
1277aa1993deSMatthew G Knepley . count - The minium size
127869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1279aa1993deSMatthew G Knepley 
1280aa1993deSMatthew G Knepley   Output Parameter:
1281aa1993deSMatthew G Knepley . array - the work array
1282aa1993deSMatthew G Knepley 
1283aa1993deSMatthew G Knepley   Level: developer
1284aa1993deSMatthew G Knepley 
128569291d52SBarry Smith   Developer Notes: count and dtype are ignored, they are only needed for DMGetWorkArray()
1286aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1287aa1993deSMatthew G Knepley @*/
128869291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1289aa1993deSMatthew G Knepley {
1290aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1291aa1993deSMatthew G Knepley 
1292aa1993deSMatthew G Knepley   PetscFunctionBegin;
1293aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1294aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1295aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1296aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1297aa1993deSMatthew G Knepley       *p           = link->next;
1298aa1993deSMatthew G Knepley       link->next   = dm->workin;
1299aa1993deSMatthew G Knepley       dm->workin   = link;
13000298fd71SBarry Smith       *(void**)mem = NULL;
1301aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1302aa1993deSMatthew G Knepley     }
1303aa1993deSMatthew G Knepley   }
1304aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1305aa1993deSMatthew G Knepley }
1306e7c4fc90SDmitry Karpeev 
1307435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1308435a35e8SMatthew G Knepley {
1309435a35e8SMatthew G Knepley   PetscFunctionBegin;
1310435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
131182f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1312435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1313435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1314435a35e8SMatthew G Knepley }
1315435a35e8SMatthew G Knepley 
13164f3b5142SJed Brown /*@C
13174d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
13184d343eeaSMatthew G Knepley 
13194d343eeaSMatthew G Knepley   Not collective
13204d343eeaSMatthew G Knepley 
13214d343eeaSMatthew G Knepley   Input Parameter:
13224d343eeaSMatthew G Knepley . dm - the DM object
13234d343eeaSMatthew G Knepley 
13244d343eeaSMatthew G Knepley   Output Parameters:
13250298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13260298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13270298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13284d343eeaSMatthew G Knepley 
13294d343eeaSMatthew G Knepley   Level: intermediate
13304d343eeaSMatthew G Knepley 
133121c9b008SJed Brown   Notes:
133221c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
133321c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
133421c9b008SJed Brown   PetscFree().
133521c9b008SJed Brown 
13364d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13374d343eeaSMatthew G Knepley @*/
133837d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13394d343eeaSMatthew G Knepley {
134037d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13414d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13424d343eeaSMatthew G Knepley 
13434d343eeaSMatthew G Knepley   PetscFunctionBegin;
13444d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134569ca1f37SDmitry Karpeev   if (numFields) {
134669ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
134769ca1f37SDmitry Karpeev     *numFields = 0;
134869ca1f37SDmitry Karpeev   }
134937d0c07bSMatthew G Knepley   if (fieldNames) {
135037d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13510298fd71SBarry Smith     *fieldNames = NULL;
135269ca1f37SDmitry Karpeev   }
135369ca1f37SDmitry Karpeev   if (fields) {
135469ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13550298fd71SBarry Smith     *fields = NULL;
135669ca1f37SDmitry Karpeev   }
135737d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
135837d0c07bSMatthew G Knepley   if (section) {
135937d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
136037d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
136137d0c07bSMatthew G Knepley 
136237d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
136337d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1364dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
136537d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
136637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
136737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
136837d0c07bSMatthew G Knepley     }
136937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
137037d0c07bSMatthew G Knepley       PetscInt gdof;
137137d0c07bSMatthew G Knepley 
137237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
137337d0c07bSMatthew G Knepley       if (gdof > 0) {
137437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
137537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
137637d0c07bSMatthew G Knepley 
137737d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
137837d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
137937d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
138037d0c07bSMatthew G Knepley         }
138137d0c07bSMatthew G Knepley       }
138237d0c07bSMatthew G Knepley     }
138337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1384785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
138537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
138637d0c07bSMatthew G Knepley     }
138737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
138837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
138937d0c07bSMatthew G Knepley 
139037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
139137d0c07bSMatthew G Knepley       if (gdof > 0) {
139237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
139337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
139437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
139537d0c07bSMatthew G Knepley 
139637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
139737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
139837d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
139937d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
140037d0c07bSMatthew G Knepley           }
140137d0c07bSMatthew G Knepley         }
140237d0c07bSMatthew G Knepley       }
140337d0c07bSMatthew G Knepley     }
14048865f1eaSKarl Rupp     if (numFields) *numFields = nF;
140537d0c07bSMatthew G Knepley     if (fieldNames) {
1406785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
140737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
140837d0c07bSMatthew G Knepley         const char *fieldName;
140937d0c07bSMatthew G Knepley 
141037d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
141137d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
141237d0c07bSMatthew G Knepley       }
141337d0c07bSMatthew G Knepley     }
141437d0c07bSMatthew G Knepley     if (fields) {
1415785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
141637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
141782f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
141837d0c07bSMatthew G Knepley       }
141937d0c07bSMatthew G Knepley     }
142037d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
14218865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
14228865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
142369ca1f37SDmitry Karpeev   }
14244d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14254d343eeaSMatthew G Knepley }
14264d343eeaSMatthew G Knepley 
142716621825SDmitry Karpeev 
142816621825SDmitry Karpeev /*@C
142916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
143016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
143116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1432e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1433e7c4fc90SDmitry Karpeev 
1434e7c4fc90SDmitry Karpeev   Not collective
1435e7c4fc90SDmitry Karpeev 
1436e7c4fc90SDmitry Karpeev   Input Parameter:
1437e7c4fc90SDmitry Karpeev . dm - the DM object
1438e7c4fc90SDmitry Karpeev 
1439e7c4fc90SDmitry Karpeev   Output Parameters:
14400298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14410298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14420298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14430298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1444e7c4fc90SDmitry Karpeev 
1445e7c4fc90SDmitry Karpeev   Level: intermediate
1446e7c4fc90SDmitry Karpeev 
1447e7c4fc90SDmitry Karpeev   Notes:
1448e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1449e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1450e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1451e7c4fc90SDmitry Karpeev 
1452e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1453e7c4fc90SDmitry Karpeev @*/
145416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1455e7c4fc90SDmitry Karpeev {
1456e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1457e7c4fc90SDmitry Karpeev 
1458e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1459e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14608865f1eaSKarl Rupp   if (len) {
14618865f1eaSKarl Rupp     PetscValidPointer(len,2);
14628865f1eaSKarl Rupp     *len = 0;
14638865f1eaSKarl Rupp   }
14648865f1eaSKarl Rupp   if (namelist) {
14658865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14668865f1eaSKarl Rupp     *namelist = 0;
14678865f1eaSKarl Rupp   }
14688865f1eaSKarl Rupp   if (islist) {
14698865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14708865f1eaSKarl Rupp     *islist = 0;
14718865f1eaSKarl Rupp   }
14728865f1eaSKarl Rupp   if (dmlist) {
14738865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14748865f1eaSKarl Rupp     *dmlist = 0;
14758865f1eaSKarl Rupp   }
1476f3f0edfdSDmitry Karpeev   /*
1477f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1478f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1479f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1480f3f0edfdSDmitry Karpeev    */
1481ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
148216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1483435a35e8SMatthew G Knepley     PetscSection section;
1484435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1485435a35e8SMatthew G Knepley 
1486435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1487435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1488435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1489f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
149003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
149103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
149203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1493435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1494435a35e8SMatthew G Knepley         const char *fieldName;
1495435a35e8SMatthew G Knepley 
149603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
149703dc3394SMatthew G. Knepley         if (namelist) {
1498435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1499435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1500435a35e8SMatthew G Knepley         }
150103dc3394SMatthew G. Knepley       }
1502435a35e8SMatthew G Knepley     } else {
150369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1504e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
15050298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1506e7c4fc90SDmitry Karpeev     }
15078865f1eaSKarl Rupp   } else {
150816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
150916621825SDmitry Karpeev   }
151016621825SDmitry Karpeev   PetscFunctionReturn(0);
151116621825SDmitry Karpeev }
151216621825SDmitry Karpeev 
1513564cec59SMatthew G. Knepley /*@
1514435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1515435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1516435a35e8SMatthew G Knepley 
1517435a35e8SMatthew G Knepley   Not collective
1518435a35e8SMatthew G Knepley 
1519435a35e8SMatthew G Knepley   Input Parameters:
1520435a35e8SMatthew G Knepley + dm - the DM object
15213cd36596SPatrick Sanan . numFields - the number of fields in this subproblem
15223cd36596SPatrick Sanan - fields - the fields in the subproblem
1523435a35e8SMatthew G Knepley 
1524435a35e8SMatthew G Knepley   Output Parameters:
15253cd36596SPatrick Sanan + is - the global indices for the subproblem
15263cd36596SPatrick Sanan - dm - the DM for the subproblem
1527435a35e8SMatthew G Knepley 
1528435a35e8SMatthew G Knepley   Level: intermediate
1529435a35e8SMatthew G Knepley 
1530435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1531435a35e8SMatthew G Knepley @*/
1532*37bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1533435a35e8SMatthew G Knepley {
1534435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1535435a35e8SMatthew G Knepley 
1536435a35e8SMatthew G Knepley   PetscFunctionBegin;
1537435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1538435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15398865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15408865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1541435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1542435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
154382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1544435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1545435a35e8SMatthew G Knepley }
1546435a35e8SMatthew G Knepley 
154716621825SDmitry Karpeev 
154816621825SDmitry Karpeev /*@C
15498d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
15508d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
15518d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
15528d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
15538d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
155416621825SDmitry Karpeev 
155516621825SDmitry Karpeev   Not collective
155616621825SDmitry Karpeev 
155716621825SDmitry Karpeev   Input Parameter:
155816621825SDmitry Karpeev . dm - the DM object
155916621825SDmitry Karpeev 
156016621825SDmitry Karpeev   Output Parameters:
15610298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
15620298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
15630298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
15640298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
15650298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
156616621825SDmitry Karpeev 
156716621825SDmitry Karpeev   Level: intermediate
156816621825SDmitry Karpeev 
156916621825SDmitry Karpeev   Notes:
157016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
157116621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
157216621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
157316621825SDmitry Karpeev 
15748d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
157516621825SDmitry Karpeev @*/
15768d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
157716621825SDmitry Karpeev {
157816621825SDmitry Karpeev   PetscErrorCode      ierr;
1579be081cd6SPeter Brune   DMSubDomainHookLink link;
1580be081cd6SPeter Brune   PetscInt            i,l;
158116621825SDmitry Karpeev 
158216621825SDmitry Karpeev   PetscFunctionBegin;
158316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
158414a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
15850298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
15860298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
15870298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
15880298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1589f3f0edfdSDmitry Karpeev   /*
1590f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1591f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1592f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1593f3f0edfdSDmitry Karpeev    */
1594ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
159516621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1596be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
159714a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1598f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1599be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1600be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1601be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1602be081cd6SPeter Brune         }
1603648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1604e7c4fc90SDmitry Karpeev       }
160514a18fd3SPeter Brune     }
160614a18fd3SPeter Brune     if (len) *len = l;
160714a18fd3SPeter Brune   }
1608e30e807fSPeter Brune   PetscFunctionReturn(0);
1609e30e807fSPeter Brune }
1610e30e807fSPeter Brune 
1611e30e807fSPeter Brune 
1612e30e807fSPeter Brune /*@C
1613e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1614e30e807fSPeter Brune 
1615e30e807fSPeter Brune   Not collective
1616e30e807fSPeter Brune 
1617e30e807fSPeter Brune   Input Parameters:
1618e30e807fSPeter Brune + dm - the DM object
1619e30e807fSPeter Brune . n  - the number of subdomain scatters
1620e30e807fSPeter Brune - subdms - the local subdomains
1621e30e807fSPeter Brune 
1622e30e807fSPeter Brune   Output Parameters:
1623e30e807fSPeter Brune + n     - the number of scatters returned
1624e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1625e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1626e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1627e30e807fSPeter Brune 
1628e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1629e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1630e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1631e30e807fSPeter Brune   solution and residual data.
1632e30e807fSPeter Brune 
1633e30e807fSPeter Brune   Level: developer
1634e30e807fSPeter Brune 
1635e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1636e30e807fSPeter Brune @*/
1637e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1638e30e807fSPeter Brune {
1639e30e807fSPeter Brune   PetscErrorCode ierr;
1640e30e807fSPeter Brune 
1641e30e807fSPeter Brune   PetscFunctionBegin;
1642e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1643e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1644e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1645e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
16466668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1647e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1648e7c4fc90SDmitry Karpeev }
1649e7c4fc90SDmitry Karpeev 
165047c6ae99SBarry Smith /*@
165147c6ae99SBarry Smith   DMRefine - Refines a DM object
165247c6ae99SBarry Smith 
165347c6ae99SBarry Smith   Collective on DM
165447c6ae99SBarry Smith 
165547c6ae99SBarry Smith   Input Parameter:
165647c6ae99SBarry Smith + dm   - the DM object
165791d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
165847c6ae99SBarry Smith 
165947c6ae99SBarry Smith   Output Parameter:
16600298fd71SBarry Smith . dmf - the refined DM, or NULL
1661ae0a1c52SMatthew G Knepley 
16620298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
166347c6ae99SBarry Smith 
166447c6ae99SBarry Smith   Level: developer
166547c6ae99SBarry Smith 
1666e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
166747c6ae99SBarry Smith @*/
16687087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
166947c6ae99SBarry Smith {
167047c6ae99SBarry Smith   PetscErrorCode   ierr;
1671c833c3b5SJed Brown   DMRefineHookLink link;
167247c6ae99SBarry Smith 
167347c6ae99SBarry Smith   PetscFunctionBegin;
1674732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16751ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1676fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
167747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
16784057135bSMatthew G Knepley   if (*dmf) {
167943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
16808865f1eaSKarl Rupp 
16818cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
16828865f1eaSKarl Rupp 
1683644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
16840598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1685656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
16868865f1eaSKarl Rupp 
1687e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1688c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
16898865f1eaSKarl Rupp       if (link->refinehook) {
16908865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
16918865f1eaSKarl Rupp       }
1692c833c3b5SJed Brown     }
1693c833c3b5SJed Brown   }
16941ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1695c833c3b5SJed Brown   PetscFunctionReturn(0);
1696c833c3b5SJed Brown }
1697c833c3b5SJed Brown 
1698bb9467b5SJed Brown /*@C
1699c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1700c833c3b5SJed Brown 
1701c833c3b5SJed Brown    Logically Collective
1702c833c3b5SJed Brown 
1703c833c3b5SJed Brown    Input Arguments:
1704c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1705c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1706c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17070298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1708c833c3b5SJed Brown 
1709c833c3b5SJed Brown    Calling sequence of refinehook:
1710c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1711c833c3b5SJed Brown 
1712c833c3b5SJed Brown +  coarse - coarse level DM
1713c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1714c833c3b5SJed Brown -  ctx - optional user-defined function context
1715c833c3b5SJed Brown 
1716c833c3b5SJed Brown    Calling sequence for interphook:
1717c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1718c833c3b5SJed Brown 
1719c833c3b5SJed Brown +  coarse - coarse level DM
1720c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1721c833c3b5SJed Brown .  fine - fine level DM to update
1722c833c3b5SJed Brown -  ctx - optional user-defined function context
1723c833c3b5SJed Brown 
1724c833c3b5SJed Brown    Level: advanced
1725c833c3b5SJed Brown 
1726c833c3b5SJed Brown    Notes:
1727c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1728c833c3b5SJed Brown 
1729c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1730c833c3b5SJed Brown 
1731bb9467b5SJed Brown    This function is currently not available from Fortran.
1732bb9467b5SJed Brown 
1733c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1734c833c3b5SJed Brown @*/
1735c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1736c833c3b5SJed Brown {
1737c833c3b5SJed Brown   PetscErrorCode   ierr;
1738c833c3b5SJed Brown   DMRefineHookLink link,*p;
1739c833c3b5SJed Brown 
1740c833c3b5SJed Brown   PetscFunctionBegin;
1741c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
17423d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
17433d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
17443d8e3701SJed Brown   }
174595dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1746c833c3b5SJed Brown   link->refinehook = refinehook;
1747c833c3b5SJed Brown   link->interphook = interphook;
1748c833c3b5SJed Brown   link->ctx        = ctx;
17490298fd71SBarry Smith   link->next       = NULL;
1750c833c3b5SJed Brown   *p               = link;
1751c833c3b5SJed Brown   PetscFunctionReturn(0);
1752c833c3b5SJed Brown }
1753c833c3b5SJed Brown 
17543d8e3701SJed Brown /*@C
17553d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
17563d8e3701SJed Brown 
17573d8e3701SJed Brown    Logically Collective
17583d8e3701SJed Brown 
17593d8e3701SJed Brown    Input Arguments:
17603d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
17613d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
17623d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17633d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
17643d8e3701SJed Brown 
17653d8e3701SJed Brown    Level: advanced
17663d8e3701SJed Brown 
17673d8e3701SJed Brown    Notes:
17683d8e3701SJed Brown    This function does nothing if the hook is not in the list.
17693d8e3701SJed Brown 
17703d8e3701SJed Brown    This function is currently not available from Fortran.
17713d8e3701SJed Brown 
17723d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
17733d8e3701SJed Brown @*/
17743d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
17753d8e3701SJed Brown {
17763d8e3701SJed Brown   PetscErrorCode   ierr;
17773d8e3701SJed Brown   DMRefineHookLink link,*p;
17783d8e3701SJed Brown 
17793d8e3701SJed Brown   PetscFunctionBegin;
17803d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
17813d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
17823d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
17833d8e3701SJed Brown       link = *p;
17843d8e3701SJed Brown       *p = link->next;
17853d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
17863d8e3701SJed Brown       break;
17873d8e3701SJed Brown     }
17883d8e3701SJed Brown   }
17893d8e3701SJed Brown   PetscFunctionReturn(0);
17903d8e3701SJed Brown }
17913d8e3701SJed Brown 
1792c833c3b5SJed Brown /*@
1793c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1794c833c3b5SJed Brown 
1795c833c3b5SJed Brown    Collective if any hooks are
1796c833c3b5SJed Brown 
1797c833c3b5SJed Brown    Input Arguments:
1798c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1799e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1800c833c3b5SJed Brown -  fine - finer DM to update
1801c833c3b5SJed Brown 
1802c833c3b5SJed Brown    Level: developer
1803c833c3b5SJed Brown 
1804c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1805c833c3b5SJed Brown @*/
1806c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1807c833c3b5SJed Brown {
1808c833c3b5SJed Brown   PetscErrorCode   ierr;
1809c833c3b5SJed Brown   DMRefineHookLink link;
1810c833c3b5SJed Brown 
1811c833c3b5SJed Brown   PetscFunctionBegin;
1812c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
18138865f1eaSKarl Rupp     if (link->interphook) {
18148865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
18158865f1eaSKarl Rupp     }
18164057135bSMatthew G Knepley   }
181747c6ae99SBarry Smith   PetscFunctionReturn(0);
181847c6ae99SBarry Smith }
181947c6ae99SBarry Smith 
1820eb3f98d2SBarry Smith /*@
1821eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1822eb3f98d2SBarry Smith 
1823eb3f98d2SBarry Smith     Not Collective
1824eb3f98d2SBarry Smith 
1825eb3f98d2SBarry Smith     Input Parameter:
1826eb3f98d2SBarry Smith .   dm - the DM object
1827eb3f98d2SBarry Smith 
1828eb3f98d2SBarry Smith     Output Parameter:
1829eb3f98d2SBarry Smith .   level - number of refinements
1830eb3f98d2SBarry Smith 
1831eb3f98d2SBarry Smith     Level: developer
1832eb3f98d2SBarry Smith 
18336a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1834eb3f98d2SBarry Smith 
1835eb3f98d2SBarry Smith @*/
1836eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1837eb3f98d2SBarry Smith {
1838eb3f98d2SBarry Smith   PetscFunctionBegin;
1839eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1840eb3f98d2SBarry Smith   *level = dm->levelup;
1841eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1842eb3f98d2SBarry Smith }
1843eb3f98d2SBarry Smith 
1844fef3a512SBarry Smith /*@
1845fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1846fef3a512SBarry Smith 
1847fef3a512SBarry Smith     Not Collective
1848fef3a512SBarry Smith 
1849fef3a512SBarry Smith     Input Parameter:
1850fef3a512SBarry Smith +   dm - the DM object
1851fef3a512SBarry Smith -   level - number of refinements
1852fef3a512SBarry Smith 
1853fef3a512SBarry Smith     Level: advanced
1854fef3a512SBarry Smith 
1855fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1856fef3a512SBarry Smith 
1857fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1858fef3a512SBarry Smith 
1859fef3a512SBarry Smith @*/
1860fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1861fef3a512SBarry Smith {
1862fef3a512SBarry Smith   PetscFunctionBegin;
1863fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1864fef3a512SBarry Smith   dm->levelup = level;
1865fef3a512SBarry Smith   PetscFunctionReturn(0);
1866fef3a512SBarry Smith }
1867fef3a512SBarry Smith 
1868bb9467b5SJed Brown /*@C
1869baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1870baf369e7SPeter Brune 
1871baf369e7SPeter Brune    Logically Collective
1872baf369e7SPeter Brune 
1873baf369e7SPeter Brune    Input Arguments:
1874baf369e7SPeter Brune +  dm - the DM
1875baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1876baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
18770298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1878baf369e7SPeter Brune 
1879baf369e7SPeter Brune    Calling sequence for beginhook:
1880baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1881baf369e7SPeter Brune 
1882baf369e7SPeter Brune +  dm - global DM
1883baf369e7SPeter Brune .  g - global vector
1884baf369e7SPeter Brune .  mode - mode
1885baf369e7SPeter Brune .  l - local vector
1886baf369e7SPeter Brune -  ctx - optional user-defined function context
1887baf369e7SPeter Brune 
1888baf369e7SPeter Brune 
1889baf369e7SPeter Brune    Calling sequence for endhook:
1890ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1891baf369e7SPeter Brune 
1892baf369e7SPeter Brune +  global - global DM
1893baf369e7SPeter Brune -  ctx - optional user-defined function context
1894baf369e7SPeter Brune 
1895baf369e7SPeter Brune    Level: advanced
1896baf369e7SPeter Brune 
1897baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1898baf369e7SPeter Brune @*/
1899baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1900baf369e7SPeter Brune {
1901baf369e7SPeter Brune   PetscErrorCode          ierr;
1902baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1903baf369e7SPeter Brune 
1904baf369e7SPeter Brune   PetscFunctionBegin;
1905baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1906baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
190795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1908baf369e7SPeter Brune   link->beginhook = beginhook;
1909baf369e7SPeter Brune   link->endhook   = endhook;
1910baf369e7SPeter Brune   link->ctx       = ctx;
19110298fd71SBarry Smith   link->next      = NULL;
1912baf369e7SPeter Brune   *p              = link;
1913baf369e7SPeter Brune   PetscFunctionReturn(0);
1914baf369e7SPeter Brune }
1915baf369e7SPeter Brune 
19164c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
19174c274da1SToby Isaac {
19184c274da1SToby Isaac   Mat cMat;
19194c274da1SToby Isaac   Vec cVec;
19204c274da1SToby Isaac   PetscSection section, cSec;
19214c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19224c274da1SToby Isaac   PetscErrorCode ierr;
19234c274da1SToby Isaac 
19244c274da1SToby Isaac   PetscFunctionBegin;
19254c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19264c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19274c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
19285db9a05bSToby Isaac     PetscInt nRows;
19295db9a05bSToby Isaac 
19305db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
19315db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
19324c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19337711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19344c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
19354c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19364c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19374c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19384c274da1SToby Isaac       if (dof) {
19394c274da1SToby Isaac         PetscScalar *vals;
19404c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
19414c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
19424c274da1SToby Isaac       }
19434c274da1SToby Isaac     }
19444c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19454c274da1SToby Isaac   }
19464c274da1SToby Isaac   PetscFunctionReturn(0);
19474c274da1SToby Isaac }
19484c274da1SToby Isaac 
194947c6ae99SBarry Smith /*@
195047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
195147c6ae99SBarry Smith 
195247c6ae99SBarry Smith     Neighbor-wise Collective on DM
195347c6ae99SBarry Smith 
195447c6ae99SBarry Smith     Input Parameters:
195547c6ae99SBarry Smith +   dm - the DM object
195647c6ae99SBarry Smith .   g - the global vector
195747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
195847c6ae99SBarry Smith -   l - the local vector
195947c6ae99SBarry Smith 
196047c6ae99SBarry Smith 
196147c6ae99SBarry Smith     Level: beginner
196247c6ae99SBarry Smith 
1963e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
196447c6ae99SBarry Smith 
196547c6ae99SBarry Smith @*/
19667087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
196747c6ae99SBarry Smith {
19687128ae9fSMatthew G Knepley   PetscSF                 sf;
196947c6ae99SBarry Smith   PetscErrorCode          ierr;
1970baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
197147c6ae99SBarry Smith 
197247c6ae99SBarry Smith   PetscFunctionBegin;
1973171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1974baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
19758865f1eaSKarl Rupp     if (link->beginhook) {
19768865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
19778865f1eaSKarl Rupp     }
1978baf369e7SPeter Brune   }
19797128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19807128ae9fSMatthew G Knepley   if (sf) {
1981ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1982ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
19837128ae9fSMatthew G Knepley 
198482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19857128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1986ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19877128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19887128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1989ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19907128ae9fSMatthew G Knepley   } else {
1991843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19927128ae9fSMatthew G Knepley   }
199347c6ae99SBarry Smith   PetscFunctionReturn(0);
199447c6ae99SBarry Smith }
199547c6ae99SBarry Smith 
199647c6ae99SBarry Smith /*@
199747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
199847c6ae99SBarry Smith 
199947c6ae99SBarry Smith     Neighbor-wise Collective on DM
200047c6ae99SBarry Smith 
200147c6ae99SBarry Smith     Input Parameters:
200247c6ae99SBarry Smith +   dm - the DM object
200347c6ae99SBarry Smith .   g - the global vector
200447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
200547c6ae99SBarry Smith -   l - the local vector
200647c6ae99SBarry Smith 
200747c6ae99SBarry Smith 
200847c6ae99SBarry Smith     Level: beginner
200947c6ae99SBarry Smith 
2010e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
201147c6ae99SBarry Smith 
201247c6ae99SBarry Smith @*/
20137087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
201447c6ae99SBarry Smith {
20157128ae9fSMatthew G Knepley   PetscSF                 sf;
201647c6ae99SBarry Smith   PetscErrorCode          ierr;
2017ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2018ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2019baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
202047c6ae99SBarry Smith 
202147c6ae99SBarry Smith   PetscFunctionBegin;
2022171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20237128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20247128ae9fSMatthew G Knepley   if (sf) {
202582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20267128ae9fSMatthew G Knepley 
20277128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2028ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20297128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20307128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2031ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20327128ae9fSMatthew G Knepley   } else {
2033843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20347128ae9fSMatthew G Knepley   }
20354c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2036baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2037baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2038baf369e7SPeter Brune   }
203947c6ae99SBarry Smith   PetscFunctionReturn(0);
204047c6ae99SBarry Smith }
204147c6ae99SBarry Smith 
2042d4d07f1eSToby Isaac /*@C
2043d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2044d4d07f1eSToby Isaac 
2045d4d07f1eSToby Isaac    Logically Collective
2046d4d07f1eSToby Isaac 
2047d4d07f1eSToby Isaac    Input Arguments:
2048d4d07f1eSToby Isaac +  dm - the DM
2049d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2050d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2051d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2052d4d07f1eSToby Isaac 
2053d4d07f1eSToby Isaac    Calling sequence for beginhook:
2054d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2055d4d07f1eSToby Isaac 
2056d4d07f1eSToby Isaac +  dm - global DM
2057d4d07f1eSToby Isaac .  l - local vector
2058d4d07f1eSToby Isaac .  mode - mode
2059d4d07f1eSToby Isaac .  g - global vector
2060d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2061d4d07f1eSToby Isaac 
2062d4d07f1eSToby Isaac 
2063d4d07f1eSToby Isaac    Calling sequence for endhook:
2064d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2065d4d07f1eSToby Isaac 
2066d4d07f1eSToby Isaac +  global - global DM
2067d4d07f1eSToby Isaac .  l - local vector
2068d4d07f1eSToby Isaac .  mode - mode
2069d4d07f1eSToby Isaac .  g - global vector
2070d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2071d4d07f1eSToby Isaac 
2072d4d07f1eSToby Isaac    Level: advanced
2073d4d07f1eSToby Isaac 
2074d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2075d4d07f1eSToby Isaac @*/
2076d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2077d4d07f1eSToby Isaac {
2078d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2079d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2080d4d07f1eSToby Isaac 
2081d4d07f1eSToby Isaac   PetscFunctionBegin;
2082d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2083d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
208495dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2085d4d07f1eSToby Isaac   link->beginhook = beginhook;
2086d4d07f1eSToby Isaac   link->endhook   = endhook;
2087d4d07f1eSToby Isaac   link->ctx       = ctx;
2088d4d07f1eSToby Isaac   link->next      = NULL;
2089d4d07f1eSToby Isaac   *p              = link;
2090d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2091d4d07f1eSToby Isaac }
2092d4d07f1eSToby Isaac 
20934c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
20944c274da1SToby Isaac {
20954c274da1SToby Isaac   Mat cMat;
20964c274da1SToby Isaac   Vec cVec;
20974c274da1SToby Isaac   PetscSection section, cSec;
20984c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20994c274da1SToby Isaac   PetscErrorCode ierr;
21004c274da1SToby Isaac 
21014c274da1SToby Isaac   PetscFunctionBegin;
21024c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21034c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21044c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
21055db9a05bSToby Isaac     PetscInt nRows;
21065db9a05bSToby Isaac 
21075db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21085db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
21094c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
21107711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21114c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21124c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21134c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21144c274da1SToby Isaac       if (dof) {
21154c274da1SToby Isaac         PetscInt d;
21164c274da1SToby Isaac         PetscScalar *vals;
21174c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
21184c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
21194c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
21204c274da1SToby Isaac          * we just extracted */
21214c274da1SToby Isaac         for (d = 0; d < dof; d++) {
21224c274da1SToby Isaac           vals[d] = 0.;
21234c274da1SToby Isaac         }
21244c274da1SToby Isaac       }
21254c274da1SToby Isaac     }
21264c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
21274c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21284c274da1SToby Isaac   }
21294c274da1SToby Isaac   PetscFunctionReturn(0);
21304c274da1SToby Isaac }
21314c274da1SToby Isaac 
213247c6ae99SBarry Smith /*@
21339a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21349a42bb27SBarry Smith 
21359a42bb27SBarry Smith     Neighbor-wise Collective on DM
21369a42bb27SBarry Smith 
21379a42bb27SBarry Smith     Input Parameters:
21389a42bb27SBarry Smith +   dm - the DM object
2139f6813fd5SJed Brown .   l - the local vector
21401eb28f2eSBarry 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.
21411eb28f2eSBarry Smith -   g - the global vector
21429a42bb27SBarry Smith 
2143d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
214484330215SMatthew 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.
21459a42bb27SBarry Smith 
21469a42bb27SBarry Smith     Level: beginner
21479a42bb27SBarry Smith 
2148e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
21499a42bb27SBarry Smith 
21509a42bb27SBarry Smith @*/
21517087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
21529a42bb27SBarry Smith {
21537128ae9fSMatthew G Knepley   PetscSF                 sf;
215484330215SMatthew G. Knepley   PetscSection            s, gs;
2155d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2156ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2157ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
215884330215SMatthew G. Knepley   PetscBool               isInsert;
215984330215SMatthew G. Knepley   PetscErrorCode          ierr;
21609a42bb27SBarry Smith 
21619a42bb27SBarry Smith   PetscFunctionBegin;
2162171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2163d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2164d4d07f1eSToby Isaac     if (link->beginhook) {
2165d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2166d4d07f1eSToby Isaac     }
2167d4d07f1eSToby Isaac   }
21684c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
21697128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
217084330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21717128ae9fSMatthew G Knepley   switch (mode) {
21727128ae9fSMatthew G Knepley   case INSERT_VALUES:
21737128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2174304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
217584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21767128ae9fSMatthew G Knepley   case ADD_VALUES:
21777128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2178304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
217984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21807128ae9fSMatthew G Knepley   default:
218182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21827128ae9fSMatthew G Knepley   }
218384330215SMatthew G. Knepley   if (sf && !isInsert) {
2184ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21857128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2186a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2187ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
218884330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
218984330215SMatthew G. Knepley   } else if (s && isInsert) {
219084330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
219184330215SMatthew G. Knepley 
219284330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
219384330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
219484330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2195ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
219684330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
219784330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2198b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
219984330215SMatthew G. Knepley 
220084330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
220103442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
220284330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2203b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
220484330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
220584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2206b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
220703442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2208b3b16f48SMatthew 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);
2209b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2210b3b16f48SMatthew G. Knepley       if (!gcdof) {
221184330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2212b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2213b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
221484330215SMatthew G. Knepley         const PetscInt *cdofs;
221584330215SMatthew G. Knepley         PetscInt        cind = 0;
221684330215SMatthew G. Knepley 
221784330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2218b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
221984330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2220b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
222184330215SMatthew G. Knepley         }
2222b3b16f48SMatthew 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);
222384330215SMatthew G. Knepley     }
2224ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22257128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
22267128ae9fSMatthew G Knepley   } else {
2227843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22287128ae9fSMatthew G Knepley   }
22299a42bb27SBarry Smith   PetscFunctionReturn(0);
22309a42bb27SBarry Smith }
22319a42bb27SBarry Smith 
22329a42bb27SBarry Smith /*@
22339a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
223447c6ae99SBarry Smith 
223547c6ae99SBarry Smith     Neighbor-wise Collective on DM
223647c6ae99SBarry Smith 
223747c6ae99SBarry Smith     Input Parameters:
223847c6ae99SBarry Smith +   dm - the DM object
2239f6813fd5SJed Brown .   l - the local vector
224047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2241f6813fd5SJed Brown -   g - the global vector
224247c6ae99SBarry Smith 
224347c6ae99SBarry Smith 
224447c6ae99SBarry Smith     Level: beginner
224547c6ae99SBarry Smith 
2246e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
224747c6ae99SBarry Smith 
224847c6ae99SBarry Smith @*/
22497087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
225047c6ae99SBarry Smith {
22517128ae9fSMatthew G Knepley   PetscSF                 sf;
225284330215SMatthew G. Knepley   PetscSection            s;
2253d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
225484330215SMatthew G. Knepley   PetscBool               isInsert;
225584330215SMatthew G. Knepley   PetscErrorCode          ierr;
225647c6ae99SBarry Smith 
225747c6ae99SBarry Smith   PetscFunctionBegin;
2258171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22597128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
226084330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22617128ae9fSMatthew G Knepley   switch (mode) {
22627128ae9fSMatthew G Knepley   case INSERT_VALUES:
22637128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
226484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22657128ae9fSMatthew G Knepley   case ADD_VALUES:
22667128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
226784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22687128ae9fSMatthew G Knepley   default:
226982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22707128ae9fSMatthew G Knepley   }
227184330215SMatthew G. Knepley   if (sf && !isInsert) {
2272ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2273ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
227484330215SMatthew G. Knepley 
2275ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22767128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2277a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2278ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22797128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
228084330215SMatthew G. Knepley   } else if (s && isInsert) {
22817128ae9fSMatthew G Knepley   } else {
2282843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22837128ae9fSMatthew G Knepley   }
2284d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2285d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2286d4d07f1eSToby Isaac   }
228747c6ae99SBarry Smith   PetscFunctionReturn(0);
228847c6ae99SBarry Smith }
228947c6ae99SBarry Smith 
2290f089877aSRichard Tran Mills /*@
2291bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2292bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2293d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2294f089877aSRichard Tran Mills 
2295bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2296f089877aSRichard Tran Mills 
2297f089877aSRichard Tran Mills    Input Parameters:
2298f089877aSRichard Tran Mills +  dm - the DM object
2299bc0a1609SRichard Tran Mills .  g - the original local vector
2300bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2301f089877aSRichard Tran Mills 
2302bc0a1609SRichard Tran Mills    Output Parameter:
2303bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2304f089877aSRichard Tran Mills 
2305f089877aSRichard Tran Mills    Level: intermediate
2306f089877aSRichard Tran Mills 
2307bc0a1609SRichard Tran Mills    Notes:
2308bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2309bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2310bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2311bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2312bc0a1609SRichard Tran Mills 
2313bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2314bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2315f089877aSRichard Tran Mills 
2316f089877aSRichard Tran Mills @*/
2317f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2318f089877aSRichard Tran Mills {
2319f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2320f089877aSRichard Tran Mills 
2321f089877aSRichard Tran Mills   PetscFunctionBegin;
2322f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2323bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2324f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2325f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2326f089877aSRichard Tran Mills }
2327f089877aSRichard Tran Mills 
2328f089877aSRichard Tran Mills /*@
2329bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2330bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2331d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2332f089877aSRichard Tran Mills 
2333bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2334f089877aSRichard Tran Mills 
2335f089877aSRichard Tran Mills    Input Parameters:
2336bc0a1609SRichard Tran Mills +  da - the DM object
2337bc0a1609SRichard Tran Mills .  g - the original local vector
2338bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2339f089877aSRichard Tran Mills 
2340bc0a1609SRichard Tran Mills    Output Parameter:
2341bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2342f089877aSRichard Tran Mills 
2343f089877aSRichard Tran Mills    Level: intermediate
2344f089877aSRichard Tran Mills 
2345bc0a1609SRichard Tran Mills    Notes:
2346bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2347bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2348bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2349bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2350bc0a1609SRichard Tran Mills 
2351bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2352bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2353f089877aSRichard Tran Mills 
2354f089877aSRichard Tran Mills @*/
2355f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2356f089877aSRichard Tran Mills {
2357f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2358f089877aSRichard Tran Mills 
2359f089877aSRichard Tran Mills   PetscFunctionBegin;
2360f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2361bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2362f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2363f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2364f089877aSRichard Tran Mills }
2365f089877aSRichard Tran Mills 
2366f089877aSRichard Tran Mills 
236747c6ae99SBarry Smith /*@
236847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
236947c6ae99SBarry Smith 
237047c6ae99SBarry Smith     Collective on DM
237147c6ae99SBarry Smith 
237247c6ae99SBarry Smith     Input Parameter:
237347c6ae99SBarry Smith +   dm - the DM object
237491d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
237547c6ae99SBarry Smith 
237647c6ae99SBarry Smith     Output Parameter:
237747c6ae99SBarry Smith .   dmc - the coarsened DM
237847c6ae99SBarry Smith 
237947c6ae99SBarry Smith     Level: developer
238047c6ae99SBarry Smith 
2381e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
238247c6ae99SBarry Smith 
238347c6ae99SBarry Smith @*/
23847087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
238547c6ae99SBarry Smith {
238647c6ae99SBarry Smith   PetscErrorCode    ierr;
2387b17ce1afSJed Brown   DMCoarsenHookLink link;
238847c6ae99SBarry Smith 
238947c6ae99SBarry Smith   PetscFunctionBegin;
2390171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2391fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
239247a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
239347c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
23948892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2395a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
239643842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
23978cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2398644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
23990598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2400656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2401e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2402b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2403b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2404b17ce1afSJed Brown   }
240547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2406b17ce1afSJed Brown   PetscFunctionReturn(0);
2407b17ce1afSJed Brown }
2408b17ce1afSJed Brown 
2409bb9467b5SJed Brown /*@C
2410b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2411b17ce1afSJed Brown 
2412b17ce1afSJed Brown    Logically Collective
2413b17ce1afSJed Brown 
2414b17ce1afSJed Brown    Input Arguments:
2415b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2416b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2417b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
24180298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2419b17ce1afSJed Brown 
2420b17ce1afSJed Brown    Calling sequence of coarsenhook:
2421b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2422b17ce1afSJed Brown 
2423b17ce1afSJed Brown +  fine - fine level DM
2424b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2425b17ce1afSJed Brown -  ctx - optional user-defined function context
2426b17ce1afSJed Brown 
2427b17ce1afSJed Brown    Calling sequence for restricthook:
2428c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2429b17ce1afSJed Brown 
2430b17ce1afSJed Brown +  fine - fine level DM
2431b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2432c833c3b5SJed Brown .  rscale - scaling vector for restriction
2433c833c3b5SJed Brown .  inject - matrix restricting by injection
2434b17ce1afSJed Brown .  coarse - coarse level DM to update
2435b17ce1afSJed Brown -  ctx - optional user-defined function context
2436b17ce1afSJed Brown 
2437b17ce1afSJed Brown    Level: advanced
2438b17ce1afSJed Brown 
2439b17ce1afSJed Brown    Notes:
2440b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2441b17ce1afSJed Brown 
2442b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2443b17ce1afSJed Brown 
2444b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2445b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2446b17ce1afSJed Brown 
2447bb9467b5SJed Brown    This function is currently not available from Fortran.
2448bb9467b5SJed Brown 
2449dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2450b17ce1afSJed Brown @*/
2451b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2452b17ce1afSJed Brown {
2453b17ce1afSJed Brown   PetscErrorCode    ierr;
2454b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2455b17ce1afSJed Brown 
2456b17ce1afSJed Brown   PetscFunctionBegin;
2457b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
24581e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
24591e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
24601e3d8eccSJed Brown   }
246195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2462b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2463b17ce1afSJed Brown   link->restricthook = restricthook;
2464b17ce1afSJed Brown   link->ctx          = ctx;
24650298fd71SBarry Smith   link->next         = NULL;
2466b17ce1afSJed Brown   *p                 = link;
2467b17ce1afSJed Brown   PetscFunctionReturn(0);
2468b17ce1afSJed Brown }
2469b17ce1afSJed Brown 
2470dc822a44SJed Brown /*@C
2471dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2472dc822a44SJed Brown 
2473dc822a44SJed Brown    Logically Collective
2474dc822a44SJed Brown 
2475dc822a44SJed Brown    Input Arguments:
2476dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2477dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2478dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2479dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2480dc822a44SJed Brown 
2481dc822a44SJed Brown    Level: advanced
2482dc822a44SJed Brown 
2483dc822a44SJed Brown    Notes:
2484dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2485dc822a44SJed Brown 
2486dc822a44SJed Brown    This function is currently not available from Fortran.
2487dc822a44SJed Brown 
2488dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2489dc822a44SJed Brown @*/
2490dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2491dc822a44SJed Brown {
2492dc822a44SJed Brown   PetscErrorCode    ierr;
2493dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2494dc822a44SJed Brown 
2495dc822a44SJed Brown   PetscFunctionBegin;
2496dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2497dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2498dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2499dc822a44SJed Brown       link = *p;
2500dc822a44SJed Brown       *p = link->next;
2501dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2502dc822a44SJed Brown       break;
2503dc822a44SJed Brown     }
2504dc822a44SJed Brown   }
2505dc822a44SJed Brown   PetscFunctionReturn(0);
2506dc822a44SJed Brown }
2507dc822a44SJed Brown 
2508dc822a44SJed Brown 
2509b17ce1afSJed Brown /*@
2510b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2511b17ce1afSJed Brown 
2512b17ce1afSJed Brown    Collective if any hooks are
2513b17ce1afSJed Brown 
2514b17ce1afSJed Brown    Input Arguments:
2515b17ce1afSJed Brown +  fine - finer DM to use as a base
2516b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2517e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2518b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2519e91eccc2SStefano Zampini -  coarse - coarser DM to update
2520b17ce1afSJed Brown 
2521b17ce1afSJed Brown    Level: developer
2522b17ce1afSJed Brown 
2523b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2524b17ce1afSJed Brown @*/
2525b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2526b17ce1afSJed Brown {
2527b17ce1afSJed Brown   PetscErrorCode    ierr;
2528b17ce1afSJed Brown   DMCoarsenHookLink link;
2529b17ce1afSJed Brown 
2530b17ce1afSJed Brown   PetscFunctionBegin;
2531b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
25328865f1eaSKarl Rupp     if (link->restricthook) {
25338865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
25348865f1eaSKarl Rupp     }
2535b17ce1afSJed Brown   }
253647c6ae99SBarry Smith   PetscFunctionReturn(0);
253747c6ae99SBarry Smith }
253847c6ae99SBarry Smith 
2539bb9467b5SJed Brown /*@C
2540be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
25415dbd56e3SPeter Brune 
25425dbd56e3SPeter Brune    Logically Collective
25435dbd56e3SPeter Brune 
25445dbd56e3SPeter Brune    Input Arguments:
25455dbd56e3SPeter Brune +  global - global DM
2546ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
25475dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
25480298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
25495dbd56e3SPeter Brune 
2550ec4806b8SPeter Brune 
2551ec4806b8SPeter Brune    Calling sequence for ddhook:
2552ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2553ec4806b8SPeter Brune 
2554ec4806b8SPeter Brune +  global - global DM
2555ec4806b8SPeter Brune .  block  - block DM
2556ec4806b8SPeter Brune -  ctx - optional user-defined function context
2557ec4806b8SPeter Brune 
25585dbd56e3SPeter Brune    Calling sequence for restricthook:
2559ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
25605dbd56e3SPeter Brune 
25615dbd56e3SPeter Brune +  global - global DM
25625dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
25635dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2564ec4806b8SPeter Brune .  block  - block DM
25655dbd56e3SPeter Brune -  ctx - optional user-defined function context
25665dbd56e3SPeter Brune 
25675dbd56e3SPeter Brune    Level: advanced
25685dbd56e3SPeter Brune 
25695dbd56e3SPeter Brune    Notes:
2570ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
25715dbd56e3SPeter Brune 
25725dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
25735dbd56e3SPeter Brune 
25745dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2575ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
25765dbd56e3SPeter Brune 
2577bb9467b5SJed Brown    This function is currently not available from Fortran.
2578bb9467b5SJed Brown 
25795dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
25805dbd56e3SPeter Brune @*/
2581be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
25825dbd56e3SPeter Brune {
25835dbd56e3SPeter Brune   PetscErrorCode      ierr;
2584be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
25855dbd56e3SPeter Brune 
25865dbd56e3SPeter Brune   PetscFunctionBegin;
25875dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2588b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2589b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2590b3a6b972SJed Brown   }
259195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
25925dbd56e3SPeter Brune   link->restricthook = restricthook;
2593be081cd6SPeter Brune   link->ddhook       = ddhook;
25945dbd56e3SPeter Brune   link->ctx          = ctx;
25950298fd71SBarry Smith   link->next         = NULL;
25965dbd56e3SPeter Brune   *p                 = link;
25975dbd56e3SPeter Brune   PetscFunctionReturn(0);
25985dbd56e3SPeter Brune }
25995dbd56e3SPeter Brune 
2600b3a6b972SJed Brown /*@C
2601b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2602b3a6b972SJed Brown 
2603b3a6b972SJed Brown    Logically Collective
2604b3a6b972SJed Brown 
2605b3a6b972SJed Brown    Input Arguments:
2606b3a6b972SJed Brown +  global - global DM
2607b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2608b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2609b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2610b3a6b972SJed Brown 
2611b3a6b972SJed Brown    Level: advanced
2612b3a6b972SJed Brown 
2613b3a6b972SJed Brown    Notes:
2614b3a6b972SJed Brown 
2615b3a6b972SJed Brown    This function is currently not available from Fortran.
2616b3a6b972SJed Brown 
2617b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2618b3a6b972SJed Brown @*/
2619b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2620b3a6b972SJed Brown {
2621b3a6b972SJed Brown   PetscErrorCode      ierr;
2622b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2623b3a6b972SJed Brown 
2624b3a6b972SJed Brown   PetscFunctionBegin;
2625b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2626b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2627b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2628b3a6b972SJed Brown       link = *p;
2629b3a6b972SJed Brown       *p = link->next;
2630b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2631b3a6b972SJed Brown       break;
2632b3a6b972SJed Brown     }
2633b3a6b972SJed Brown   }
2634b3a6b972SJed Brown   PetscFunctionReturn(0);
2635b3a6b972SJed Brown }
2636b3a6b972SJed Brown 
26375dbd56e3SPeter Brune /*@
2638be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
26395dbd56e3SPeter Brune 
26405dbd56e3SPeter Brune    Collective if any hooks are
26415dbd56e3SPeter Brune 
26425dbd56e3SPeter Brune    Input Arguments:
26435dbd56e3SPeter Brune +  fine - finer DM to use as a base
2644be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2645be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
26465dbd56e3SPeter Brune -  coarse - coarer DM to update
26475dbd56e3SPeter Brune 
26485dbd56e3SPeter Brune    Level: developer
26495dbd56e3SPeter Brune 
26505dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
26515dbd56e3SPeter Brune @*/
2652be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
26535dbd56e3SPeter Brune {
26545dbd56e3SPeter Brune   PetscErrorCode      ierr;
2655be081cd6SPeter Brune   DMSubDomainHookLink link;
26565dbd56e3SPeter Brune 
26575dbd56e3SPeter Brune   PetscFunctionBegin;
2658be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
26598865f1eaSKarl Rupp     if (link->restricthook) {
26608865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
26618865f1eaSKarl Rupp     }
26625dbd56e3SPeter Brune   }
26635dbd56e3SPeter Brune   PetscFunctionReturn(0);
26645dbd56e3SPeter Brune }
26655dbd56e3SPeter Brune 
26665fe1f584SPeter Brune /*@
26676a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
26685fe1f584SPeter Brune 
26695fe1f584SPeter Brune     Not Collective
26705fe1f584SPeter Brune 
26715fe1f584SPeter Brune     Input Parameter:
26725fe1f584SPeter Brune .   dm - the DM object
26735fe1f584SPeter Brune 
26745fe1f584SPeter Brune     Output Parameter:
26756a7d9d85SPeter Brune .   level - number of coarsenings
26765fe1f584SPeter Brune 
26775fe1f584SPeter Brune     Level: developer
26785fe1f584SPeter Brune 
26796a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
26805fe1f584SPeter Brune 
26815fe1f584SPeter Brune @*/
26825fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
26835fe1f584SPeter Brune {
26845fe1f584SPeter Brune   PetscFunctionBegin;
26855fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26865fe1f584SPeter Brune   *level = dm->leveldown;
26875fe1f584SPeter Brune   PetscFunctionReturn(0);
26885fe1f584SPeter Brune }
26895fe1f584SPeter Brune 
26905fe1f584SPeter Brune 
26915fe1f584SPeter Brune 
269247c6ae99SBarry Smith /*@C
269347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
269447c6ae99SBarry Smith 
269547c6ae99SBarry Smith     Collective on DM
269647c6ae99SBarry Smith 
269747c6ae99SBarry Smith     Input Parameter:
269847c6ae99SBarry Smith +   dm - the DM object
269947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
270047c6ae99SBarry Smith 
270147c6ae99SBarry Smith     Output Parameter:
270247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
270347c6ae99SBarry Smith 
270447c6ae99SBarry Smith     Level: developer
270547c6ae99SBarry Smith 
2706e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
270747c6ae99SBarry Smith 
270847c6ae99SBarry Smith @*/
27097087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
271047c6ae99SBarry Smith {
271147c6ae99SBarry Smith   PetscErrorCode ierr;
271247c6ae99SBarry Smith 
271347c6ae99SBarry Smith   PetscFunctionBegin;
2714171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2715ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
271647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
271747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
271847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
271947c6ae99SBarry Smith   } else if (dm->ops->refine) {
272047c6ae99SBarry Smith     PetscInt i;
272147c6ae99SBarry Smith 
2722ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
272347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2724ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
272547c6ae99SBarry Smith     }
2726ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
272747c6ae99SBarry Smith   PetscFunctionReturn(0);
272847c6ae99SBarry Smith }
272947c6ae99SBarry Smith 
273047c6ae99SBarry Smith /*@C
273147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
273247c6ae99SBarry Smith 
273347c6ae99SBarry Smith     Collective on DM
273447c6ae99SBarry Smith 
273547c6ae99SBarry Smith     Input Parameter:
273647c6ae99SBarry Smith +   dm - the DM object
273747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
273847c6ae99SBarry Smith 
273947c6ae99SBarry Smith     Output Parameter:
274047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
274147c6ae99SBarry Smith 
274247c6ae99SBarry Smith     Level: developer
274347c6ae99SBarry Smith 
2744e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
274547c6ae99SBarry Smith 
274647c6ae99SBarry Smith @*/
27477087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
274847c6ae99SBarry Smith {
274947c6ae99SBarry Smith   PetscErrorCode ierr;
275047c6ae99SBarry Smith 
275147c6ae99SBarry Smith   PetscFunctionBegin;
2752171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2753ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
275447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
275547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
275647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
275747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
275847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
275947c6ae99SBarry Smith     PetscInt i;
276047c6ae99SBarry Smith 
2761ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
276247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2763ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
276447c6ae99SBarry Smith     }
2765ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
276647c6ae99SBarry Smith   PetscFunctionReturn(0);
276747c6ae99SBarry Smith }
276847c6ae99SBarry Smith 
276947c6ae99SBarry Smith /*@
2770e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
277147c6ae99SBarry Smith    grids associated with two DMs.
277247c6ae99SBarry Smith 
277347c6ae99SBarry Smith    Collective on DM
277447c6ae99SBarry Smith 
277547c6ae99SBarry Smith    Input Parameters:
277647c6ae99SBarry Smith +  dmc - the coarse grid DM
277747c6ae99SBarry Smith -  dmf - the fine grid DM
277847c6ae99SBarry Smith 
277947c6ae99SBarry Smith    Output Parameters:
278047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
278147c6ae99SBarry Smith 
278247c6ae99SBarry Smith    Level: intermediate
278347c6ae99SBarry Smith 
278447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
278547c6ae99SBarry Smith 
2786e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
278747c6ae99SBarry Smith @*/
2788e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
278947c6ae99SBarry Smith {
279047c6ae99SBarry Smith   PetscErrorCode ierr;
279147c6ae99SBarry Smith 
279247c6ae99SBarry Smith   PetscFunctionBegin;
2793171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2794171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
279547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
279647c6ae99SBarry Smith   PetscFunctionReturn(0);
279747c6ae99SBarry Smith }
279847c6ae99SBarry Smith 
27991a266240SBarry Smith /*@C
28001a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
28011a266240SBarry Smith 
28021a266240SBarry Smith     Not Collective
28031a266240SBarry Smith 
28041a266240SBarry Smith     Input Parameters:
28051a266240SBarry Smith +   dm - the DM object
28061a266240SBarry Smith -   destroy - the destroy function
28071a266240SBarry Smith 
28081a266240SBarry Smith     Level: intermediate
28091a266240SBarry Smith 
2810e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
28111a266240SBarry Smith 
2812f07f9ceaSJed Brown @*/
28131a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
28141a266240SBarry Smith {
28151a266240SBarry Smith   PetscFunctionBegin;
2816171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28171a266240SBarry Smith   dm->ctxdestroy = destroy;
28181a266240SBarry Smith   PetscFunctionReturn(0);
28191a266240SBarry Smith }
28201a266240SBarry Smith 
2821b07ff414SBarry Smith /*@
28221b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
282347c6ae99SBarry Smith 
282447c6ae99SBarry Smith     Not Collective
282547c6ae99SBarry Smith 
282647c6ae99SBarry Smith     Input Parameters:
282747c6ae99SBarry Smith +   dm - the DM object
282847c6ae99SBarry Smith -   ctx - the user context
282947c6ae99SBarry Smith 
283047c6ae99SBarry Smith     Level: intermediate
283147c6ae99SBarry Smith 
2832e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
283347c6ae99SBarry Smith 
283447c6ae99SBarry Smith @*/
28351b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
283647c6ae99SBarry Smith {
283747c6ae99SBarry Smith   PetscFunctionBegin;
2838171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
283947c6ae99SBarry Smith   dm->ctx = ctx;
284047c6ae99SBarry Smith   PetscFunctionReturn(0);
284147c6ae99SBarry Smith }
284247c6ae99SBarry Smith 
284347c6ae99SBarry Smith /*@
28441b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
284547c6ae99SBarry Smith 
284647c6ae99SBarry Smith     Not Collective
284747c6ae99SBarry Smith 
284847c6ae99SBarry Smith     Input Parameter:
284947c6ae99SBarry Smith .   dm - the DM object
285047c6ae99SBarry Smith 
285147c6ae99SBarry Smith     Output Parameter:
285247c6ae99SBarry Smith .   ctx - the user context
285347c6ae99SBarry Smith 
285447c6ae99SBarry Smith     Level: intermediate
285547c6ae99SBarry Smith 
2856e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
285747c6ae99SBarry Smith 
285847c6ae99SBarry Smith @*/
28591b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
286047c6ae99SBarry Smith {
286147c6ae99SBarry Smith   PetscFunctionBegin;
2862171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28631b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
286447c6ae99SBarry Smith   PetscFunctionReturn(0);
286547c6ae99SBarry Smith }
286647c6ae99SBarry Smith 
286708da532bSDmitry Karpeev /*@C
2868df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
286908da532bSDmitry Karpeev 
287008da532bSDmitry Karpeev     Logically Collective on DM
287108da532bSDmitry Karpeev 
287208da532bSDmitry Karpeev     Input Parameter:
287308da532bSDmitry Karpeev +   dm - the DM object
28740298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
287508da532bSDmitry Karpeev 
287608da532bSDmitry Karpeev     Level: intermediate
287708da532bSDmitry Karpeev 
2878835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
287908da532bSDmitry Karpeev          DMSetJacobian()
288008da532bSDmitry Karpeev 
288108da532bSDmitry Karpeev @*/
288208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
288308da532bSDmitry Karpeev {
288408da532bSDmitry Karpeev   PetscFunctionBegin;
288508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
288608da532bSDmitry Karpeev   PetscFunctionReturn(0);
288708da532bSDmitry Karpeev }
288808da532bSDmitry Karpeev 
288908da532bSDmitry Karpeev /*@
289008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
289108da532bSDmitry Karpeev 
289208da532bSDmitry Karpeev     Not Collective
289308da532bSDmitry Karpeev 
289408da532bSDmitry Karpeev     Input Parameter:
289508da532bSDmitry Karpeev .   dm - the DM object to destroy
289608da532bSDmitry Karpeev 
289708da532bSDmitry Karpeev     Output Parameter:
289808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
289908da532bSDmitry Karpeev 
290008da532bSDmitry Karpeev     Level: developer
290108da532bSDmitry Karpeev 
290274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
290308da532bSDmitry Karpeev 
290408da532bSDmitry Karpeev @*/
290508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
290608da532bSDmitry Karpeev {
290708da532bSDmitry Karpeev   PetscFunctionBegin;
290808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
290908da532bSDmitry Karpeev   PetscFunctionReturn(0);
291008da532bSDmitry Karpeev }
291108da532bSDmitry Karpeev 
291208da532bSDmitry Karpeev /*@C
291308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
291408da532bSDmitry Karpeev 
291508da532bSDmitry Karpeev     Logically Collective on DM
291608da532bSDmitry Karpeev 
291708da532bSDmitry Karpeev     Input Parameters:
2918907376e6SBarry Smith .   dm - the DM object
291908da532bSDmitry Karpeev 
292008da532bSDmitry Karpeev     Output parameters:
292108da532bSDmitry Karpeev +   xl - lower bound
292208da532bSDmitry Karpeev -   xu - upper bound
292308da532bSDmitry Karpeev 
2924907376e6SBarry Smith     Level: advanced
2925907376e6SBarry Smith 
2926907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
292708da532bSDmitry Karpeev 
292874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
292908da532bSDmitry Karpeev 
293008da532bSDmitry Karpeev @*/
293108da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
293208da532bSDmitry Karpeev {
293308da532bSDmitry Karpeev   PetscErrorCode ierr;
29345fd66863SKarl Rupp 
293508da532bSDmitry Karpeev   PetscFunctionBegin;
293608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
293708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
293808da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
293908da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
29408865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
294108da532bSDmitry Karpeev   PetscFunctionReturn(0);
294208da532bSDmitry Karpeev }
294308da532bSDmitry Karpeev 
2944b0ae01b7SPeter Brune /*@
2945b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2946b0ae01b7SPeter Brune 
2947b0ae01b7SPeter Brune     Not Collective
2948b0ae01b7SPeter Brune 
2949b0ae01b7SPeter Brune     Input Parameter:
2950b0ae01b7SPeter Brune .   dm - the DM object
2951b0ae01b7SPeter Brune 
2952b0ae01b7SPeter Brune     Output Parameter:
2953b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2954b0ae01b7SPeter Brune 
2955b0ae01b7SPeter Brune     Level: developer
2956b0ae01b7SPeter Brune 
2957b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2958b0ae01b7SPeter Brune 
2959b0ae01b7SPeter Brune @*/
2960b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2961b0ae01b7SPeter Brune {
2962b0ae01b7SPeter Brune   PetscFunctionBegin;
2963b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2964b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2965b0ae01b7SPeter Brune }
2966b0ae01b7SPeter Brune 
29673ad4599aSBarry Smith /*@
29683ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
29693ad4599aSBarry Smith 
29703ad4599aSBarry Smith     Not Collective
29713ad4599aSBarry Smith 
29723ad4599aSBarry Smith     Input Parameter:
29733ad4599aSBarry Smith .   dm - the DM object
29743ad4599aSBarry Smith 
29753ad4599aSBarry Smith     Output Parameter:
29763ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
29773ad4599aSBarry Smith 
29783ad4599aSBarry Smith     Level: developer
29793ad4599aSBarry Smith 
29803ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
29813ad4599aSBarry Smith 
29823ad4599aSBarry Smith @*/
29833ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
29843ad4599aSBarry Smith {
29853ad4599aSBarry Smith   PetscFunctionBegin;
29863ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
29873ad4599aSBarry Smith   PetscFunctionReturn(0);
29883ad4599aSBarry Smith }
29893ad4599aSBarry Smith 
2990748fac09SDmitry Karpeev /*@C
299108da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
299208da532bSDmitry Karpeev 
299308da532bSDmitry Karpeev     Collective on DM
299408da532bSDmitry Karpeev 
299508da532bSDmitry Karpeev     Input Parameter:
299608da532bSDmitry Karpeev +   dm - the DM object
29970298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
299808da532bSDmitry Karpeev 
299908da532bSDmitry Karpeev     Level: developer
300008da532bSDmitry Karpeev 
300174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
300208da532bSDmitry Karpeev 
300308da532bSDmitry Karpeev @*/
300408da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
300508da532bSDmitry Karpeev {
300608da532bSDmitry Karpeev   PetscErrorCode ierr;
30075fd66863SKarl Rupp 
300808da532bSDmitry Karpeev   PetscFunctionBegin;
300908da532bSDmitry Karpeev   if (x) {
301008da532bSDmitry Karpeev     if (!dm->x) {
301108da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
301208da532bSDmitry Karpeev     }
301308da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
30148865f1eaSKarl Rupp   } else if (dm->x) {
301508da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
301608da532bSDmitry Karpeev   }
301708da532bSDmitry Karpeev   PetscFunctionReturn(0);
301808da532bSDmitry Karpeev }
301908da532bSDmitry Karpeev 
30200298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3021264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3022264ace61SBarry Smith 
3023264ace61SBarry Smith /*@C
3024264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3025264ace61SBarry Smith 
3026264ace61SBarry Smith   Collective on DM
3027264ace61SBarry Smith 
3028264ace61SBarry Smith   Input Parameters:
3029264ace61SBarry Smith + dm     - The DM object
3030264ace61SBarry Smith - method - The name of the DM type
3031264ace61SBarry Smith 
3032264ace61SBarry Smith   Options Database Key:
3033264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3034264ace61SBarry Smith 
3035264ace61SBarry Smith   Notes:
3036e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3037264ace61SBarry Smith 
3038264ace61SBarry Smith   Level: intermediate
3039264ace61SBarry Smith 
3040264ace61SBarry Smith .keywords: DM, set, type
3041264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3042264ace61SBarry Smith @*/
304319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3044264ace61SBarry Smith {
3045264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3046264ace61SBarry Smith   PetscBool      match;
3047264ace61SBarry Smith   PetscErrorCode ierr;
3048264ace61SBarry Smith 
3049264ace61SBarry Smith   PetscFunctionBegin;
3050264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3051251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3052264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3053264ace61SBarry Smith 
30540f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
30551c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3056ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3057264ace61SBarry Smith 
3058264ace61SBarry Smith   if (dm->ops->destroy) {
3059264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
30600298fd71SBarry Smith     dm->ops->destroy = NULL;
3061264ace61SBarry Smith   }
3062264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3063264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3064264ace61SBarry Smith   PetscFunctionReturn(0);
3065264ace61SBarry Smith }
3066264ace61SBarry Smith 
3067264ace61SBarry Smith /*@C
3068264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3069264ace61SBarry Smith 
3070264ace61SBarry Smith   Not Collective
3071264ace61SBarry Smith 
3072264ace61SBarry Smith   Input Parameter:
3073264ace61SBarry Smith . dm  - The DM
3074264ace61SBarry Smith 
3075264ace61SBarry Smith   Output Parameter:
3076264ace61SBarry Smith . type - The DM type name
3077264ace61SBarry Smith 
3078264ace61SBarry Smith   Level: intermediate
3079264ace61SBarry Smith 
3080264ace61SBarry Smith .keywords: DM, get, type, name
3081264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3082264ace61SBarry Smith @*/
308319fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3084264ace61SBarry Smith {
3085264ace61SBarry Smith   PetscErrorCode ierr;
3086264ace61SBarry Smith 
3087264ace61SBarry Smith   PetscFunctionBegin;
3088264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3089c959eef4SJed Brown   PetscValidPointer(type,2);
3090607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3091264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3092264ace61SBarry Smith   PetscFunctionReturn(0);
3093264ace61SBarry Smith }
3094264ace61SBarry Smith 
309567a56275SMatthew G Knepley /*@C
309667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
309767a56275SMatthew G Knepley 
309867a56275SMatthew G Knepley   Collective on DM
309967a56275SMatthew G Knepley 
310067a56275SMatthew G Knepley   Input Parameters:
310167a56275SMatthew G Knepley + dm - the DM
310267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
310367a56275SMatthew G Knepley 
310467a56275SMatthew G Knepley   Output Parameter:
310567a56275SMatthew G Knepley . M - pointer to new DM
310667a56275SMatthew G Knepley 
310767a56275SMatthew G Knepley   Notes:
310867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
310967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
311067a56275SMatthew G Knepley   of the input DM.
311167a56275SMatthew G Knepley 
311267a56275SMatthew G Knepley   Level: intermediate
311367a56275SMatthew G Knepley 
311467a56275SMatthew G Knepley .seealso: DMCreate()
311567a56275SMatthew G Knepley @*/
311619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
311767a56275SMatthew G Knepley {
311867a56275SMatthew G Knepley   DM             B;
311967a56275SMatthew G Knepley   char           convname[256];
3120c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
312167a56275SMatthew G Knepley   PetscErrorCode ierr;
312267a56275SMatthew G Knepley 
312367a56275SMatthew G Knepley   PetscFunctionBegin;
312467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
312567a56275SMatthew G Knepley   PetscValidType(dm,1);
312667a56275SMatthew G Knepley   PetscValidPointer(M,3);
3127251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3128c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3129c067b6caSMatthew G. Knepley   if (sametype) {
3130c067b6caSMatthew G. Knepley     *M   = dm;
3131c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3132c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3133c067b6caSMatthew G. Knepley   } else {
31340298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
313567a56275SMatthew G Knepley 
313667a56275SMatthew G Knepley     /*
313767a56275SMatthew G Knepley        Order of precedence:
313867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
313967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
314067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
314167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
314267a56275SMatthew G Knepley        5) Use a really basic converter.
314367a56275SMatthew G Knepley     */
314467a56275SMatthew G Knepley 
314567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
314667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
314767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
314867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
314967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
315067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
31510005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
315267a56275SMatthew G Knepley     if (conv) goto foundconv;
315367a56275SMatthew G Knepley 
315467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
315582f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
315667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
315767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
315867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
315967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
316067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
316167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
31620005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
316367a56275SMatthew G Knepley     if (conv) {
3164fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
316567a56275SMatthew G Knepley       goto foundconv;
316667a56275SMatthew G Knepley     }
316767a56275SMatthew G Knepley 
316867a56275SMatthew G Knepley #if 0
316967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
317067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3171fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
317267a56275SMatthew G Knepley     if (conv) goto foundconv;
317367a56275SMatthew G Knepley 
317467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
317567a56275SMatthew G Knepley     if (dm->ops->convert) {
317667a56275SMatthew G Knepley       conv = dm->ops->convert;
317767a56275SMatthew G Knepley     }
317867a56275SMatthew G Knepley     if (conv) goto foundconv;
317967a56275SMatthew G Knepley #endif
318067a56275SMatthew G Knepley 
318167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
318282f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
318367a56275SMatthew G Knepley 
318467a56275SMatthew G Knepley foundconv:
318567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
318667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
318712fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
318890b157c4SStefano Zampini     {
318990b157c4SStefano Zampini       PetscBool             isper;
319012fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
319112fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
319290b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
319390b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
319412fa691eSMatthew G. Knepley     }
319567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
319667a56275SMatthew G Knepley   }
319767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
319867a56275SMatthew G Knepley   PetscFunctionReturn(0);
319967a56275SMatthew G Knepley }
3200264ace61SBarry Smith 
3201264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3202264ace61SBarry Smith 
3203264ace61SBarry Smith /*@C
32041c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
32051c84c290SBarry Smith 
32061c84c290SBarry Smith   Not Collective
32071c84c290SBarry Smith 
32081c84c290SBarry Smith   Input Parameters:
32091c84c290SBarry Smith + name        - The name of a new user-defined creation routine
32101c84c290SBarry Smith - create_func - The creation routine itself
32111c84c290SBarry Smith 
32121c84c290SBarry Smith   Notes:
32131c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
32141c84c290SBarry Smith 
32151c84c290SBarry Smith 
32161c84c290SBarry Smith   Sample usage:
32171c84c290SBarry Smith .vb
3218bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
32191c84c290SBarry Smith .ve
32201c84c290SBarry Smith 
32211c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
32221c84c290SBarry Smith .vb
32231c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
32241c84c290SBarry Smith     DMSetType(DM,"my_da");
32251c84c290SBarry Smith .ve
32261c84c290SBarry Smith    or at runtime via the option
32271c84c290SBarry Smith .vb
32281c84c290SBarry Smith     -da_type my_da
32291c84c290SBarry Smith .ve
3230264ace61SBarry Smith 
3231264ace61SBarry Smith   Level: advanced
32321c84c290SBarry Smith 
32331c84c290SBarry Smith .keywords: DM, register
3234bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
32351c84c290SBarry Smith 
3236264ace61SBarry Smith @*/
3237bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3238264ace61SBarry Smith {
3239264ace61SBarry Smith   PetscErrorCode ierr;
3240264ace61SBarry Smith 
3241264ace61SBarry Smith   PetscFunctionBegin;
3242a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3243264ace61SBarry Smith   PetscFunctionReturn(0);
3244264ace61SBarry Smith }
3245264ace61SBarry Smith 
3246b859378eSBarry Smith /*@C
324755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3248b859378eSBarry Smith 
3249b859378eSBarry Smith   Collective on PetscViewer
3250b859378eSBarry Smith 
3251b859378eSBarry Smith   Input Parameters:
3252b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3253b859378eSBarry Smith            some related function before a call to DMLoad().
3254b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3255b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3256b859378eSBarry Smith 
3257b859378eSBarry Smith    Level: intermediate
3258b859378eSBarry Smith 
3259b859378eSBarry Smith   Notes:
326055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3261b859378eSBarry Smith 
3262b859378eSBarry Smith   Notes for advanced users:
3263b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3264b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3265b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3266b859378eSBarry Smith   format is
3267b859378eSBarry Smith .vb
3268b859378eSBarry Smith      has not yet been determined
3269b859378eSBarry Smith .ve
3270b859378eSBarry Smith 
3271b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3272b859378eSBarry Smith @*/
3273b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3274b859378eSBarry Smith {
32759331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3276b859378eSBarry Smith   PetscErrorCode ierr;
3277b859378eSBarry Smith 
3278b859378eSBarry Smith   PetscFunctionBegin;
3279b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3280b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
328132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
32829331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
32839331c7a4SMatthew G. Knepley   if (isbinary) {
32849331c7a4SMatthew G. Knepley     PetscInt classid;
32859331c7a4SMatthew G. Knepley     char     type[256];
3286b859378eSBarry Smith 
3287060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
32889200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3289060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
329032c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
32919331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32929331c7a4SMatthew G. Knepley   } else if (ishdf5) {
32939331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32949331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3295b859378eSBarry Smith   PetscFunctionReturn(0);
3296b859378eSBarry Smith }
3297b859378eSBarry Smith 
32987da65231SMatthew G Knepley /******************************** FEM Support **********************************/
32997da65231SMatthew G Knepley 
3300a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3301a6dfd86eSKarl Rupp {
33021d47ebbbSSatish Balay   PetscInt       f;
33031b30c384SMatthew G Knepley   PetscErrorCode ierr;
33041b30c384SMatthew G Knepley 
33057da65231SMatthew G Knepley   PetscFunctionBegin;
330674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33071d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
330857622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
33097da65231SMatthew G Knepley   }
33107da65231SMatthew G Knepley   PetscFunctionReturn(0);
33117da65231SMatthew G Knepley }
33127da65231SMatthew G Knepley 
3313a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3314a6dfd86eSKarl Rupp {
33151b30c384SMatthew G Knepley   PetscInt       f, g;
33167da65231SMatthew G Knepley   PetscErrorCode ierr;
33177da65231SMatthew G Knepley 
33187da65231SMatthew G Knepley   PetscFunctionBegin;
331974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33201d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
332174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
33221d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3323e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
33247da65231SMatthew G Knepley     }
332574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
33267da65231SMatthew G Knepley   }
33277da65231SMatthew G Knepley   PetscFunctionReturn(0);
33287da65231SMatthew G Knepley }
3329e7c4fc90SDmitry Karpeev 
33306113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3331e759306cSMatthew G. Knepley {
33320c5b8624SToby Isaac   PetscInt          localSize, bs;
33330c5b8624SToby Isaac   PetscMPIInt       size;
33340c5b8624SToby Isaac   Vec               x, xglob;
33350c5b8624SToby Isaac   const PetscScalar *xarray;
3336e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3337e759306cSMatthew G. Knepley 
3338e759306cSMatthew G. Knepley   PetscFunctionBegin;
33399852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3340e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3341e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
33426113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
33430c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
33440c5b8624SToby Isaac   if (size > 1) {
33450c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
33460c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
33470c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
33480c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
33490c5b8624SToby Isaac   } else {
33500c5b8624SToby Isaac     xglob = x;
33510c5b8624SToby Isaac   }
33520c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
33530c5b8624SToby Isaac   if (size > 1) {
33540c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
33550c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
33560c5b8624SToby Isaac   }
3357e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3358e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3359e759306cSMatthew G. Knepley }
3360e759306cSMatthew G. Knepley 
336188ed4aceSMatthew G Knepley /*@
336288ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
336388ed4aceSMatthew G Knepley 
336488ed4aceSMatthew G Knepley   Input Parameter:
336588ed4aceSMatthew G Knepley . dm - The DM
336688ed4aceSMatthew G Knepley 
336788ed4aceSMatthew G Knepley   Output Parameter:
336888ed4aceSMatthew G Knepley . section - The PetscSection
336988ed4aceSMatthew G Knepley 
337088ed4aceSMatthew G Knepley   Level: intermediate
337188ed4aceSMatthew G Knepley 
337288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
337388ed4aceSMatthew G Knepley 
337488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
337588ed4aceSMatthew G Knepley @*/
33760adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
33770adebc6cSBarry Smith {
3378fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3379fd59a867SMatthew G. Knepley 
338088ed4aceSMatthew G Knepley   PetscFunctionBegin;
338188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
338288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
33832f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
33842f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3385ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
33862f0f8703SMatthew G. Knepley   }
338788ed4aceSMatthew G Knepley   *section = dm->defaultSection;
338888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
338988ed4aceSMatthew G Knepley }
339088ed4aceSMatthew G Knepley 
339188ed4aceSMatthew G Knepley /*@
339288ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
339388ed4aceSMatthew G Knepley 
339488ed4aceSMatthew G Knepley   Input Parameters:
339588ed4aceSMatthew G Knepley + dm - The DM
339688ed4aceSMatthew G Knepley - section - The PetscSection
339788ed4aceSMatthew G Knepley 
339888ed4aceSMatthew G Knepley   Level: intermediate
339988ed4aceSMatthew G Knepley 
340088ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
340188ed4aceSMatthew G Knepley 
340288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
340388ed4aceSMatthew G Knepley @*/
34040adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
34050adebc6cSBarry Smith {
3406c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3407af122d2aSMatthew G Knepley   PetscInt       f;
340888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
340988ed4aceSMatthew G Knepley 
341088ed4aceSMatthew G Knepley   PetscFunctionBegin;
341188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3412c473ab19SMatthew G. Knepley   if (section) {
34131d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34141d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3415c473ab19SMatthew G. Knepley   }
341688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
341788ed4aceSMatthew G Knepley   dm->defaultSection = section;
3418c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3419af122d2aSMatthew G Knepley   if (numFields) {
3420af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3421af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
34220f21e855SMatthew G. Knepley       PetscObject disc;
3423af122d2aSMatthew G Knepley       const char *name;
3424af122d2aSMatthew G Knepley 
3425af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
34260f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
34270f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3428af122d2aSMatthew G Knepley     }
3429af122d2aSMatthew G Knepley   }
34301d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
34311d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
343288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
343388ed4aceSMatthew G Knepley }
343488ed4aceSMatthew G Knepley 
34359435951eSToby Isaac /*@
34369435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
34379435951eSToby Isaac 
3438e228b242SToby Isaac   not collective
3439e228b242SToby Isaac 
34409435951eSToby Isaac   Input Parameter:
34419435951eSToby Isaac . dm - The DM
34429435951eSToby Isaac 
34439435951eSToby Isaac   Output Parameter:
34449435951eSToby 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.
34459435951eSToby 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.
34469435951eSToby Isaac 
34479435951eSToby Isaac   Level: advanced
34489435951eSToby Isaac 
34499435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
34509435951eSToby Isaac 
34519435951eSToby Isaac .seealso: DMSetDefaultConstraints()
34529435951eSToby Isaac @*/
34539435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
34549435951eSToby Isaac {
34559435951eSToby Isaac   PetscErrorCode ierr;
34569435951eSToby Isaac 
34579435951eSToby Isaac   PetscFunctionBegin;
34589435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34599435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
346045a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
346145a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
34629435951eSToby Isaac   PetscFunctionReturn(0);
34639435951eSToby Isaac }
34649435951eSToby Isaac 
34659435951eSToby Isaac /*@
34669435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
34679435951eSToby Isaac 
34689435951eSToby 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().
34699435951eSToby Isaac 
34709435951eSToby 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.
34719435951eSToby Isaac 
3472e228b242SToby Isaac   collective on dm
3473e228b242SToby Isaac 
34749435951eSToby Isaac   Input Parameters:
34759435951eSToby Isaac + dm - The DM
3476e228b242SToby 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).
3477e228b242SToby 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).
34789435951eSToby Isaac 
34799435951eSToby Isaac   Level: advanced
34809435951eSToby Isaac 
34819435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
34829435951eSToby Isaac 
34839435951eSToby Isaac .seealso: DMGetDefaultConstraints()
34849435951eSToby Isaac @*/
34859435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
34869435951eSToby Isaac {
3487e228b242SToby Isaac   PetscMPIInt result;
34889435951eSToby Isaac   PetscErrorCode ierr;
34899435951eSToby Isaac 
34909435951eSToby Isaac   PetscFunctionBegin;
34919435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3492e228b242SToby Isaac   if (section) {
3493e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3494e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3495f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3496e228b242SToby Isaac   }
3497e228b242SToby Isaac   if (mat) {
3498e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3499e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3500f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3501e228b242SToby Isaac   }
35029435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
35039435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
35049435951eSToby Isaac   dm->defaultConstraintSection = section;
35059435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
35069435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
35079435951eSToby Isaac   dm->defaultConstraintMat = mat;
35089435951eSToby Isaac   PetscFunctionReturn(0);
35099435951eSToby Isaac }
35109435951eSToby Isaac 
3511f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3512507e4973SMatthew G. Knepley /*
3513507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3514507e4973SMatthew G. Knepley 
3515507e4973SMatthew G. Knepley   Input Parameters:
3516507e4973SMatthew G. Knepley + dm - The DM
3517507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3518507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3519507e4973SMatthew G. Knepley 
3520507e4973SMatthew G. Knepley   Level: intermediate
3521507e4973SMatthew G. Knepley 
3522507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3523507e4973SMatthew G. Knepley */
3524f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3525507e4973SMatthew G. Knepley {
3526507e4973SMatthew G. Knepley   MPI_Comm        comm;
3527507e4973SMatthew G. Knepley   PetscLayout     layout;
3528507e4973SMatthew G. Knepley   const PetscInt *ranges;
3529507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3530507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3531507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3532507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3533507e4973SMatthew G. Knepley 
3534507e4973SMatthew G. Knepley   PetscFunctionBegin;
3535507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3536507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3537507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3538507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3539507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3540507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3541507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3542507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3543507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3544507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3545507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3546507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3547f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3548507e4973SMatthew G. Knepley 
3549507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3550507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3551507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3552507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3553507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3554507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3555507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3556507e4973SMatthew 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;}
3557507e4973SMatthew 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;}
3558507e4973SMatthew G. Knepley     if (gdof < 0) {
3559507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3560507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3561507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3562507e4973SMatthew G. Knepley 
3563507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3564507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3565507e4973SMatthew 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;}
3566507e4973SMatthew G. Knepley       }
3567507e4973SMatthew G. Knepley     }
3568507e4973SMatthew G. Knepley   }
3569507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3570507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3571b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3572507e4973SMatthew G. Knepley   if (!gvalid) {
3573507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3574507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3575507e4973SMatthew G. Knepley   }
3576507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3577507e4973SMatthew G. Knepley }
3578f741bcd2SMatthew G. Knepley #endif
3579507e4973SMatthew G. Knepley 
358088ed4aceSMatthew G Knepley /*@
358188ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
358288ed4aceSMatthew G Knepley 
35838b1ab98fSJed Brown   Collective on DM
35848b1ab98fSJed Brown 
358588ed4aceSMatthew G Knepley   Input Parameter:
358688ed4aceSMatthew G Knepley . dm - The DM
358788ed4aceSMatthew G Knepley 
358888ed4aceSMatthew G Knepley   Output Parameter:
358988ed4aceSMatthew G Knepley . section - The PetscSection
359088ed4aceSMatthew G Knepley 
359188ed4aceSMatthew G Knepley   Level: intermediate
359288ed4aceSMatthew G Knepley 
359388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
359488ed4aceSMatthew G Knepley 
359588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
359688ed4aceSMatthew G Knepley @*/
35970adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
35980adebc6cSBarry Smith {
359988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
360088ed4aceSMatthew G Knepley 
360188ed4aceSMatthew G Knepley   PetscFunctionBegin;
360288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
360388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
360488ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3605fd59a867SMatthew G. Knepley     PetscSection s;
3606fd59a867SMatthew G. Knepley 
3607fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3608fd59a867SMatthew 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");
3609fd59a867SMatthew 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");
361015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3611cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3612ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3613685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
361488ed4aceSMatthew G Knepley   }
361588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
361688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
361788ed4aceSMatthew G Knepley }
361888ed4aceSMatthew G Knepley 
3619b21d0597SMatthew G Knepley /*@
3620b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3621b21d0597SMatthew G Knepley 
3622b21d0597SMatthew G Knepley   Input Parameters:
3623b21d0597SMatthew G Knepley + dm - The DM
36245080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3625b21d0597SMatthew G Knepley 
3626b21d0597SMatthew G Knepley   Level: intermediate
3627b21d0597SMatthew G Knepley 
3628b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3629b21d0597SMatthew G Knepley 
3630b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3631b21d0597SMatthew G Knepley @*/
36320adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
36330adebc6cSBarry Smith {
3634b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3635b21d0597SMatthew G Knepley 
3636b21d0597SMatthew G Knepley   PetscFunctionBegin;
3637b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36385080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36391d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3640b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3641b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3642507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3643f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3644507e4973SMatthew G. Knepley #endif
3645b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3646b21d0597SMatthew G Knepley }
3647b21d0597SMatthew G Knepley 
364888ed4aceSMatthew G Knepley /*@
364988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
365088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
365188ed4aceSMatthew G Knepley 
365288ed4aceSMatthew G Knepley   Input Parameter:
365388ed4aceSMatthew G Knepley . dm - The DM
365488ed4aceSMatthew G Knepley 
365588ed4aceSMatthew G Knepley   Output Parameter:
365688ed4aceSMatthew G Knepley . sf - The PetscSF
365788ed4aceSMatthew G Knepley 
365888ed4aceSMatthew G Knepley   Level: intermediate
365988ed4aceSMatthew G Knepley 
366088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
366188ed4aceSMatthew G Knepley 
366288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
366388ed4aceSMatthew G Knepley @*/
36640adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
36650adebc6cSBarry Smith {
366688ed4aceSMatthew G Knepley   PetscInt       nroots;
366788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
366888ed4aceSMatthew G Knepley 
366988ed4aceSMatthew G Knepley   PetscFunctionBegin;
367088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
367188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
36720298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
367388ed4aceSMatthew G Knepley   if (nroots < 0) {
367488ed4aceSMatthew G Knepley     PetscSection section, gSection;
367588ed4aceSMatthew G Knepley 
367688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
367731ea6d37SMatthew G Knepley     if (section) {
367888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
367988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
368031ea6d37SMatthew G Knepley     } else {
36810298fd71SBarry Smith       *sf = NULL;
368231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
368331ea6d37SMatthew G Knepley     }
368488ed4aceSMatthew G Knepley   }
368588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
368688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
368788ed4aceSMatthew G Knepley }
368888ed4aceSMatthew G Knepley 
368988ed4aceSMatthew G Knepley /*@
369088ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
369188ed4aceSMatthew G Knepley 
369288ed4aceSMatthew G Knepley   Input Parameters:
369388ed4aceSMatthew G Knepley + dm - The DM
369488ed4aceSMatthew G Knepley - sf - The PetscSF
369588ed4aceSMatthew G Knepley 
369688ed4aceSMatthew G Knepley   Level: intermediate
369788ed4aceSMatthew G Knepley 
369888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
369988ed4aceSMatthew G Knepley 
370088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
370188ed4aceSMatthew G Knepley @*/
37020adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
37030adebc6cSBarry Smith {
370488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
370588ed4aceSMatthew G Knepley 
370688ed4aceSMatthew G Knepley   PetscFunctionBegin;
370788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
370888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
370988ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
371088ed4aceSMatthew G Knepley   dm->defaultSF = sf;
371188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
371288ed4aceSMatthew G Knepley }
371388ed4aceSMatthew G Knepley 
371488ed4aceSMatthew G Knepley /*@C
371588ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
371688ed4aceSMatthew G Knepley   describing the data layout.
371788ed4aceSMatthew G Knepley 
371888ed4aceSMatthew G Knepley   Input Parameters:
371988ed4aceSMatthew G Knepley + dm - The DM
372088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
372188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
372288ed4aceSMatthew G Knepley 
372388ed4aceSMatthew G Knepley   Level: intermediate
372488ed4aceSMatthew G Knepley 
372588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
372688ed4aceSMatthew G Knepley @*/
372788ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
372888ed4aceSMatthew G Knepley {
372982f516ccSBarry Smith   MPI_Comm       comm;
373088ed4aceSMatthew G Knepley   PetscLayout    layout;
373188ed4aceSMatthew G Knepley   const PetscInt *ranges;
373288ed4aceSMatthew G Knepley   PetscInt       *local;
373388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3734ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
373588ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
373688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
373788ed4aceSMatthew G Knepley 
373888ed4aceSMatthew G Knepley   PetscFunctionBegin;
373982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
374088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374188ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
374288ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
374388ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
374488ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
374588ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
374688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
374788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
374888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
374988ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3750ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
37516636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
375288ed4aceSMatthew G Knepley 
37536636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37546636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3755235fbf56SMatthew 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));
37566636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
375788ed4aceSMatthew G Knepley   }
3758785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3759785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
376088ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
37611f588964SMatthew G Knepley     const PetscInt *cind;
37626636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
376388ed4aceSMatthew G Knepley 
376488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
376588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
376688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
376788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
376888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37696636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
377088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
37716636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
37726636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
37736636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3774057b4bcdSMatthew 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);
37756636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
37766636e97aSMatthew G Knepley     }
377788ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
377888ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
377988ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
378088ed4aceSMatthew G Knepley     }
378188ed4aceSMatthew G Knepley     if (gdof < 0) {
37826636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
378388ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
378488ed4aceSMatthew G Knepley 
378505376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
378631d3f06eSJed Brown         if (r < 0) r = -(r+2);
378705376888SMatthew 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);
378888ed4aceSMatthew G Knepley         remote[l].rank  = r;
378988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
379088ed4aceSMatthew G Knepley       }
379188ed4aceSMatthew G Knepley     } else {
37926636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
379388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
379488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
379588ed4aceSMatthew G Knepley       }
379688ed4aceSMatthew G Knepley     }
379788ed4aceSMatthew G Knepley   }
37986636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
379988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
380088ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
380188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
380288ed4aceSMatthew G Knepley }
3803af122d2aSMatthew G Knepley 
3804b21d0597SMatthew G Knepley /*@
3805b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3806b21d0597SMatthew G Knepley 
3807b21d0597SMatthew G Knepley   Input Parameter:
3808b21d0597SMatthew G Knepley . dm - The DM
3809b21d0597SMatthew G Knepley 
3810b21d0597SMatthew G Knepley   Output Parameter:
3811b21d0597SMatthew G Knepley . sf - The PetscSF
3812b21d0597SMatthew G Knepley 
3813b21d0597SMatthew G Knepley   Level: intermediate
3814b21d0597SMatthew G Knepley 
3815b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3816b21d0597SMatthew G Knepley 
3817057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3818b21d0597SMatthew G Knepley @*/
38190adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
38200adebc6cSBarry Smith {
3821b21d0597SMatthew G Knepley   PetscFunctionBegin;
3822b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3823b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3824b21d0597SMatthew G Knepley   *sf = dm->sf;
3825b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3826b21d0597SMatthew G Knepley }
3827b21d0597SMatthew G Knepley 
3828057b4bcdSMatthew G Knepley /*@
3829057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3830057b4bcdSMatthew G Knepley 
3831057b4bcdSMatthew G Knepley   Input Parameters:
3832057b4bcdSMatthew G Knepley + dm - The DM
3833057b4bcdSMatthew G Knepley - sf - The PetscSF
3834057b4bcdSMatthew G Knepley 
3835057b4bcdSMatthew G Knepley   Level: intermediate
3836057b4bcdSMatthew G Knepley 
3837057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3838057b4bcdSMatthew G Knepley @*/
38390adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
38400adebc6cSBarry Smith {
3841057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3842057b4bcdSMatthew G Knepley 
3843057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3844057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3845057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3846057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3847057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3848057b4bcdSMatthew G Knepley   dm->sf = sf;
3849057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3850057b4bcdSMatthew G Knepley }
3851057b4bcdSMatthew G Knepley 
38522764a2aaSMatthew G. Knepley /*@
38532764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
38542764a2aaSMatthew G. Knepley 
38552764a2aaSMatthew G. Knepley   Input Parameter:
38562764a2aaSMatthew G. Knepley . dm - The DM
38572764a2aaSMatthew G. Knepley 
38582764a2aaSMatthew G. Knepley   Output Parameter:
38592764a2aaSMatthew G. Knepley . prob - The PetscDS
38602764a2aaSMatthew G. Knepley 
38612764a2aaSMatthew G. Knepley   Level: developer
38622764a2aaSMatthew G. Knepley 
38632764a2aaSMatthew G. Knepley .seealso: DMSetDS()
38642764a2aaSMatthew G. Knepley @*/
38652764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3866af122d2aSMatthew G Knepley {
3867af122d2aSMatthew G Knepley   PetscFunctionBegin;
3868af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38690f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
38700f21e855SMatthew G. Knepley   *prob = dm->prob;
38710f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38720f21e855SMatthew G. Knepley }
38730f21e855SMatthew G. Knepley 
38742764a2aaSMatthew G. Knepley /*@
38752764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
38762764a2aaSMatthew G. Knepley 
38772764a2aaSMatthew G. Knepley   Input Parameters:
38782764a2aaSMatthew G. Knepley + dm - The DM
38792764a2aaSMatthew G. Knepley - prob - The PetscDS
38802764a2aaSMatthew G. Knepley 
38812764a2aaSMatthew G. Knepley   Level: developer
38822764a2aaSMatthew G. Knepley 
38832764a2aaSMatthew G. Knepley .seealso: DMGetDS()
38842764a2aaSMatthew G. Knepley @*/
38852764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
38860f21e855SMatthew G. Knepley {
38870f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38880f21e855SMatthew G. Knepley 
38890f21e855SMatthew G. Knepley   PetscFunctionBegin;
38900f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38912764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
389237316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
38932764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
38940f21e855SMatthew G. Knepley   dm->prob = prob;
38950f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38960f21e855SMatthew G. Knepley }
38970f21e855SMatthew G. Knepley 
38980f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
38990f21e855SMatthew G. Knepley {
39000f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39010f21e855SMatthew G. Knepley 
39020f21e855SMatthew G. Knepley   PetscFunctionBegin;
39030f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39042764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3905af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3906af122d2aSMatthew G Knepley }
3907af122d2aSMatthew G Knepley 
3908af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3909af122d2aSMatthew G Knepley {
39100f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3911af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3912af122d2aSMatthew G Knepley 
3913af122d2aSMatthew G Knepley   PetscFunctionBegin;
3914af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39152764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
39160f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
39170f21e855SMatthew G. Knepley     PetscContainer obj;
39180f21e855SMatthew G. Knepley 
39190f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
39202764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
39210f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3922af122d2aSMatthew G Knepley   }
3923af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3924af122d2aSMatthew G Knepley }
3925af122d2aSMatthew G Knepley 
3926c1929be8SMatthew G. Knepley /*@
3927c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3928c1929be8SMatthew G. Knepley 
3929c1929be8SMatthew G. Knepley   Not collective
3930c1929be8SMatthew G. Knepley 
3931c1929be8SMatthew G. Knepley   Input Parameters:
3932c1929be8SMatthew G. Knepley + dm - The DM
3933c1929be8SMatthew G. Knepley - f  - The field number
3934c1929be8SMatthew G. Knepley 
3935c1929be8SMatthew G. Knepley   Output Parameter:
3936c1929be8SMatthew G. Knepley . field - The discretization object
3937c1929be8SMatthew G. Knepley 
3938c1929be8SMatthew G. Knepley   Level: developer
3939c1929be8SMatthew G. Knepley 
3940c1929be8SMatthew G. Knepley .seealso: DMSetField()
3941c1929be8SMatthew G. Knepley @*/
3942af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3943af122d2aSMatthew G Knepley {
39440f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39450f21e855SMatthew G. Knepley 
3946af122d2aSMatthew G Knepley   PetscFunctionBegin;
3947af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39482764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3949decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3950decb47aaSMatthew G. Knepley }
3951decb47aaSMatthew G. Knepley 
3952c1929be8SMatthew G. Knepley /*@
3953c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3954c1929be8SMatthew G. Knepley 
3955c1929be8SMatthew G. Knepley   Logically collective on DM
3956c1929be8SMatthew G. Knepley 
3957c1929be8SMatthew G. Knepley   Input Parameters:
3958c1929be8SMatthew G. Knepley + dm - The DM
3959c1929be8SMatthew G. Knepley . f  - The field number
3960c1929be8SMatthew G. Knepley - field - The discretization object
3961c1929be8SMatthew G. Knepley 
3962c1929be8SMatthew G. Knepley   Level: developer
3963c1929be8SMatthew G. Knepley 
3964c1929be8SMatthew G. Knepley .seealso: DMGetField()
3965c1929be8SMatthew G. Knepley @*/
3966decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3967decb47aaSMatthew G. Knepley {
3968decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3969decb47aaSMatthew G. Knepley 
3970decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3971decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39722764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3973af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3974af122d2aSMatthew G Knepley }
39756636e97aSMatthew G Knepley 
3976b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3977b64e0483SPeter Brune {
3978b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3979b64e0483SPeter Brune   PetscErrorCode ierr;
3980b64e0483SPeter Brune   Vec coords,ccoords;
39816dbf9973SLawrence Mitchell   Mat inject;
3982b64e0483SPeter Brune   PetscFunctionBegin;
3983b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3984b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3985b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3986b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3987b64e0483SPeter Brune   if (coords && !ccoords) {
3988b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
39896668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
39906dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
39912adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
39926dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3993b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3994b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3995b64e0483SPeter Brune   }
3996b64e0483SPeter Brune   PetscFunctionReturn(0);
3997b64e0483SPeter Brune }
3998b64e0483SPeter Brune 
399903dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
400003dadc2fSPeter Brune {
400103dadc2fSPeter Brune   DM dm_coord,subdm_coord;
400203dadc2fSPeter Brune   PetscErrorCode ierr;
400303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
400403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
400503dadc2fSPeter Brune   PetscFunctionBegin;
400603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
400703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
400803dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
400903dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
401003dadc2fSPeter Brune   if (coords && !ccoords) {
401103dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
40126668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
401303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
401424640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
401503dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
401603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
401703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
401803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
401903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
402003dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
402103dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
402203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
402303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
402403dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
402503dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
402603dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
402703dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
402803dadc2fSPeter Brune   }
402903dadc2fSPeter Brune   PetscFunctionReturn(0);
403003dadc2fSPeter Brune }
403103dadc2fSPeter Brune 
4032c73cfb54SMatthew G. Knepley /*@
4033c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4034c73cfb54SMatthew G. Knepley 
4035c73cfb54SMatthew G. Knepley   Not collective
4036c73cfb54SMatthew G. Knepley 
4037c73cfb54SMatthew G. Knepley   Input Parameter:
4038c73cfb54SMatthew G. Knepley . dm - The DM
4039c73cfb54SMatthew G. Knepley 
4040c73cfb54SMatthew G. Knepley   Output Parameter:
4041c73cfb54SMatthew G. Knepley . dim - The topological dimension
4042c73cfb54SMatthew G. Knepley 
4043c73cfb54SMatthew G. Knepley   Level: beginner
4044c73cfb54SMatthew G. Knepley 
4045c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4046c73cfb54SMatthew G. Knepley @*/
4047c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4048c73cfb54SMatthew G. Knepley {
4049c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4050c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4051c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4052c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4053c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4054c73cfb54SMatthew G. Knepley }
4055c73cfb54SMatthew G. Knepley 
4056c73cfb54SMatthew G. Knepley /*@
4057c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4058c73cfb54SMatthew G. Knepley 
4059c73cfb54SMatthew G. Knepley   Collective on dm
4060c73cfb54SMatthew G. Knepley 
4061c73cfb54SMatthew G. Knepley   Input Parameters:
4062c73cfb54SMatthew G. Knepley + dm - The DM
4063c73cfb54SMatthew G. Knepley - dim - The topological dimension
4064c73cfb54SMatthew G. Knepley 
4065c73cfb54SMatthew G. Knepley   Level: beginner
4066c73cfb54SMatthew G. Knepley 
4067c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4068c73cfb54SMatthew G. Knepley @*/
4069c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4070c73cfb54SMatthew G. Knepley {
4071c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4072c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4073c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4074c73cfb54SMatthew G. Knepley   dm->dim = dim;
4075c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4076c73cfb54SMatthew G. Knepley }
4077c73cfb54SMatthew G. Knepley 
4078793f3fe5SMatthew G. Knepley /*@
4079793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4080793f3fe5SMatthew G. Knepley 
4081793f3fe5SMatthew G. Knepley   Collective on DM
4082793f3fe5SMatthew G. Knepley 
4083793f3fe5SMatthew G. Knepley   Input Parameters:
4084793f3fe5SMatthew G. Knepley + dm - the DM
4085793f3fe5SMatthew G. Knepley - dim - the dimension
4086793f3fe5SMatthew G. Knepley 
4087793f3fe5SMatthew G. Knepley   Output Parameters:
4088793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4089793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4090793f3fe5SMatthew G. Knepley 
4091793f3fe5SMatthew G. Knepley   Note:
4092793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4093793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4094793f3fe5SMatthew G. Knepley   then the interval is empty.
4095793f3fe5SMatthew G. Knepley 
4096793f3fe5SMatthew G. Knepley   Level: intermediate
4097793f3fe5SMatthew G. Knepley 
4098793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4099793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4100793f3fe5SMatthew G. Knepley @*/
4101793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4102793f3fe5SMatthew G. Knepley {
4103793f3fe5SMatthew G. Knepley   PetscInt       d;
4104793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4105793f3fe5SMatthew G. Knepley 
4106793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4107793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4108793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4109793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4110793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4111793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4112793f3fe5SMatthew G. Knepley }
4113793f3fe5SMatthew G. Knepley 
41146636e97aSMatthew G Knepley /*@
41156636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
41166636e97aSMatthew G Knepley 
41176636e97aSMatthew G Knepley   Collective on DM
41186636e97aSMatthew G Knepley 
41196636e97aSMatthew G Knepley   Input Parameters:
41206636e97aSMatthew G Knepley + dm - the DM
41216636e97aSMatthew G Knepley - c - coordinate vector
41226636e97aSMatthew G Knepley 
41236636e97aSMatthew G Knepley   Note:
41246636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
41256636e97aSMatthew G Knepley 
41266636e97aSMatthew G Knepley   Level: intermediate
41276636e97aSMatthew G Knepley 
41286636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41296636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
41306636e97aSMatthew G Knepley @*/
41316636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
41326636e97aSMatthew G Knepley {
41336636e97aSMatthew G Knepley   PetscErrorCode ierr;
41346636e97aSMatthew G Knepley 
41356636e97aSMatthew G Knepley   PetscFunctionBegin;
41366636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41376636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41386636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41396636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41406636e97aSMatthew G Knepley   dm->coordinates = c;
41416636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4142b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
414303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
41446636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41456636e97aSMatthew G Knepley }
41466636e97aSMatthew G Knepley 
41476636e97aSMatthew G Knepley /*@
41486636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
41496636e97aSMatthew G Knepley 
41506636e97aSMatthew G Knepley   Collective on DM
41516636e97aSMatthew G Knepley 
41526636e97aSMatthew G Knepley    Input Parameters:
41536636e97aSMatthew G Knepley +  dm - the DM
41546636e97aSMatthew G Knepley -  c - coordinate vector
41556636e97aSMatthew G Knepley 
41566636e97aSMatthew G Knepley   Note:
41576636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
41586636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
41596636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
41606636e97aSMatthew G Knepley 
41616636e97aSMatthew G Knepley   Level: intermediate
41626636e97aSMatthew G Knepley 
41636636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41646636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
41656636e97aSMatthew G Knepley @*/
41666636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
41676636e97aSMatthew G Knepley {
41686636e97aSMatthew G Knepley   PetscErrorCode ierr;
41696636e97aSMatthew G Knepley 
41706636e97aSMatthew G Knepley   PetscFunctionBegin;
41716636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41726636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41736636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41746636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
41758865f1eaSKarl Rupp 
41766636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
41778865f1eaSKarl Rupp 
41786636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41796636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41806636e97aSMatthew G Knepley }
41816636e97aSMatthew G Knepley 
41826636e97aSMatthew G Knepley /*@
41836636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
41846636e97aSMatthew G Knepley 
41856636e97aSMatthew G Knepley   Not Collective
41866636e97aSMatthew G Knepley 
41876636e97aSMatthew G Knepley   Input Parameter:
41886636e97aSMatthew G Knepley . dm - the DM
41896636e97aSMatthew G Knepley 
41906636e97aSMatthew G Knepley   Output Parameter:
41916636e97aSMatthew G Knepley . c - global coordinate vector
41926636e97aSMatthew G Knepley 
41936636e97aSMatthew G Knepley   Note:
41946636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
41956636e97aSMatthew G Knepley 
41966636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
41976636e97aSMatthew G Knepley 
41986636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
41996636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42006636e97aSMatthew G Knepley 
42016636e97aSMatthew G Knepley   Level: intermediate
42026636e97aSMatthew G Knepley 
42036636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42046636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
42056636e97aSMatthew G Knepley @*/
42066636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
42076636e97aSMatthew G Knepley {
42086636e97aSMatthew G Knepley   PetscErrorCode ierr;
42096636e97aSMatthew G Knepley 
42106636e97aSMatthew G Knepley   PetscFunctionBegin;
42116636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42126636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42131f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
42140298fd71SBarry Smith     DM cdm = NULL;
42156636e97aSMatthew G Knepley 
42166636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42176636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
42186636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
42196636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42206636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42216636e97aSMatthew G Knepley   }
42226636e97aSMatthew G Knepley   *c = dm->coordinates;
42236636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42246636e97aSMatthew G Knepley }
42256636e97aSMatthew G Knepley 
42266636e97aSMatthew G Knepley /*@
42276636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
42286636e97aSMatthew G Knepley 
42296636e97aSMatthew G Knepley   Collective on DM
42306636e97aSMatthew G Knepley 
42316636e97aSMatthew G Knepley   Input Parameter:
42326636e97aSMatthew G Knepley . dm - the DM
42336636e97aSMatthew G Knepley 
42346636e97aSMatthew G Knepley   Output Parameter:
42356636e97aSMatthew G Knepley . c - coordinate vector
42366636e97aSMatthew G Knepley 
42376636e97aSMatthew G Knepley   Note:
42386636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42396636e97aSMatthew G Knepley 
42406636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
42416636e97aSMatthew G Knepley 
42426636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42436636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42446636e97aSMatthew G Knepley 
42456636e97aSMatthew G Knepley   Level: intermediate
42466636e97aSMatthew G Knepley 
42476636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42486636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
42496636e97aSMatthew G Knepley @*/
42506636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
42516636e97aSMatthew G Knepley {
42526636e97aSMatthew G Knepley   PetscErrorCode ierr;
42536636e97aSMatthew G Knepley 
42546636e97aSMatthew G Knepley   PetscFunctionBegin;
42556636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42566636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42571f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
42580298fd71SBarry Smith     DM cdm = NULL;
42596636e97aSMatthew G Knepley 
42606636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42616636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
42626636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
42636636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42646636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42656636e97aSMatthew G Knepley   }
42666636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
42676636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42686636e97aSMatthew G Knepley }
42696636e97aSMatthew G Knepley 
42706636e97aSMatthew G Knepley /*@
42711cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
42726636e97aSMatthew G Knepley 
42736636e97aSMatthew G Knepley   Collective on DM
42746636e97aSMatthew G Knepley 
42756636e97aSMatthew G Knepley   Input Parameter:
42766636e97aSMatthew G Knepley . dm - the DM
42776636e97aSMatthew G Knepley 
42786636e97aSMatthew G Knepley   Output Parameter:
42796636e97aSMatthew G Knepley . cdm - coordinate DM
42806636e97aSMatthew G Knepley 
42816636e97aSMatthew G Knepley   Level: intermediate
42826636e97aSMatthew G Knepley 
42836636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42841cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
42856636e97aSMatthew G Knepley @*/
42866636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
42876636e97aSMatthew G Knepley {
42886636e97aSMatthew G Knepley   PetscErrorCode ierr;
42896636e97aSMatthew G Knepley 
42906636e97aSMatthew G Knepley   PetscFunctionBegin;
42916636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42926636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
42936636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
429482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
42956636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
42966636e97aSMatthew G Knepley   }
42976636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
42986636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42996636e97aSMatthew G Knepley }
4300e87bb0d3SMatthew G Knepley 
43011cfe2091SMatthew G. Knepley /*@
43021cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
43031cfe2091SMatthew G. Knepley 
43041cfe2091SMatthew G. Knepley   Logically Collective on DM
43051cfe2091SMatthew G. Knepley 
43061cfe2091SMatthew G. Knepley   Input Parameters:
43071cfe2091SMatthew G. Knepley + dm - the DM
43081cfe2091SMatthew G. Knepley - cdm - coordinate DM
43091cfe2091SMatthew G. Knepley 
43101cfe2091SMatthew G. Knepley   Level: intermediate
43111cfe2091SMatthew G. Knepley 
43121cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43131cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43141cfe2091SMatthew G. Knepley @*/
43151cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
43161cfe2091SMatthew G. Knepley {
43171cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
43181cfe2091SMatthew G. Knepley 
43191cfe2091SMatthew G. Knepley   PetscFunctionBegin;
43201cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43211cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4322f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
43231cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
43241cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
43251cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
43261cfe2091SMatthew G. Knepley }
43271cfe2091SMatthew G. Knepley 
432846e270d4SMatthew G. Knepley /*@
432946e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
433046e270d4SMatthew G. Knepley 
433146e270d4SMatthew G. Knepley   Not Collective
433246e270d4SMatthew G. Knepley 
433346e270d4SMatthew G. Knepley   Input Parameter:
433446e270d4SMatthew G. Knepley . dm - The DM object
433546e270d4SMatthew G. Knepley 
433646e270d4SMatthew G. Knepley   Output Parameter:
433746e270d4SMatthew G. Knepley . dim - The embedding dimension
433846e270d4SMatthew G. Knepley 
433946e270d4SMatthew G. Knepley   Level: intermediate
434046e270d4SMatthew G. Knepley 
434146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
434246e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
434346e270d4SMatthew G. Knepley @*/
434446e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
434546e270d4SMatthew G. Knepley {
434646e270d4SMatthew G. Knepley   PetscFunctionBegin;
434746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
434846e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
43499a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
43509a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
43519a9a41abSToby Isaac   }
435246e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
435346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
435446e270d4SMatthew G. Knepley }
435546e270d4SMatthew G. Knepley 
435646e270d4SMatthew G. Knepley /*@
435746e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
435846e270d4SMatthew G. Knepley 
435946e270d4SMatthew G. Knepley   Not Collective
436046e270d4SMatthew G. Knepley 
436146e270d4SMatthew G. Knepley   Input Parameters:
436246e270d4SMatthew G. Knepley + dm  - The DM object
436346e270d4SMatthew G. Knepley - dim - The embedding dimension
436446e270d4SMatthew G. Knepley 
436546e270d4SMatthew G. Knepley   Level: intermediate
436646e270d4SMatthew G. Knepley 
436746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
436846e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
436946e270d4SMatthew G. Knepley @*/
437046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
437146e270d4SMatthew G. Knepley {
437246e270d4SMatthew G. Knepley   PetscFunctionBegin;
437346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
437446e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
437546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
437646e270d4SMatthew G. Knepley }
437746e270d4SMatthew G. Knepley 
4378e8abe2deSMatthew G. Knepley /*@
4379e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4380e8abe2deSMatthew G. Knepley 
4381e8abe2deSMatthew G. Knepley   Not Collective
4382e8abe2deSMatthew G. Knepley 
4383e8abe2deSMatthew G. Knepley   Input Parameter:
4384e8abe2deSMatthew G. Knepley . dm - The DM object
4385e8abe2deSMatthew G. Knepley 
4386e8abe2deSMatthew G. Knepley   Output Parameter:
4387e8abe2deSMatthew G. Knepley . section - The PetscSection object
4388e8abe2deSMatthew G. Knepley 
4389e8abe2deSMatthew G. Knepley   Level: intermediate
4390e8abe2deSMatthew G. Knepley 
4391e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4392e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4393e8abe2deSMatthew G. Knepley @*/
4394e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4395e8abe2deSMatthew G. Knepley {
4396e8abe2deSMatthew G. Knepley   DM             cdm;
4397e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4398e8abe2deSMatthew G. Knepley 
4399e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4400e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4401e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4402e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4403e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4404e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4405e8abe2deSMatthew G. Knepley }
4406e8abe2deSMatthew G. Knepley 
4407e8abe2deSMatthew G. Knepley /*@
4408e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4409e8abe2deSMatthew G. Knepley 
4410e8abe2deSMatthew G. Knepley   Not Collective
4411e8abe2deSMatthew G. Knepley 
4412e8abe2deSMatthew G. Knepley   Input Parameters:
4413e8abe2deSMatthew G. Knepley + dm      - The DM object
441446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4415e8abe2deSMatthew G. Knepley - section - The PetscSection object
4416e8abe2deSMatthew G. Knepley 
4417e8abe2deSMatthew G. Knepley   Level: intermediate
4418e8abe2deSMatthew G. Knepley 
4419e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4420e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4421e8abe2deSMatthew G. Knepley @*/
442246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4423e8abe2deSMatthew G. Knepley {
4424e8abe2deSMatthew G. Knepley   DM             cdm;
4425e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4426e8abe2deSMatthew G. Knepley 
4427e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4428e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
442946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4430e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4431e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
443246e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
44334c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
443446e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
443546e270d4SMatthew G. Knepley 
443646e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
443746e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
443846e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
443946e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
444046e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
444146e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
444246e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
444346e270d4SMatthew G. Knepley     }
44446be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
444546e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
444646e270d4SMatthew G. Knepley   }
4447e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4448e8abe2deSMatthew G. Knepley }
4449e8abe2deSMatthew G. Knepley 
44505dc8c3f7SMatthew G. Knepley /*@C
445190b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
44525dc8c3f7SMatthew G. Knepley 
44535dc8c3f7SMatthew G. Knepley   Input Parameters:
445490b157c4SStefano Zampini . dm      - The DM object
445590b157c4SStefano Zampini 
445690b157c4SStefano Zampini   Output Parameters:
445790b157c4SStefano Zampini + per     - Whether the DM is periodic or not
44585dc8c3f7SMatthew 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
44595dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44605dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44615dc8c3f7SMatthew G. Knepley 
44625dc8c3f7SMatthew G. Knepley   Level: developer
44635dc8c3f7SMatthew G. Knepley 
44645dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44655dc8c3f7SMatthew G. Knepley @*/
446690b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4467c6b900c6SMatthew G. Knepley {
4468c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4469c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
447090b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4471c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4472c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
44735dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4474c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4475c6b900c6SMatthew G. Knepley }
4476c6b900c6SMatthew G. Knepley 
44775dc8c3f7SMatthew G. Knepley /*@C
44785dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
44795dc8c3f7SMatthew G. Knepley 
44805dc8c3f7SMatthew G. Knepley   Input Parameters:
44815dc8c3f7SMatthew G. Knepley + dm      - The DM object
448290b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
44835dc8c3f7SMatthew 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
44845dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44855dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44865dc8c3f7SMatthew G. Knepley 
44875dc8c3f7SMatthew G. Knepley   Level: developer
44885dc8c3f7SMatthew G. Knepley 
44895dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44905dc8c3f7SMatthew G. Knepley @*/
449190b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4492c6b900c6SMatthew G. Knepley {
4493c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4494c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4495c6b900c6SMatthew G. Knepley 
4496c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4497c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
449890b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
449990b157c4SStefano Zampini   if (maxCell) {
450090b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
450190b157c4SStefano Zampini     PetscValidPointer(L,4);
450290b157c4SStefano Zampini     PetscValidPointer(bd,5);
450390b157c4SStefano Zampini   }
45045dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
45055dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
450690b157c4SStefano Zampini   if (maxCell) {
45075dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
45085dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
450990b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
451090b157c4SStefano Zampini   } else {
451190b157c4SStefano Zampini     dm->periodic = per;
451290b157c4SStefano Zampini   }
4513c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4514c6b900c6SMatthew G. Knepley }
4515c6b900c6SMatthew G. Knepley 
45162e17dfb7SMatthew G. Knepley /*@
45172e17dfb7SMatthew 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.
45182e17dfb7SMatthew G. Knepley 
45192e17dfb7SMatthew G. Knepley   Input Parameters:
45202e17dfb7SMatthew G. Knepley + dm     - The DM
452165da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
452265da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
45232e17dfb7SMatthew G. Knepley 
45242e17dfb7SMatthew G. Knepley   Output Parameter:
45252e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45262e17dfb7SMatthew G. Knepley 
45272e17dfb7SMatthew G. Knepley   Level: developer
45282e17dfb7SMatthew G. Knepley 
45292e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45302e17dfb7SMatthew G. Knepley @*/
453165da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
45322e17dfb7SMatthew G. Knepley {
45332e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
45342e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
45352e17dfb7SMatthew G. Knepley 
45362e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45372e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
45382e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45392e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45402e17dfb7SMatthew G. Knepley   } else {
454165da65dcSMatthew G. Knepley     if (endpoint) {
454265da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4543da3333bfSMatthew 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)) {
4544da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
454565da65dcSMatthew G. Knepley         } else {
4546da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
454765da65dcSMatthew G. Knepley         }
454865da65dcSMatthew G. Knepley       }
454965da65dcSMatthew G. Knepley     } else {
45502e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
45511118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
45522e17dfb7SMatthew G. Knepley       }
45532e17dfb7SMatthew G. Knepley     }
455465da65dcSMatthew G. Knepley   }
45552e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45562e17dfb7SMatthew G. Knepley }
45572e17dfb7SMatthew G. Knepley 
45582e17dfb7SMatthew G. Knepley /*
45592e17dfb7SMatthew 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.
45602e17dfb7SMatthew G. Knepley 
45612e17dfb7SMatthew G. Knepley   Input Parameters:
45622e17dfb7SMatthew G. Knepley + dm     - The DM
45632e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
45642e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
45652e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
45662e17dfb7SMatthew G. Knepley 
45672e17dfb7SMatthew G. Knepley   Output Parameter:
45682e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45692e17dfb7SMatthew G. Knepley 
45702e17dfb7SMatthew G. Knepley   Level: developer
45712e17dfb7SMatthew G. Knepley 
45722e17dfb7SMatthew 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
45732e17dfb7SMatthew G. Knepley 
45742e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45752e17dfb7SMatthew G. Knepley */
45762e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
45772e17dfb7SMatthew G. Knepley {
45782e17dfb7SMatthew G. Knepley   PetscInt d;
45792e17dfb7SMatthew G. Knepley 
45802e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45812e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45822e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45832e17dfb7SMatthew G. Knepley   } else {
45842e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4585908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
45862e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
45872e17dfb7SMatthew G. Knepley       } else {
45882e17dfb7SMatthew G. Knepley         out[d] = in[d];
45892e17dfb7SMatthew G. Knepley       }
45902e17dfb7SMatthew G. Knepley     }
45912e17dfb7SMatthew G. Knepley   }
45922e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45932e17dfb7SMatthew G. Knepley }
45942e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
45952e17dfb7SMatthew G. Knepley {
45962e17dfb7SMatthew G. Knepley   PetscInt d;
45972e17dfb7SMatthew G. Knepley 
45982e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45992e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46002e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46012e17dfb7SMatthew G. Knepley   } else {
46022e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4603908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
46042e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
46052e17dfb7SMatthew G. Knepley       } else {
46062e17dfb7SMatthew G. Knepley         out[d] = in[d];
46072e17dfb7SMatthew G. Knepley       }
46082e17dfb7SMatthew G. Knepley     }
46092e17dfb7SMatthew G. Knepley   }
46102e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46112e17dfb7SMatthew G. Knepley }
46122e17dfb7SMatthew G. Knepley 
46132e17dfb7SMatthew G. Knepley /*
46142e17dfb7SMatthew 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.
46152e17dfb7SMatthew G. Knepley 
46162e17dfb7SMatthew G. Knepley   Input Parameters:
46172e17dfb7SMatthew G. Knepley + dm     - The DM
46182e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
46192e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
46202e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
46212e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
46222e17dfb7SMatthew G. Knepley 
46232e17dfb7SMatthew G. Knepley   Output Parameter:
46242e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
46252e17dfb7SMatthew G. Knepley 
46262e17dfb7SMatthew G. Knepley   Level: developer
46272e17dfb7SMatthew G. Knepley 
46282e17dfb7SMatthew 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
46292e17dfb7SMatthew G. Knepley 
46302e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
46312e17dfb7SMatthew G. Knepley */
46322e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46332e17dfb7SMatthew G. Knepley {
46342e17dfb7SMatthew G. Knepley   PetscInt d;
46352e17dfb7SMatthew G. Knepley 
46362e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46372e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46382e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
46392e17dfb7SMatthew G. Knepley   } else {
46402e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4641908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
46422e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46432e17dfb7SMatthew G. Knepley       } else {
46442e17dfb7SMatthew G. Knepley         out[d] += in[d];
46452e17dfb7SMatthew G. Knepley       }
46462e17dfb7SMatthew G. Knepley     }
46472e17dfb7SMatthew G. Knepley   }
46482e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46492e17dfb7SMatthew G. Knepley }
46502e17dfb7SMatthew G. Knepley 
465136447a5eSToby Isaac /*@
465236447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
465336447a5eSToby Isaac 
465436447a5eSToby Isaac   Input Parameter:
465536447a5eSToby Isaac . dm - The DM
465636447a5eSToby Isaac 
465736447a5eSToby Isaac   Output Parameter:
465836447a5eSToby Isaac   areLocalized - True if localized
465936447a5eSToby Isaac 
466036447a5eSToby Isaac   Level: developer
466136447a5eSToby Isaac 
466236447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
466336447a5eSToby Isaac @*/
466436447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
466536447a5eSToby Isaac {
466636447a5eSToby Isaac   DM             cdm;
466736447a5eSToby Isaac   PetscSection   coordSection;
466836447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
466936447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
467036447a5eSToby Isaac   PetscErrorCode ierr;
467136447a5eSToby Isaac 
467236447a5eSToby Isaac   PetscFunctionBegin;
467336447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
467492c9c85fSStefano Zampini   if (!dm->periodic) {
46758b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
46768b09590cSToby Isaac     PetscFunctionReturn(0);
46778b09590cSToby Isaac   }
467836447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
467936447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
468036447a5eSToby Isaac   {
468136447a5eSToby Isaac     PetscBool isplex;
468236447a5eSToby Isaac 
468336447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
468436447a5eSToby Isaac     if (isplex) {
468536447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
468636447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
468736447a5eSToby Isaac   }
468836447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
468936447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
46905a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
469136447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
469236447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
469336447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
469436447a5eSToby Isaac       break;
469536447a5eSToby Isaac     }
469636447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
46975a42012cSToby Isaac     if (dof) {
46985a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
469936447a5eSToby Isaac       break;
470036447a5eSToby Isaac     }
470136447a5eSToby Isaac   }
47025a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
470336447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
470436447a5eSToby Isaac   PetscFunctionReturn(0);
470536447a5eSToby Isaac }
470636447a5eSToby Isaac 
470736447a5eSToby Isaac 
47082e17dfb7SMatthew G. Knepley /*@
4709492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
47102e17dfb7SMatthew G. Knepley 
47112e17dfb7SMatthew G. Knepley   Input Parameter:
47122e17dfb7SMatthew G. Knepley . dm - The DM
47132e17dfb7SMatthew G. Knepley 
47142e17dfb7SMatthew G. Knepley   Level: developer
47152e17dfb7SMatthew G. Knepley 
47162e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
47172e17dfb7SMatthew G. Knepley @*/
47182e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
47192e17dfb7SMatthew G. Knepley {
47202e17dfb7SMatthew G. Knepley   DM             cdm;
47212e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
47222e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
47233e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
47243e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4725e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
47263e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
47273e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
47282e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
47292e17dfb7SMatthew G. Knepley 
47302e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47312e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
473292c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
47332e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
47342e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
47352e17dfb7SMatthew G. Knepley   {
47362e17dfb7SMatthew G. Knepley     PetscBool isplex;
47372e17dfb7SMatthew G. Knepley 
47382e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
47392e17dfb7SMatthew G. Knepley     if (isplex) {
47402e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
47413e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
474269291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
47433e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
47443e922f36SToby Isaac       newStart = vStart;
47453e922f36SToby Isaac       newEnd   = vEnd;
47463e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
47473e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
47483e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
47493e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
47503e922f36SToby Isaac       }
47512e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
47522e17dfb7SMatthew G. Knepley   }
47532e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
47542e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
47553e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4756e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47573e922f36SToby Isaac 
47582e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
47592e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
47602e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
47612e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
47623e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
47633e922f36SToby Isaac 
476469291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
47653e922f36SToby Isaac   localized = &anchor[bs];
47663e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
47673e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
47683e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
47693e922f36SToby Isaac 
47703e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
47713e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
47723e922f36SToby Isaac       PetscInt     b;
47733e922f36SToby Isaac 
47743e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
47753e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47763e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
47773e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
47783e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
47793e922f36SToby Isaac         for (b = 0; b < bs; b++) {
47803e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
47813e922f36SToby Isaac         }
47823e922f36SToby Isaac         if (b < bs) break;
47833e922f36SToby Isaac       }
47843e922f36SToby Isaac       if (d < dof/bs) {
47853e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
47863e922f36SToby Isaac           PetscInt cdof;
47873e922f36SToby Isaac 
47883e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
47893e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
47903e922f36SToby Isaac         }
47913e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
47923e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
47933e922f36SToby Isaac       }
47943e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47953e922f36SToby Isaac     }
47963e922f36SToby Isaac   }
47973e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
47983e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
479969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
48003e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
480169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
48023e922f36SToby Isaac     PetscFunctionReturn(0);
48033e922f36SToby Isaac   }
48042e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
48052e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
48062e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
48072e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
48082e17dfb7SMatthew G. Knepley   }
48092e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
48102e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4811c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
48122e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
48132e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
48142e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
48152e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4816c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
48172e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
48182e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
48192e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
48202e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
48212e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
48222e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
48232e17dfb7SMatthew G. Knepley   }
48243e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
48253e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
48263e922f36SToby Isaac 
48272e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
48282e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
48293e922f36SToby Isaac       PetscInt     b, cdof;
48302e17dfb7SMatthew G. Knepley 
48313e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
48323e922f36SToby Isaac       if (!cdof) continue;
48332e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48342e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
48352e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48362e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
48372e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48382e17dfb7SMatthew G. Knepley     }
48393e922f36SToby Isaac   }
484069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
484169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
4842c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
48432e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
48442e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
48452e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
48462e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
48472e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
48482e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48492e17dfb7SMatthew G. Knepley }
48502e17dfb7SMatthew G. Knepley 
4851e87bb0d3SMatthew G Knepley /*@
48523a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4853e87bb0d3SMatthew G Knepley 
48543a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4855e87bb0d3SMatthew G Knepley 
4856e87bb0d3SMatthew G Knepley   Input Parameters:
4857e87bb0d3SMatthew G Knepley + dm - The DM
48583a93e3b7SToby Isaac . v - The Vec of points
485962a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
48603a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4861e87bb0d3SMatthew G Knepley 
486261e3bb9bSMatthew G Knepley   Output Parameter:
486362a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
486462a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
48653a93e3b7SToby Isaac 
4866e87bb0d3SMatthew G Knepley 
4867e87bb0d3SMatthew G Knepley   Level: developer
486861e3bb9bSMatthew G Knepley 
486962a38674SMatthew G. Knepley   Notes:
48703a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
487162a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
48723a93e3b7SToby Isaac 
48733a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
487462a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
48753a93e3b7SToby Isaac 
48763a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
48773a93e3b7SToby Isaac 
487862a38674SMatthew G. Knepley $    const PetscSFNode *cells;
487962a38674SMatthew G. Knepley $    PetscInt           nFound;
488062a38674SMatthew G. Knepley $    const PetscSFNode *found;
488162a38674SMatthew G. Knepley $
488262a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
48833a93e3b7SToby Isaac 
48843a93e3b7SToby 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
48853a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
48863a93e3b7SToby Isaac 
488761e3bb9bSMatthew G Knepley .keywords: point location, mesh
488862a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
488961e3bb9bSMatthew G Knepley @*/
489062a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4891e87bb0d3SMatthew G Knepley {
4892735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4893735aa83eSMatthew G Knepley 
4894e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4895e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4896e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4897e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
48983a93e3b7SToby Isaac   if (*cellSF) {
48993a93e3b7SToby Isaac     PetscMPIInt result;
49003a93e3b7SToby Isaac 
4901e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
4902a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
49033a93e3b7SToby 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");
4904e0fc9d1bSMatthew G. Knepley   } else {
49053a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
49063a93e3b7SToby Isaac   }
490747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4908735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
490962a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
491082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
491147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4912e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4913e87bb0d3SMatthew G Knepley }
491414f150ffSMatthew G. Knepley 
4915f4d763aaSMatthew G. Knepley /*@
4916f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4917f4d763aaSMatthew G. Knepley 
4918f4d763aaSMatthew G. Knepley   Input Parameter:
4919f4d763aaSMatthew G. Knepley . dm - The original DM
4920f4d763aaSMatthew G. Knepley 
4921f4d763aaSMatthew G. Knepley   Output Parameter:
4922f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4923f4d763aaSMatthew G. Knepley 
4924f4d763aaSMatthew G. Knepley   Level: intermediate
4925f4d763aaSMatthew G. Knepley 
4926f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4927f4d763aaSMatthew G. Knepley @*/
492814f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
492914f150ffSMatthew G. Knepley {
4930c26acbdeSMatthew G. Knepley   PetscSection   section;
49312d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
493214f150ffSMatthew G. Knepley   PetscErrorCode ierr;
493314f150ffSMatthew G. Knepley 
493414f150ffSMatthew G. Knepley   PetscFunctionBegin;
493514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
493614f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4937c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4938c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4939127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
49402d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
4941c26acbdeSMatthew G. Knepley     *odm = dm;
4942c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4943c26acbdeSMatthew G. Knepley   }
494414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
49450305aff6SMatthew G. Knepley     PetscDS      ds;
4946c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
494714f150ffSMatthew G. Knepley     PetscSF      sf;
494814f150ffSMatthew G. Knepley 
494914f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
49500305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
49510305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
495214f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
495314f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
495414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
495514f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
495615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
495714f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
495814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
495914f150ffSMatthew G. Knepley   }
496014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
496114f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
496214f150ffSMatthew G. Knepley }
4963f4d763aaSMatthew G. Knepley 
4964f4d763aaSMatthew G. Knepley /*@
4965cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4966f4d763aaSMatthew G. Knepley 
4967f4d763aaSMatthew G. Knepley   Input Parameter:
4968f4d763aaSMatthew G. Knepley . dm - The original DM
4969f4d763aaSMatthew G. Knepley 
4970cdb7a50dSMatthew G. Knepley   Output Parameters:
4971cdb7a50dSMatthew G. Knepley + num - The output sequence number
4972cdb7a50dSMatthew G. Knepley - val - The output sequence value
4973f4d763aaSMatthew G. Knepley 
4974f4d763aaSMatthew G. Knepley   Level: intermediate
4975f4d763aaSMatthew G. Knepley 
4976f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4977f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4978f4d763aaSMatthew G. Knepley 
4979f4d763aaSMatthew G. Knepley .seealso: VecView()
4980f4d763aaSMatthew G. Knepley @*/
4981cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4982f4d763aaSMatthew G. Knepley {
4983f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4984f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4985cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4986cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4987f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4988f4d763aaSMatthew G. Knepley }
4989f4d763aaSMatthew G. Knepley 
4990f4d763aaSMatthew G. Knepley /*@
4991cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4992f4d763aaSMatthew G. Knepley 
4993f4d763aaSMatthew G. Knepley   Input Parameters:
4994f4d763aaSMatthew G. Knepley + dm - The original DM
4995cdb7a50dSMatthew G. Knepley . num - The output sequence number
4996cdb7a50dSMatthew G. Knepley - val - The output sequence value
4997f4d763aaSMatthew G. Knepley 
4998f4d763aaSMatthew G. Knepley   Level: intermediate
4999f4d763aaSMatthew G. Knepley 
5000f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5001f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5002f4d763aaSMatthew G. Knepley 
5003f4d763aaSMatthew G. Knepley .seealso: VecView()
5004f4d763aaSMatthew G. Knepley @*/
5005cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5006f4d763aaSMatthew G. Knepley {
5007f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5008f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5009f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5010cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5011cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5012cdb7a50dSMatthew G. Knepley }
5013cdb7a50dSMatthew G. Knepley 
5014cdb7a50dSMatthew G. Knepley /*@C
5015cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5016cdb7a50dSMatthew G. Knepley 
5017cdb7a50dSMatthew G. Knepley   Input Parameters:
5018cdb7a50dSMatthew G. Knepley + dm   - The original DM
5019cdb7a50dSMatthew G. Knepley . name - The sequence name
5020cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5021cdb7a50dSMatthew G. Knepley 
5022cdb7a50dSMatthew G. Knepley   Output Parameter:
5023cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5024cdb7a50dSMatthew G. Knepley 
5025cdb7a50dSMatthew G. Knepley   Level: intermediate
5026cdb7a50dSMatthew G. Knepley 
5027cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5028cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5029cdb7a50dSMatthew G. Knepley 
5030cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5031cdb7a50dSMatthew G. Knepley @*/
5032cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5033cdb7a50dSMatthew G. Knepley {
5034cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5035cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5036cdb7a50dSMatthew G. Knepley 
5037cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5038cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5039cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5040cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5041cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5042cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5043cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5044cdb7a50dSMatthew G. Knepley     PetscScalar value;
5045cdb7a50dSMatthew G. Knepley 
504639d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
50474aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5048cdb7a50dSMatthew G. Knepley #endif
5049cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5050f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5051f4d763aaSMatthew G. Knepley }
50528e4ac7eaSMatthew G. Knepley 
50538e4ac7eaSMatthew G. Knepley /*@
50548e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
50558e4ac7eaSMatthew G. Knepley 
50568e4ac7eaSMatthew G. Knepley   Not collective
50578e4ac7eaSMatthew G. Knepley 
50588e4ac7eaSMatthew G. Knepley   Input Parameter:
50598e4ac7eaSMatthew G. Knepley . dm - The DM
50608e4ac7eaSMatthew G. Knepley 
50618e4ac7eaSMatthew G. Knepley   Output Parameter:
50628e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
50638e4ac7eaSMatthew G. Knepley 
50648e4ac7eaSMatthew G. Knepley   Level: beginner
50658e4ac7eaSMatthew G. Knepley 
50668e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
50678e4ac7eaSMatthew G. Knepley @*/
50688e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
50698e4ac7eaSMatthew G. Knepley {
50708e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
50718e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50728e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
50738e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
50748e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
50758e4ac7eaSMatthew G. Knepley }
50768e4ac7eaSMatthew G. Knepley 
50778e4ac7eaSMatthew G. Knepley /*@
50788e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
50798e4ac7eaSMatthew G. Knepley 
50808e4ac7eaSMatthew G. Knepley   Collective on dm
50818e4ac7eaSMatthew G. Knepley 
50828e4ac7eaSMatthew G. Knepley   Input Parameters:
50838e4ac7eaSMatthew G. Knepley + dm - The DM
50848e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
50858e4ac7eaSMatthew G. Knepley 
50868e4ac7eaSMatthew G. Knepley   Level: beginner
50878e4ac7eaSMatthew G. Knepley 
50888e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
50898e4ac7eaSMatthew G. Knepley @*/
50908e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
50918e4ac7eaSMatthew G. Knepley {
50928e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
50938e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50948e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
50958e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
50968e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
50978e4ac7eaSMatthew G. Knepley }
5098c58f1c22SToby Isaac 
5099c58f1c22SToby Isaac 
5100c58f1c22SToby Isaac /*@C
5101c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5102c58f1c22SToby Isaac 
5103c58f1c22SToby Isaac   Not Collective
5104c58f1c22SToby Isaac 
5105c58f1c22SToby Isaac   Input Parameters:
5106c58f1c22SToby Isaac + dm   - The DM object
5107c58f1c22SToby Isaac - name - The label name
5108c58f1c22SToby Isaac 
5109c58f1c22SToby Isaac   Level: intermediate
5110c58f1c22SToby Isaac 
5111c58f1c22SToby Isaac .keywords: mesh
5112c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5113c58f1c22SToby Isaac @*/
5114c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5115c58f1c22SToby Isaac {
5116c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5117c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5118c58f1c22SToby Isaac   PetscErrorCode ierr;
5119c58f1c22SToby Isaac 
5120c58f1c22SToby Isaac   PetscFunctionBegin;
5121c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5122c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5123c58f1c22SToby Isaac   while (next) {
5124c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5125c58f1c22SToby Isaac     if (flg) break;
5126c58f1c22SToby Isaac     next = next->next;
5127c58f1c22SToby Isaac   }
5128c58f1c22SToby Isaac   if (!flg) {
5129c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5130c58f1c22SToby Isaac 
5131c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5132c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5133c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5134c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5135c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5136c58f1c22SToby Isaac   }
5137c58f1c22SToby Isaac   PetscFunctionReturn(0);
5138c58f1c22SToby Isaac }
5139c58f1c22SToby Isaac 
5140c58f1c22SToby Isaac /*@C
5141c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5142c58f1c22SToby Isaac 
5143c58f1c22SToby Isaac   Not Collective
5144c58f1c22SToby Isaac 
5145c58f1c22SToby Isaac   Input Parameters:
5146c58f1c22SToby Isaac + dm   - The DM object
5147c58f1c22SToby Isaac . name - The label name
5148c58f1c22SToby Isaac - point - The mesh point
5149c58f1c22SToby Isaac 
5150c58f1c22SToby Isaac   Output Parameter:
5151c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5152c58f1c22SToby Isaac 
5153c58f1c22SToby Isaac   Level: beginner
5154c58f1c22SToby Isaac 
5155c58f1c22SToby Isaac .keywords: mesh
5156c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5157c58f1c22SToby Isaac @*/
5158c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5159c58f1c22SToby Isaac {
5160c58f1c22SToby Isaac   DMLabel        label;
5161c58f1c22SToby Isaac   PetscErrorCode ierr;
5162c58f1c22SToby Isaac 
5163c58f1c22SToby Isaac   PetscFunctionBegin;
5164c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5165c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5166c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
516713903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5168c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5169c58f1c22SToby Isaac   PetscFunctionReturn(0);
5170c58f1c22SToby Isaac }
5171c58f1c22SToby Isaac 
5172c58f1c22SToby Isaac /*@C
5173c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5174c58f1c22SToby Isaac 
5175c58f1c22SToby Isaac   Not Collective
5176c58f1c22SToby Isaac 
5177c58f1c22SToby Isaac   Input Parameters:
5178c58f1c22SToby Isaac + dm   - The DM object
5179c58f1c22SToby Isaac . name - The label name
5180c58f1c22SToby Isaac . point - The mesh point
5181c58f1c22SToby Isaac - value - The label value for this point
5182c58f1c22SToby Isaac 
5183c58f1c22SToby Isaac   Output Parameter:
5184c58f1c22SToby Isaac 
5185c58f1c22SToby Isaac   Level: beginner
5186c58f1c22SToby Isaac 
5187c58f1c22SToby Isaac .keywords: mesh
5188c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5189c58f1c22SToby Isaac @*/
5190c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5191c58f1c22SToby Isaac {
5192c58f1c22SToby Isaac   DMLabel        label;
5193c58f1c22SToby Isaac   PetscErrorCode ierr;
5194c58f1c22SToby Isaac 
5195c58f1c22SToby Isaac   PetscFunctionBegin;
5196c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5197c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5198c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5199c58f1c22SToby Isaac   if (!label) {
5200c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5201c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5202c58f1c22SToby Isaac   }
5203c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5204c58f1c22SToby Isaac   PetscFunctionReturn(0);
5205c58f1c22SToby Isaac }
5206c58f1c22SToby Isaac 
5207c58f1c22SToby Isaac /*@C
5208c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5209c58f1c22SToby Isaac 
5210c58f1c22SToby Isaac   Not Collective
5211c58f1c22SToby Isaac 
5212c58f1c22SToby Isaac   Input Parameters:
5213c58f1c22SToby Isaac + dm   - The DM object
5214c58f1c22SToby Isaac . name - The label name
5215c58f1c22SToby Isaac . point - The mesh point
5216c58f1c22SToby Isaac - value - The label value for this point
5217c58f1c22SToby Isaac 
5218c58f1c22SToby Isaac   Output Parameter:
5219c58f1c22SToby Isaac 
5220c58f1c22SToby Isaac   Level: beginner
5221c58f1c22SToby Isaac 
5222c58f1c22SToby Isaac .keywords: mesh
5223c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5224c58f1c22SToby Isaac @*/
5225c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5226c58f1c22SToby Isaac {
5227c58f1c22SToby Isaac   DMLabel        label;
5228c58f1c22SToby Isaac   PetscErrorCode ierr;
5229c58f1c22SToby Isaac 
5230c58f1c22SToby Isaac   PetscFunctionBegin;
5231c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5232c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5233c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5234c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5235c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5236c58f1c22SToby Isaac   PetscFunctionReturn(0);
5237c58f1c22SToby Isaac }
5238c58f1c22SToby Isaac 
5239c58f1c22SToby Isaac /*@C
5240c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5241c58f1c22SToby Isaac 
5242c58f1c22SToby Isaac   Not Collective
5243c58f1c22SToby Isaac 
5244c58f1c22SToby Isaac   Input Parameters:
5245c58f1c22SToby Isaac + dm   - The DM object
5246c58f1c22SToby Isaac - name - The label name
5247c58f1c22SToby Isaac 
5248c58f1c22SToby Isaac   Output Parameter:
5249c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5250c58f1c22SToby Isaac 
5251c58f1c22SToby Isaac   Level: beginner
5252c58f1c22SToby Isaac 
5253c58f1c22SToby Isaac .keywords: mesh
5254c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5255c58f1c22SToby Isaac @*/
5256c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5257c58f1c22SToby Isaac {
5258c58f1c22SToby Isaac   DMLabel        label;
5259c58f1c22SToby Isaac   PetscErrorCode ierr;
5260c58f1c22SToby Isaac 
5261c58f1c22SToby Isaac   PetscFunctionBegin;
5262c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5263c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5264c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5265c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5266c58f1c22SToby Isaac   *size = 0;
5267c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5268c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5269c58f1c22SToby Isaac   PetscFunctionReturn(0);
5270c58f1c22SToby Isaac }
5271c58f1c22SToby Isaac 
5272c58f1c22SToby Isaac /*@C
5273c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5274c58f1c22SToby Isaac 
5275c58f1c22SToby Isaac   Not Collective
5276c58f1c22SToby Isaac 
5277c58f1c22SToby Isaac   Input Parameters:
5278c58f1c22SToby Isaac + mesh - The DM object
5279c58f1c22SToby Isaac - name - The label name
5280c58f1c22SToby Isaac 
5281c58f1c22SToby Isaac   Output Parameter:
5282c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5283c58f1c22SToby Isaac 
5284c58f1c22SToby Isaac   Level: beginner
5285c58f1c22SToby Isaac 
5286c58f1c22SToby Isaac .keywords: mesh
5287c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5288c58f1c22SToby Isaac @*/
5289c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5290c58f1c22SToby Isaac {
5291c58f1c22SToby Isaac   DMLabel        label;
5292c58f1c22SToby Isaac   PetscErrorCode ierr;
5293c58f1c22SToby Isaac 
5294c58f1c22SToby Isaac   PetscFunctionBegin;
5295c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5296c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5297c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5298c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5299c58f1c22SToby Isaac   *ids = NULL;
5300c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5301c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5302c58f1c22SToby Isaac   PetscFunctionReturn(0);
5303c58f1c22SToby Isaac }
5304c58f1c22SToby Isaac 
5305c58f1c22SToby Isaac /*@C
5306c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5307c58f1c22SToby Isaac 
5308c58f1c22SToby Isaac   Not Collective
5309c58f1c22SToby Isaac 
5310c58f1c22SToby Isaac   Input Parameters:
5311c58f1c22SToby Isaac + dm - The DM object
5312c58f1c22SToby Isaac . name - The label name
5313c58f1c22SToby Isaac - value - The stratum value
5314c58f1c22SToby Isaac 
5315c58f1c22SToby Isaac   Output Parameter:
5316c58f1c22SToby Isaac . size - The stratum size
5317c58f1c22SToby Isaac 
5318c58f1c22SToby Isaac   Level: beginner
5319c58f1c22SToby Isaac 
5320c58f1c22SToby Isaac .keywords: mesh
5321c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5322c58f1c22SToby Isaac @*/
5323c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5324c58f1c22SToby Isaac {
5325c58f1c22SToby Isaac   DMLabel        label;
5326c58f1c22SToby Isaac   PetscErrorCode ierr;
5327c58f1c22SToby Isaac 
5328c58f1c22SToby Isaac   PetscFunctionBegin;
5329c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5330c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5331c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5332c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5333c58f1c22SToby Isaac   *size = 0;
5334c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5335c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5336c58f1c22SToby Isaac   PetscFunctionReturn(0);
5337c58f1c22SToby Isaac }
5338c58f1c22SToby Isaac 
5339c58f1c22SToby Isaac /*@C
5340c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5341c58f1c22SToby Isaac 
5342c58f1c22SToby Isaac   Not Collective
5343c58f1c22SToby Isaac 
5344c58f1c22SToby Isaac   Input Parameters:
5345c58f1c22SToby Isaac + dm - The DM object
5346c58f1c22SToby Isaac . name - The label name
5347c58f1c22SToby Isaac - value - The stratum value
5348c58f1c22SToby Isaac 
5349c58f1c22SToby Isaac   Output Parameter:
5350c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5351c58f1c22SToby Isaac 
5352c58f1c22SToby Isaac   Level: beginner
5353c58f1c22SToby Isaac 
5354c58f1c22SToby Isaac .keywords: mesh
5355c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5356c58f1c22SToby Isaac @*/
5357c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5358c58f1c22SToby Isaac {
5359c58f1c22SToby Isaac   DMLabel        label;
5360c58f1c22SToby Isaac   PetscErrorCode ierr;
5361c58f1c22SToby Isaac 
5362c58f1c22SToby Isaac   PetscFunctionBegin;
5363c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5364c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5365c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5366c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5367c58f1c22SToby Isaac   *points = NULL;
5368c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5369c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5370c58f1c22SToby Isaac   PetscFunctionReturn(0);
5371c58f1c22SToby Isaac }
5372c58f1c22SToby Isaac 
53734de306b1SToby Isaac /*@C
53744de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
53754de306b1SToby Isaac 
53764de306b1SToby Isaac   Not Collective
53774de306b1SToby Isaac 
53784de306b1SToby Isaac   Input Parameters:
53794de306b1SToby Isaac + dm - The DM object
53804de306b1SToby Isaac . name - The label name
53814de306b1SToby Isaac . value - The stratum value
53824de306b1SToby Isaac - points - The stratum points
53834de306b1SToby Isaac 
53844de306b1SToby Isaac   Level: beginner
53854de306b1SToby Isaac 
53864de306b1SToby Isaac .keywords: mesh
53874de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
53884de306b1SToby Isaac @*/
53894de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
53904de306b1SToby Isaac {
53914de306b1SToby Isaac   DMLabel        label;
53924de306b1SToby Isaac   PetscErrorCode ierr;
53934de306b1SToby Isaac 
53944de306b1SToby Isaac   PetscFunctionBegin;
53954de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53964de306b1SToby Isaac   PetscValidCharPointer(name, 2);
53974de306b1SToby Isaac   PetscValidPointer(points, 4);
53984de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
53994de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
54004de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
54014de306b1SToby Isaac   PetscFunctionReturn(0);
54024de306b1SToby Isaac }
54034de306b1SToby Isaac 
5404c58f1c22SToby Isaac /*@C
5405c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5406c58f1c22SToby Isaac 
5407c58f1c22SToby Isaac   Not Collective
5408c58f1c22SToby Isaac 
5409c58f1c22SToby Isaac   Input Parameters:
5410c58f1c22SToby Isaac + dm   - The DM object
5411c58f1c22SToby Isaac . name - The label name
5412c58f1c22SToby Isaac - value - The label value for this point
5413c58f1c22SToby Isaac 
5414c58f1c22SToby Isaac   Output Parameter:
5415c58f1c22SToby Isaac 
5416c58f1c22SToby Isaac   Level: beginner
5417c58f1c22SToby Isaac 
5418c58f1c22SToby Isaac .keywords: mesh
5419c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5420c58f1c22SToby Isaac @*/
5421c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5422c58f1c22SToby Isaac {
5423c58f1c22SToby Isaac   DMLabel        label;
5424c58f1c22SToby Isaac   PetscErrorCode ierr;
5425c58f1c22SToby Isaac 
5426c58f1c22SToby Isaac   PetscFunctionBegin;
5427c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5428c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5429c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5430c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5431c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5432c58f1c22SToby Isaac   PetscFunctionReturn(0);
5433c58f1c22SToby Isaac }
5434c58f1c22SToby Isaac 
5435c58f1c22SToby Isaac /*@
5436c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5437c58f1c22SToby Isaac 
5438c58f1c22SToby Isaac   Not Collective
5439c58f1c22SToby Isaac 
5440c58f1c22SToby Isaac   Input Parameter:
5441c58f1c22SToby Isaac . dm   - The DM object
5442c58f1c22SToby Isaac 
5443c58f1c22SToby Isaac   Output Parameter:
5444c58f1c22SToby Isaac . numLabels - the number of Labels
5445c58f1c22SToby Isaac 
5446c58f1c22SToby Isaac   Level: intermediate
5447c58f1c22SToby Isaac 
5448c58f1c22SToby Isaac .keywords: mesh
5449c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5450c58f1c22SToby Isaac @*/
5451c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5452c58f1c22SToby Isaac {
5453c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5454c58f1c22SToby Isaac   PetscInt  n    = 0;
5455c58f1c22SToby Isaac 
5456c58f1c22SToby Isaac   PetscFunctionBegin;
5457c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5458c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5459c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5460c58f1c22SToby Isaac   *numLabels = n;
5461c58f1c22SToby Isaac   PetscFunctionReturn(0);
5462c58f1c22SToby Isaac }
5463c58f1c22SToby Isaac 
5464c58f1c22SToby Isaac /*@C
5465c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5466c58f1c22SToby Isaac 
5467c58f1c22SToby Isaac   Not Collective
5468c58f1c22SToby Isaac 
5469c58f1c22SToby Isaac   Input Parameters:
5470c58f1c22SToby Isaac + dm - The DM object
5471c58f1c22SToby Isaac - n  - the label number
5472c58f1c22SToby Isaac 
5473c58f1c22SToby Isaac   Output Parameter:
5474c58f1c22SToby Isaac . name - the label name
5475c58f1c22SToby Isaac 
5476c58f1c22SToby Isaac   Level: intermediate
5477c58f1c22SToby Isaac 
5478c58f1c22SToby Isaac .keywords: mesh
5479c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5480c58f1c22SToby Isaac @*/
5481c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5482c58f1c22SToby Isaac {
5483c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5484c58f1c22SToby Isaac   PetscInt  l    = 0;
5485c58f1c22SToby Isaac 
5486c58f1c22SToby Isaac   PetscFunctionBegin;
5487c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5488c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5489c58f1c22SToby Isaac   while (next) {
5490c58f1c22SToby Isaac     if (l == n) {
5491c58f1c22SToby Isaac       *name = next->label->name;
5492c58f1c22SToby Isaac       PetscFunctionReturn(0);
5493c58f1c22SToby Isaac     }
5494c58f1c22SToby Isaac     ++l;
5495c58f1c22SToby Isaac     next = next->next;
5496c58f1c22SToby Isaac   }
5497c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5498c58f1c22SToby Isaac }
5499c58f1c22SToby Isaac 
5500c58f1c22SToby Isaac /*@C
5501c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5502c58f1c22SToby Isaac 
5503c58f1c22SToby Isaac   Not Collective
5504c58f1c22SToby Isaac 
5505c58f1c22SToby Isaac   Input Parameters:
5506c58f1c22SToby Isaac + dm   - The DM object
5507c58f1c22SToby Isaac - name - The label name
5508c58f1c22SToby Isaac 
5509c58f1c22SToby Isaac   Output Parameter:
5510c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5511c58f1c22SToby Isaac 
5512c58f1c22SToby Isaac   Level: intermediate
5513c58f1c22SToby Isaac 
5514c58f1c22SToby Isaac .keywords: mesh
5515c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5516c58f1c22SToby Isaac @*/
5517c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5518c58f1c22SToby Isaac {
5519c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5520c58f1c22SToby Isaac   PetscErrorCode ierr;
5521c58f1c22SToby Isaac 
5522c58f1c22SToby Isaac   PetscFunctionBegin;
5523c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5524c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5525c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5526c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5527c58f1c22SToby Isaac   while (next) {
5528c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5529c58f1c22SToby Isaac     if (*hasLabel) break;
5530c58f1c22SToby Isaac     next = next->next;
5531c58f1c22SToby Isaac   }
5532c58f1c22SToby Isaac   PetscFunctionReturn(0);
5533c58f1c22SToby Isaac }
5534c58f1c22SToby Isaac 
5535c58f1c22SToby Isaac /*@C
5536c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5537c58f1c22SToby Isaac 
5538c58f1c22SToby Isaac   Not Collective
5539c58f1c22SToby Isaac 
5540c58f1c22SToby Isaac   Input Parameters:
5541c58f1c22SToby Isaac + dm   - The DM object
5542c58f1c22SToby Isaac - name - The label name
5543c58f1c22SToby Isaac 
5544c58f1c22SToby Isaac   Output Parameter:
5545c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5546c58f1c22SToby Isaac 
5547c58f1c22SToby Isaac   Level: intermediate
5548c58f1c22SToby Isaac 
5549c58f1c22SToby Isaac .keywords: mesh
5550c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5551c58f1c22SToby Isaac @*/
5552c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5553c58f1c22SToby Isaac {
5554c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5555c58f1c22SToby Isaac   PetscBool      hasLabel;
5556c58f1c22SToby Isaac   PetscErrorCode ierr;
5557c58f1c22SToby Isaac 
5558c58f1c22SToby Isaac   PetscFunctionBegin;
5559c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5560c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5561c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5562c58f1c22SToby Isaac   *label = NULL;
5563c58f1c22SToby Isaac   while (next) {
5564c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5565c58f1c22SToby Isaac     if (hasLabel) {
5566c58f1c22SToby Isaac       *label = next->label;
5567c58f1c22SToby Isaac       break;
5568c58f1c22SToby Isaac     }
5569c58f1c22SToby Isaac     next = next->next;
5570c58f1c22SToby Isaac   }
5571c58f1c22SToby Isaac   PetscFunctionReturn(0);
5572c58f1c22SToby Isaac }
5573c58f1c22SToby Isaac 
5574c58f1c22SToby Isaac /*@C
5575c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5576c58f1c22SToby Isaac 
5577c58f1c22SToby Isaac   Not Collective
5578c58f1c22SToby Isaac 
5579c58f1c22SToby Isaac   Input Parameters:
5580c58f1c22SToby Isaac + dm - The DM object
5581c58f1c22SToby Isaac - n  - the label number
5582c58f1c22SToby Isaac 
5583c58f1c22SToby Isaac   Output Parameter:
5584c58f1c22SToby Isaac . label - the label
5585c58f1c22SToby Isaac 
5586c58f1c22SToby Isaac   Level: intermediate
5587c58f1c22SToby Isaac 
5588c58f1c22SToby Isaac .keywords: mesh
5589c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5590c58f1c22SToby Isaac @*/
5591c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5592c58f1c22SToby Isaac {
5593c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5594c58f1c22SToby Isaac   PetscInt    l    = 0;
5595c58f1c22SToby Isaac 
5596c58f1c22SToby Isaac   PetscFunctionBegin;
5597c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5598c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5599c58f1c22SToby Isaac   while (next) {
5600c58f1c22SToby Isaac     if (l == n) {
5601c58f1c22SToby Isaac       *label = next->label;
5602c58f1c22SToby Isaac       PetscFunctionReturn(0);
5603c58f1c22SToby Isaac     }
5604c58f1c22SToby Isaac     ++l;
5605c58f1c22SToby Isaac     next = next->next;
5606c58f1c22SToby Isaac   }
5607c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5608c58f1c22SToby Isaac }
5609c58f1c22SToby Isaac 
5610c58f1c22SToby Isaac /*@C
5611c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5612c58f1c22SToby Isaac 
5613c58f1c22SToby Isaac   Not Collective
5614c58f1c22SToby Isaac 
5615c58f1c22SToby Isaac   Input Parameters:
5616c58f1c22SToby Isaac + dm   - The DM object
5617c58f1c22SToby Isaac - label - The DMLabel
5618c58f1c22SToby Isaac 
5619c58f1c22SToby Isaac   Level: developer
5620c58f1c22SToby Isaac 
5621c58f1c22SToby Isaac .keywords: mesh
5622c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5623c58f1c22SToby Isaac @*/
5624c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5625c58f1c22SToby Isaac {
5626c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5627c58f1c22SToby Isaac   PetscBool      hasLabel;
5628c58f1c22SToby Isaac   PetscErrorCode ierr;
5629c58f1c22SToby Isaac 
5630c58f1c22SToby Isaac   PetscFunctionBegin;
5631c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5632c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5633c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5634c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5635c58f1c22SToby Isaac   tmpLabel->label  = label;
5636c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5637c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5638c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5639c58f1c22SToby Isaac   PetscFunctionReturn(0);
5640c58f1c22SToby Isaac }
5641c58f1c22SToby Isaac 
5642c58f1c22SToby Isaac /*@C
5643c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5644c58f1c22SToby Isaac 
5645c58f1c22SToby Isaac   Not Collective
5646c58f1c22SToby Isaac 
5647c58f1c22SToby Isaac   Input Parameters:
5648c58f1c22SToby Isaac + dm   - The DM object
5649c58f1c22SToby Isaac - name - The label name
5650c58f1c22SToby Isaac 
5651c58f1c22SToby Isaac   Output Parameter:
5652c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5653c58f1c22SToby Isaac 
5654c58f1c22SToby Isaac   Level: developer
5655c58f1c22SToby Isaac 
5656c58f1c22SToby Isaac .keywords: mesh
5657c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5658c58f1c22SToby Isaac @*/
5659c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5660c58f1c22SToby Isaac {
5661c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5662c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5663c58f1c22SToby Isaac   PetscBool      hasLabel;
5664c58f1c22SToby Isaac   PetscErrorCode ierr;
5665c58f1c22SToby Isaac 
5666c58f1c22SToby Isaac   PetscFunctionBegin;
5667c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5668c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5669c58f1c22SToby Isaac   *label = NULL;
5670c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5671c58f1c22SToby Isaac   while (next) {
5672c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5673c58f1c22SToby Isaac     if (hasLabel) {
5674c58f1c22SToby Isaac       if (last) last->next       = next->next;
5675c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5676c58f1c22SToby Isaac       next->next = NULL;
5677c58f1c22SToby Isaac       *label     = next->label;
5678c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5679c58f1c22SToby Isaac       if (hasLabel) {
5680c58f1c22SToby Isaac         dm->depthLabel = NULL;
5681c58f1c22SToby Isaac       }
5682c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5683c58f1c22SToby Isaac       break;
5684c58f1c22SToby Isaac     }
5685c58f1c22SToby Isaac     last = next;
5686c58f1c22SToby Isaac     next = next->next;
5687c58f1c22SToby Isaac   }
5688c58f1c22SToby Isaac   PetscFunctionReturn(0);
5689c58f1c22SToby Isaac }
5690c58f1c22SToby Isaac 
5691c58f1c22SToby Isaac /*@C
5692c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5693c58f1c22SToby Isaac 
5694c58f1c22SToby Isaac   Not Collective
5695c58f1c22SToby Isaac 
5696c58f1c22SToby Isaac   Input Parameters:
5697c58f1c22SToby Isaac + dm   - The DM object
5698c58f1c22SToby Isaac - name - The label name
5699c58f1c22SToby Isaac 
5700c58f1c22SToby Isaac   Output Parameter:
5701c58f1c22SToby Isaac . output - The flag for output
5702c58f1c22SToby Isaac 
5703c58f1c22SToby Isaac   Level: developer
5704c58f1c22SToby Isaac 
5705c58f1c22SToby Isaac .keywords: mesh
5706c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5707c58f1c22SToby Isaac @*/
5708c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5709c58f1c22SToby Isaac {
5710c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5711c58f1c22SToby Isaac   PetscErrorCode ierr;
5712c58f1c22SToby Isaac 
5713c58f1c22SToby Isaac   PetscFunctionBegin;
5714c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5715c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5716c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5717c58f1c22SToby Isaac   while (next) {
5718c58f1c22SToby Isaac     PetscBool flg;
5719c58f1c22SToby Isaac 
5720c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5721c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5722c58f1c22SToby Isaac     next = next->next;
5723c58f1c22SToby Isaac   }
5724c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5725c58f1c22SToby Isaac }
5726c58f1c22SToby Isaac 
5727c58f1c22SToby Isaac /*@C
5728c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5729c58f1c22SToby Isaac 
5730c58f1c22SToby Isaac   Not Collective
5731c58f1c22SToby Isaac 
5732c58f1c22SToby Isaac   Input Parameters:
5733c58f1c22SToby Isaac + dm     - The DM object
5734c58f1c22SToby Isaac . name   - The label name
5735c58f1c22SToby Isaac - output - The flag for output
5736c58f1c22SToby Isaac 
5737c58f1c22SToby Isaac   Level: developer
5738c58f1c22SToby Isaac 
5739c58f1c22SToby Isaac .keywords: mesh
5740c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5741c58f1c22SToby Isaac @*/
5742c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5743c58f1c22SToby Isaac {
5744c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5745c58f1c22SToby Isaac   PetscErrorCode ierr;
5746c58f1c22SToby Isaac 
5747c58f1c22SToby Isaac   PetscFunctionBegin;
5748c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5749c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5750c58f1c22SToby Isaac   while (next) {
5751c58f1c22SToby Isaac     PetscBool flg;
5752c58f1c22SToby Isaac 
5753c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5754c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5755c58f1c22SToby Isaac     next = next->next;
5756c58f1c22SToby Isaac   }
5757c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5758c58f1c22SToby Isaac }
5759c58f1c22SToby Isaac 
5760c58f1c22SToby Isaac 
5761c58f1c22SToby Isaac /*@
5762c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5763c58f1c22SToby Isaac 
5764c58f1c22SToby Isaac   Collective on DM
5765c58f1c22SToby Isaac 
5766c58f1c22SToby Isaac   Input Parameter:
57672e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5768c58f1c22SToby Isaac 
5769c58f1c22SToby Isaac   Output Parameter:
57702e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5771c58f1c22SToby Isaac 
5772c58f1c22SToby Isaac   Level: intermediate
5773c58f1c22SToby Isaac 
5774c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5775c58f1c22SToby Isaac 
5776c58f1c22SToby Isaac .keywords: mesh
5777c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5778c58f1c22SToby Isaac @*/
5779c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5780c58f1c22SToby Isaac {
5781c58f1c22SToby Isaac   PetscInt       numLabels, l;
5782c58f1c22SToby Isaac   PetscErrorCode ierr;
5783c58f1c22SToby Isaac 
5784c58f1c22SToby Isaac   PetscFunctionBegin;
5785c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5786c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5787c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5788c58f1c22SToby Isaac     DMLabel     label, labelNew;
5789c58f1c22SToby Isaac     const char *name;
5790c58f1c22SToby Isaac     PetscBool   flg;
5791c58f1c22SToby Isaac 
5792c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5793c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5794c58f1c22SToby Isaac     if (flg) continue;
5795c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5796c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5797c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5798c58f1c22SToby Isaac   }
5799c58f1c22SToby Isaac   PetscFunctionReturn(0);
5800c58f1c22SToby Isaac }
5801a8fb8f29SToby Isaac 
5802a8fb8f29SToby Isaac /*@
5803a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5804a8fb8f29SToby Isaac 
5805a8fb8f29SToby Isaac   Input Parameter:
5806a8fb8f29SToby Isaac . dm - The DM object
5807a8fb8f29SToby Isaac 
5808a8fb8f29SToby Isaac   Output Parameter:
5809a8fb8f29SToby Isaac . cdm - The coarse DM
5810a8fb8f29SToby Isaac 
5811a8fb8f29SToby Isaac   Level: intermediate
5812a8fb8f29SToby Isaac 
5813a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5814a8fb8f29SToby Isaac @*/
5815a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5816a8fb8f29SToby Isaac {
5817a8fb8f29SToby Isaac   PetscFunctionBegin;
5818a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5819a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5820a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5821a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5822a8fb8f29SToby Isaac }
5823a8fb8f29SToby Isaac 
5824a8fb8f29SToby Isaac /*@
5825a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5826a8fb8f29SToby Isaac 
5827a8fb8f29SToby Isaac   Input Parameters:
5828a8fb8f29SToby Isaac + dm - The DM object
5829a8fb8f29SToby Isaac - cdm - The coarse DM
5830a8fb8f29SToby Isaac 
5831a8fb8f29SToby Isaac   Level: intermediate
5832a8fb8f29SToby Isaac 
5833a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5834a8fb8f29SToby Isaac @*/
5835a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5836a8fb8f29SToby Isaac {
5837a8fb8f29SToby Isaac   PetscErrorCode ierr;
5838a8fb8f29SToby Isaac 
5839a8fb8f29SToby Isaac   PetscFunctionBegin;
5840a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5841a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5842a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5843a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5844a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5845a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5846a8fb8f29SToby Isaac }
5847a8fb8f29SToby Isaac 
584888bdff64SToby Isaac /*@
584988bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
585088bdff64SToby Isaac 
585188bdff64SToby Isaac   Input Parameter:
585288bdff64SToby Isaac . dm - The DM object
585388bdff64SToby Isaac 
585488bdff64SToby Isaac   Output Parameter:
585588bdff64SToby Isaac . fdm - The fine DM
585688bdff64SToby Isaac 
585788bdff64SToby Isaac   Level: intermediate
585888bdff64SToby Isaac 
585988bdff64SToby Isaac .seealso: DMSetFineDM()
586088bdff64SToby Isaac @*/
586188bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
586288bdff64SToby Isaac {
586388bdff64SToby Isaac   PetscFunctionBegin;
586488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
586588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
586688bdff64SToby Isaac   *fdm = dm->fineMesh;
586788bdff64SToby Isaac   PetscFunctionReturn(0);
586888bdff64SToby Isaac }
586988bdff64SToby Isaac 
587088bdff64SToby Isaac /*@
587188bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
587288bdff64SToby Isaac 
587388bdff64SToby Isaac   Input Parameters:
587488bdff64SToby Isaac + dm - The DM object
587588bdff64SToby Isaac - fdm - The fine DM
587688bdff64SToby Isaac 
587788bdff64SToby Isaac   Level: intermediate
587888bdff64SToby Isaac 
587988bdff64SToby Isaac .seealso: DMGetFineDM()
588088bdff64SToby Isaac @*/
588188bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
588288bdff64SToby Isaac {
588388bdff64SToby Isaac   PetscErrorCode ierr;
588488bdff64SToby Isaac 
588588bdff64SToby Isaac   PetscFunctionBegin;
588688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
588788bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
588888bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
588988bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
589088bdff64SToby Isaac   dm->fineMesh = fdm;
589188bdff64SToby Isaac   PetscFunctionReturn(0);
589288bdff64SToby Isaac }
589388bdff64SToby Isaac 
5894a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5895a6ba4734SToby Isaac 
5896a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5897a6ba4734SToby Isaac {
5898a6ba4734SToby Isaac   PetscErrorCode ierr;
5899a6ba4734SToby Isaac 
5900a6ba4734SToby Isaac   PetscFunctionBegin;
5901e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
5902a6ba4734SToby Isaac   PetscFunctionReturn(0);
5903a6ba4734SToby Isaac }
5904a6ba4734SToby Isaac 
5905a6ba4734SToby Isaac /*@C
5906a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5907a6ba4734SToby Isaac 
5908a6ba4734SToby Isaac   Input Parameters:
59094c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
5910f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5911a6ba4734SToby Isaac . name        - The BC name
5912a6ba4734SToby Isaac . labelname   - The label defining constrained points
5913a6ba4734SToby Isaac . field       - The field to constrain
5914a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5915a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5916a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5917a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5918a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5919a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5920a6ba4734SToby Isaac 
5921a6ba4734SToby Isaac   Options Database Keys:
5922a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5923a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5924a6ba4734SToby Isaac 
5925a6ba4734SToby Isaac   Level: developer
5926a6ba4734SToby Isaac 
5927a6ba4734SToby Isaac .seealso: DMGetBoundary()
5928a6ba4734SToby Isaac @*/
5929a30ec4eaSSatish 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)
5930a6ba4734SToby Isaac {
5931a6ba4734SToby Isaac   PetscErrorCode ierr;
5932a6ba4734SToby Isaac 
5933a6ba4734SToby Isaac   PetscFunctionBegin;
5934a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5935f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
5936a6ba4734SToby Isaac   PetscFunctionReturn(0);
5937a6ba4734SToby Isaac }
5938a6ba4734SToby Isaac 
5939a6ba4734SToby Isaac /*@
5940a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5941a6ba4734SToby Isaac 
5942a6ba4734SToby Isaac   Input Parameters:
5943a6ba4734SToby Isaac . dm - The mesh object
5944a6ba4734SToby Isaac 
5945a6ba4734SToby Isaac   Output Parameters:
5946a6ba4734SToby Isaac . numBd - The number of BC
5947a6ba4734SToby Isaac 
5948a6ba4734SToby Isaac   Level: intermediate
5949a6ba4734SToby Isaac 
5950a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5951a6ba4734SToby Isaac @*/
5952a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5953a6ba4734SToby Isaac {
595458ebd649SToby Isaac   PetscErrorCode ierr;
5955a6ba4734SToby Isaac 
5956a6ba4734SToby Isaac   PetscFunctionBegin;
5957a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
595858ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
5959a6ba4734SToby Isaac   PetscFunctionReturn(0);
5960a6ba4734SToby Isaac }
5961a6ba4734SToby Isaac 
5962a6ba4734SToby Isaac /*@C
59631c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
5964a6ba4734SToby Isaac 
5965a6ba4734SToby Isaac   Input Parameters:
5966a6ba4734SToby Isaac + dm          - The mesh object
5967a6ba4734SToby Isaac - bd          - The BC number
5968a6ba4734SToby Isaac 
5969a6ba4734SToby Isaac   Output Parameters:
5970f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5971a6ba4734SToby Isaac . name        - The BC name
5972a6ba4734SToby Isaac . labelname   - The label defining constrained points
5973a6ba4734SToby Isaac . field       - The field to constrain
5974a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5975a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5976a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5977a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5978a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5979a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5980a6ba4734SToby Isaac 
5981a6ba4734SToby Isaac   Options Database Keys:
5982a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5983a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5984a6ba4734SToby Isaac 
5985a6ba4734SToby Isaac   Level: developer
5986a6ba4734SToby Isaac 
5987a6ba4734SToby Isaac .seealso: DMAddBoundary()
5988a6ba4734SToby Isaac @*/
5989a30ec4eaSSatish 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)
5990a6ba4734SToby Isaac {
599158ebd649SToby Isaac   PetscErrorCode ierr;
5992a6ba4734SToby Isaac 
5993a6ba4734SToby Isaac   PetscFunctionBegin;
5994a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5995f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
5996a6ba4734SToby Isaac   PetscFunctionReturn(0);
5997a6ba4734SToby Isaac }
5998a6ba4734SToby Isaac 
5999e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6000e6f8dbb6SToby Isaac {
6001dff059c6SToby Isaac   DMBoundary *lastnext;
6002e6f8dbb6SToby Isaac   DSBoundary dsbound;
6003e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6004e6f8dbb6SToby Isaac 
6005e6f8dbb6SToby Isaac   PetscFunctionBegin;
6006e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
600747a1f5adSToby Isaac   if (dm->boundary) {
600847a1f5adSToby Isaac     DMBoundary next = dm->boundary;
600947a1f5adSToby Isaac 
601047a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
601147a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
601247a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
601347a1f5adSToby Isaac     while (next) {
601447a1f5adSToby Isaac       DMBoundary b = next;
601547a1f5adSToby Isaac 
601647a1f5adSToby Isaac       next = b->next;
601747a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6018a6ba4734SToby Isaac     }
601947a1f5adSToby Isaac     dm->boundary = NULL;
6020a6ba4734SToby Isaac   }
602147a1f5adSToby Isaac 
6022dff059c6SToby Isaac   lastnext = &(dm->boundary);
6023e6f8dbb6SToby Isaac   while (dsbound) {
6024e6f8dbb6SToby Isaac     DMBoundary dmbound;
6025e6f8dbb6SToby Isaac 
6026e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6027e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6028e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6029e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
603047a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6031dff059c6SToby Isaac     *lastnext = dmbound;
6032dff059c6SToby Isaac     lastnext = &(dmbound->next);
6033dff059c6SToby Isaac     dsbound = dsbound->next;
6034a6ba4734SToby Isaac   }
6035a6ba4734SToby Isaac   PetscFunctionReturn(0);
6036a6ba4734SToby Isaac }
6037a6ba4734SToby Isaac 
6038a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6039a6ba4734SToby Isaac {
6040b95f2879SToby Isaac   DMBoundary     b;
6041a6ba4734SToby Isaac   PetscErrorCode ierr;
6042a6ba4734SToby Isaac 
6043a6ba4734SToby Isaac   PetscFunctionBegin;
6044a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6045a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6046a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6047e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6048b95f2879SToby Isaac   b = dm->boundary;
6049a6ba4734SToby Isaac   while (b && !(*isBd)) {
6050e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6051e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
60523424c85cSToby Isaac 
60533424c85cSToby Isaac     if (label) {
6054a6ba4734SToby Isaac       PetscInt i;
6055a6ba4734SToby Isaac 
6056e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6057e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6058a6ba4734SToby Isaac       }
6059a6ba4734SToby Isaac     }
6060a6ba4734SToby Isaac     b = b->next;
6061a6ba4734SToby Isaac   }
6062a6ba4734SToby Isaac   PetscFunctionReturn(0);
6063a6ba4734SToby Isaac }
60644d6f44ffSToby Isaac 
60654d6f44ffSToby Isaac /*@C
60664d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
60674d6f44ffSToby Isaac 
60684d6f44ffSToby Isaac   Input Parameters:
60694d6f44ffSToby Isaac + dm      - The DM
60700709b2feSToby Isaac . time    - The time
60714d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
60724d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
60734d6f44ffSToby Isaac - mode    - The insertion mode for values
60744d6f44ffSToby Isaac 
60754d6f44ffSToby Isaac   Output Parameter:
60764d6f44ffSToby Isaac . X - vector
60774d6f44ffSToby Isaac 
60784d6f44ffSToby Isaac    Calling sequence of func:
60790709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
60804d6f44ffSToby Isaac 
60814d6f44ffSToby Isaac +  dim - The spatial dimension
60824d6f44ffSToby Isaac .  x   - The coordinates
60834d6f44ffSToby Isaac .  Nf  - The number of fields
60844d6f44ffSToby Isaac .  u   - The output field values
60854d6f44ffSToby Isaac -  ctx - optional user-defined function context
60864d6f44ffSToby Isaac 
60874d6f44ffSToby Isaac   Level: developer
60884d6f44ffSToby Isaac 
60892716604bSToby Isaac .seealso: DMComputeL2Diff()
60904d6f44ffSToby Isaac @*/
60910709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
60924d6f44ffSToby Isaac {
60934d6f44ffSToby Isaac   Vec            localX;
60944d6f44ffSToby Isaac   PetscErrorCode ierr;
60954d6f44ffSToby Isaac 
60964d6f44ffSToby Isaac   PetscFunctionBegin;
60974d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60984d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
60990709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61004d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
61014d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
61024d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
61034d6f44ffSToby Isaac   PetscFunctionReturn(0);
61044d6f44ffSToby Isaac }
61054d6f44ffSToby Isaac 
61060709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
61074d6f44ffSToby Isaac {
61084d6f44ffSToby Isaac   PetscErrorCode ierr;
61094d6f44ffSToby Isaac 
61104d6f44ffSToby Isaac   PetscFunctionBegin;
61114d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61124d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
61130918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
61140709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61154d6f44ffSToby Isaac   PetscFunctionReturn(0);
61164d6f44ffSToby Isaac }
61174d6f44ffSToby Isaac 
61182c53366bSMatthew G. Knepley PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
61192c53366bSMatthew G. Knepley {
61202c53366bSMatthew G. Knepley   Vec            localX;
61212c53366bSMatthew G. Knepley   PetscErrorCode ierr;
61222c53366bSMatthew G. Knepley 
61232c53366bSMatthew G. Knepley   PetscFunctionBegin;
61242c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61252c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
61262c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61272c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
61282c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
61292c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
61302c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
61312c53366bSMatthew G. Knepley }
61322c53366bSMatthew G. Knepley 
61331c531cf8SMatthew 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)
61344d6f44ffSToby Isaac {
61354d6f44ffSToby Isaac   PetscErrorCode ierr;
61364d6f44ffSToby Isaac 
61374d6f44ffSToby Isaac   PetscFunctionBegin;
61384d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61394d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
61400918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
61411c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61424d6f44ffSToby Isaac   PetscFunctionReturn(0);
61434d6f44ffSToby Isaac }
61442716604bSToby Isaac 
61458c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
61468c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
61478c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
61488c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6149191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
61508c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
61518c6c5593SMatthew G. Knepley {
61528c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
61538c6c5593SMatthew G. Knepley 
61548c6c5593SMatthew G. Knepley   PetscFunctionBegin;
61558c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61568c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
61578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
61580918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
61598c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
61608c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
61618c6c5593SMatthew G. Knepley }
61628c6c5593SMatthew G. Knepley 
61631c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
61648c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
61658c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
61668c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6167191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
61688c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
61698c6c5593SMatthew G. Knepley {
61708c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
61718c6c5593SMatthew G. Knepley 
61728c6c5593SMatthew G. Knepley   PetscFunctionBegin;
61738c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61748c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
61758c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
61760918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
61771c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
61788c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
61798c6c5593SMatthew G. Knepley }
61808c6c5593SMatthew G. Knepley 
61812716604bSToby Isaac /*@C
61822716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
61832716604bSToby Isaac 
61842716604bSToby Isaac   Input Parameters:
61852716604bSToby Isaac + dm    - The DM
61860709b2feSToby Isaac . time  - The time
61872716604bSToby Isaac . funcs - The functions to evaluate for each field component
61882716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
61892716604bSToby Isaac - X     - The coefficient vector u_h
61902716604bSToby Isaac 
61912716604bSToby Isaac   Output Parameter:
61922716604bSToby Isaac . diff - The diff ||u - u_h||_2
61932716604bSToby Isaac 
61942716604bSToby Isaac   Level: developer
61952716604bSToby Isaac 
61961189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
61972716604bSToby Isaac @*/
61980709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
61992716604bSToby Isaac {
62002716604bSToby Isaac   PetscErrorCode ierr;
62012716604bSToby Isaac 
62022716604bSToby Isaac   PetscFunctionBegin;
62032716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6204b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62050918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
62060709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
62072716604bSToby Isaac   PetscFunctionReturn(0);
62082716604bSToby Isaac }
6209b698f381SToby Isaac 
6210b698f381SToby Isaac /*@C
6211b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6212b698f381SToby Isaac 
6213b698f381SToby Isaac   Input Parameters:
6214b698f381SToby Isaac + dm    - The DM
6215b698f381SToby Isaac , time  - The time
6216b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6217b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6218b698f381SToby Isaac . X     - The coefficient vector u_h
6219b698f381SToby Isaac - n     - The vector to project along
6220b698f381SToby Isaac 
6221b698f381SToby Isaac   Output Parameter:
6222b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6223b698f381SToby Isaac 
6224b698f381SToby Isaac   Level: developer
6225b698f381SToby Isaac 
6226b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6227b698f381SToby Isaac @*/
6228b698f381SToby 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)
6229b698f381SToby Isaac {
6230b698f381SToby Isaac   PetscErrorCode ierr;
6231b698f381SToby Isaac 
6232b698f381SToby Isaac   PetscFunctionBegin;
6233b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6234b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6235b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6236b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6237b698f381SToby Isaac   PetscFunctionReturn(0);
6238b698f381SToby Isaac }
6239b698f381SToby Isaac 
62402a16baeaSToby Isaac /*@C
62412a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
62422a16baeaSToby Isaac 
62432a16baeaSToby Isaac   Input Parameters:
62442a16baeaSToby Isaac + dm    - The DM
62452a16baeaSToby Isaac . time  - The time
62462a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
62472a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
62482a16baeaSToby Isaac - X     - The coefficient vector u_h
62492a16baeaSToby Isaac 
62502a16baeaSToby Isaac   Output Parameter:
62512a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
62522a16baeaSToby Isaac 
62532a16baeaSToby Isaac   Level: developer
62542a16baeaSToby Isaac 
62551189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62562a16baeaSToby Isaac @*/
62571189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
62582a16baeaSToby Isaac {
62592a16baeaSToby Isaac   PetscErrorCode ierr;
62602a16baeaSToby Isaac 
62612a16baeaSToby Isaac   PetscFunctionBegin;
62622a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62632a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62640918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
62652a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
62662a16baeaSToby Isaac   PetscFunctionReturn(0);
62672a16baeaSToby Isaac }
62682a16baeaSToby Isaac 
6269df0b854cSToby Isaac /*@C
6270df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6271cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6272df0b854cSToby Isaac 
6273df0b854cSToby Isaac   Collective on dm
6274df0b854cSToby Isaac 
6275df0b854cSToby Isaac   Input parameters:
6276df0b854cSToby Isaac + dm - the pre-adaptation DM object
6277a1b0c543SToby Isaac - label - label with the flags
6278df0b854cSToby Isaac 
6279df0b854cSToby Isaac   Output parameters:
62800d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6281df0b854cSToby Isaac 
6282df0b854cSToby Isaac   Level: intermediate
62830d1cd5e0SMatthew G. Knepley 
62840d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6285df0b854cSToby Isaac @*/
62860d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6287df0b854cSToby Isaac {
6288df0b854cSToby Isaac   PetscErrorCode ierr;
6289df0b854cSToby Isaac 
6290df0b854cSToby Isaac   PetscFunctionBegin;
6291df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6292a1b0c543SToby Isaac   PetscValidPointer(label,2);
62930d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
62940d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
62956f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
62960d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
62970d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
62980d1cd5e0SMatthew G. Knepley }
62990d1cd5e0SMatthew G. Knepley 
63000d1cd5e0SMatthew G. Knepley /*@C
63010d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
63020d1cd5e0SMatthew G. Knepley 
63030d1cd5e0SMatthew G. Knepley   Input Parameters:
63040d1cd5e0SMatthew G. Knepley + dm - The DM object
63050d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
63066f25b0d8SLisandro 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_".
63070d1cd5e0SMatthew G. Knepley 
63080d1cd5e0SMatthew G. Knepley   Output Parameter:
63090d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
63100d1cd5e0SMatthew G. Knepley 
63110d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
63120d1cd5e0SMatthew G. Knepley 
63130d1cd5e0SMatthew G. Knepley   Level: advanced
63140d1cd5e0SMatthew G. Knepley 
63150d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
63160d1cd5e0SMatthew G. Knepley @*/
63170d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
63180d1cd5e0SMatthew G. Knepley {
63190d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
63200d1cd5e0SMatthew G. Knepley 
63210d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
63220d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63230d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
63240d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
63250d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
63266f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
63276f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
63280d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6329df0b854cSToby Isaac   PetscFunctionReturn(0);
6330df0b854cSToby Isaac }
6331c4088d22SMatthew G. Knepley 
6332502a2867SDave May /*@C
6333502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6334502a2867SDave May 
6335502a2867SDave May  Not Collective
6336502a2867SDave May 
6337502a2867SDave May  Input Parameter:
6338502a2867SDave May  . dm    - The DM
6339502a2867SDave May 
6340502a2867SDave May  Output Parameter:
6341502a2867SDave May  . nranks - the number of neighbours
6342502a2867SDave May  . ranks - the neighbors ranks
6343502a2867SDave May 
6344502a2867SDave May  Notes:
6345502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6346502a2867SDave May 
6347502a2867SDave May  Level: beginner
6348502a2867SDave May 
6349dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6350502a2867SDave May @*/
6351502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6352502a2867SDave May {
6353502a2867SDave May   PetscErrorCode ierr;
6354502a2867SDave May 
6355502a2867SDave May   PetscFunctionBegin;
6356502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63570918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6358502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6359502a2867SDave May   PetscFunctionReturn(0);
6360502a2867SDave May }
6361502a2867SDave May 
6362531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6363531c7667SBarry Smith 
6364531c7667SBarry Smith /*
6365531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6366531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6367531c7667SBarry Smith */
6368531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6369531c7667SBarry Smith {
6370531c7667SBarry Smith   PetscErrorCode ierr;
6371531c7667SBarry Smith 
6372531c7667SBarry Smith   PetscFunctionBegin;
6373531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6374531c7667SBarry Smith     Vec x1local;
6375531c7667SBarry Smith     DM  dm;
6376531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6377531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6378531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6379531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6380531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6381531c7667SBarry Smith     x1   = x1local;
6382531c7667SBarry Smith   }
6383531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6384531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6385531c7667SBarry Smith     DM  dm;
6386531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6387531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6388531c7667SBarry Smith   }
6389531c7667SBarry Smith   PetscFunctionReturn(0);
6390531c7667SBarry Smith }
6391531c7667SBarry Smith 
6392531c7667SBarry Smith /*@
6393531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6394531c7667SBarry Smith 
6395531c7667SBarry Smith     Input Parameter:
6396531c7667SBarry Smith .    coloring - the MatFDColoring object
6397531c7667SBarry Smith 
6398531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6399531c7667SBarry Smith 
64001b266c99SBarry Smith     Level: advanced
64011b266c99SBarry Smith 
6402531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6403531c7667SBarry Smith @*/
6404531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6405531c7667SBarry Smith {
6406531c7667SBarry Smith   PetscFunctionBegin;
6407531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6408531c7667SBarry Smith   PetscFunctionReturn(0);
6409531c7667SBarry Smith }
6410