xref: /petsc/src/dm/interface/dm.c (revision 76a8abe0e2b3324ea6e141c3dc7c9c387c682989)
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;
800*76a8abe0SBarry Smith   PetscMPIInt       size;
801*76a8abe0SBarry Smith   PetscViewerFormat format;
80247c6ae99SBarry Smith 
80347c6ae99SBarry Smith   PetscFunctionBegin;
804171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8053014e516SBarry Smith   if (!v) {
806ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8073014e516SBarry Smith   }
808*76a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
809*76a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
810*76a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
81198c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
81232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
81332c0f0efSBarry Smith   if (isbinary) {
81455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
81532c0f0efSBarry Smith     char     type[256];
81632c0f0efSBarry Smith 
81732c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
81832c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
81932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
82032c0f0efSBarry Smith   }
8210c010503SBarry Smith   if (dm->ops->view) {
8220c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
82347c6ae99SBarry Smith   }
82447c6ae99SBarry Smith   PetscFunctionReturn(0);
82547c6ae99SBarry Smith }
82647c6ae99SBarry Smith 
82747c6ae99SBarry Smith /*@
8288472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
82947c6ae99SBarry Smith 
83047c6ae99SBarry Smith     Collective on DM
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith     Input Parameter:
83347c6ae99SBarry Smith .   dm - the DM object
83447c6ae99SBarry Smith 
83547c6ae99SBarry Smith     Output Parameter:
83647c6ae99SBarry Smith .   vec - the global vector
83747c6ae99SBarry Smith 
838073dac72SJed Brown     Level: beginner
83947c6ae99SBarry Smith 
840e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith @*/
8437087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
84447c6ae99SBarry Smith {
84547c6ae99SBarry Smith   PetscErrorCode ierr;
84647c6ae99SBarry Smith 
84747c6ae99SBarry Smith   PetscFunctionBegin;
848171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84947c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
85047c6ae99SBarry Smith   PetscFunctionReturn(0);
85147c6ae99SBarry Smith }
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith /*@
8548472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith     Not Collective
85747c6ae99SBarry Smith 
85847c6ae99SBarry Smith     Input Parameter:
85947c6ae99SBarry Smith .   dm - the DM object
86047c6ae99SBarry Smith 
86147c6ae99SBarry Smith     Output Parameter:
86247c6ae99SBarry Smith .   vec - the local vector
86347c6ae99SBarry Smith 
864073dac72SJed Brown     Level: beginner
86547c6ae99SBarry Smith 
866e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith @*/
8697087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
87047c6ae99SBarry Smith {
87147c6ae99SBarry Smith   PetscErrorCode ierr;
87247c6ae99SBarry Smith 
87347c6ae99SBarry Smith   PetscFunctionBegin;
874171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87547c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
87647c6ae99SBarry Smith   PetscFunctionReturn(0);
87747c6ae99SBarry Smith }
87847c6ae99SBarry Smith 
8791411c6eeSJed Brown /*@
8801411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8811411c6eeSJed Brown 
8821411c6eeSJed Brown    Collective on DM
8831411c6eeSJed Brown 
8841411c6eeSJed Brown    Input Parameter:
8851411c6eeSJed Brown .  dm - the DM that provides the mapping
8861411c6eeSJed Brown 
8871411c6eeSJed Brown    Output Parameter:
8881411c6eeSJed Brown .  ltog - the mapping
8891411c6eeSJed Brown 
8901411c6eeSJed Brown    Level: intermediate
8911411c6eeSJed Brown 
8921411c6eeSJed Brown    Notes:
8931411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8941411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8951411c6eeSJed Brown 
896fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8971411c6eeSJed Brown @*/
8987087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8991411c6eeSJed Brown {
900bff27382SMatthew G. Knepley   PetscInt       bs = -1, bsLocal, bsMin, bsMax;
9011411c6eeSJed Brown   PetscErrorCode ierr;
9021411c6eeSJed Brown 
9031411c6eeSJed Brown   PetscFunctionBegin;
9041411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9051411c6eeSJed Brown   PetscValidPointer(ltog,2);
9061411c6eeSJed Brown   if (!dm->ltogmap) {
90737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
90837d0c07bSMatthew G Knepley 
90937d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
91037d0c07bSMatthew G Knepley     if (section) {
911a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
91237d0c07bSMatthew G Knepley       PetscInt       *ltog;
913ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
91437d0c07bSMatthew G Knepley 
91537d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
91637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
917ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
918ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
91937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
920a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
92137d0c07bSMatthew G Knepley 
92237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
92337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
924a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
925a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
92637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
9271a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
9281a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
9291a7dc684SMatthew G. Knepley         if (dof) {
930b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
931b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
9321a7dc684SMatthew G. Knepley         }
93337d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
934a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
935a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
93637d0c07bSMatthew G Knepley         }
93737d0c07bSMatthew G Knepley       }
938bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
939bff27382SMatthew G. Knepley       bsLocal = bs;
940bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
941bff27382SMatthew G. Knepley       bsLocal = bs < 0 ? bsMax : bs;
942bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
943bff27382SMatthew G. Knepley       if (bsMin != bsMax) {bs = 1;}
944bff27382SMatthew G. Knepley       else                {bs = bsMax;}
9457591dbb2SMatthew G. Knepley       bs   = bs < 0 ? 1 : bs;
9467591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
947ccf3bd66SMatthew G. Knepley       if (bs > 1) {
948ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
949ccf3bd66SMatthew G. Knepley         n /= bs;
950ccf3bd66SMatthew G. Knepley       }
951ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9523bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
95337d0c07bSMatthew G Knepley     } else {
954184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
955184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9561411c6eeSJed Brown     }
95737d0c07bSMatthew G Knepley   }
9581411c6eeSJed Brown   *ltog = dm->ltogmap;
9591411c6eeSJed Brown   PetscFunctionReturn(0);
9601411c6eeSJed Brown }
9611411c6eeSJed Brown 
9621411c6eeSJed Brown /*@
9631411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9641411c6eeSJed Brown 
9651411c6eeSJed Brown    Not Collective
9661411c6eeSJed Brown 
9671411c6eeSJed Brown    Input Parameter:
9681411c6eeSJed Brown .  dm - the DM with block structure
9691411c6eeSJed Brown 
9701411c6eeSJed Brown    Output Parameter:
9711411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9721411c6eeSJed Brown 
9731411c6eeSJed Brown    Level: intermediate
9741411c6eeSJed Brown 
975fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9761411c6eeSJed Brown @*/
9777087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9781411c6eeSJed Brown {
9791411c6eeSJed Brown   PetscFunctionBegin;
9801411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9811411c6eeSJed Brown   PetscValidPointer(bs,2);
9821411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
9831411c6eeSJed Brown   *bs = dm->bs;
9841411c6eeSJed Brown   PetscFunctionReturn(0);
9851411c6eeSJed Brown }
9861411c6eeSJed Brown 
98747c6ae99SBarry Smith /*@
9888472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
98947c6ae99SBarry Smith 
99047c6ae99SBarry Smith     Collective on DM
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith     Input Parameter:
99347c6ae99SBarry Smith +   dm1 - the DM object
99447c6ae99SBarry Smith -   dm2 - the second, finer DM object
99547c6ae99SBarry Smith 
99647c6ae99SBarry Smith     Output Parameter:
99747c6ae99SBarry Smith +  mat - the interpolation
99847c6ae99SBarry Smith -  vec - the scaling (optional)
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith     Level: developer
100147c6ae99SBarry Smith 
100285afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
100385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1004d52bd9f3SBarry Smith 
10051f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1006d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
100785afcc9aSBarry Smith 
100885afcc9aSBarry Smith 
10093ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith @*/
1012e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
101347c6ae99SBarry Smith {
101447c6ae99SBarry Smith   PetscErrorCode ierr;
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith   PetscFunctionBegin;
1017171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1018171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1019cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
102025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1021cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
102247c6ae99SBarry Smith   PetscFunctionReturn(0);
102347c6ae99SBarry Smith }
102447c6ae99SBarry Smith 
10253ad4599aSBarry Smith /*@
10263ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10273ad4599aSBarry Smith 
10283ad4599aSBarry Smith     Collective on DM
10293ad4599aSBarry Smith 
10303ad4599aSBarry Smith     Input Parameter:
10313ad4599aSBarry Smith +   dm1 - the DM object
10323ad4599aSBarry Smith -   dm2 - the second, finer DM object
10333ad4599aSBarry Smith 
10343ad4599aSBarry Smith     Output Parameter:
10353ad4599aSBarry Smith .  mat - the restriction
10363ad4599aSBarry Smith 
10373ad4599aSBarry Smith 
10383ad4599aSBarry Smith     Level: developer
10393ad4599aSBarry Smith 
10403ad4599aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
10413ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10423ad4599aSBarry Smith 
10433ad4599aSBarry Smith 
10443ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10453ad4599aSBarry Smith 
10463ad4599aSBarry Smith @*/
10473ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10483ad4599aSBarry Smith {
10493ad4599aSBarry Smith   PetscErrorCode ierr;
10503ad4599aSBarry Smith 
10513ad4599aSBarry Smith   PetscFunctionBegin;
10523ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10533ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10543ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10557294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
10563ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10573ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10583ad4599aSBarry Smith   PetscFunctionReturn(0);
10593ad4599aSBarry Smith }
10603ad4599aSBarry Smith 
106147c6ae99SBarry Smith /*@
10628472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
106347c6ae99SBarry Smith 
106447c6ae99SBarry Smith     Collective on DM
106547c6ae99SBarry Smith 
106647c6ae99SBarry Smith     Input Parameter:
106747c6ae99SBarry Smith +   dm1 - the DM object
106847c6ae99SBarry Smith -   dm2 - the second, finer DM object
106947c6ae99SBarry Smith 
107047c6ae99SBarry Smith     Output Parameter:
10716dbf9973SLawrence Mitchell .   mat - the injection
107247c6ae99SBarry Smith 
107347c6ae99SBarry Smith     Level: developer
107447c6ae99SBarry Smith 
107585afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
107685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
107785afcc9aSBarry Smith 
1078e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith @*/
10816dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
108247c6ae99SBarry Smith {
108347c6ae99SBarry Smith   PetscErrorCode ierr;
108447c6ae99SBarry Smith 
108547c6ae99SBarry Smith   PetscFunctionBegin;
1086171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1087171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10880c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
10896dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
109047c6ae99SBarry Smith   PetscFunctionReturn(0);
109147c6ae99SBarry Smith }
109247c6ae99SBarry Smith 
1093b412c318SBarry Smith /*@
1094b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
109547c6ae99SBarry Smith 
109647c6ae99SBarry Smith     Collective on DM
109747c6ae99SBarry Smith 
109847c6ae99SBarry Smith     Input Parameter:
109947c6ae99SBarry Smith +   dm - the DM object
11005bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith     Output Parameter:
110347c6ae99SBarry Smith .   coloring - the coloring
110447c6ae99SBarry Smith 
110547c6ae99SBarry Smith     Level: developer
110647c6ae99SBarry Smith 
1107b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
110847c6ae99SBarry Smith 
1109aab9d709SJed Brown @*/
1110b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
111147c6ae99SBarry Smith {
111247c6ae99SBarry Smith   PetscErrorCode ierr;
111347c6ae99SBarry Smith 
111447c6ae99SBarry Smith   PetscFunctionBegin;
1115171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1116ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1117b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
111847c6ae99SBarry Smith   PetscFunctionReturn(0);
111947c6ae99SBarry Smith }
112047c6ae99SBarry Smith 
1121b412c318SBarry Smith /*@
11228472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
112347c6ae99SBarry Smith 
112447c6ae99SBarry Smith     Collective on DM
112547c6ae99SBarry Smith 
112647c6ae99SBarry Smith     Input Parameter:
1127b412c318SBarry Smith .   dm - the DM object
112847c6ae99SBarry Smith 
112947c6ae99SBarry Smith     Output Parameter:
113047c6ae99SBarry Smith .   mat - the empty Jacobian
113147c6ae99SBarry Smith 
1132073dac72SJed Brown     Level: beginner
113347c6ae99SBarry Smith 
113494013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
113594013140SBarry Smith        do not need to do it yourself.
113694013140SBarry Smith 
113794013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
11387889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
113994013140SBarry Smith 
114094013140SBarry 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
114194013140SBarry Smith        internally by PETSc.
114294013140SBarry Smith 
114394013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1144aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
114594013140SBarry Smith 
1146b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
114747c6ae99SBarry Smith 
1148aab9d709SJed Brown @*/
1149b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
115047c6ae99SBarry Smith {
115147c6ae99SBarry Smith   PetscErrorCode ierr;
115247c6ae99SBarry Smith 
115347c6ae99SBarry Smith   PetscFunctionBegin;
1154171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1155607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1156c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1157c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1158b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
115947c6ae99SBarry Smith   PetscFunctionReturn(0);
116047c6ae99SBarry Smith }
116147c6ae99SBarry Smith 
1162732e2eb9SMatthew G Knepley /*@
1163950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1164732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1165732e2eb9SMatthew G Knepley 
11668472ad0fSDave May   Logically Collective on DM
1167732e2eb9SMatthew G Knepley 
1168732e2eb9SMatthew G Knepley   Input Parameter:
1169732e2eb9SMatthew G Knepley + dm - the DM
1170732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1171732e2eb9SMatthew G Knepley 
1172732e2eb9SMatthew G Knepley   Level: developer
1173b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1174732e2eb9SMatthew G Knepley @*/
1175732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1176732e2eb9SMatthew G Knepley {
1177732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1178732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1179732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1180732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1181732e2eb9SMatthew G Knepley }
1182732e2eb9SMatthew G Knepley 
1183b06ff27eSHong Zhang /*@
1184b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1185b06ff27eSHong Zhang     but the array for values will not be allocated.
1186b06ff27eSHong Zhang 
1187b06ff27eSHong Zhang   Logically Collective on DM
1188b06ff27eSHong Zhang 
1189b06ff27eSHong Zhang   Input Parameter:
1190b06ff27eSHong Zhang + dm - the DM
1191b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1192b06ff27eSHong Zhang 
1193b06ff27eSHong Zhang   Level: developer
1194b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1195b06ff27eSHong Zhang @*/
1196b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1197b06ff27eSHong Zhang {
1198b06ff27eSHong Zhang   PetscFunctionBegin;
1199b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1200b06ff27eSHong Zhang   dm->structure_only = only;
1201b06ff27eSHong Zhang   PetscFunctionReturn(0);
1202b06ff27eSHong Zhang }
1203b06ff27eSHong Zhang 
1204a89ea682SMatthew G Knepley /*@C
1205aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1206a89ea682SMatthew G Knepley 
1207a89ea682SMatthew G Knepley   Not Collective
1208a89ea682SMatthew G Knepley 
1209a89ea682SMatthew G Knepley   Input Parameters:
1210a89ea682SMatthew G Knepley + dm - the DM object
1211aa1993deSMatthew G Knepley . count - The minium size
1212aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1213a89ea682SMatthew G Knepley 
1214a89ea682SMatthew G Knepley   Output Parameter:
1215a89ea682SMatthew G Knepley . array - the work array
1216a89ea682SMatthew G Knepley 
1217a89ea682SMatthew G Knepley   Level: developer
1218a89ea682SMatthew G Knepley 
1219a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1220a89ea682SMatthew G Knepley @*/
1221aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1222a89ea682SMatthew G Knepley {
1223a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1224aa1993deSMatthew G Knepley   DMWorkLink     link;
1225854ce69bSBarry Smith   size_t         dsize;
1226a89ea682SMatthew G Knepley 
1227a89ea682SMatthew G Knepley   PetscFunctionBegin;
1228a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1229aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1230aa1993deSMatthew G Knepley   if (dm->workin) {
1231aa1993deSMatthew G Knepley     link       = dm->workin;
1232aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1233aa1993deSMatthew G Knepley   } else {
1234b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1235a89ea682SMatthew G Knepley   }
1236854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1237854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1238aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1239854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1240854ce69bSBarry Smith     link->bytes = dsize*count;
1241aa1993deSMatthew G Knepley   }
1242aa1993deSMatthew G Knepley   link->next   = dm->workout;
1243aa1993deSMatthew G Knepley   dm->workout  = link;
1244aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1245a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1246a89ea682SMatthew G Knepley }
1247a89ea682SMatthew G Knepley 
1248aa1993deSMatthew G Knepley /*@C
1249aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1250aa1993deSMatthew G Knepley 
1251aa1993deSMatthew G Knepley   Not Collective
1252aa1993deSMatthew G Knepley 
1253aa1993deSMatthew G Knepley   Input Parameters:
1254aa1993deSMatthew G Knepley + dm - the DM object
1255aa1993deSMatthew G Knepley . count - The minium size
1256aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1257aa1993deSMatthew G Knepley 
1258aa1993deSMatthew G Knepley   Output Parameter:
1259aa1993deSMatthew G Knepley . array - the work array
1260aa1993deSMatthew G Knepley 
1261aa1993deSMatthew G Knepley   Level: developer
1262aa1993deSMatthew G Knepley 
1263aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1264aa1993deSMatthew G Knepley @*/
1265aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1266aa1993deSMatthew G Knepley {
1267aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1268aa1993deSMatthew G Knepley 
1269aa1993deSMatthew G Knepley   PetscFunctionBegin;
1270aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1271aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1272aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1273aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1274aa1993deSMatthew G Knepley       *p           = link->next;
1275aa1993deSMatthew G Knepley       link->next   = dm->workin;
1276aa1993deSMatthew G Knepley       dm->workin   = link;
12770298fd71SBarry Smith       *(void**)mem = NULL;
1278aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1279aa1993deSMatthew G Knepley     }
1280aa1993deSMatthew G Knepley   }
1281aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1282aa1993deSMatthew G Knepley }
1283e7c4fc90SDmitry Karpeev 
1284435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1285435a35e8SMatthew G Knepley {
1286435a35e8SMatthew G Knepley   PetscFunctionBegin;
1287435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
128882f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1289435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1290435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1291435a35e8SMatthew G Knepley }
1292435a35e8SMatthew G Knepley 
12934f3b5142SJed Brown /*@C
12944d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
12954d343eeaSMatthew G Knepley 
12964d343eeaSMatthew G Knepley   Not collective
12974d343eeaSMatthew G Knepley 
12984d343eeaSMatthew G Knepley   Input Parameter:
12994d343eeaSMatthew G Knepley . dm - the DM object
13004d343eeaSMatthew G Knepley 
13014d343eeaSMatthew G Knepley   Output Parameters:
13020298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13030298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13040298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13054d343eeaSMatthew G Knepley 
13064d343eeaSMatthew G Knepley   Level: intermediate
13074d343eeaSMatthew G Knepley 
130821c9b008SJed Brown   Notes:
130921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
131021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
131121c9b008SJed Brown   PetscFree().
131221c9b008SJed Brown 
13134d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13144d343eeaSMatthew G Knepley @*/
131537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13164d343eeaSMatthew G Knepley {
131737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13184d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13194d343eeaSMatthew G Knepley 
13204d343eeaSMatthew G Knepley   PetscFunctionBegin;
13214d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
132269ca1f37SDmitry Karpeev   if (numFields) {
132369ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
132469ca1f37SDmitry Karpeev     *numFields = 0;
132569ca1f37SDmitry Karpeev   }
132637d0c07bSMatthew G Knepley   if (fieldNames) {
132737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13280298fd71SBarry Smith     *fieldNames = NULL;
132969ca1f37SDmitry Karpeev   }
133069ca1f37SDmitry Karpeev   if (fields) {
133169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13320298fd71SBarry Smith     *fields = NULL;
133369ca1f37SDmitry Karpeev   }
133437d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
133537d0c07bSMatthew G Knepley   if (section) {
133637d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
133737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
133837d0c07bSMatthew G Knepley 
133937d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
134037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1341dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
134237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
134337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
134437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
134537d0c07bSMatthew G Knepley     }
134637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
134737d0c07bSMatthew G Knepley       PetscInt gdof;
134837d0c07bSMatthew G Knepley 
134937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
135037d0c07bSMatthew G Knepley       if (gdof > 0) {
135137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
135237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
135337d0c07bSMatthew G Knepley 
135437d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
135537d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
135637d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
135737d0c07bSMatthew G Knepley         }
135837d0c07bSMatthew G Knepley       }
135937d0c07bSMatthew G Knepley     }
136037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1361785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
136237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
136337d0c07bSMatthew G Knepley     }
136437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
136537d0c07bSMatthew G Knepley       PetscInt gdof, goff;
136637d0c07bSMatthew G Knepley 
136737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
136837d0c07bSMatthew G Knepley       if (gdof > 0) {
136937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
137037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
137137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
137237d0c07bSMatthew G Knepley 
137337d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
137437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
137537d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
137637d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
137737d0c07bSMatthew G Knepley           }
137837d0c07bSMatthew G Knepley         }
137937d0c07bSMatthew G Knepley       }
138037d0c07bSMatthew G Knepley     }
13818865f1eaSKarl Rupp     if (numFields) *numFields = nF;
138237d0c07bSMatthew G Knepley     if (fieldNames) {
1383785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
138437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
138537d0c07bSMatthew G Knepley         const char *fieldName;
138637d0c07bSMatthew G Knepley 
138737d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
138837d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
138937d0c07bSMatthew G Knepley       }
139037d0c07bSMatthew G Knepley     }
139137d0c07bSMatthew G Knepley     if (fields) {
1392785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
139337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
139482f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
139537d0c07bSMatthew G Knepley       }
139637d0c07bSMatthew G Knepley     }
139737d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
13988865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
13998865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
140069ca1f37SDmitry Karpeev   }
14014d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14024d343eeaSMatthew G Knepley }
14034d343eeaSMatthew G Knepley 
140416621825SDmitry Karpeev 
140516621825SDmitry Karpeev /*@C
140616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
140716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
140816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1409e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1410e7c4fc90SDmitry Karpeev 
1411e7c4fc90SDmitry Karpeev   Not collective
1412e7c4fc90SDmitry Karpeev 
1413e7c4fc90SDmitry Karpeev   Input Parameter:
1414e7c4fc90SDmitry Karpeev . dm - the DM object
1415e7c4fc90SDmitry Karpeev 
1416e7c4fc90SDmitry Karpeev   Output Parameters:
14170298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14180298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14190298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14200298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1421e7c4fc90SDmitry Karpeev 
1422e7c4fc90SDmitry Karpeev   Level: intermediate
1423e7c4fc90SDmitry Karpeev 
1424e7c4fc90SDmitry Karpeev   Notes:
1425e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1426e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1427e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1428e7c4fc90SDmitry Karpeev 
1429e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1430e7c4fc90SDmitry Karpeev @*/
143116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1432e7c4fc90SDmitry Karpeev {
1433e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1434e7c4fc90SDmitry Karpeev 
1435e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1436e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14378865f1eaSKarl Rupp   if (len) {
14388865f1eaSKarl Rupp     PetscValidPointer(len,2);
14398865f1eaSKarl Rupp     *len = 0;
14408865f1eaSKarl Rupp   }
14418865f1eaSKarl Rupp   if (namelist) {
14428865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14438865f1eaSKarl Rupp     *namelist = 0;
14448865f1eaSKarl Rupp   }
14458865f1eaSKarl Rupp   if (islist) {
14468865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14478865f1eaSKarl Rupp     *islist = 0;
14488865f1eaSKarl Rupp   }
14498865f1eaSKarl Rupp   if (dmlist) {
14508865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14518865f1eaSKarl Rupp     *dmlist = 0;
14528865f1eaSKarl Rupp   }
1453f3f0edfdSDmitry Karpeev   /*
1454f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1455f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1456f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1457f3f0edfdSDmitry Karpeev    */
1458ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
145916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1460435a35e8SMatthew G Knepley     PetscSection section;
1461435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1462435a35e8SMatthew G Knepley 
1463435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1464435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1465435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1466f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
146703dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
146803dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
146903dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1470435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1471435a35e8SMatthew G Knepley         const char *fieldName;
1472435a35e8SMatthew G Knepley 
147303dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
147403dc3394SMatthew G. Knepley         if (namelist) {
1475435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1476435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1477435a35e8SMatthew G Knepley         }
147803dc3394SMatthew G. Knepley       }
1479435a35e8SMatthew G Knepley     } else {
148069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1481e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
14820298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1483e7c4fc90SDmitry Karpeev     }
14848865f1eaSKarl Rupp   } else {
148516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
148616621825SDmitry Karpeev   }
148716621825SDmitry Karpeev   PetscFunctionReturn(0);
148816621825SDmitry Karpeev }
148916621825SDmitry Karpeev 
1490564cec59SMatthew G. Knepley /*@
1491435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1492435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1493435a35e8SMatthew G Knepley 
1494435a35e8SMatthew G Knepley   Not collective
1495435a35e8SMatthew G Knepley 
1496435a35e8SMatthew G Knepley   Input Parameters:
1497435a35e8SMatthew G Knepley + dm - the DM object
14983cd36596SPatrick Sanan . numFields - the number of fields in this subproblem
14993cd36596SPatrick Sanan - fields - the fields in the subproblem
1500435a35e8SMatthew G Knepley 
1501435a35e8SMatthew G Knepley   Output Parameters:
15023cd36596SPatrick Sanan + is - the global indices for the subproblem
15033cd36596SPatrick Sanan - dm - the DM for the subproblem
1504435a35e8SMatthew G Knepley 
1505435a35e8SMatthew G Knepley   Level: intermediate
1506435a35e8SMatthew G Knepley 
1507435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1508435a35e8SMatthew G Knepley @*/
1509435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1510435a35e8SMatthew G Knepley {
1511435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1512435a35e8SMatthew G Knepley 
1513435a35e8SMatthew G Knepley   PetscFunctionBegin;
1514435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1515435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15168865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15178865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1518435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1519435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
152082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1521435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1522435a35e8SMatthew G Knepley }
1523435a35e8SMatthew G Knepley 
152416621825SDmitry Karpeev 
152516621825SDmitry Karpeev /*@C
15268d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
15278d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
15288d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
15298d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
15308d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
153116621825SDmitry Karpeev 
153216621825SDmitry Karpeev   Not collective
153316621825SDmitry Karpeev 
153416621825SDmitry Karpeev   Input Parameter:
153516621825SDmitry Karpeev . dm - the DM object
153616621825SDmitry Karpeev 
153716621825SDmitry Karpeev   Output Parameters:
15380298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
15390298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
15400298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
15410298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
15420298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
154316621825SDmitry Karpeev 
154416621825SDmitry Karpeev   Level: intermediate
154516621825SDmitry Karpeev 
154616621825SDmitry Karpeev   Notes:
154716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
154816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
154916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
155016621825SDmitry Karpeev 
15518d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
155216621825SDmitry Karpeev @*/
15538d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
155416621825SDmitry Karpeev {
155516621825SDmitry Karpeev   PetscErrorCode      ierr;
1556be081cd6SPeter Brune   DMSubDomainHookLink link;
1557be081cd6SPeter Brune   PetscInt            i,l;
155816621825SDmitry Karpeev 
155916621825SDmitry Karpeev   PetscFunctionBegin;
156016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
156114a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
15620298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
15630298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
15640298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
15650298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1566f3f0edfdSDmitry Karpeev   /*
1567f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1568f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1569f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1570f3f0edfdSDmitry Karpeev    */
1571ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
157216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1573be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
157414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1575f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1576be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1577be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1578be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1579be081cd6SPeter Brune         }
1580648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1581e7c4fc90SDmitry Karpeev       }
158214a18fd3SPeter Brune     }
158314a18fd3SPeter Brune     if (len) *len = l;
158414a18fd3SPeter Brune   }
1585e30e807fSPeter Brune   PetscFunctionReturn(0);
1586e30e807fSPeter Brune }
1587e30e807fSPeter Brune 
1588e30e807fSPeter Brune 
1589e30e807fSPeter Brune /*@C
1590e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1591e30e807fSPeter Brune 
1592e30e807fSPeter Brune   Not collective
1593e30e807fSPeter Brune 
1594e30e807fSPeter Brune   Input Parameters:
1595e30e807fSPeter Brune + dm - the DM object
1596e30e807fSPeter Brune . n  - the number of subdomain scatters
1597e30e807fSPeter Brune - subdms - the local subdomains
1598e30e807fSPeter Brune 
1599e30e807fSPeter Brune   Output Parameters:
1600e30e807fSPeter Brune + n     - the number of scatters returned
1601e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1602e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1603e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1604e30e807fSPeter Brune 
1605e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1606e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1607e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1608e30e807fSPeter Brune   solution and residual data.
1609e30e807fSPeter Brune 
1610e30e807fSPeter Brune   Level: developer
1611e30e807fSPeter Brune 
1612e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1613e30e807fSPeter Brune @*/
1614e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1615e30e807fSPeter Brune {
1616e30e807fSPeter Brune   PetscErrorCode ierr;
1617e30e807fSPeter Brune 
1618e30e807fSPeter Brune   PetscFunctionBegin;
1619e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1620e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1621e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1622e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
16236668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1624e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1625e7c4fc90SDmitry Karpeev }
1626e7c4fc90SDmitry Karpeev 
162747c6ae99SBarry Smith /*@
162847c6ae99SBarry Smith   DMRefine - Refines a DM object
162947c6ae99SBarry Smith 
163047c6ae99SBarry Smith   Collective on DM
163147c6ae99SBarry Smith 
163247c6ae99SBarry Smith   Input Parameter:
163347c6ae99SBarry Smith + dm   - the DM object
163491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
163547c6ae99SBarry Smith 
163647c6ae99SBarry Smith   Output Parameter:
16370298fd71SBarry Smith . dmf - the refined DM, or NULL
1638ae0a1c52SMatthew G Knepley 
16390298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
164047c6ae99SBarry Smith 
164147c6ae99SBarry Smith   Level: developer
164247c6ae99SBarry Smith 
1643e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
164447c6ae99SBarry Smith @*/
16457087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
164647c6ae99SBarry Smith {
164747c6ae99SBarry Smith   PetscErrorCode   ierr;
1648c833c3b5SJed Brown   DMRefineHookLink link;
164947c6ae99SBarry Smith 
165047c6ae99SBarry Smith   PetscFunctionBegin;
1651732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16521ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1653fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
165447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
16554057135bSMatthew G Knepley   if (*dmf) {
165643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
16578865f1eaSKarl Rupp 
16588cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
16598865f1eaSKarl Rupp 
1660644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
16610598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1662656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
16638865f1eaSKarl Rupp 
1664e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1665c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
16668865f1eaSKarl Rupp       if (link->refinehook) {
16678865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
16688865f1eaSKarl Rupp       }
1669c833c3b5SJed Brown     }
1670c833c3b5SJed Brown   }
16711ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1672c833c3b5SJed Brown   PetscFunctionReturn(0);
1673c833c3b5SJed Brown }
1674c833c3b5SJed Brown 
1675bb9467b5SJed Brown /*@C
1676c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1677c833c3b5SJed Brown 
1678c833c3b5SJed Brown    Logically Collective
1679c833c3b5SJed Brown 
1680c833c3b5SJed Brown    Input Arguments:
1681c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1682c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1683c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
16840298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1685c833c3b5SJed Brown 
1686c833c3b5SJed Brown    Calling sequence of refinehook:
1687c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1688c833c3b5SJed Brown 
1689c833c3b5SJed Brown +  coarse - coarse level DM
1690c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1691c833c3b5SJed Brown -  ctx - optional user-defined function context
1692c833c3b5SJed Brown 
1693c833c3b5SJed Brown    Calling sequence for interphook:
1694c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1695c833c3b5SJed Brown 
1696c833c3b5SJed Brown +  coarse - coarse level DM
1697c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1698c833c3b5SJed Brown .  fine - fine level DM to update
1699c833c3b5SJed Brown -  ctx - optional user-defined function context
1700c833c3b5SJed Brown 
1701c833c3b5SJed Brown    Level: advanced
1702c833c3b5SJed Brown 
1703c833c3b5SJed Brown    Notes:
1704c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1705c833c3b5SJed Brown 
1706c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1707c833c3b5SJed Brown 
1708bb9467b5SJed Brown    This function is currently not available from Fortran.
1709bb9467b5SJed Brown 
1710c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1711c833c3b5SJed Brown @*/
1712c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1713c833c3b5SJed Brown {
1714c833c3b5SJed Brown   PetscErrorCode   ierr;
1715c833c3b5SJed Brown   DMRefineHookLink link,*p;
1716c833c3b5SJed Brown 
1717c833c3b5SJed Brown   PetscFunctionBegin;
1718c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
17193d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
17203d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
17213d8e3701SJed Brown   }
172295dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1723c833c3b5SJed Brown   link->refinehook = refinehook;
1724c833c3b5SJed Brown   link->interphook = interphook;
1725c833c3b5SJed Brown   link->ctx        = ctx;
17260298fd71SBarry Smith   link->next       = NULL;
1727c833c3b5SJed Brown   *p               = link;
1728c833c3b5SJed Brown   PetscFunctionReturn(0);
1729c833c3b5SJed Brown }
1730c833c3b5SJed Brown 
17313d8e3701SJed Brown /*@C
17323d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
17333d8e3701SJed Brown 
17343d8e3701SJed Brown    Logically Collective
17353d8e3701SJed Brown 
17363d8e3701SJed Brown    Input Arguments:
17373d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
17383d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
17393d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17403d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
17413d8e3701SJed Brown 
17423d8e3701SJed Brown    Level: advanced
17433d8e3701SJed Brown 
17443d8e3701SJed Brown    Notes:
17453d8e3701SJed Brown    This function does nothing if the hook is not in the list.
17463d8e3701SJed Brown 
17473d8e3701SJed Brown    This function is currently not available from Fortran.
17483d8e3701SJed Brown 
17493d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
17503d8e3701SJed Brown @*/
17513d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
17523d8e3701SJed Brown {
17533d8e3701SJed Brown   PetscErrorCode   ierr;
17543d8e3701SJed Brown   DMRefineHookLink link,*p;
17553d8e3701SJed Brown 
17563d8e3701SJed Brown   PetscFunctionBegin;
17573d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
17583d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
17593d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
17603d8e3701SJed Brown       link = *p;
17613d8e3701SJed Brown       *p = link->next;
17623d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
17633d8e3701SJed Brown       break;
17643d8e3701SJed Brown     }
17653d8e3701SJed Brown   }
17663d8e3701SJed Brown   PetscFunctionReturn(0);
17673d8e3701SJed Brown }
17683d8e3701SJed Brown 
1769c833c3b5SJed Brown /*@
1770c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1771c833c3b5SJed Brown 
1772c833c3b5SJed Brown    Collective if any hooks are
1773c833c3b5SJed Brown 
1774c833c3b5SJed Brown    Input Arguments:
1775c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1776e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1777c833c3b5SJed Brown -  fine - finer DM to update
1778c833c3b5SJed Brown 
1779c833c3b5SJed Brown    Level: developer
1780c833c3b5SJed Brown 
1781c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1782c833c3b5SJed Brown @*/
1783c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1784c833c3b5SJed Brown {
1785c833c3b5SJed Brown   PetscErrorCode   ierr;
1786c833c3b5SJed Brown   DMRefineHookLink link;
1787c833c3b5SJed Brown 
1788c833c3b5SJed Brown   PetscFunctionBegin;
1789c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
17908865f1eaSKarl Rupp     if (link->interphook) {
17918865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
17928865f1eaSKarl Rupp     }
17934057135bSMatthew G Knepley   }
179447c6ae99SBarry Smith   PetscFunctionReturn(0);
179547c6ae99SBarry Smith }
179647c6ae99SBarry Smith 
1797eb3f98d2SBarry Smith /*@
1798eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1799eb3f98d2SBarry Smith 
1800eb3f98d2SBarry Smith     Not Collective
1801eb3f98d2SBarry Smith 
1802eb3f98d2SBarry Smith     Input Parameter:
1803eb3f98d2SBarry Smith .   dm - the DM object
1804eb3f98d2SBarry Smith 
1805eb3f98d2SBarry Smith     Output Parameter:
1806eb3f98d2SBarry Smith .   level - number of refinements
1807eb3f98d2SBarry Smith 
1808eb3f98d2SBarry Smith     Level: developer
1809eb3f98d2SBarry Smith 
18106a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1811eb3f98d2SBarry Smith 
1812eb3f98d2SBarry Smith @*/
1813eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1814eb3f98d2SBarry Smith {
1815eb3f98d2SBarry Smith   PetscFunctionBegin;
1816eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1817eb3f98d2SBarry Smith   *level = dm->levelup;
1818eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1819eb3f98d2SBarry Smith }
1820eb3f98d2SBarry Smith 
1821fef3a512SBarry Smith /*@
1822fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1823fef3a512SBarry Smith 
1824fef3a512SBarry Smith     Not Collective
1825fef3a512SBarry Smith 
1826fef3a512SBarry Smith     Input Parameter:
1827fef3a512SBarry Smith +   dm - the DM object
1828fef3a512SBarry Smith -   level - number of refinements
1829fef3a512SBarry Smith 
1830fef3a512SBarry Smith     Level: advanced
1831fef3a512SBarry Smith 
1832fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1833fef3a512SBarry Smith 
1834fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1835fef3a512SBarry Smith 
1836fef3a512SBarry Smith @*/
1837fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1838fef3a512SBarry Smith {
1839fef3a512SBarry Smith   PetscFunctionBegin;
1840fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1841fef3a512SBarry Smith   dm->levelup = level;
1842fef3a512SBarry Smith   PetscFunctionReturn(0);
1843fef3a512SBarry Smith }
1844fef3a512SBarry Smith 
1845bb9467b5SJed Brown /*@C
1846baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1847baf369e7SPeter Brune 
1848baf369e7SPeter Brune    Logically Collective
1849baf369e7SPeter Brune 
1850baf369e7SPeter Brune    Input Arguments:
1851baf369e7SPeter Brune +  dm - the DM
1852baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1853baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
18540298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1855baf369e7SPeter Brune 
1856baf369e7SPeter Brune    Calling sequence for beginhook:
1857baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1858baf369e7SPeter Brune 
1859baf369e7SPeter Brune +  dm - global DM
1860baf369e7SPeter Brune .  g - global vector
1861baf369e7SPeter Brune .  mode - mode
1862baf369e7SPeter Brune .  l - local vector
1863baf369e7SPeter Brune -  ctx - optional user-defined function context
1864baf369e7SPeter Brune 
1865baf369e7SPeter Brune 
1866baf369e7SPeter Brune    Calling sequence for endhook:
1867ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1868baf369e7SPeter Brune 
1869baf369e7SPeter Brune +  global - global DM
1870baf369e7SPeter Brune -  ctx - optional user-defined function context
1871baf369e7SPeter Brune 
1872baf369e7SPeter Brune    Level: advanced
1873baf369e7SPeter Brune 
1874baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1875baf369e7SPeter Brune @*/
1876baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1877baf369e7SPeter Brune {
1878baf369e7SPeter Brune   PetscErrorCode          ierr;
1879baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1880baf369e7SPeter Brune 
1881baf369e7SPeter Brune   PetscFunctionBegin;
1882baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1883baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
188495dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1885baf369e7SPeter Brune   link->beginhook = beginhook;
1886baf369e7SPeter Brune   link->endhook   = endhook;
1887baf369e7SPeter Brune   link->ctx       = ctx;
18880298fd71SBarry Smith   link->next      = NULL;
1889baf369e7SPeter Brune   *p              = link;
1890baf369e7SPeter Brune   PetscFunctionReturn(0);
1891baf369e7SPeter Brune }
1892baf369e7SPeter Brune 
18934c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
18944c274da1SToby Isaac {
18954c274da1SToby Isaac   Mat cMat;
18964c274da1SToby Isaac   Vec cVec;
18974c274da1SToby Isaac   PetscSection section, cSec;
18984c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18994c274da1SToby Isaac   PetscErrorCode ierr;
19004c274da1SToby Isaac 
19014c274da1SToby Isaac   PetscFunctionBegin;
19024c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19034c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19044c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
19055db9a05bSToby Isaac     PetscInt nRows;
19065db9a05bSToby Isaac 
19075db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
19085db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
19094c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19107711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19114c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
19124c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19134c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19144c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19154c274da1SToby Isaac       if (dof) {
19164c274da1SToby Isaac         PetscScalar *vals;
19174c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
19184c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
19194c274da1SToby Isaac       }
19204c274da1SToby Isaac     }
19214c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19224c274da1SToby Isaac   }
19234c274da1SToby Isaac   PetscFunctionReturn(0);
19244c274da1SToby Isaac }
19254c274da1SToby Isaac 
192647c6ae99SBarry Smith /*@
192747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
192847c6ae99SBarry Smith 
192947c6ae99SBarry Smith     Neighbor-wise Collective on DM
193047c6ae99SBarry Smith 
193147c6ae99SBarry Smith     Input Parameters:
193247c6ae99SBarry Smith +   dm - the DM object
193347c6ae99SBarry Smith .   g - the global vector
193447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
193547c6ae99SBarry Smith -   l - the local vector
193647c6ae99SBarry Smith 
193747c6ae99SBarry Smith 
193847c6ae99SBarry Smith     Level: beginner
193947c6ae99SBarry Smith 
1940e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
194147c6ae99SBarry Smith 
194247c6ae99SBarry Smith @*/
19437087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
194447c6ae99SBarry Smith {
19457128ae9fSMatthew G Knepley   PetscSF                 sf;
194647c6ae99SBarry Smith   PetscErrorCode          ierr;
1947baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
194847c6ae99SBarry Smith 
194947c6ae99SBarry Smith   PetscFunctionBegin;
1950171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1951baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
19528865f1eaSKarl Rupp     if (link->beginhook) {
19538865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
19548865f1eaSKarl Rupp     }
1955baf369e7SPeter Brune   }
19567128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19577128ae9fSMatthew G Knepley   if (sf) {
1958ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1959ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
19607128ae9fSMatthew G Knepley 
196182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19627128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1963ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19647128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19657128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1966ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19677128ae9fSMatthew G Knepley   } else {
1968843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19697128ae9fSMatthew G Knepley   }
197047c6ae99SBarry Smith   PetscFunctionReturn(0);
197147c6ae99SBarry Smith }
197247c6ae99SBarry Smith 
197347c6ae99SBarry Smith /*@
197447c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
197547c6ae99SBarry Smith 
197647c6ae99SBarry Smith     Neighbor-wise Collective on DM
197747c6ae99SBarry Smith 
197847c6ae99SBarry Smith     Input Parameters:
197947c6ae99SBarry Smith +   dm - the DM object
198047c6ae99SBarry Smith .   g - the global vector
198147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
198247c6ae99SBarry Smith -   l - the local vector
198347c6ae99SBarry Smith 
198447c6ae99SBarry Smith 
198547c6ae99SBarry Smith     Level: beginner
198647c6ae99SBarry Smith 
1987e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
198847c6ae99SBarry Smith 
198947c6ae99SBarry Smith @*/
19907087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
199147c6ae99SBarry Smith {
19927128ae9fSMatthew G Knepley   PetscSF                 sf;
199347c6ae99SBarry Smith   PetscErrorCode          ierr;
1994ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1995ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1996baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
199747c6ae99SBarry Smith 
199847c6ae99SBarry Smith   PetscFunctionBegin;
1999171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20007128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20017128ae9fSMatthew G Knepley   if (sf) {
200282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20037128ae9fSMatthew G Knepley 
20047128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2005ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20067128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20077128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2008ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20097128ae9fSMatthew G Knepley   } else {
2010843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20117128ae9fSMatthew G Knepley   }
20124c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2013baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2014baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2015baf369e7SPeter Brune   }
201647c6ae99SBarry Smith   PetscFunctionReturn(0);
201747c6ae99SBarry Smith }
201847c6ae99SBarry Smith 
2019d4d07f1eSToby Isaac /*@C
2020d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2021d4d07f1eSToby Isaac 
2022d4d07f1eSToby Isaac    Logically Collective
2023d4d07f1eSToby Isaac 
2024d4d07f1eSToby Isaac    Input Arguments:
2025d4d07f1eSToby Isaac +  dm - the DM
2026d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2027d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2028d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2029d4d07f1eSToby Isaac 
2030d4d07f1eSToby Isaac    Calling sequence for beginhook:
2031d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2032d4d07f1eSToby Isaac 
2033d4d07f1eSToby Isaac +  dm - global DM
2034d4d07f1eSToby Isaac .  l - local vector
2035d4d07f1eSToby Isaac .  mode - mode
2036d4d07f1eSToby Isaac .  g - global vector
2037d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2038d4d07f1eSToby Isaac 
2039d4d07f1eSToby Isaac 
2040d4d07f1eSToby Isaac    Calling sequence for endhook:
2041d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2042d4d07f1eSToby Isaac 
2043d4d07f1eSToby Isaac +  global - global DM
2044d4d07f1eSToby Isaac .  l - local vector
2045d4d07f1eSToby Isaac .  mode - mode
2046d4d07f1eSToby Isaac .  g - global vector
2047d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2048d4d07f1eSToby Isaac 
2049d4d07f1eSToby Isaac    Level: advanced
2050d4d07f1eSToby Isaac 
2051d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2052d4d07f1eSToby Isaac @*/
2053d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2054d4d07f1eSToby Isaac {
2055d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2056d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2057d4d07f1eSToby Isaac 
2058d4d07f1eSToby Isaac   PetscFunctionBegin;
2059d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2060d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
206195dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2062d4d07f1eSToby Isaac   link->beginhook = beginhook;
2063d4d07f1eSToby Isaac   link->endhook   = endhook;
2064d4d07f1eSToby Isaac   link->ctx       = ctx;
2065d4d07f1eSToby Isaac   link->next      = NULL;
2066d4d07f1eSToby Isaac   *p              = link;
2067d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2068d4d07f1eSToby Isaac }
2069d4d07f1eSToby Isaac 
20704c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
20714c274da1SToby Isaac {
20724c274da1SToby Isaac   Mat cMat;
20734c274da1SToby Isaac   Vec cVec;
20744c274da1SToby Isaac   PetscSection section, cSec;
20754c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20764c274da1SToby Isaac   PetscErrorCode ierr;
20774c274da1SToby Isaac 
20784c274da1SToby Isaac   PetscFunctionBegin;
20794c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20804c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20814c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
20825db9a05bSToby Isaac     PetscInt nRows;
20835db9a05bSToby Isaac 
20845db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
20855db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
20864c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
20877711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
20884c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20894c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20904c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20914c274da1SToby Isaac       if (dof) {
20924c274da1SToby Isaac         PetscInt d;
20934c274da1SToby Isaac         PetscScalar *vals;
20944c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
20954c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
20964c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
20974c274da1SToby Isaac          * we just extracted */
20984c274da1SToby Isaac         for (d = 0; d < dof; d++) {
20994c274da1SToby Isaac           vals[d] = 0.;
21004c274da1SToby Isaac         }
21014c274da1SToby Isaac       }
21024c274da1SToby Isaac     }
21034c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
21044c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21054c274da1SToby Isaac   }
21064c274da1SToby Isaac   PetscFunctionReturn(0);
21074c274da1SToby Isaac }
21084c274da1SToby Isaac 
210947c6ae99SBarry Smith /*@
21109a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21119a42bb27SBarry Smith 
21129a42bb27SBarry Smith     Neighbor-wise Collective on DM
21139a42bb27SBarry Smith 
21149a42bb27SBarry Smith     Input Parameters:
21159a42bb27SBarry Smith +   dm - the DM object
2116f6813fd5SJed Brown .   l - the local vector
21171eb28f2eSBarry 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.
21181eb28f2eSBarry Smith -   g - the global vector
21199a42bb27SBarry Smith 
2120d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
212184330215SMatthew 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.
21229a42bb27SBarry Smith 
21239a42bb27SBarry Smith     Level: beginner
21249a42bb27SBarry Smith 
2125e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
21269a42bb27SBarry Smith 
21279a42bb27SBarry Smith @*/
21287087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
21299a42bb27SBarry Smith {
21307128ae9fSMatthew G Knepley   PetscSF                 sf;
213184330215SMatthew G. Knepley   PetscSection            s, gs;
2132d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2133ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2134ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
213584330215SMatthew G. Knepley   PetscBool               isInsert;
213684330215SMatthew G. Knepley   PetscErrorCode          ierr;
21379a42bb27SBarry Smith 
21389a42bb27SBarry Smith   PetscFunctionBegin;
2139171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2140d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2141d4d07f1eSToby Isaac     if (link->beginhook) {
2142d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2143d4d07f1eSToby Isaac     }
2144d4d07f1eSToby Isaac   }
21454c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
21467128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
214784330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21487128ae9fSMatthew G Knepley   switch (mode) {
21497128ae9fSMatthew G Knepley   case INSERT_VALUES:
21507128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2151304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
215284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21537128ae9fSMatthew G Knepley   case ADD_VALUES:
21547128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2155304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
215684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21577128ae9fSMatthew G Knepley   default:
215882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21597128ae9fSMatthew G Knepley   }
216084330215SMatthew G. Knepley   if (sf && !isInsert) {
2161ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21627128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2163a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2164ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
216584330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
216684330215SMatthew G. Knepley   } else if (s && isInsert) {
216784330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
216884330215SMatthew G. Knepley 
216984330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
217084330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
217184330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2172ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
217384330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
217484330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2175b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
217684330215SMatthew G. Knepley 
217784330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
217803442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
217984330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2180b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
218184330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
218284330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2183b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
218403442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2185b3b16f48SMatthew 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);
2186b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2187b3b16f48SMatthew G. Knepley       if (!gcdof) {
218884330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2189b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2190b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
219184330215SMatthew G. Knepley         const PetscInt *cdofs;
219284330215SMatthew G. Knepley         PetscInt        cind = 0;
219384330215SMatthew G. Knepley 
219484330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2195b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
219684330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2197b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
219884330215SMatthew G. Knepley         }
2199b3b16f48SMatthew 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);
220084330215SMatthew G. Knepley     }
2201ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22027128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
22037128ae9fSMatthew G Knepley   } else {
2204843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22057128ae9fSMatthew G Knepley   }
22069a42bb27SBarry Smith   PetscFunctionReturn(0);
22079a42bb27SBarry Smith }
22089a42bb27SBarry Smith 
22099a42bb27SBarry Smith /*@
22109a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
221147c6ae99SBarry Smith 
221247c6ae99SBarry Smith     Neighbor-wise Collective on DM
221347c6ae99SBarry Smith 
221447c6ae99SBarry Smith     Input Parameters:
221547c6ae99SBarry Smith +   dm - the DM object
2216f6813fd5SJed Brown .   l - the local vector
221747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2218f6813fd5SJed Brown -   g - the global vector
221947c6ae99SBarry Smith 
222047c6ae99SBarry Smith 
222147c6ae99SBarry Smith     Level: beginner
222247c6ae99SBarry Smith 
2223e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
222447c6ae99SBarry Smith 
222547c6ae99SBarry Smith @*/
22267087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
222747c6ae99SBarry Smith {
22287128ae9fSMatthew G Knepley   PetscSF                 sf;
222984330215SMatthew G. Knepley   PetscSection            s;
2230d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
223184330215SMatthew G. Knepley   PetscBool               isInsert;
223284330215SMatthew G. Knepley   PetscErrorCode          ierr;
223347c6ae99SBarry Smith 
223447c6ae99SBarry Smith   PetscFunctionBegin;
2235171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22367128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
223784330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22387128ae9fSMatthew G Knepley   switch (mode) {
22397128ae9fSMatthew G Knepley   case INSERT_VALUES:
22407128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
224184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22427128ae9fSMatthew G Knepley   case ADD_VALUES:
22437128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
224484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22457128ae9fSMatthew G Knepley   default:
224682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22477128ae9fSMatthew G Knepley   }
224884330215SMatthew G. Knepley   if (sf && !isInsert) {
2249ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2250ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
225184330215SMatthew G. Knepley 
2252ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22537128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2254a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2255ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22567128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
225784330215SMatthew G. Knepley   } else if (s && isInsert) {
22587128ae9fSMatthew G Knepley   } else {
2259843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22607128ae9fSMatthew G Knepley   }
2261d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2262d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2263d4d07f1eSToby Isaac   }
226447c6ae99SBarry Smith   PetscFunctionReturn(0);
226547c6ae99SBarry Smith }
226647c6ae99SBarry Smith 
2267f089877aSRichard Tran Mills /*@
2268bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2269bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2270d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2271f089877aSRichard Tran Mills 
2272bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2273f089877aSRichard Tran Mills 
2274f089877aSRichard Tran Mills    Input Parameters:
2275f089877aSRichard Tran Mills +  dm - the DM object
2276bc0a1609SRichard Tran Mills .  g - the original local vector
2277bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2278f089877aSRichard Tran Mills 
2279bc0a1609SRichard Tran Mills    Output Parameter:
2280bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2281f089877aSRichard Tran Mills 
2282f089877aSRichard Tran Mills    Level: intermediate
2283f089877aSRichard Tran Mills 
2284bc0a1609SRichard Tran Mills    Notes:
2285bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2286bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2287bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2288bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2289bc0a1609SRichard Tran Mills 
2290bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2291bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2292f089877aSRichard Tran Mills 
2293f089877aSRichard Tran Mills @*/
2294f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2295f089877aSRichard Tran Mills {
2296f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2297f089877aSRichard Tran Mills 
2298f089877aSRichard Tran Mills   PetscFunctionBegin;
2299f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2300bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2301f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2302f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2303f089877aSRichard Tran Mills }
2304f089877aSRichard Tran Mills 
2305f089877aSRichard Tran Mills /*@
2306bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2307bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2308d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2309f089877aSRichard Tran Mills 
2310bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2311f089877aSRichard Tran Mills 
2312f089877aSRichard Tran Mills    Input Parameters:
2313bc0a1609SRichard Tran Mills +  da - the DM object
2314bc0a1609SRichard Tran Mills .  g - the original local vector
2315bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2316f089877aSRichard Tran Mills 
2317bc0a1609SRichard Tran Mills    Output Parameter:
2318bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2319f089877aSRichard Tran Mills 
2320f089877aSRichard Tran Mills    Level: intermediate
2321f089877aSRichard Tran Mills 
2322bc0a1609SRichard Tran Mills    Notes:
2323bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2324bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2325bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2326bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2327bc0a1609SRichard Tran Mills 
2328bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2329bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2330f089877aSRichard Tran Mills 
2331f089877aSRichard Tran Mills @*/
2332f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2333f089877aSRichard Tran Mills {
2334f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2335f089877aSRichard Tran Mills 
2336f089877aSRichard Tran Mills   PetscFunctionBegin;
2337f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2338bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2339f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2340f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2341f089877aSRichard Tran Mills }
2342f089877aSRichard Tran Mills 
2343f089877aSRichard Tran Mills 
234447c6ae99SBarry Smith /*@
234547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
234647c6ae99SBarry Smith 
234747c6ae99SBarry Smith     Collective on DM
234847c6ae99SBarry Smith 
234947c6ae99SBarry Smith     Input Parameter:
235047c6ae99SBarry Smith +   dm - the DM object
235191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
235247c6ae99SBarry Smith 
235347c6ae99SBarry Smith     Output Parameter:
235447c6ae99SBarry Smith .   dmc - the coarsened DM
235547c6ae99SBarry Smith 
235647c6ae99SBarry Smith     Level: developer
235747c6ae99SBarry Smith 
2358e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
235947c6ae99SBarry Smith 
236047c6ae99SBarry Smith @*/
23617087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
236247c6ae99SBarry Smith {
236347c6ae99SBarry Smith   PetscErrorCode    ierr;
2364b17ce1afSJed Brown   DMCoarsenHookLink link;
236547c6ae99SBarry Smith 
236647c6ae99SBarry Smith   PetscFunctionBegin;
2367171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2368fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
236947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
237047c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
23718892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2372a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
237343842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
23748cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2375644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
23760598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2377656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2378e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2379b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2380b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2381b17ce1afSJed Brown   }
238247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2383b17ce1afSJed Brown   PetscFunctionReturn(0);
2384b17ce1afSJed Brown }
2385b17ce1afSJed Brown 
2386bb9467b5SJed Brown /*@C
2387b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2388b17ce1afSJed Brown 
2389b17ce1afSJed Brown    Logically Collective
2390b17ce1afSJed Brown 
2391b17ce1afSJed Brown    Input Arguments:
2392b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2393b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2394b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
23950298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2396b17ce1afSJed Brown 
2397b17ce1afSJed Brown    Calling sequence of coarsenhook:
2398b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2399b17ce1afSJed Brown 
2400b17ce1afSJed Brown +  fine - fine level DM
2401b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2402b17ce1afSJed Brown -  ctx - optional user-defined function context
2403b17ce1afSJed Brown 
2404b17ce1afSJed Brown    Calling sequence for restricthook:
2405c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2406b17ce1afSJed Brown 
2407b17ce1afSJed Brown +  fine - fine level DM
2408b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2409c833c3b5SJed Brown .  rscale - scaling vector for restriction
2410c833c3b5SJed Brown .  inject - matrix restricting by injection
2411b17ce1afSJed Brown .  coarse - coarse level DM to update
2412b17ce1afSJed Brown -  ctx - optional user-defined function context
2413b17ce1afSJed Brown 
2414b17ce1afSJed Brown    Level: advanced
2415b17ce1afSJed Brown 
2416b17ce1afSJed Brown    Notes:
2417b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2418b17ce1afSJed Brown 
2419b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2420b17ce1afSJed Brown 
2421b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2422b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2423b17ce1afSJed Brown 
2424bb9467b5SJed Brown    This function is currently not available from Fortran.
2425bb9467b5SJed Brown 
2426dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2427b17ce1afSJed Brown @*/
2428b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2429b17ce1afSJed Brown {
2430b17ce1afSJed Brown   PetscErrorCode    ierr;
2431b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2432b17ce1afSJed Brown 
2433b17ce1afSJed Brown   PetscFunctionBegin;
2434b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
24351e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
24361e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
24371e3d8eccSJed Brown   }
243895dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2439b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2440b17ce1afSJed Brown   link->restricthook = restricthook;
2441b17ce1afSJed Brown   link->ctx          = ctx;
24420298fd71SBarry Smith   link->next         = NULL;
2443b17ce1afSJed Brown   *p                 = link;
2444b17ce1afSJed Brown   PetscFunctionReturn(0);
2445b17ce1afSJed Brown }
2446b17ce1afSJed Brown 
2447dc822a44SJed Brown /*@C
2448dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2449dc822a44SJed Brown 
2450dc822a44SJed Brown    Logically Collective
2451dc822a44SJed Brown 
2452dc822a44SJed Brown    Input Arguments:
2453dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2454dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2455dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2456dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2457dc822a44SJed Brown 
2458dc822a44SJed Brown    Level: advanced
2459dc822a44SJed Brown 
2460dc822a44SJed Brown    Notes:
2461dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2462dc822a44SJed Brown 
2463dc822a44SJed Brown    This function is currently not available from Fortran.
2464dc822a44SJed Brown 
2465dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2466dc822a44SJed Brown @*/
2467dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2468dc822a44SJed Brown {
2469dc822a44SJed Brown   PetscErrorCode    ierr;
2470dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2471dc822a44SJed Brown 
2472dc822a44SJed Brown   PetscFunctionBegin;
2473dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2474dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2475dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2476dc822a44SJed Brown       link = *p;
2477dc822a44SJed Brown       *p = link->next;
2478dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2479dc822a44SJed Brown       break;
2480dc822a44SJed Brown     }
2481dc822a44SJed Brown   }
2482dc822a44SJed Brown   PetscFunctionReturn(0);
2483dc822a44SJed Brown }
2484dc822a44SJed Brown 
2485dc822a44SJed Brown 
2486b17ce1afSJed Brown /*@
2487b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2488b17ce1afSJed Brown 
2489b17ce1afSJed Brown    Collective if any hooks are
2490b17ce1afSJed Brown 
2491b17ce1afSJed Brown    Input Arguments:
2492b17ce1afSJed Brown +  fine - finer DM to use as a base
2493b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2494e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2495b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2496e91eccc2SStefano Zampini -  coarse - coarser DM to update
2497b17ce1afSJed Brown 
2498b17ce1afSJed Brown    Level: developer
2499b17ce1afSJed Brown 
2500b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2501b17ce1afSJed Brown @*/
2502b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2503b17ce1afSJed Brown {
2504b17ce1afSJed Brown   PetscErrorCode    ierr;
2505b17ce1afSJed Brown   DMCoarsenHookLink link;
2506b17ce1afSJed Brown 
2507b17ce1afSJed Brown   PetscFunctionBegin;
2508b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
25098865f1eaSKarl Rupp     if (link->restricthook) {
25108865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
25118865f1eaSKarl Rupp     }
2512b17ce1afSJed Brown   }
251347c6ae99SBarry Smith   PetscFunctionReturn(0);
251447c6ae99SBarry Smith }
251547c6ae99SBarry Smith 
2516bb9467b5SJed Brown /*@C
2517be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
25185dbd56e3SPeter Brune 
25195dbd56e3SPeter Brune    Logically Collective
25205dbd56e3SPeter Brune 
25215dbd56e3SPeter Brune    Input Arguments:
25225dbd56e3SPeter Brune +  global - global DM
2523ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
25245dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
25250298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
25265dbd56e3SPeter Brune 
2527ec4806b8SPeter Brune 
2528ec4806b8SPeter Brune    Calling sequence for ddhook:
2529ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2530ec4806b8SPeter Brune 
2531ec4806b8SPeter Brune +  global - global DM
2532ec4806b8SPeter Brune .  block  - block DM
2533ec4806b8SPeter Brune -  ctx - optional user-defined function context
2534ec4806b8SPeter Brune 
25355dbd56e3SPeter Brune    Calling sequence for restricthook:
2536ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
25375dbd56e3SPeter Brune 
25385dbd56e3SPeter Brune +  global - global DM
25395dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
25405dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2541ec4806b8SPeter Brune .  block  - block DM
25425dbd56e3SPeter Brune -  ctx - optional user-defined function context
25435dbd56e3SPeter Brune 
25445dbd56e3SPeter Brune    Level: advanced
25455dbd56e3SPeter Brune 
25465dbd56e3SPeter Brune    Notes:
2547ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
25485dbd56e3SPeter Brune 
25495dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
25505dbd56e3SPeter Brune 
25515dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2552ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
25535dbd56e3SPeter Brune 
2554bb9467b5SJed Brown    This function is currently not available from Fortran.
2555bb9467b5SJed Brown 
25565dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
25575dbd56e3SPeter Brune @*/
2558be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
25595dbd56e3SPeter Brune {
25605dbd56e3SPeter Brune   PetscErrorCode      ierr;
2561be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
25625dbd56e3SPeter Brune 
25635dbd56e3SPeter Brune   PetscFunctionBegin;
25645dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2565b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2566b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2567b3a6b972SJed Brown   }
256895dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
25695dbd56e3SPeter Brune   link->restricthook = restricthook;
2570be081cd6SPeter Brune   link->ddhook       = ddhook;
25715dbd56e3SPeter Brune   link->ctx          = ctx;
25720298fd71SBarry Smith   link->next         = NULL;
25735dbd56e3SPeter Brune   *p                 = link;
25745dbd56e3SPeter Brune   PetscFunctionReturn(0);
25755dbd56e3SPeter Brune }
25765dbd56e3SPeter Brune 
2577b3a6b972SJed Brown /*@C
2578b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2579b3a6b972SJed Brown 
2580b3a6b972SJed Brown    Logically Collective
2581b3a6b972SJed Brown 
2582b3a6b972SJed Brown    Input Arguments:
2583b3a6b972SJed Brown +  global - global DM
2584b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2585b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2586b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2587b3a6b972SJed Brown 
2588b3a6b972SJed Brown    Level: advanced
2589b3a6b972SJed Brown 
2590b3a6b972SJed Brown    Notes:
2591b3a6b972SJed Brown 
2592b3a6b972SJed Brown    This function is currently not available from Fortran.
2593b3a6b972SJed Brown 
2594b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2595b3a6b972SJed Brown @*/
2596b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2597b3a6b972SJed Brown {
2598b3a6b972SJed Brown   PetscErrorCode      ierr;
2599b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2600b3a6b972SJed Brown 
2601b3a6b972SJed Brown   PetscFunctionBegin;
2602b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2603b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2604b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2605b3a6b972SJed Brown       link = *p;
2606b3a6b972SJed Brown       *p = link->next;
2607b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2608b3a6b972SJed Brown       break;
2609b3a6b972SJed Brown     }
2610b3a6b972SJed Brown   }
2611b3a6b972SJed Brown   PetscFunctionReturn(0);
2612b3a6b972SJed Brown }
2613b3a6b972SJed Brown 
26145dbd56e3SPeter Brune /*@
2615be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
26165dbd56e3SPeter Brune 
26175dbd56e3SPeter Brune    Collective if any hooks are
26185dbd56e3SPeter Brune 
26195dbd56e3SPeter Brune    Input Arguments:
26205dbd56e3SPeter Brune +  fine - finer DM to use as a base
2621be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2622be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
26235dbd56e3SPeter Brune -  coarse - coarer DM to update
26245dbd56e3SPeter Brune 
26255dbd56e3SPeter Brune    Level: developer
26265dbd56e3SPeter Brune 
26275dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
26285dbd56e3SPeter Brune @*/
2629be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
26305dbd56e3SPeter Brune {
26315dbd56e3SPeter Brune   PetscErrorCode      ierr;
2632be081cd6SPeter Brune   DMSubDomainHookLink link;
26335dbd56e3SPeter Brune 
26345dbd56e3SPeter Brune   PetscFunctionBegin;
2635be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
26368865f1eaSKarl Rupp     if (link->restricthook) {
26378865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
26388865f1eaSKarl Rupp     }
26395dbd56e3SPeter Brune   }
26405dbd56e3SPeter Brune   PetscFunctionReturn(0);
26415dbd56e3SPeter Brune }
26425dbd56e3SPeter Brune 
26435fe1f584SPeter Brune /*@
26446a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
26455fe1f584SPeter Brune 
26465fe1f584SPeter Brune     Not Collective
26475fe1f584SPeter Brune 
26485fe1f584SPeter Brune     Input Parameter:
26495fe1f584SPeter Brune .   dm - the DM object
26505fe1f584SPeter Brune 
26515fe1f584SPeter Brune     Output Parameter:
26526a7d9d85SPeter Brune .   level - number of coarsenings
26535fe1f584SPeter Brune 
26545fe1f584SPeter Brune     Level: developer
26555fe1f584SPeter Brune 
26566a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
26575fe1f584SPeter Brune 
26585fe1f584SPeter Brune @*/
26595fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
26605fe1f584SPeter Brune {
26615fe1f584SPeter Brune   PetscFunctionBegin;
26625fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26635fe1f584SPeter Brune   *level = dm->leveldown;
26645fe1f584SPeter Brune   PetscFunctionReturn(0);
26655fe1f584SPeter Brune }
26665fe1f584SPeter Brune 
26675fe1f584SPeter Brune 
26685fe1f584SPeter Brune 
266947c6ae99SBarry Smith /*@C
267047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
267147c6ae99SBarry Smith 
267247c6ae99SBarry Smith     Collective on DM
267347c6ae99SBarry Smith 
267447c6ae99SBarry Smith     Input Parameter:
267547c6ae99SBarry Smith +   dm - the DM object
267647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
267747c6ae99SBarry Smith 
267847c6ae99SBarry Smith     Output Parameter:
267947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
268047c6ae99SBarry Smith 
268147c6ae99SBarry Smith     Level: developer
268247c6ae99SBarry Smith 
2683e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
268447c6ae99SBarry Smith 
268547c6ae99SBarry Smith @*/
26867087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
268747c6ae99SBarry Smith {
268847c6ae99SBarry Smith   PetscErrorCode ierr;
268947c6ae99SBarry Smith 
269047c6ae99SBarry Smith   PetscFunctionBegin;
2691171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2692ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
269347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
269447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
269547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
269647c6ae99SBarry Smith   } else if (dm->ops->refine) {
269747c6ae99SBarry Smith     PetscInt i;
269847c6ae99SBarry Smith 
2699ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
270047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2701ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
270247c6ae99SBarry Smith     }
2703ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
270447c6ae99SBarry Smith   PetscFunctionReturn(0);
270547c6ae99SBarry Smith }
270647c6ae99SBarry Smith 
270747c6ae99SBarry Smith /*@C
270847c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
270947c6ae99SBarry Smith 
271047c6ae99SBarry Smith     Collective on DM
271147c6ae99SBarry Smith 
271247c6ae99SBarry Smith     Input Parameter:
271347c6ae99SBarry Smith +   dm - the DM object
271447c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
271547c6ae99SBarry Smith 
271647c6ae99SBarry Smith     Output Parameter:
271747c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
271847c6ae99SBarry Smith 
271947c6ae99SBarry Smith     Level: developer
272047c6ae99SBarry Smith 
2721e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
272247c6ae99SBarry Smith 
272347c6ae99SBarry Smith @*/
27247087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
272547c6ae99SBarry Smith {
272647c6ae99SBarry Smith   PetscErrorCode ierr;
272747c6ae99SBarry Smith 
272847c6ae99SBarry Smith   PetscFunctionBegin;
2729171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2730ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
273147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
273247c6ae99SBarry Smith   PetscValidPointer(dmc,3);
273347c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
273447c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
273547c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
273647c6ae99SBarry Smith     PetscInt i;
273747c6ae99SBarry Smith 
2738ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
273947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2740ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
274147c6ae99SBarry Smith     }
2742ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
274347c6ae99SBarry Smith   PetscFunctionReturn(0);
274447c6ae99SBarry Smith }
274547c6ae99SBarry Smith 
274647c6ae99SBarry Smith /*@
2747e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
274847c6ae99SBarry Smith    grids associated with two DMs.
274947c6ae99SBarry Smith 
275047c6ae99SBarry Smith    Collective on DM
275147c6ae99SBarry Smith 
275247c6ae99SBarry Smith    Input Parameters:
275347c6ae99SBarry Smith +  dmc - the coarse grid DM
275447c6ae99SBarry Smith -  dmf - the fine grid DM
275547c6ae99SBarry Smith 
275647c6ae99SBarry Smith    Output Parameters:
275747c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
275847c6ae99SBarry Smith 
275947c6ae99SBarry Smith    Level: intermediate
276047c6ae99SBarry Smith 
276147c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
276247c6ae99SBarry Smith 
2763e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
276447c6ae99SBarry Smith @*/
2765e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
276647c6ae99SBarry Smith {
276747c6ae99SBarry Smith   PetscErrorCode ierr;
276847c6ae99SBarry Smith 
276947c6ae99SBarry Smith   PetscFunctionBegin;
2770171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2771171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
277247c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
277347c6ae99SBarry Smith   PetscFunctionReturn(0);
277447c6ae99SBarry Smith }
277547c6ae99SBarry Smith 
27761a266240SBarry Smith /*@C
27771a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
27781a266240SBarry Smith 
27791a266240SBarry Smith     Not Collective
27801a266240SBarry Smith 
27811a266240SBarry Smith     Input Parameters:
27821a266240SBarry Smith +   dm - the DM object
27831a266240SBarry Smith -   destroy - the destroy function
27841a266240SBarry Smith 
27851a266240SBarry Smith     Level: intermediate
27861a266240SBarry Smith 
2787e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
27881a266240SBarry Smith 
2789f07f9ceaSJed Brown @*/
27901a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
27911a266240SBarry Smith {
27921a266240SBarry Smith   PetscFunctionBegin;
2793171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27941a266240SBarry Smith   dm->ctxdestroy = destroy;
27951a266240SBarry Smith   PetscFunctionReturn(0);
27961a266240SBarry Smith }
27971a266240SBarry Smith 
2798b07ff414SBarry Smith /*@
27991b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
280047c6ae99SBarry Smith 
280147c6ae99SBarry Smith     Not Collective
280247c6ae99SBarry Smith 
280347c6ae99SBarry Smith     Input Parameters:
280447c6ae99SBarry Smith +   dm - the DM object
280547c6ae99SBarry Smith -   ctx - the user context
280647c6ae99SBarry Smith 
280747c6ae99SBarry Smith     Level: intermediate
280847c6ae99SBarry Smith 
2809e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
281047c6ae99SBarry Smith 
281147c6ae99SBarry Smith @*/
28121b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
281347c6ae99SBarry Smith {
281447c6ae99SBarry Smith   PetscFunctionBegin;
2815171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
281647c6ae99SBarry Smith   dm->ctx = ctx;
281747c6ae99SBarry Smith   PetscFunctionReturn(0);
281847c6ae99SBarry Smith }
281947c6ae99SBarry Smith 
282047c6ae99SBarry Smith /*@
28211b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
282247c6ae99SBarry Smith 
282347c6ae99SBarry Smith     Not Collective
282447c6ae99SBarry Smith 
282547c6ae99SBarry Smith     Input Parameter:
282647c6ae99SBarry Smith .   dm - the DM object
282747c6ae99SBarry Smith 
282847c6ae99SBarry Smith     Output Parameter:
282947c6ae99SBarry Smith .   ctx - the user context
283047c6ae99SBarry Smith 
283147c6ae99SBarry Smith     Level: intermediate
283247c6ae99SBarry Smith 
2833e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
283447c6ae99SBarry Smith 
283547c6ae99SBarry Smith @*/
28361b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
283747c6ae99SBarry Smith {
283847c6ae99SBarry Smith   PetscFunctionBegin;
2839171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28401b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
284147c6ae99SBarry Smith   PetscFunctionReturn(0);
284247c6ae99SBarry Smith }
284347c6ae99SBarry Smith 
284408da532bSDmitry Karpeev /*@C
2845df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
284608da532bSDmitry Karpeev 
284708da532bSDmitry Karpeev     Logically Collective on DM
284808da532bSDmitry Karpeev 
284908da532bSDmitry Karpeev     Input Parameter:
285008da532bSDmitry Karpeev +   dm - the DM object
28510298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
285208da532bSDmitry Karpeev 
285308da532bSDmitry Karpeev     Level: intermediate
285408da532bSDmitry Karpeev 
2855835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
285608da532bSDmitry Karpeev          DMSetJacobian()
285708da532bSDmitry Karpeev 
285808da532bSDmitry Karpeev @*/
285908da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
286008da532bSDmitry Karpeev {
286108da532bSDmitry Karpeev   PetscFunctionBegin;
286208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
286308da532bSDmitry Karpeev   PetscFunctionReturn(0);
286408da532bSDmitry Karpeev }
286508da532bSDmitry Karpeev 
286608da532bSDmitry Karpeev /*@
286708da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
286808da532bSDmitry Karpeev 
286908da532bSDmitry Karpeev     Not Collective
287008da532bSDmitry Karpeev 
287108da532bSDmitry Karpeev     Input Parameter:
287208da532bSDmitry Karpeev .   dm - the DM object to destroy
287308da532bSDmitry Karpeev 
287408da532bSDmitry Karpeev     Output Parameter:
287508da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
287608da532bSDmitry Karpeev 
287708da532bSDmitry Karpeev     Level: developer
287808da532bSDmitry Karpeev 
287974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
288008da532bSDmitry Karpeev 
288108da532bSDmitry Karpeev @*/
288208da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
288308da532bSDmitry Karpeev {
288408da532bSDmitry Karpeev   PetscFunctionBegin;
288508da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
288608da532bSDmitry Karpeev   PetscFunctionReturn(0);
288708da532bSDmitry Karpeev }
288808da532bSDmitry Karpeev 
288908da532bSDmitry Karpeev /*@C
289008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
289108da532bSDmitry Karpeev 
289208da532bSDmitry Karpeev     Logically Collective on DM
289308da532bSDmitry Karpeev 
289408da532bSDmitry Karpeev     Input Parameters:
2895907376e6SBarry Smith .   dm - the DM object
289608da532bSDmitry Karpeev 
289708da532bSDmitry Karpeev     Output parameters:
289808da532bSDmitry Karpeev +   xl - lower bound
289908da532bSDmitry Karpeev -   xu - upper bound
290008da532bSDmitry Karpeev 
2901907376e6SBarry Smith     Level: advanced
2902907376e6SBarry Smith 
2903907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
290408da532bSDmitry Karpeev 
290574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
290608da532bSDmitry Karpeev 
290708da532bSDmitry Karpeev @*/
290808da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
290908da532bSDmitry Karpeev {
291008da532bSDmitry Karpeev   PetscErrorCode ierr;
29115fd66863SKarl Rupp 
291208da532bSDmitry Karpeev   PetscFunctionBegin;
291308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
291408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
291508da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
291608da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
29178865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
291808da532bSDmitry Karpeev   PetscFunctionReturn(0);
291908da532bSDmitry Karpeev }
292008da532bSDmitry Karpeev 
2921b0ae01b7SPeter Brune /*@
2922b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2923b0ae01b7SPeter Brune 
2924b0ae01b7SPeter Brune     Not Collective
2925b0ae01b7SPeter Brune 
2926b0ae01b7SPeter Brune     Input Parameter:
2927b0ae01b7SPeter Brune .   dm - the DM object
2928b0ae01b7SPeter Brune 
2929b0ae01b7SPeter Brune     Output Parameter:
2930b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2931b0ae01b7SPeter Brune 
2932b0ae01b7SPeter Brune     Level: developer
2933b0ae01b7SPeter Brune 
2934b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2935b0ae01b7SPeter Brune 
2936b0ae01b7SPeter Brune @*/
2937b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2938b0ae01b7SPeter Brune {
2939b0ae01b7SPeter Brune   PetscFunctionBegin;
2940b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2941b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2942b0ae01b7SPeter Brune }
2943b0ae01b7SPeter Brune 
29443ad4599aSBarry Smith /*@
29453ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
29463ad4599aSBarry Smith 
29473ad4599aSBarry Smith     Not Collective
29483ad4599aSBarry Smith 
29493ad4599aSBarry Smith     Input Parameter:
29503ad4599aSBarry Smith .   dm - the DM object
29513ad4599aSBarry Smith 
29523ad4599aSBarry Smith     Output Parameter:
29533ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
29543ad4599aSBarry Smith 
29553ad4599aSBarry Smith     Level: developer
29563ad4599aSBarry Smith 
29573ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
29583ad4599aSBarry Smith 
29593ad4599aSBarry Smith @*/
29603ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
29613ad4599aSBarry Smith {
29623ad4599aSBarry Smith   PetscFunctionBegin;
29633ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
29643ad4599aSBarry Smith   PetscFunctionReturn(0);
29653ad4599aSBarry Smith }
29663ad4599aSBarry Smith 
2967748fac09SDmitry Karpeev /*@C
296808da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
296908da532bSDmitry Karpeev 
297008da532bSDmitry Karpeev     Collective on DM
297108da532bSDmitry Karpeev 
297208da532bSDmitry Karpeev     Input Parameter:
297308da532bSDmitry Karpeev +   dm - the DM object
29740298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
297508da532bSDmitry Karpeev 
297608da532bSDmitry Karpeev     Level: developer
297708da532bSDmitry Karpeev 
297874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
297908da532bSDmitry Karpeev 
298008da532bSDmitry Karpeev @*/
298108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
298208da532bSDmitry Karpeev {
298308da532bSDmitry Karpeev   PetscErrorCode ierr;
29845fd66863SKarl Rupp 
298508da532bSDmitry Karpeev   PetscFunctionBegin;
298608da532bSDmitry Karpeev   if (x) {
298708da532bSDmitry Karpeev     if (!dm->x) {
298808da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
298908da532bSDmitry Karpeev     }
299008da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
29918865f1eaSKarl Rupp   } else if (dm->x) {
299208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
299308da532bSDmitry Karpeev   }
299408da532bSDmitry Karpeev   PetscFunctionReturn(0);
299508da532bSDmitry Karpeev }
299608da532bSDmitry Karpeev 
29970298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2998264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2999264ace61SBarry Smith 
3000264ace61SBarry Smith /*@C
3001264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3002264ace61SBarry Smith 
3003264ace61SBarry Smith   Collective on DM
3004264ace61SBarry Smith 
3005264ace61SBarry Smith   Input Parameters:
3006264ace61SBarry Smith + dm     - The DM object
3007264ace61SBarry Smith - method - The name of the DM type
3008264ace61SBarry Smith 
3009264ace61SBarry Smith   Options Database Key:
3010264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3011264ace61SBarry Smith 
3012264ace61SBarry Smith   Notes:
3013e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3014264ace61SBarry Smith 
3015264ace61SBarry Smith   Level: intermediate
3016264ace61SBarry Smith 
3017264ace61SBarry Smith .keywords: DM, set, type
3018264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3019264ace61SBarry Smith @*/
302019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3021264ace61SBarry Smith {
3022264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3023264ace61SBarry Smith   PetscBool      match;
3024264ace61SBarry Smith   PetscErrorCode ierr;
3025264ace61SBarry Smith 
3026264ace61SBarry Smith   PetscFunctionBegin;
3027264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3028251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3029264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3030264ace61SBarry Smith 
30310f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
30321c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3033ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3034264ace61SBarry Smith 
3035264ace61SBarry Smith   if (dm->ops->destroy) {
3036264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
30370298fd71SBarry Smith     dm->ops->destroy = NULL;
3038264ace61SBarry Smith   }
3039264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3040264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3041264ace61SBarry Smith   PetscFunctionReturn(0);
3042264ace61SBarry Smith }
3043264ace61SBarry Smith 
3044264ace61SBarry Smith /*@C
3045264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3046264ace61SBarry Smith 
3047264ace61SBarry Smith   Not Collective
3048264ace61SBarry Smith 
3049264ace61SBarry Smith   Input Parameter:
3050264ace61SBarry Smith . dm  - The DM
3051264ace61SBarry Smith 
3052264ace61SBarry Smith   Output Parameter:
3053264ace61SBarry Smith . type - The DM type name
3054264ace61SBarry Smith 
3055264ace61SBarry Smith   Level: intermediate
3056264ace61SBarry Smith 
3057264ace61SBarry Smith .keywords: DM, get, type, name
3058264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3059264ace61SBarry Smith @*/
306019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3061264ace61SBarry Smith {
3062264ace61SBarry Smith   PetscErrorCode ierr;
3063264ace61SBarry Smith 
3064264ace61SBarry Smith   PetscFunctionBegin;
3065264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3066c959eef4SJed Brown   PetscValidPointer(type,2);
3067607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3068264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3069264ace61SBarry Smith   PetscFunctionReturn(0);
3070264ace61SBarry Smith }
3071264ace61SBarry Smith 
307267a56275SMatthew G Knepley /*@C
307367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
307467a56275SMatthew G Knepley 
307567a56275SMatthew G Knepley   Collective on DM
307667a56275SMatthew G Knepley 
307767a56275SMatthew G Knepley   Input Parameters:
307867a56275SMatthew G Knepley + dm - the DM
307967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
308067a56275SMatthew G Knepley 
308167a56275SMatthew G Knepley   Output Parameter:
308267a56275SMatthew G Knepley . M - pointer to new DM
308367a56275SMatthew G Knepley 
308467a56275SMatthew G Knepley   Notes:
308567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
308667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
308767a56275SMatthew G Knepley   of the input DM.
308867a56275SMatthew G Knepley 
308967a56275SMatthew G Knepley   Level: intermediate
309067a56275SMatthew G Knepley 
309167a56275SMatthew G Knepley .seealso: DMCreate()
309267a56275SMatthew G Knepley @*/
309319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
309467a56275SMatthew G Knepley {
309567a56275SMatthew G Knepley   DM             B;
309667a56275SMatthew G Knepley   char           convname[256];
3097c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
309867a56275SMatthew G Knepley   PetscErrorCode ierr;
309967a56275SMatthew G Knepley 
310067a56275SMatthew G Knepley   PetscFunctionBegin;
310167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
310267a56275SMatthew G Knepley   PetscValidType(dm,1);
310367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3104251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3105c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3106c067b6caSMatthew G. Knepley   if (sametype) {
3107c067b6caSMatthew G. Knepley     *M   = dm;
3108c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3109c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3110c067b6caSMatthew G. Knepley   } else {
31110298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
311267a56275SMatthew G Knepley 
311367a56275SMatthew G Knepley     /*
311467a56275SMatthew G Knepley        Order of precedence:
311567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
311667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
311767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
311867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
311967a56275SMatthew G Knepley        5) Use a really basic converter.
312067a56275SMatthew G Knepley     */
312167a56275SMatthew G Knepley 
312267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
312367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
312467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
312567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
312667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
312767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
31280005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
312967a56275SMatthew G Knepley     if (conv) goto foundconv;
313067a56275SMatthew G Knepley 
313167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
313282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
313367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
313467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
313567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
313667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
313767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
313867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
31390005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
314067a56275SMatthew G Knepley     if (conv) {
3141fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
314267a56275SMatthew G Knepley       goto foundconv;
314367a56275SMatthew G Knepley     }
314467a56275SMatthew G Knepley 
314567a56275SMatthew G Knepley #if 0
314667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
314767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3148fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
314967a56275SMatthew G Knepley     if (conv) goto foundconv;
315067a56275SMatthew G Knepley 
315167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
315267a56275SMatthew G Knepley     if (dm->ops->convert) {
315367a56275SMatthew G Knepley       conv = dm->ops->convert;
315467a56275SMatthew G Knepley     }
315567a56275SMatthew G Knepley     if (conv) goto foundconv;
315667a56275SMatthew G Knepley #endif
315767a56275SMatthew G Knepley 
315867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
315982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
316067a56275SMatthew G Knepley 
316167a56275SMatthew G Knepley foundconv:
316267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
316367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
316412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
316590b157c4SStefano Zampini     {
316690b157c4SStefano Zampini       PetscBool             isper;
316712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
316812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
316990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
317090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
317112fa691eSMatthew G. Knepley     }
317267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
317367a56275SMatthew G Knepley   }
317467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
317567a56275SMatthew G Knepley   PetscFunctionReturn(0);
317667a56275SMatthew G Knepley }
3177264ace61SBarry Smith 
3178264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3179264ace61SBarry Smith 
3180264ace61SBarry Smith /*@C
31811c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
31821c84c290SBarry Smith 
31831c84c290SBarry Smith   Not Collective
31841c84c290SBarry Smith 
31851c84c290SBarry Smith   Input Parameters:
31861c84c290SBarry Smith + name        - The name of a new user-defined creation routine
31871c84c290SBarry Smith - create_func - The creation routine itself
31881c84c290SBarry Smith 
31891c84c290SBarry Smith   Notes:
31901c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
31911c84c290SBarry Smith 
31921c84c290SBarry Smith 
31931c84c290SBarry Smith   Sample usage:
31941c84c290SBarry Smith .vb
3195bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
31961c84c290SBarry Smith .ve
31971c84c290SBarry Smith 
31981c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
31991c84c290SBarry Smith .vb
32001c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
32011c84c290SBarry Smith     DMSetType(DM,"my_da");
32021c84c290SBarry Smith .ve
32031c84c290SBarry Smith    or at runtime via the option
32041c84c290SBarry Smith .vb
32051c84c290SBarry Smith     -da_type my_da
32061c84c290SBarry Smith .ve
3207264ace61SBarry Smith 
3208264ace61SBarry Smith   Level: advanced
32091c84c290SBarry Smith 
32101c84c290SBarry Smith .keywords: DM, register
3211bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
32121c84c290SBarry Smith 
3213264ace61SBarry Smith @*/
3214bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3215264ace61SBarry Smith {
3216264ace61SBarry Smith   PetscErrorCode ierr;
3217264ace61SBarry Smith 
3218264ace61SBarry Smith   PetscFunctionBegin;
3219a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3220264ace61SBarry Smith   PetscFunctionReturn(0);
3221264ace61SBarry Smith }
3222264ace61SBarry Smith 
3223b859378eSBarry Smith /*@C
322455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3225b859378eSBarry Smith 
3226b859378eSBarry Smith   Collective on PetscViewer
3227b859378eSBarry Smith 
3228b859378eSBarry Smith   Input Parameters:
3229b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3230b859378eSBarry Smith            some related function before a call to DMLoad().
3231b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3232b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3233b859378eSBarry Smith 
3234b859378eSBarry Smith    Level: intermediate
3235b859378eSBarry Smith 
3236b859378eSBarry Smith   Notes:
323755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3238b859378eSBarry Smith 
3239b859378eSBarry Smith   Notes for advanced users:
3240b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3241b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3242b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3243b859378eSBarry Smith   format is
3244b859378eSBarry Smith .vb
3245b859378eSBarry Smith      has not yet been determined
3246b859378eSBarry Smith .ve
3247b859378eSBarry Smith 
3248b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3249b859378eSBarry Smith @*/
3250b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3251b859378eSBarry Smith {
32529331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3253b859378eSBarry Smith   PetscErrorCode ierr;
3254b859378eSBarry Smith 
3255b859378eSBarry Smith   PetscFunctionBegin;
3256b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3257b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
325832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
32599331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
32609331c7a4SMatthew G. Knepley   if (isbinary) {
32619331c7a4SMatthew G. Knepley     PetscInt classid;
32629331c7a4SMatthew G. Knepley     char     type[256];
3263b859378eSBarry Smith 
3264060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
32659200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3266060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
326732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
32689331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32699331c7a4SMatthew G. Knepley   } else if (ishdf5) {
32709331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32719331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3272b859378eSBarry Smith   PetscFunctionReturn(0);
3273b859378eSBarry Smith }
3274b859378eSBarry Smith 
32757da65231SMatthew G Knepley /******************************** FEM Support **********************************/
32767da65231SMatthew G Knepley 
3277a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3278a6dfd86eSKarl Rupp {
32791d47ebbbSSatish Balay   PetscInt       f;
32801b30c384SMatthew G Knepley   PetscErrorCode ierr;
32811b30c384SMatthew G Knepley 
32827da65231SMatthew G Knepley   PetscFunctionBegin;
328374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32841d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
328557622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
32867da65231SMatthew G Knepley   }
32877da65231SMatthew G Knepley   PetscFunctionReturn(0);
32887da65231SMatthew G Knepley }
32897da65231SMatthew G Knepley 
3290a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3291a6dfd86eSKarl Rupp {
32921b30c384SMatthew G Knepley   PetscInt       f, g;
32937da65231SMatthew G Knepley   PetscErrorCode ierr;
32947da65231SMatthew G Knepley 
32957da65231SMatthew G Knepley   PetscFunctionBegin;
329674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32971d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
329874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
32991d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3300e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
33017da65231SMatthew G Knepley     }
330274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
33037da65231SMatthew G Knepley   }
33047da65231SMatthew G Knepley   PetscFunctionReturn(0);
33057da65231SMatthew G Knepley }
3306e7c4fc90SDmitry Karpeev 
33076113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3308e759306cSMatthew G. Knepley {
33090c5b8624SToby Isaac   PetscInt          localSize, bs;
33100c5b8624SToby Isaac   PetscMPIInt       size;
33110c5b8624SToby Isaac   Vec               x, xglob;
33120c5b8624SToby Isaac   const PetscScalar *xarray;
3313e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3314e759306cSMatthew G. Knepley 
3315e759306cSMatthew G. Knepley   PetscFunctionBegin;
33169852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3317e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3318e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
33196113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
33200c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
33210c5b8624SToby Isaac   if (size > 1) {
33220c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
33230c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
33240c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
33250c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
33260c5b8624SToby Isaac   } else {
33270c5b8624SToby Isaac     xglob = x;
33280c5b8624SToby Isaac   }
33290c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
33300c5b8624SToby Isaac   if (size > 1) {
33310c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
33320c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
33330c5b8624SToby Isaac   }
3334e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3335e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3336e759306cSMatthew G. Knepley }
3337e759306cSMatthew G. Knepley 
333888ed4aceSMatthew G Knepley /*@
333988ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
334088ed4aceSMatthew G Knepley 
334188ed4aceSMatthew G Knepley   Input Parameter:
334288ed4aceSMatthew G Knepley . dm - The DM
334388ed4aceSMatthew G Knepley 
334488ed4aceSMatthew G Knepley   Output Parameter:
334588ed4aceSMatthew G Knepley . section - The PetscSection
334688ed4aceSMatthew G Knepley 
334788ed4aceSMatthew G Knepley   Level: intermediate
334888ed4aceSMatthew G Knepley 
334988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
335088ed4aceSMatthew G Knepley 
335188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
335288ed4aceSMatthew G Knepley @*/
33530adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
33540adebc6cSBarry Smith {
3355fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3356fd59a867SMatthew G. Knepley 
335788ed4aceSMatthew G Knepley   PetscFunctionBegin;
335888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
335988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
33602f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
33612f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3362ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
33632f0f8703SMatthew G. Knepley   }
336488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
336588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
336688ed4aceSMatthew G Knepley }
336788ed4aceSMatthew G Knepley 
336888ed4aceSMatthew G Knepley /*@
336988ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
337088ed4aceSMatthew G Knepley 
337188ed4aceSMatthew G Knepley   Input Parameters:
337288ed4aceSMatthew G Knepley + dm - The DM
337388ed4aceSMatthew G Knepley - section - The PetscSection
337488ed4aceSMatthew G Knepley 
337588ed4aceSMatthew G Knepley   Level: intermediate
337688ed4aceSMatthew G Knepley 
337788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
337888ed4aceSMatthew G Knepley 
337988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
338088ed4aceSMatthew G Knepley @*/
33810adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
33820adebc6cSBarry Smith {
3383c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3384af122d2aSMatthew G Knepley   PetscInt       f;
338588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
338688ed4aceSMatthew G Knepley 
338788ed4aceSMatthew G Knepley   PetscFunctionBegin;
338888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3389c473ab19SMatthew G. Knepley   if (section) {
33901d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33911d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3392c473ab19SMatthew G. Knepley   }
339388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
339488ed4aceSMatthew G Knepley   dm->defaultSection = section;
3395c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3396af122d2aSMatthew G Knepley   if (numFields) {
3397af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3398af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
33990f21e855SMatthew G. Knepley       PetscObject disc;
3400af122d2aSMatthew G Knepley       const char *name;
3401af122d2aSMatthew G Knepley 
3402af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
34030f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
34040f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3405af122d2aSMatthew G Knepley     }
3406af122d2aSMatthew G Knepley   }
34071d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
34081d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
340988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
341088ed4aceSMatthew G Knepley }
341188ed4aceSMatthew G Knepley 
34129435951eSToby Isaac /*@
34139435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
34149435951eSToby Isaac 
3415e228b242SToby Isaac   not collective
3416e228b242SToby Isaac 
34179435951eSToby Isaac   Input Parameter:
34189435951eSToby Isaac . dm - The DM
34199435951eSToby Isaac 
34209435951eSToby Isaac   Output Parameter:
34219435951eSToby 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.
34229435951eSToby 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.
34239435951eSToby Isaac 
34249435951eSToby Isaac   Level: advanced
34259435951eSToby Isaac 
34269435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
34279435951eSToby Isaac 
34289435951eSToby Isaac .seealso: DMSetDefaultConstraints()
34299435951eSToby Isaac @*/
34309435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
34319435951eSToby Isaac {
34329435951eSToby Isaac   PetscErrorCode ierr;
34339435951eSToby Isaac 
34349435951eSToby Isaac   PetscFunctionBegin;
34359435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34369435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
343745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
343845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
34399435951eSToby Isaac   PetscFunctionReturn(0);
34409435951eSToby Isaac }
34419435951eSToby Isaac 
34429435951eSToby Isaac /*@
34439435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
34449435951eSToby Isaac 
34459435951eSToby 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().
34469435951eSToby Isaac 
34479435951eSToby 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.
34489435951eSToby Isaac 
3449e228b242SToby Isaac   collective on dm
3450e228b242SToby Isaac 
34519435951eSToby Isaac   Input Parameters:
34529435951eSToby Isaac + dm - The DM
3453e228b242SToby 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).
3454e228b242SToby 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).
34559435951eSToby Isaac 
34569435951eSToby Isaac   Level: advanced
34579435951eSToby Isaac 
34589435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
34599435951eSToby Isaac 
34609435951eSToby Isaac .seealso: DMGetDefaultConstraints()
34619435951eSToby Isaac @*/
34629435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
34639435951eSToby Isaac {
3464e228b242SToby Isaac   PetscMPIInt result;
34659435951eSToby Isaac   PetscErrorCode ierr;
34669435951eSToby Isaac 
34679435951eSToby Isaac   PetscFunctionBegin;
34689435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3469e228b242SToby Isaac   if (section) {
3470e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3471e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3472f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3473e228b242SToby Isaac   }
3474e228b242SToby Isaac   if (mat) {
3475e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3476e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3477f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3478e228b242SToby Isaac   }
34799435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
34809435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
34819435951eSToby Isaac   dm->defaultConstraintSection = section;
34829435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
34839435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
34849435951eSToby Isaac   dm->defaultConstraintMat = mat;
34859435951eSToby Isaac   PetscFunctionReturn(0);
34869435951eSToby Isaac }
34879435951eSToby Isaac 
3488f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3489507e4973SMatthew G. Knepley /*
3490507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3491507e4973SMatthew G. Knepley 
3492507e4973SMatthew G. Knepley   Input Parameters:
3493507e4973SMatthew G. Knepley + dm - The DM
3494507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3495507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3496507e4973SMatthew G. Knepley 
3497507e4973SMatthew G. Knepley   Level: intermediate
3498507e4973SMatthew G. Knepley 
3499507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3500507e4973SMatthew G. Knepley */
3501f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3502507e4973SMatthew G. Knepley {
3503507e4973SMatthew G. Knepley   MPI_Comm        comm;
3504507e4973SMatthew G. Knepley   PetscLayout     layout;
3505507e4973SMatthew G. Knepley   const PetscInt *ranges;
3506507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3507507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3508507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3509507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3510507e4973SMatthew G. Knepley 
3511507e4973SMatthew G. Knepley   PetscFunctionBegin;
3512507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3513507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3514507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3515507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3516507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3517507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3518507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3519507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3520507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3521507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3522507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3523507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3524f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3525507e4973SMatthew G. Knepley 
3526507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3527507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3528507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3529507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3530507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3531507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3532507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3533507e4973SMatthew 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;}
3534507e4973SMatthew 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;}
3535507e4973SMatthew G. Knepley     if (gdof < 0) {
3536507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3537507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3538507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3539507e4973SMatthew G. Knepley 
3540507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3541507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3542507e4973SMatthew 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;}
3543507e4973SMatthew G. Knepley       }
3544507e4973SMatthew G. Knepley     }
3545507e4973SMatthew G. Knepley   }
3546507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3547507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3548b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3549507e4973SMatthew G. Knepley   if (!gvalid) {
3550507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3551507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3552507e4973SMatthew G. Knepley   }
3553507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3554507e4973SMatthew G. Knepley }
3555f741bcd2SMatthew G. Knepley #endif
3556507e4973SMatthew G. Knepley 
355788ed4aceSMatthew G Knepley /*@
355888ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
355988ed4aceSMatthew G Knepley 
35608b1ab98fSJed Brown   Collective on DM
35618b1ab98fSJed Brown 
356288ed4aceSMatthew G Knepley   Input Parameter:
356388ed4aceSMatthew G Knepley . dm - The DM
356488ed4aceSMatthew G Knepley 
356588ed4aceSMatthew G Knepley   Output Parameter:
356688ed4aceSMatthew G Knepley . section - The PetscSection
356788ed4aceSMatthew G Knepley 
356888ed4aceSMatthew G Knepley   Level: intermediate
356988ed4aceSMatthew G Knepley 
357088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
357188ed4aceSMatthew G Knepley 
357288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
357388ed4aceSMatthew G Knepley @*/
35740adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
35750adebc6cSBarry Smith {
357688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
357788ed4aceSMatthew G Knepley 
357888ed4aceSMatthew G Knepley   PetscFunctionBegin;
357988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
358088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
358188ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3582fd59a867SMatthew G. Knepley     PetscSection s;
3583fd59a867SMatthew G. Knepley 
3584fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3585fd59a867SMatthew 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");
3586fd59a867SMatthew 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");
358715b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3588cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3589ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3590685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
359188ed4aceSMatthew G Knepley   }
359288ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
359388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
359488ed4aceSMatthew G Knepley }
359588ed4aceSMatthew G Knepley 
3596b21d0597SMatthew G Knepley /*@
3597b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3598b21d0597SMatthew G Knepley 
3599b21d0597SMatthew G Knepley   Input Parameters:
3600b21d0597SMatthew G Knepley + dm - The DM
36015080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3602b21d0597SMatthew G Knepley 
3603b21d0597SMatthew G Knepley   Level: intermediate
3604b21d0597SMatthew G Knepley 
3605b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3606b21d0597SMatthew G Knepley 
3607b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3608b21d0597SMatthew G Knepley @*/
36090adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
36100adebc6cSBarry Smith {
3611b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3612b21d0597SMatthew G Knepley 
3613b21d0597SMatthew G Knepley   PetscFunctionBegin;
3614b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36155080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36161d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3617b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3618b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3619507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3620f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3621507e4973SMatthew G. Knepley #endif
3622b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3623b21d0597SMatthew G Knepley }
3624b21d0597SMatthew G Knepley 
362588ed4aceSMatthew G Knepley /*@
362688ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
362788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
362888ed4aceSMatthew G Knepley 
362988ed4aceSMatthew G Knepley   Input Parameter:
363088ed4aceSMatthew G Knepley . dm - The DM
363188ed4aceSMatthew G Knepley 
363288ed4aceSMatthew G Knepley   Output Parameter:
363388ed4aceSMatthew G Knepley . sf - The PetscSF
363488ed4aceSMatthew G Knepley 
363588ed4aceSMatthew G Knepley   Level: intermediate
363688ed4aceSMatthew G Knepley 
363788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
363888ed4aceSMatthew G Knepley 
363988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
364088ed4aceSMatthew G Knepley @*/
36410adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
36420adebc6cSBarry Smith {
364388ed4aceSMatthew G Knepley   PetscInt       nroots;
364488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
364588ed4aceSMatthew G Knepley 
364688ed4aceSMatthew G Knepley   PetscFunctionBegin;
364788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
364888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
36490298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
365088ed4aceSMatthew G Knepley   if (nroots < 0) {
365188ed4aceSMatthew G Knepley     PetscSection section, gSection;
365288ed4aceSMatthew G Knepley 
365388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
365431ea6d37SMatthew G Knepley     if (section) {
365588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
365688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
365731ea6d37SMatthew G Knepley     } else {
36580298fd71SBarry Smith       *sf = NULL;
365931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
366031ea6d37SMatthew G Knepley     }
366188ed4aceSMatthew G Knepley   }
366288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
366388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
366488ed4aceSMatthew G Knepley }
366588ed4aceSMatthew G Knepley 
366688ed4aceSMatthew G Knepley /*@
366788ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
366888ed4aceSMatthew G Knepley 
366988ed4aceSMatthew G Knepley   Input Parameters:
367088ed4aceSMatthew G Knepley + dm - The DM
367188ed4aceSMatthew G Knepley - sf - The PetscSF
367288ed4aceSMatthew G Knepley 
367388ed4aceSMatthew G Knepley   Level: intermediate
367488ed4aceSMatthew G Knepley 
367588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
367688ed4aceSMatthew G Knepley 
367788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
367888ed4aceSMatthew G Knepley @*/
36790adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
36800adebc6cSBarry Smith {
368188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
368288ed4aceSMatthew G Knepley 
368388ed4aceSMatthew G Knepley   PetscFunctionBegin;
368488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
368588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
368688ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
368788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
368888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
368988ed4aceSMatthew G Knepley }
369088ed4aceSMatthew G Knepley 
369188ed4aceSMatthew G Knepley /*@C
369288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
369388ed4aceSMatthew G Knepley   describing the data layout.
369488ed4aceSMatthew G Knepley 
369588ed4aceSMatthew G Knepley   Input Parameters:
369688ed4aceSMatthew G Knepley + dm - The DM
369788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
369888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
369988ed4aceSMatthew G Knepley 
370088ed4aceSMatthew G Knepley   Level: intermediate
370188ed4aceSMatthew G Knepley 
370288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
370388ed4aceSMatthew G Knepley @*/
370488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
370588ed4aceSMatthew G Knepley {
370682f516ccSBarry Smith   MPI_Comm       comm;
370788ed4aceSMatthew G Knepley   PetscLayout    layout;
370888ed4aceSMatthew G Knepley   const PetscInt *ranges;
370988ed4aceSMatthew G Knepley   PetscInt       *local;
371088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3711ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
371288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
371388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
371488ed4aceSMatthew G Knepley 
371588ed4aceSMatthew G Knepley   PetscFunctionBegin;
371682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
371788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
371888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
371988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
372088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
372188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
372288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
372388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
372488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
372588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
372688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3727ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
37286636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
372988ed4aceSMatthew G Knepley 
37306636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37316636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3732235fbf56SMatthew 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));
37336636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
373488ed4aceSMatthew G Knepley   }
3735785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3736785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
373788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
37381f588964SMatthew G Knepley     const PetscInt *cind;
37396636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
374088ed4aceSMatthew G Knepley 
374188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
374288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
374388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
374488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
374588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37466636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
374788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
37486636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
37496636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
37506636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3751057b4bcdSMatthew 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);
37526636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
37536636e97aSMatthew G Knepley     }
375488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
375588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
375688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
375788ed4aceSMatthew G Knepley     }
375888ed4aceSMatthew G Knepley     if (gdof < 0) {
37596636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
376088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
376188ed4aceSMatthew G Knepley 
376205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
376331d3f06eSJed Brown         if (r < 0) r = -(r+2);
376405376888SMatthew 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);
376588ed4aceSMatthew G Knepley         remote[l].rank  = r;
376688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
376788ed4aceSMatthew G Knepley       }
376888ed4aceSMatthew G Knepley     } else {
37696636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
377088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
377188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
377288ed4aceSMatthew G Knepley       }
377388ed4aceSMatthew G Knepley     }
377488ed4aceSMatthew G Knepley   }
37756636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
377688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
377788ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
377888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
377988ed4aceSMatthew G Knepley }
3780af122d2aSMatthew G Knepley 
3781b21d0597SMatthew G Knepley /*@
3782b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3783b21d0597SMatthew G Knepley 
3784b21d0597SMatthew G Knepley   Input Parameter:
3785b21d0597SMatthew G Knepley . dm - The DM
3786b21d0597SMatthew G Knepley 
3787b21d0597SMatthew G Knepley   Output Parameter:
3788b21d0597SMatthew G Knepley . sf - The PetscSF
3789b21d0597SMatthew G Knepley 
3790b21d0597SMatthew G Knepley   Level: intermediate
3791b21d0597SMatthew G Knepley 
3792b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3793b21d0597SMatthew G Knepley 
3794057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3795b21d0597SMatthew G Knepley @*/
37960adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
37970adebc6cSBarry Smith {
3798b21d0597SMatthew G Knepley   PetscFunctionBegin;
3799b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3800b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3801b21d0597SMatthew G Knepley   *sf = dm->sf;
3802b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3803b21d0597SMatthew G Knepley }
3804b21d0597SMatthew G Knepley 
3805057b4bcdSMatthew G Knepley /*@
3806057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3807057b4bcdSMatthew G Knepley 
3808057b4bcdSMatthew G Knepley   Input Parameters:
3809057b4bcdSMatthew G Knepley + dm - The DM
3810057b4bcdSMatthew G Knepley - sf - The PetscSF
3811057b4bcdSMatthew G Knepley 
3812057b4bcdSMatthew G Knepley   Level: intermediate
3813057b4bcdSMatthew G Knepley 
3814057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3815057b4bcdSMatthew G Knepley @*/
38160adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
38170adebc6cSBarry Smith {
3818057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3819057b4bcdSMatthew G Knepley 
3820057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3821057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3822057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3823057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3824057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3825057b4bcdSMatthew G Knepley   dm->sf = sf;
3826057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3827057b4bcdSMatthew G Knepley }
3828057b4bcdSMatthew G Knepley 
38292764a2aaSMatthew G. Knepley /*@
38302764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
38312764a2aaSMatthew G. Knepley 
38322764a2aaSMatthew G. Knepley   Input Parameter:
38332764a2aaSMatthew G. Knepley . dm - The DM
38342764a2aaSMatthew G. Knepley 
38352764a2aaSMatthew G. Knepley   Output Parameter:
38362764a2aaSMatthew G. Knepley . prob - The PetscDS
38372764a2aaSMatthew G. Knepley 
38382764a2aaSMatthew G. Knepley   Level: developer
38392764a2aaSMatthew G. Knepley 
38402764a2aaSMatthew G. Knepley .seealso: DMSetDS()
38412764a2aaSMatthew G. Knepley @*/
38422764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3843af122d2aSMatthew G Knepley {
3844af122d2aSMatthew G Knepley   PetscFunctionBegin;
3845af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38460f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
38470f21e855SMatthew G. Knepley   *prob = dm->prob;
38480f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38490f21e855SMatthew G. Knepley }
38500f21e855SMatthew G. Knepley 
38512764a2aaSMatthew G. Knepley /*@
38522764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
38532764a2aaSMatthew G. Knepley 
38542764a2aaSMatthew G. Knepley   Input Parameters:
38552764a2aaSMatthew G. Knepley + dm - The DM
38562764a2aaSMatthew G. Knepley - prob - The PetscDS
38572764a2aaSMatthew G. Knepley 
38582764a2aaSMatthew G. Knepley   Level: developer
38592764a2aaSMatthew G. Knepley 
38602764a2aaSMatthew G. Knepley .seealso: DMGetDS()
38612764a2aaSMatthew G. Knepley @*/
38622764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
38630f21e855SMatthew G. Knepley {
38640f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38650f21e855SMatthew G. Knepley 
38660f21e855SMatthew G. Knepley   PetscFunctionBegin;
38670f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38682764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
386937316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
38702764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
38710f21e855SMatthew G. Knepley   dm->prob = prob;
38720f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38730f21e855SMatthew G. Knepley }
38740f21e855SMatthew G. Knepley 
38750f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
38760f21e855SMatthew G. Knepley {
38770f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38780f21e855SMatthew G. Knepley 
38790f21e855SMatthew G. Knepley   PetscFunctionBegin;
38800f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38812764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3882af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3883af122d2aSMatthew G Knepley }
3884af122d2aSMatthew G Knepley 
3885af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3886af122d2aSMatthew G Knepley {
38870f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3888af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3889af122d2aSMatthew G Knepley 
3890af122d2aSMatthew G Knepley   PetscFunctionBegin;
3891af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38922764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
38930f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
38940f21e855SMatthew G. Knepley     PetscContainer obj;
38950f21e855SMatthew G. Knepley 
38960f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
38972764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
38980f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3899af122d2aSMatthew G Knepley   }
3900af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3901af122d2aSMatthew G Knepley }
3902af122d2aSMatthew G Knepley 
3903c1929be8SMatthew G. Knepley /*@
3904c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3905c1929be8SMatthew G. Knepley 
3906c1929be8SMatthew G. Knepley   Not collective
3907c1929be8SMatthew G. Knepley 
3908c1929be8SMatthew G. Knepley   Input Parameters:
3909c1929be8SMatthew G. Knepley + dm - The DM
3910c1929be8SMatthew G. Knepley - f  - The field number
3911c1929be8SMatthew G. Knepley 
3912c1929be8SMatthew G. Knepley   Output Parameter:
3913c1929be8SMatthew G. Knepley . field - The discretization object
3914c1929be8SMatthew G. Knepley 
3915c1929be8SMatthew G. Knepley   Level: developer
3916c1929be8SMatthew G. Knepley 
3917c1929be8SMatthew G. Knepley .seealso: DMSetField()
3918c1929be8SMatthew G. Knepley @*/
3919af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3920af122d2aSMatthew G Knepley {
39210f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39220f21e855SMatthew G. Knepley 
3923af122d2aSMatthew G Knepley   PetscFunctionBegin;
3924af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39252764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3926decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3927decb47aaSMatthew G. Knepley }
3928decb47aaSMatthew G. Knepley 
3929c1929be8SMatthew G. Knepley /*@
3930c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3931c1929be8SMatthew G. Knepley 
3932c1929be8SMatthew G. Knepley   Logically collective on DM
3933c1929be8SMatthew G. Knepley 
3934c1929be8SMatthew G. Knepley   Input Parameters:
3935c1929be8SMatthew G. Knepley + dm - The DM
3936c1929be8SMatthew G. Knepley . f  - The field number
3937c1929be8SMatthew G. Knepley - field - The discretization object
3938c1929be8SMatthew G. Knepley 
3939c1929be8SMatthew G. Knepley   Level: developer
3940c1929be8SMatthew G. Knepley 
3941c1929be8SMatthew G. Knepley .seealso: DMGetField()
3942c1929be8SMatthew G. Knepley @*/
3943decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3944decb47aaSMatthew G. Knepley {
3945decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3946decb47aaSMatthew G. Knepley 
3947decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3948decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39492764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3950af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3951af122d2aSMatthew G Knepley }
39526636e97aSMatthew G Knepley 
3953b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3954b64e0483SPeter Brune {
3955b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3956b64e0483SPeter Brune   PetscErrorCode ierr;
3957b64e0483SPeter Brune   Vec coords,ccoords;
39586dbf9973SLawrence Mitchell   Mat inject;
3959b64e0483SPeter Brune   PetscFunctionBegin;
3960b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3961b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3962b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3963b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3964b64e0483SPeter Brune   if (coords && !ccoords) {
3965b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
39666668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
39676dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
39682adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
39696dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3970b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3971b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3972b64e0483SPeter Brune   }
3973b64e0483SPeter Brune   PetscFunctionReturn(0);
3974b64e0483SPeter Brune }
3975b64e0483SPeter Brune 
397603dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
397703dadc2fSPeter Brune {
397803dadc2fSPeter Brune   DM dm_coord,subdm_coord;
397903dadc2fSPeter Brune   PetscErrorCode ierr;
398003dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
398103dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
398203dadc2fSPeter Brune   PetscFunctionBegin;
398303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
398403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
398503dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
398603dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
398703dadc2fSPeter Brune   if (coords && !ccoords) {
398803dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
39896668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
399003dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
399124640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
399203dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
399303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
399403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
399503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
399603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
399703dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
399803dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
399903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
400003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
400103dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
400203dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
400303dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
400403dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
400503dadc2fSPeter Brune   }
400603dadc2fSPeter Brune   PetscFunctionReturn(0);
400703dadc2fSPeter Brune }
400803dadc2fSPeter Brune 
4009c73cfb54SMatthew G. Knepley /*@
4010c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4011c73cfb54SMatthew G. Knepley 
4012c73cfb54SMatthew G. Knepley   Not collective
4013c73cfb54SMatthew G. Knepley 
4014c73cfb54SMatthew G. Knepley   Input Parameter:
4015c73cfb54SMatthew G. Knepley . dm - The DM
4016c73cfb54SMatthew G. Knepley 
4017c73cfb54SMatthew G. Knepley   Output Parameter:
4018c73cfb54SMatthew G. Knepley . dim - The topological dimension
4019c73cfb54SMatthew G. Knepley 
4020c73cfb54SMatthew G. Knepley   Level: beginner
4021c73cfb54SMatthew G. Knepley 
4022c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4023c73cfb54SMatthew G. Knepley @*/
4024c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4025c73cfb54SMatthew G. Knepley {
4026c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4027c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4028c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4029c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4030c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4031c73cfb54SMatthew G. Knepley }
4032c73cfb54SMatthew G. Knepley 
4033c73cfb54SMatthew G. Knepley /*@
4034c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4035c73cfb54SMatthew G. Knepley 
4036c73cfb54SMatthew G. Knepley   Collective on dm
4037c73cfb54SMatthew G. Knepley 
4038c73cfb54SMatthew G. Knepley   Input Parameters:
4039c73cfb54SMatthew G. Knepley + dm - The DM
4040c73cfb54SMatthew G. Knepley - dim - The topological dimension
4041c73cfb54SMatthew G. Knepley 
4042c73cfb54SMatthew G. Knepley   Level: beginner
4043c73cfb54SMatthew G. Knepley 
4044c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4045c73cfb54SMatthew G. Knepley @*/
4046c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4047c73cfb54SMatthew G. Knepley {
4048c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4049c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4050c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4051c73cfb54SMatthew G. Knepley   dm->dim = dim;
4052c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4053c73cfb54SMatthew G. Knepley }
4054c73cfb54SMatthew G. Knepley 
4055793f3fe5SMatthew G. Knepley /*@
4056793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4057793f3fe5SMatthew G. Knepley 
4058793f3fe5SMatthew G. Knepley   Collective on DM
4059793f3fe5SMatthew G. Knepley 
4060793f3fe5SMatthew G. Knepley   Input Parameters:
4061793f3fe5SMatthew G. Knepley + dm - the DM
4062793f3fe5SMatthew G. Knepley - dim - the dimension
4063793f3fe5SMatthew G. Knepley 
4064793f3fe5SMatthew G. Knepley   Output Parameters:
4065793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4066793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4067793f3fe5SMatthew G. Knepley 
4068793f3fe5SMatthew G. Knepley   Note:
4069793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4070793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4071793f3fe5SMatthew G. Knepley   then the interval is empty.
4072793f3fe5SMatthew G. Knepley 
4073793f3fe5SMatthew G. Knepley   Level: intermediate
4074793f3fe5SMatthew G. Knepley 
4075793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4076793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4077793f3fe5SMatthew G. Knepley @*/
4078793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4079793f3fe5SMatthew G. Knepley {
4080793f3fe5SMatthew G. Knepley   PetscInt       d;
4081793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4082793f3fe5SMatthew G. Knepley 
4083793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4084793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4085793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4086793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4087793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4088793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4089793f3fe5SMatthew G. Knepley }
4090793f3fe5SMatthew G. Knepley 
40916636e97aSMatthew G Knepley /*@
40926636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
40936636e97aSMatthew G Knepley 
40946636e97aSMatthew G Knepley   Collective on DM
40956636e97aSMatthew G Knepley 
40966636e97aSMatthew G Knepley   Input Parameters:
40976636e97aSMatthew G Knepley + dm - the DM
40986636e97aSMatthew G Knepley - c - coordinate vector
40996636e97aSMatthew G Knepley 
41006636e97aSMatthew G Knepley   Note:
41016636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
41026636e97aSMatthew G Knepley 
41036636e97aSMatthew G Knepley   Level: intermediate
41046636e97aSMatthew G Knepley 
41056636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41066636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
41076636e97aSMatthew G Knepley @*/
41086636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
41096636e97aSMatthew G Knepley {
41106636e97aSMatthew G Knepley   PetscErrorCode ierr;
41116636e97aSMatthew G Knepley 
41126636e97aSMatthew G Knepley   PetscFunctionBegin;
41136636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41146636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41156636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41166636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41176636e97aSMatthew G Knepley   dm->coordinates = c;
41186636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4119b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
412003dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
41216636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41226636e97aSMatthew G Knepley }
41236636e97aSMatthew G Knepley 
41246636e97aSMatthew G Knepley /*@
41256636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
41266636e97aSMatthew G Knepley 
41276636e97aSMatthew G Knepley   Collective on DM
41286636e97aSMatthew G Knepley 
41296636e97aSMatthew G Knepley    Input Parameters:
41306636e97aSMatthew G Knepley +  dm - the DM
41316636e97aSMatthew G Knepley -  c - coordinate vector
41326636e97aSMatthew G Knepley 
41336636e97aSMatthew G Knepley   Note:
41346636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
41356636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
41366636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
41376636e97aSMatthew G Knepley 
41386636e97aSMatthew G Knepley   Level: intermediate
41396636e97aSMatthew G Knepley 
41406636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41416636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
41426636e97aSMatthew G Knepley @*/
41436636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
41446636e97aSMatthew G Knepley {
41456636e97aSMatthew G Knepley   PetscErrorCode ierr;
41466636e97aSMatthew G Knepley 
41476636e97aSMatthew G Knepley   PetscFunctionBegin;
41486636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41496636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41506636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41516636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
41528865f1eaSKarl Rupp 
41536636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
41548865f1eaSKarl Rupp 
41556636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41566636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41576636e97aSMatthew G Knepley }
41586636e97aSMatthew G Knepley 
41596636e97aSMatthew G Knepley /*@
41606636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
41616636e97aSMatthew G Knepley 
41626636e97aSMatthew G Knepley   Not Collective
41636636e97aSMatthew G Knepley 
41646636e97aSMatthew G Knepley   Input Parameter:
41656636e97aSMatthew G Knepley . dm - the DM
41666636e97aSMatthew G Knepley 
41676636e97aSMatthew G Knepley   Output Parameter:
41686636e97aSMatthew G Knepley . c - global coordinate vector
41696636e97aSMatthew G Knepley 
41706636e97aSMatthew G Knepley   Note:
41716636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
41726636e97aSMatthew G Knepley 
41736636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
41746636e97aSMatthew G Knepley 
41756636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
41766636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
41776636e97aSMatthew G Knepley 
41786636e97aSMatthew G Knepley   Level: intermediate
41796636e97aSMatthew G Knepley 
41806636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41816636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
41826636e97aSMatthew G Knepley @*/
41836636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
41846636e97aSMatthew G Knepley {
41856636e97aSMatthew G Knepley   PetscErrorCode ierr;
41866636e97aSMatthew G Knepley 
41876636e97aSMatthew G Knepley   PetscFunctionBegin;
41886636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41896636e97aSMatthew G Knepley   PetscValidPointer(c,2);
41901f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
41910298fd71SBarry Smith     DM cdm = NULL;
41926636e97aSMatthew G Knepley 
41936636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
41946636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
41956636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
41966636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
41976636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
41986636e97aSMatthew G Knepley   }
41996636e97aSMatthew G Knepley   *c = dm->coordinates;
42006636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42016636e97aSMatthew G Knepley }
42026636e97aSMatthew G Knepley 
42036636e97aSMatthew G Knepley /*@
42046636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
42056636e97aSMatthew G Knepley 
42066636e97aSMatthew G Knepley   Collective on DM
42076636e97aSMatthew G Knepley 
42086636e97aSMatthew G Knepley   Input Parameter:
42096636e97aSMatthew G Knepley . dm - the DM
42106636e97aSMatthew G Knepley 
42116636e97aSMatthew G Knepley   Output Parameter:
42126636e97aSMatthew G Knepley . c - coordinate vector
42136636e97aSMatthew G Knepley 
42146636e97aSMatthew G Knepley   Note:
42156636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42166636e97aSMatthew G Knepley 
42176636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
42186636e97aSMatthew G Knepley 
42196636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42206636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42216636e97aSMatthew G Knepley 
42226636e97aSMatthew G Knepley   Level: intermediate
42236636e97aSMatthew G Knepley 
42246636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42256636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
42266636e97aSMatthew G Knepley @*/
42276636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
42286636e97aSMatthew G Knepley {
42296636e97aSMatthew G Knepley   PetscErrorCode ierr;
42306636e97aSMatthew G Knepley 
42316636e97aSMatthew G Knepley   PetscFunctionBegin;
42326636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42336636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42341f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
42350298fd71SBarry Smith     DM cdm = NULL;
42366636e97aSMatthew G Knepley 
42376636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42386636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
42396636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
42406636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42416636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42426636e97aSMatthew G Knepley   }
42436636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
42446636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42456636e97aSMatthew G Knepley }
42466636e97aSMatthew G Knepley 
42476636e97aSMatthew G Knepley /*@
42481cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
42496636e97aSMatthew G Knepley 
42506636e97aSMatthew G Knepley   Collective on DM
42516636e97aSMatthew G Knepley 
42526636e97aSMatthew G Knepley   Input Parameter:
42536636e97aSMatthew G Knepley . dm - the DM
42546636e97aSMatthew G Knepley 
42556636e97aSMatthew G Knepley   Output Parameter:
42566636e97aSMatthew G Knepley . cdm - coordinate DM
42576636e97aSMatthew G Knepley 
42586636e97aSMatthew G Knepley   Level: intermediate
42596636e97aSMatthew G Knepley 
42606636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42611cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
42626636e97aSMatthew G Knepley @*/
42636636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
42646636e97aSMatthew G Knepley {
42656636e97aSMatthew G Knepley   PetscErrorCode ierr;
42666636e97aSMatthew G Knepley 
42676636e97aSMatthew G Knepley   PetscFunctionBegin;
42686636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42696636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
42706636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
427182f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
42726636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
42736636e97aSMatthew G Knepley   }
42746636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
42756636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42766636e97aSMatthew G Knepley }
4277e87bb0d3SMatthew G Knepley 
42781cfe2091SMatthew G. Knepley /*@
42791cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
42801cfe2091SMatthew G. Knepley 
42811cfe2091SMatthew G. Knepley   Logically Collective on DM
42821cfe2091SMatthew G. Knepley 
42831cfe2091SMatthew G. Knepley   Input Parameters:
42841cfe2091SMatthew G. Knepley + dm - the DM
42851cfe2091SMatthew G. Knepley - cdm - coordinate DM
42861cfe2091SMatthew G. Knepley 
42871cfe2091SMatthew G. Knepley   Level: intermediate
42881cfe2091SMatthew G. Knepley 
42891cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42901cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
42911cfe2091SMatthew G. Knepley @*/
42921cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
42931cfe2091SMatthew G. Knepley {
42941cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
42951cfe2091SMatthew G. Knepley 
42961cfe2091SMatthew G. Knepley   PetscFunctionBegin;
42971cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42981cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4299f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
43001cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
43011cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
43021cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
43031cfe2091SMatthew G. Knepley }
43041cfe2091SMatthew G. Knepley 
430546e270d4SMatthew G. Knepley /*@
430646e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
430746e270d4SMatthew G. Knepley 
430846e270d4SMatthew G. Knepley   Not Collective
430946e270d4SMatthew G. Knepley 
431046e270d4SMatthew G. Knepley   Input Parameter:
431146e270d4SMatthew G. Knepley . dm - The DM object
431246e270d4SMatthew G. Knepley 
431346e270d4SMatthew G. Knepley   Output Parameter:
431446e270d4SMatthew G. Knepley . dim - The embedding dimension
431546e270d4SMatthew G. Knepley 
431646e270d4SMatthew G. Knepley   Level: intermediate
431746e270d4SMatthew G. Knepley 
431846e270d4SMatthew G. Knepley .keywords: mesh, coordinates
431946e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
432046e270d4SMatthew G. Knepley @*/
432146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
432246e270d4SMatthew G. Knepley {
432346e270d4SMatthew G. Knepley   PetscFunctionBegin;
432446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
432546e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
43269a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
43279a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
43289a9a41abSToby Isaac   }
432946e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
433046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
433146e270d4SMatthew G. Knepley }
433246e270d4SMatthew G. Knepley 
433346e270d4SMatthew G. Knepley /*@
433446e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
433546e270d4SMatthew G. Knepley 
433646e270d4SMatthew G. Knepley   Not Collective
433746e270d4SMatthew G. Knepley 
433846e270d4SMatthew G. Knepley   Input Parameters:
433946e270d4SMatthew G. Knepley + dm  - The DM object
434046e270d4SMatthew G. Knepley - dim - The embedding dimension
434146e270d4SMatthew G. Knepley 
434246e270d4SMatthew G. Knepley   Level: intermediate
434346e270d4SMatthew G. Knepley 
434446e270d4SMatthew G. Knepley .keywords: mesh, coordinates
434546e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
434646e270d4SMatthew G. Knepley @*/
434746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
434846e270d4SMatthew G. Knepley {
434946e270d4SMatthew G. Knepley   PetscFunctionBegin;
435046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
435146e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
435246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
435346e270d4SMatthew G. Knepley }
435446e270d4SMatthew G. Knepley 
4355e8abe2deSMatthew G. Knepley /*@
4356e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4357e8abe2deSMatthew G. Knepley 
4358e8abe2deSMatthew G. Knepley   Not Collective
4359e8abe2deSMatthew G. Knepley 
4360e8abe2deSMatthew G. Knepley   Input Parameter:
4361e8abe2deSMatthew G. Knepley . dm - The DM object
4362e8abe2deSMatthew G. Knepley 
4363e8abe2deSMatthew G. Knepley   Output Parameter:
4364e8abe2deSMatthew G. Knepley . section - The PetscSection object
4365e8abe2deSMatthew G. Knepley 
4366e8abe2deSMatthew G. Knepley   Level: intermediate
4367e8abe2deSMatthew G. Knepley 
4368e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4369e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4370e8abe2deSMatthew G. Knepley @*/
4371e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4372e8abe2deSMatthew G. Knepley {
4373e8abe2deSMatthew G. Knepley   DM             cdm;
4374e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4375e8abe2deSMatthew G. Knepley 
4376e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4377e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4378e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4379e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4380e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4381e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4382e8abe2deSMatthew G. Knepley }
4383e8abe2deSMatthew G. Knepley 
4384e8abe2deSMatthew G. Knepley /*@
4385e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4386e8abe2deSMatthew G. Knepley 
4387e8abe2deSMatthew G. Knepley   Not Collective
4388e8abe2deSMatthew G. Knepley 
4389e8abe2deSMatthew G. Knepley   Input Parameters:
4390e8abe2deSMatthew G. Knepley + dm      - The DM object
439146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4392e8abe2deSMatthew G. Knepley - section - The PetscSection object
4393e8abe2deSMatthew G. Knepley 
4394e8abe2deSMatthew G. Knepley   Level: intermediate
4395e8abe2deSMatthew G. Knepley 
4396e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4397e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4398e8abe2deSMatthew G. Knepley @*/
439946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4400e8abe2deSMatthew G. Knepley {
4401e8abe2deSMatthew G. Knepley   DM             cdm;
4402e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4403e8abe2deSMatthew G. Knepley 
4404e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4405e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
440646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4407e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4408e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
440946e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
44104c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
441146e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
441246e270d4SMatthew G. Knepley 
441346e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
441446e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
441546e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
441646e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
441746e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
441846e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
441946e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
442046e270d4SMatthew G. Knepley     }
44216be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
442246e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
442346e270d4SMatthew G. Knepley   }
4424e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4425e8abe2deSMatthew G. Knepley }
4426e8abe2deSMatthew G. Knepley 
44275dc8c3f7SMatthew G. Knepley /*@C
442890b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
44295dc8c3f7SMatthew G. Knepley 
44305dc8c3f7SMatthew G. Knepley   Input Parameters:
443190b157c4SStefano Zampini . dm      - The DM object
443290b157c4SStefano Zampini 
443390b157c4SStefano Zampini   Output Parameters:
443490b157c4SStefano Zampini + per     - Whether the DM is periodic or not
44355dc8c3f7SMatthew 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
44365dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44375dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44385dc8c3f7SMatthew G. Knepley 
44395dc8c3f7SMatthew G. Knepley   Level: developer
44405dc8c3f7SMatthew G. Knepley 
44415dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44425dc8c3f7SMatthew G. Knepley @*/
444390b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4444c6b900c6SMatthew G. Knepley {
4445c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4446c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
444790b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4448c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4449c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
44505dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4451c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4452c6b900c6SMatthew G. Knepley }
4453c6b900c6SMatthew G. Knepley 
44545dc8c3f7SMatthew G. Knepley /*@C
44555dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
44565dc8c3f7SMatthew G. Knepley 
44575dc8c3f7SMatthew G. Knepley   Input Parameters:
44585dc8c3f7SMatthew G. Knepley + dm      - The DM object
445990b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
44605dc8c3f7SMatthew 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
44615dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44625dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44635dc8c3f7SMatthew G. Knepley 
44645dc8c3f7SMatthew G. Knepley   Level: developer
44655dc8c3f7SMatthew G. Knepley 
44665dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44675dc8c3f7SMatthew G. Knepley @*/
446890b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4469c6b900c6SMatthew G. Knepley {
4470c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4471c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4472c6b900c6SMatthew G. Knepley 
4473c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4474c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
447590b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
447690b157c4SStefano Zampini   if (maxCell) {
447790b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
447890b157c4SStefano Zampini     PetscValidPointer(L,4);
447990b157c4SStefano Zampini     PetscValidPointer(bd,5);
448090b157c4SStefano Zampini   }
44815dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
44825dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
448390b157c4SStefano Zampini   if (maxCell) {
44845dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
44855dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
448690b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
448790b157c4SStefano Zampini   } else {
448890b157c4SStefano Zampini     dm->periodic = per;
448990b157c4SStefano Zampini   }
4490c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4491c6b900c6SMatthew G. Knepley }
4492c6b900c6SMatthew G. Knepley 
44932e17dfb7SMatthew G. Knepley /*@
44942e17dfb7SMatthew 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.
44952e17dfb7SMatthew G. Knepley 
44962e17dfb7SMatthew G. Knepley   Input Parameters:
44972e17dfb7SMatthew G. Knepley + dm     - The DM
449865da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
449965da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
45002e17dfb7SMatthew G. Knepley 
45012e17dfb7SMatthew G. Knepley   Output Parameter:
45022e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45032e17dfb7SMatthew G. Knepley 
45042e17dfb7SMatthew G. Knepley   Level: developer
45052e17dfb7SMatthew G. Knepley 
45062e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45072e17dfb7SMatthew G. Knepley @*/
450865da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
45092e17dfb7SMatthew G. Knepley {
45102e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
45112e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
45122e17dfb7SMatthew G. Knepley 
45132e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45142e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
45152e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45162e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45172e17dfb7SMatthew G. Knepley   } else {
451865da65dcSMatthew G. Knepley     if (endpoint) {
451965da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4520da3333bfSMatthew 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)) {
4521da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
452265da65dcSMatthew G. Knepley         } else {
4523da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
452465da65dcSMatthew G. Knepley         }
452565da65dcSMatthew G. Knepley       }
452665da65dcSMatthew G. Knepley     } else {
45272e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
45281118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
45292e17dfb7SMatthew G. Knepley       }
45302e17dfb7SMatthew G. Knepley     }
453165da65dcSMatthew G. Knepley   }
45322e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45332e17dfb7SMatthew G. Knepley }
45342e17dfb7SMatthew G. Knepley 
45352e17dfb7SMatthew G. Knepley /*
45362e17dfb7SMatthew 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.
45372e17dfb7SMatthew G. Knepley 
45382e17dfb7SMatthew G. Knepley   Input Parameters:
45392e17dfb7SMatthew G. Knepley + dm     - The DM
45402e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
45412e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
45422e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
45432e17dfb7SMatthew G. Knepley 
45442e17dfb7SMatthew G. Knepley   Output Parameter:
45452e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45462e17dfb7SMatthew G. Knepley 
45472e17dfb7SMatthew G. Knepley   Level: developer
45482e17dfb7SMatthew G. Knepley 
45492e17dfb7SMatthew 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
45502e17dfb7SMatthew G. Knepley 
45512e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45522e17dfb7SMatthew G. Knepley */
45532e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
45542e17dfb7SMatthew G. Knepley {
45552e17dfb7SMatthew G. Knepley   PetscInt d;
45562e17dfb7SMatthew G. Knepley 
45572e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45582e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45592e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45602e17dfb7SMatthew G. Knepley   } else {
45612e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4562908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
45632e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
45642e17dfb7SMatthew G. Knepley       } else {
45652e17dfb7SMatthew G. Knepley         out[d] = in[d];
45662e17dfb7SMatthew G. Knepley       }
45672e17dfb7SMatthew G. Knepley     }
45682e17dfb7SMatthew G. Knepley   }
45692e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45702e17dfb7SMatthew G. Knepley }
45712e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
45722e17dfb7SMatthew G. Knepley {
45732e17dfb7SMatthew G. Knepley   PetscInt d;
45742e17dfb7SMatthew G. Knepley 
45752e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45762e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45772e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45782e17dfb7SMatthew G. Knepley   } else {
45792e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4580908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
45812e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
45822e17dfb7SMatthew G. Knepley       } else {
45832e17dfb7SMatthew G. Knepley         out[d] = in[d];
45842e17dfb7SMatthew G. Knepley       }
45852e17dfb7SMatthew G. Knepley     }
45862e17dfb7SMatthew G. Knepley   }
45872e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45882e17dfb7SMatthew G. Knepley }
45892e17dfb7SMatthew G. Knepley 
45902e17dfb7SMatthew G. Knepley /*
45912e17dfb7SMatthew 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.
45922e17dfb7SMatthew G. Knepley 
45932e17dfb7SMatthew G. Knepley   Input Parameters:
45942e17dfb7SMatthew G. Knepley + dm     - The DM
45952e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
45962e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
45972e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
45982e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
45992e17dfb7SMatthew G. Knepley 
46002e17dfb7SMatthew G. Knepley   Output Parameter:
46012e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
46022e17dfb7SMatthew G. Knepley 
46032e17dfb7SMatthew G. Knepley   Level: developer
46042e17dfb7SMatthew G. Knepley 
46052e17dfb7SMatthew 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
46062e17dfb7SMatthew G. Knepley 
46072e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
46082e17dfb7SMatthew G. Knepley */
46092e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46102e17dfb7SMatthew G. Knepley {
46112e17dfb7SMatthew G. Knepley   PetscInt d;
46122e17dfb7SMatthew G. Knepley 
46132e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46142e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46152e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
46162e17dfb7SMatthew G. Knepley   } else {
46172e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4618908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
46192e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46202e17dfb7SMatthew G. Knepley       } else {
46212e17dfb7SMatthew G. Knepley         out[d] += in[d];
46222e17dfb7SMatthew G. Knepley       }
46232e17dfb7SMatthew G. Knepley     }
46242e17dfb7SMatthew G. Knepley   }
46252e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46262e17dfb7SMatthew G. Knepley }
46272e17dfb7SMatthew G. Knepley 
462836447a5eSToby Isaac /*@
462936447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
463036447a5eSToby Isaac 
463136447a5eSToby Isaac   Input Parameter:
463236447a5eSToby Isaac . dm - The DM
463336447a5eSToby Isaac 
463436447a5eSToby Isaac   Output Parameter:
463536447a5eSToby Isaac   areLocalized - True if localized
463636447a5eSToby Isaac 
463736447a5eSToby Isaac   Level: developer
463836447a5eSToby Isaac 
463936447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
464036447a5eSToby Isaac @*/
464136447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
464236447a5eSToby Isaac {
464336447a5eSToby Isaac   DM             cdm;
464436447a5eSToby Isaac   PetscSection   coordSection;
464536447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
464636447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
464736447a5eSToby Isaac   PetscErrorCode ierr;
464836447a5eSToby Isaac 
464936447a5eSToby Isaac   PetscFunctionBegin;
465036447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
465192c9c85fSStefano Zampini   if (!dm->periodic) {
46528b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
46538b09590cSToby Isaac     PetscFunctionReturn(0);
46548b09590cSToby Isaac   }
465536447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
465636447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
465736447a5eSToby Isaac   {
465836447a5eSToby Isaac     PetscBool isplex;
465936447a5eSToby Isaac 
466036447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
466136447a5eSToby Isaac     if (isplex) {
466236447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
466336447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
466436447a5eSToby Isaac   }
466536447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
466636447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
46675a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
466836447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
466936447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
467036447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
467136447a5eSToby Isaac       break;
467236447a5eSToby Isaac     }
467336447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
46745a42012cSToby Isaac     if (dof) {
46755a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
467636447a5eSToby Isaac       break;
467736447a5eSToby Isaac     }
467836447a5eSToby Isaac   }
46795a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
468036447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
468136447a5eSToby Isaac   PetscFunctionReturn(0);
468236447a5eSToby Isaac }
468336447a5eSToby Isaac 
468436447a5eSToby Isaac 
46852e17dfb7SMatthew G. Knepley /*@
4686492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
46872e17dfb7SMatthew G. Knepley 
46882e17dfb7SMatthew G. Knepley   Input Parameter:
46892e17dfb7SMatthew G. Knepley . dm - The DM
46902e17dfb7SMatthew G. Knepley 
46912e17dfb7SMatthew G. Knepley   Level: developer
46922e17dfb7SMatthew G. Knepley 
46932e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
46942e17dfb7SMatthew G. Knepley @*/
46952e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
46962e17dfb7SMatthew G. Knepley {
46972e17dfb7SMatthew G. Knepley   DM             cdm;
46982e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
46992e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
47003e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
47013e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4702e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
47033e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
47043e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
47052e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
47062e17dfb7SMatthew G. Knepley 
47072e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47082e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
470992c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
47102e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
47112e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
47122e17dfb7SMatthew G. Knepley   {
47132e17dfb7SMatthew G. Knepley     PetscBool isplex;
47142e17dfb7SMatthew G. Knepley 
47152e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
47162e17dfb7SMatthew G. Knepley     if (isplex) {
47172e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
47183e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
47193e922f36SToby Isaac       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
47203e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
47213e922f36SToby Isaac       newStart = vStart;
47223e922f36SToby Isaac       newEnd   = vEnd;
47233e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
47243e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
47253e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
47263e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
47273e922f36SToby Isaac       }
47282e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
47292e17dfb7SMatthew G. Knepley   }
47302e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
47312e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
47323e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4733e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47343e922f36SToby Isaac 
47352e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
47362e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
47372e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
47382e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
47393e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
47403e922f36SToby Isaac 
47413e922f36SToby Isaac   ierr = DMGetWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47423e922f36SToby Isaac   localized = &anchor[bs];
47433e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
47443e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
47453e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
47463e922f36SToby Isaac 
47473e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
47483e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
47493e922f36SToby Isaac       PetscInt     b;
47503e922f36SToby Isaac 
47513e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
47523e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47533e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
47543e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
47553e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
47563e922f36SToby Isaac         for (b = 0; b < bs; b++) {
47573e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
47583e922f36SToby Isaac         }
47593e922f36SToby Isaac         if (b < bs) break;
47603e922f36SToby Isaac       }
47613e922f36SToby Isaac       if (d < dof/bs) {
47623e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
47633e922f36SToby Isaac           PetscInt cdof;
47643e922f36SToby Isaac 
47653e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
47663e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
47673e922f36SToby Isaac         }
47683e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
47693e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
47703e922f36SToby Isaac       }
47713e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47723e922f36SToby Isaac     }
47733e922f36SToby Isaac   }
47743e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
47753e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
47763e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47773e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
47783e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
47793e922f36SToby Isaac     PetscFunctionReturn(0);
47803e922f36SToby Isaac   }
47812e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
47822e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
47832e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
47842e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
47852e17dfb7SMatthew G. Knepley   }
47862e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
47872e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4788c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
47892e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
47902e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
47912e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
47922e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4793c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
47942e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
47952e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
47962e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
47972e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
47982e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
47992e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
48002e17dfb7SMatthew G. Knepley   }
48013e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
48023e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
48033e922f36SToby Isaac 
48042e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
48052e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
48063e922f36SToby Isaac       PetscInt     b, cdof;
48072e17dfb7SMatthew G. Knepley 
48083e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
48093e922f36SToby Isaac       if (!cdof) continue;
48102e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48112e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
48122e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48132e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
48142e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48152e17dfb7SMatthew G. Knepley     }
48163e922f36SToby Isaac   }
48173e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
48183e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
4819c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
48202e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
48212e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
48222e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
48232e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
48242e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
48252e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48262e17dfb7SMatthew G. Knepley }
48272e17dfb7SMatthew G. Knepley 
4828e87bb0d3SMatthew G Knepley /*@
48293a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4830e87bb0d3SMatthew G Knepley 
48313a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4832e87bb0d3SMatthew G Knepley 
4833e87bb0d3SMatthew G Knepley   Input Parameters:
4834e87bb0d3SMatthew G Knepley + dm - The DM
48353a93e3b7SToby Isaac . v - The Vec of points
483662a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
48373a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4838e87bb0d3SMatthew G Knepley 
483961e3bb9bSMatthew G Knepley   Output Parameter:
484062a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
484162a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
48423a93e3b7SToby Isaac 
4843e87bb0d3SMatthew G Knepley 
4844e87bb0d3SMatthew G Knepley   Level: developer
484561e3bb9bSMatthew G Knepley 
484662a38674SMatthew G. Knepley   Notes:
48473a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
484862a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
48493a93e3b7SToby Isaac 
48503a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
485162a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
48523a93e3b7SToby Isaac 
48533a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
48543a93e3b7SToby Isaac 
485562a38674SMatthew G. Knepley $    const PetscSFNode *cells;
485662a38674SMatthew G. Knepley $    PetscInt           nFound;
485762a38674SMatthew G. Knepley $    const PetscSFNode *found;
485862a38674SMatthew G. Knepley $
485962a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
48603a93e3b7SToby Isaac 
48613a93e3b7SToby 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
48623a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
48633a93e3b7SToby Isaac 
486461e3bb9bSMatthew G Knepley .keywords: point location, mesh
486562a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
486661e3bb9bSMatthew G Knepley @*/
486762a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4868e87bb0d3SMatthew G Knepley {
4869735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4870735aa83eSMatthew G Knepley 
4871e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4872e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4873e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4874e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
48753a93e3b7SToby Isaac   if (*cellSF) {
48763a93e3b7SToby Isaac     PetscMPIInt result;
48773a93e3b7SToby Isaac 
4878e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
4879a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
48803a93e3b7SToby 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");
4881e0fc9d1bSMatthew G. Knepley   } else {
48823a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
48833a93e3b7SToby Isaac   }
488447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4885735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
488662a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
488782f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
488847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4889e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4890e87bb0d3SMatthew G Knepley }
489114f150ffSMatthew G. Knepley 
4892f4d763aaSMatthew G. Knepley /*@
4893f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4894f4d763aaSMatthew G. Knepley 
4895f4d763aaSMatthew G. Knepley   Input Parameter:
4896f4d763aaSMatthew G. Knepley . dm - The original DM
4897f4d763aaSMatthew G. Knepley 
4898f4d763aaSMatthew G. Knepley   Output Parameter:
4899f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4900f4d763aaSMatthew G. Knepley 
4901f4d763aaSMatthew G. Knepley   Level: intermediate
4902f4d763aaSMatthew G. Knepley 
4903f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4904f4d763aaSMatthew G. Knepley @*/
490514f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
490614f150ffSMatthew G. Knepley {
4907c26acbdeSMatthew G. Knepley   PetscSection   section;
49082d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
490914f150ffSMatthew G. Knepley   PetscErrorCode ierr;
491014f150ffSMatthew G. Knepley 
491114f150ffSMatthew G. Knepley   PetscFunctionBegin;
491214f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
491314f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4914c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4915c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4916127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
49172d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
4918c26acbdeSMatthew G. Knepley     *odm = dm;
4919c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4920c26acbdeSMatthew G. Knepley   }
492114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
49220305aff6SMatthew G. Knepley     PetscDS      ds;
4923c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
492414f150ffSMatthew G. Knepley     PetscSF      sf;
492514f150ffSMatthew G. Knepley 
492614f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
49270305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
49280305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
492914f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
493014f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
493114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
493214f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
493315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
493414f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
493514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
493614f150ffSMatthew G. Knepley   }
493714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
493814f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
493914f150ffSMatthew G. Knepley }
4940f4d763aaSMatthew G. Knepley 
4941f4d763aaSMatthew G. Knepley /*@
4942cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4943f4d763aaSMatthew G. Knepley 
4944f4d763aaSMatthew G. Knepley   Input Parameter:
4945f4d763aaSMatthew G. Knepley . dm - The original DM
4946f4d763aaSMatthew G. Knepley 
4947cdb7a50dSMatthew G. Knepley   Output Parameters:
4948cdb7a50dSMatthew G. Knepley + num - The output sequence number
4949cdb7a50dSMatthew G. Knepley - val - The output sequence value
4950f4d763aaSMatthew G. Knepley 
4951f4d763aaSMatthew G. Knepley   Level: intermediate
4952f4d763aaSMatthew G. Knepley 
4953f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4954f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4955f4d763aaSMatthew G. Knepley 
4956f4d763aaSMatthew G. Knepley .seealso: VecView()
4957f4d763aaSMatthew G. Knepley @*/
4958cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4959f4d763aaSMatthew G. Knepley {
4960f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4961f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4962cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4963cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4964f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4965f4d763aaSMatthew G. Knepley }
4966f4d763aaSMatthew G. Knepley 
4967f4d763aaSMatthew G. Knepley /*@
4968cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4969f4d763aaSMatthew G. Knepley 
4970f4d763aaSMatthew G. Knepley   Input Parameters:
4971f4d763aaSMatthew G. Knepley + dm - The original DM
4972cdb7a50dSMatthew G. Knepley . num - The output sequence number
4973cdb7a50dSMatthew G. Knepley - val - The output sequence value
4974f4d763aaSMatthew G. Knepley 
4975f4d763aaSMatthew G. Knepley   Level: intermediate
4976f4d763aaSMatthew G. Knepley 
4977f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4978f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4979f4d763aaSMatthew G. Knepley 
4980f4d763aaSMatthew G. Knepley .seealso: VecView()
4981f4d763aaSMatthew G. Knepley @*/
4982cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4983f4d763aaSMatthew G. Knepley {
4984f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4985f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4986f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4987cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4988cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4989cdb7a50dSMatthew G. Knepley }
4990cdb7a50dSMatthew G. Knepley 
4991cdb7a50dSMatthew G. Knepley /*@C
4992cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4993cdb7a50dSMatthew G. Knepley 
4994cdb7a50dSMatthew G. Knepley   Input Parameters:
4995cdb7a50dSMatthew G. Knepley + dm   - The original DM
4996cdb7a50dSMatthew G. Knepley . name - The sequence name
4997cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4998cdb7a50dSMatthew G. Knepley 
4999cdb7a50dSMatthew G. Knepley   Output Parameter:
5000cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5001cdb7a50dSMatthew G. Knepley 
5002cdb7a50dSMatthew G. Knepley   Level: intermediate
5003cdb7a50dSMatthew G. Knepley 
5004cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5005cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5006cdb7a50dSMatthew G. Knepley 
5007cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5008cdb7a50dSMatthew G. Knepley @*/
5009cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5010cdb7a50dSMatthew G. Knepley {
5011cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5012cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5013cdb7a50dSMatthew G. Knepley 
5014cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5015cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5016cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5017cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5018cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5019cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5020cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5021cdb7a50dSMatthew G. Knepley     PetscScalar value;
5022cdb7a50dSMatthew G. Knepley 
502339d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
50244aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5025cdb7a50dSMatthew G. Knepley #endif
5026cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5027f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5028f4d763aaSMatthew G. Knepley }
50298e4ac7eaSMatthew G. Knepley 
50308e4ac7eaSMatthew G. Knepley /*@
50318e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
50328e4ac7eaSMatthew G. Knepley 
50338e4ac7eaSMatthew G. Knepley   Not collective
50348e4ac7eaSMatthew G. Knepley 
50358e4ac7eaSMatthew G. Knepley   Input Parameter:
50368e4ac7eaSMatthew G. Knepley . dm - The DM
50378e4ac7eaSMatthew G. Knepley 
50388e4ac7eaSMatthew G. Knepley   Output Parameter:
50398e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
50408e4ac7eaSMatthew G. Knepley 
50418e4ac7eaSMatthew G. Knepley   Level: beginner
50428e4ac7eaSMatthew G. Knepley 
50438e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
50448e4ac7eaSMatthew G. Knepley @*/
50458e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
50468e4ac7eaSMatthew G. Knepley {
50478e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
50488e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50498e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
50508e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
50518e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
50528e4ac7eaSMatthew G. Knepley }
50538e4ac7eaSMatthew G. Knepley 
50548e4ac7eaSMatthew G. Knepley /*@
50558e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
50568e4ac7eaSMatthew G. Knepley 
50578e4ac7eaSMatthew G. Knepley   Collective on dm
50588e4ac7eaSMatthew G. Knepley 
50598e4ac7eaSMatthew G. Knepley   Input Parameters:
50608e4ac7eaSMatthew G. Knepley + dm - The DM
50618e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
50628e4ac7eaSMatthew G. Knepley 
50638e4ac7eaSMatthew G. Knepley   Level: beginner
50648e4ac7eaSMatthew G. Knepley 
50658e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
50668e4ac7eaSMatthew G. Knepley @*/
50678e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
50688e4ac7eaSMatthew G. Knepley {
50698e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
50708e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50718e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
50728e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
50738e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
50748e4ac7eaSMatthew G. Knepley }
5075c58f1c22SToby Isaac 
5076c58f1c22SToby Isaac 
5077c58f1c22SToby Isaac /*@C
5078c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5079c58f1c22SToby Isaac 
5080c58f1c22SToby Isaac   Not Collective
5081c58f1c22SToby Isaac 
5082c58f1c22SToby Isaac   Input Parameters:
5083c58f1c22SToby Isaac + dm   - The DM object
5084c58f1c22SToby Isaac - name - The label name
5085c58f1c22SToby Isaac 
5086c58f1c22SToby Isaac   Level: intermediate
5087c58f1c22SToby Isaac 
5088c58f1c22SToby Isaac .keywords: mesh
5089c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5090c58f1c22SToby Isaac @*/
5091c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5092c58f1c22SToby Isaac {
5093c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5094c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5095c58f1c22SToby Isaac   PetscErrorCode ierr;
5096c58f1c22SToby Isaac 
5097c58f1c22SToby Isaac   PetscFunctionBegin;
5098c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5099c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5100c58f1c22SToby Isaac   while (next) {
5101c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5102c58f1c22SToby Isaac     if (flg) break;
5103c58f1c22SToby Isaac     next = next->next;
5104c58f1c22SToby Isaac   }
5105c58f1c22SToby Isaac   if (!flg) {
5106c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5107c58f1c22SToby Isaac 
5108c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5109c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5110c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5111c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5112c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5113c58f1c22SToby Isaac   }
5114c58f1c22SToby Isaac   PetscFunctionReturn(0);
5115c58f1c22SToby Isaac }
5116c58f1c22SToby Isaac 
5117c58f1c22SToby Isaac /*@C
5118c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5119c58f1c22SToby Isaac 
5120c58f1c22SToby Isaac   Not Collective
5121c58f1c22SToby Isaac 
5122c58f1c22SToby Isaac   Input Parameters:
5123c58f1c22SToby Isaac + dm   - The DM object
5124c58f1c22SToby Isaac . name - The label name
5125c58f1c22SToby Isaac - point - The mesh point
5126c58f1c22SToby Isaac 
5127c58f1c22SToby Isaac   Output Parameter:
5128c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5129c58f1c22SToby Isaac 
5130c58f1c22SToby Isaac   Level: beginner
5131c58f1c22SToby Isaac 
5132c58f1c22SToby Isaac .keywords: mesh
5133c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5134c58f1c22SToby Isaac @*/
5135c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5136c58f1c22SToby Isaac {
5137c58f1c22SToby Isaac   DMLabel        label;
5138c58f1c22SToby Isaac   PetscErrorCode ierr;
5139c58f1c22SToby Isaac 
5140c58f1c22SToby Isaac   PetscFunctionBegin;
5141c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5142c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5143c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
514413903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5145c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5146c58f1c22SToby Isaac   PetscFunctionReturn(0);
5147c58f1c22SToby Isaac }
5148c58f1c22SToby Isaac 
5149c58f1c22SToby Isaac /*@C
5150c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5151c58f1c22SToby Isaac 
5152c58f1c22SToby Isaac   Not Collective
5153c58f1c22SToby Isaac 
5154c58f1c22SToby Isaac   Input Parameters:
5155c58f1c22SToby Isaac + dm   - The DM object
5156c58f1c22SToby Isaac . name - The label name
5157c58f1c22SToby Isaac . point - The mesh point
5158c58f1c22SToby Isaac - value - The label value for this point
5159c58f1c22SToby Isaac 
5160c58f1c22SToby Isaac   Output Parameter:
5161c58f1c22SToby Isaac 
5162c58f1c22SToby Isaac   Level: beginner
5163c58f1c22SToby Isaac 
5164c58f1c22SToby Isaac .keywords: mesh
5165c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5166c58f1c22SToby Isaac @*/
5167c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5168c58f1c22SToby Isaac {
5169c58f1c22SToby Isaac   DMLabel        label;
5170c58f1c22SToby Isaac   PetscErrorCode ierr;
5171c58f1c22SToby Isaac 
5172c58f1c22SToby Isaac   PetscFunctionBegin;
5173c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5174c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5175c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5176c58f1c22SToby Isaac   if (!label) {
5177c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5178c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5179c58f1c22SToby Isaac   }
5180c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5181c58f1c22SToby Isaac   PetscFunctionReturn(0);
5182c58f1c22SToby Isaac }
5183c58f1c22SToby Isaac 
5184c58f1c22SToby Isaac /*@C
5185c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5186c58f1c22SToby Isaac 
5187c58f1c22SToby Isaac   Not Collective
5188c58f1c22SToby Isaac 
5189c58f1c22SToby Isaac   Input Parameters:
5190c58f1c22SToby Isaac + dm   - The DM object
5191c58f1c22SToby Isaac . name - The label name
5192c58f1c22SToby Isaac . point - The mesh point
5193c58f1c22SToby Isaac - value - The label value for this point
5194c58f1c22SToby Isaac 
5195c58f1c22SToby Isaac   Output Parameter:
5196c58f1c22SToby Isaac 
5197c58f1c22SToby Isaac   Level: beginner
5198c58f1c22SToby Isaac 
5199c58f1c22SToby Isaac .keywords: mesh
5200c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5201c58f1c22SToby Isaac @*/
5202c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5203c58f1c22SToby Isaac {
5204c58f1c22SToby Isaac   DMLabel        label;
5205c58f1c22SToby Isaac   PetscErrorCode ierr;
5206c58f1c22SToby Isaac 
5207c58f1c22SToby Isaac   PetscFunctionBegin;
5208c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5209c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5210c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5211c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5212c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5213c58f1c22SToby Isaac   PetscFunctionReturn(0);
5214c58f1c22SToby Isaac }
5215c58f1c22SToby Isaac 
5216c58f1c22SToby Isaac /*@C
5217c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5218c58f1c22SToby Isaac 
5219c58f1c22SToby Isaac   Not Collective
5220c58f1c22SToby Isaac 
5221c58f1c22SToby Isaac   Input Parameters:
5222c58f1c22SToby Isaac + dm   - The DM object
5223c58f1c22SToby Isaac - name - The label name
5224c58f1c22SToby Isaac 
5225c58f1c22SToby Isaac   Output Parameter:
5226c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5227c58f1c22SToby Isaac 
5228c58f1c22SToby Isaac   Level: beginner
5229c58f1c22SToby Isaac 
5230c58f1c22SToby Isaac .keywords: mesh
5231c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5232c58f1c22SToby Isaac @*/
5233c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5234c58f1c22SToby Isaac {
5235c58f1c22SToby Isaac   DMLabel        label;
5236c58f1c22SToby Isaac   PetscErrorCode ierr;
5237c58f1c22SToby Isaac 
5238c58f1c22SToby Isaac   PetscFunctionBegin;
5239c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5240c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5241c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5242c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5243c58f1c22SToby Isaac   *size = 0;
5244c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5245c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5246c58f1c22SToby Isaac   PetscFunctionReturn(0);
5247c58f1c22SToby Isaac }
5248c58f1c22SToby Isaac 
5249c58f1c22SToby Isaac /*@C
5250c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5251c58f1c22SToby Isaac 
5252c58f1c22SToby Isaac   Not Collective
5253c58f1c22SToby Isaac 
5254c58f1c22SToby Isaac   Input Parameters:
5255c58f1c22SToby Isaac + mesh - The DM object
5256c58f1c22SToby Isaac - name - The label name
5257c58f1c22SToby Isaac 
5258c58f1c22SToby Isaac   Output Parameter:
5259c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5260c58f1c22SToby Isaac 
5261c58f1c22SToby Isaac   Level: beginner
5262c58f1c22SToby Isaac 
5263c58f1c22SToby Isaac .keywords: mesh
5264c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5265c58f1c22SToby Isaac @*/
5266c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5267c58f1c22SToby Isaac {
5268c58f1c22SToby Isaac   DMLabel        label;
5269c58f1c22SToby Isaac   PetscErrorCode ierr;
5270c58f1c22SToby Isaac 
5271c58f1c22SToby Isaac   PetscFunctionBegin;
5272c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5273c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5274c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5275c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5276c58f1c22SToby Isaac   *ids = NULL;
5277c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5278c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5279c58f1c22SToby Isaac   PetscFunctionReturn(0);
5280c58f1c22SToby Isaac }
5281c58f1c22SToby Isaac 
5282c58f1c22SToby Isaac /*@C
5283c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5284c58f1c22SToby Isaac 
5285c58f1c22SToby Isaac   Not Collective
5286c58f1c22SToby Isaac 
5287c58f1c22SToby Isaac   Input Parameters:
5288c58f1c22SToby Isaac + dm - The DM object
5289c58f1c22SToby Isaac . name - The label name
5290c58f1c22SToby Isaac - value - The stratum value
5291c58f1c22SToby Isaac 
5292c58f1c22SToby Isaac   Output Parameter:
5293c58f1c22SToby Isaac . size - The stratum size
5294c58f1c22SToby Isaac 
5295c58f1c22SToby Isaac   Level: beginner
5296c58f1c22SToby Isaac 
5297c58f1c22SToby Isaac .keywords: mesh
5298c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5299c58f1c22SToby Isaac @*/
5300c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5301c58f1c22SToby Isaac {
5302c58f1c22SToby Isaac   DMLabel        label;
5303c58f1c22SToby Isaac   PetscErrorCode ierr;
5304c58f1c22SToby Isaac 
5305c58f1c22SToby Isaac   PetscFunctionBegin;
5306c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5307c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5308c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5309c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5310c58f1c22SToby Isaac   *size = 0;
5311c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5312c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5313c58f1c22SToby Isaac   PetscFunctionReturn(0);
5314c58f1c22SToby Isaac }
5315c58f1c22SToby Isaac 
5316c58f1c22SToby Isaac /*@C
5317c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5318c58f1c22SToby Isaac 
5319c58f1c22SToby Isaac   Not Collective
5320c58f1c22SToby Isaac 
5321c58f1c22SToby Isaac   Input Parameters:
5322c58f1c22SToby Isaac + dm - The DM object
5323c58f1c22SToby Isaac . name - The label name
5324c58f1c22SToby Isaac - value - The stratum value
5325c58f1c22SToby Isaac 
5326c58f1c22SToby Isaac   Output Parameter:
5327c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5328c58f1c22SToby Isaac 
5329c58f1c22SToby Isaac   Level: beginner
5330c58f1c22SToby Isaac 
5331c58f1c22SToby Isaac .keywords: mesh
5332c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5333c58f1c22SToby Isaac @*/
5334c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5335c58f1c22SToby Isaac {
5336c58f1c22SToby Isaac   DMLabel        label;
5337c58f1c22SToby Isaac   PetscErrorCode ierr;
5338c58f1c22SToby Isaac 
5339c58f1c22SToby Isaac   PetscFunctionBegin;
5340c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5341c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5342c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5343c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5344c58f1c22SToby Isaac   *points = NULL;
5345c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5346c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5347c58f1c22SToby Isaac   PetscFunctionReturn(0);
5348c58f1c22SToby Isaac }
5349c58f1c22SToby Isaac 
53504de306b1SToby Isaac /*@C
53514de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
53524de306b1SToby Isaac 
53534de306b1SToby Isaac   Not Collective
53544de306b1SToby Isaac 
53554de306b1SToby Isaac   Input Parameters:
53564de306b1SToby Isaac + dm - The DM object
53574de306b1SToby Isaac . name - The label name
53584de306b1SToby Isaac . value - The stratum value
53594de306b1SToby Isaac - points - The stratum points
53604de306b1SToby Isaac 
53614de306b1SToby Isaac   Level: beginner
53624de306b1SToby Isaac 
53634de306b1SToby Isaac .keywords: mesh
53644de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
53654de306b1SToby Isaac @*/
53664de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
53674de306b1SToby Isaac {
53684de306b1SToby Isaac   DMLabel        label;
53694de306b1SToby Isaac   PetscErrorCode ierr;
53704de306b1SToby Isaac 
53714de306b1SToby Isaac   PetscFunctionBegin;
53724de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53734de306b1SToby Isaac   PetscValidCharPointer(name, 2);
53744de306b1SToby Isaac   PetscValidPointer(points, 4);
53754de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
53764de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
53774de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
53784de306b1SToby Isaac   PetscFunctionReturn(0);
53794de306b1SToby Isaac }
53804de306b1SToby Isaac 
5381c58f1c22SToby Isaac /*@C
5382c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5383c58f1c22SToby Isaac 
5384c58f1c22SToby Isaac   Not Collective
5385c58f1c22SToby Isaac 
5386c58f1c22SToby Isaac   Input Parameters:
5387c58f1c22SToby Isaac + dm   - The DM object
5388c58f1c22SToby Isaac . name - The label name
5389c58f1c22SToby Isaac - value - The label value for this point
5390c58f1c22SToby Isaac 
5391c58f1c22SToby Isaac   Output Parameter:
5392c58f1c22SToby Isaac 
5393c58f1c22SToby Isaac   Level: beginner
5394c58f1c22SToby Isaac 
5395c58f1c22SToby Isaac .keywords: mesh
5396c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5397c58f1c22SToby Isaac @*/
5398c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5399c58f1c22SToby Isaac {
5400c58f1c22SToby Isaac   DMLabel        label;
5401c58f1c22SToby Isaac   PetscErrorCode ierr;
5402c58f1c22SToby Isaac 
5403c58f1c22SToby Isaac   PetscFunctionBegin;
5404c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5405c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5406c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5407c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5408c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5409c58f1c22SToby Isaac   PetscFunctionReturn(0);
5410c58f1c22SToby Isaac }
5411c58f1c22SToby Isaac 
5412c58f1c22SToby Isaac /*@
5413c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5414c58f1c22SToby Isaac 
5415c58f1c22SToby Isaac   Not Collective
5416c58f1c22SToby Isaac 
5417c58f1c22SToby Isaac   Input Parameter:
5418c58f1c22SToby Isaac . dm   - The DM object
5419c58f1c22SToby Isaac 
5420c58f1c22SToby Isaac   Output Parameter:
5421c58f1c22SToby Isaac . numLabels - the number of Labels
5422c58f1c22SToby Isaac 
5423c58f1c22SToby Isaac   Level: intermediate
5424c58f1c22SToby Isaac 
5425c58f1c22SToby Isaac .keywords: mesh
5426c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5427c58f1c22SToby Isaac @*/
5428c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5429c58f1c22SToby Isaac {
5430c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5431c58f1c22SToby Isaac   PetscInt  n    = 0;
5432c58f1c22SToby Isaac 
5433c58f1c22SToby Isaac   PetscFunctionBegin;
5434c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5435c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5436c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5437c58f1c22SToby Isaac   *numLabels = n;
5438c58f1c22SToby Isaac   PetscFunctionReturn(0);
5439c58f1c22SToby Isaac }
5440c58f1c22SToby Isaac 
5441c58f1c22SToby Isaac /*@C
5442c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5443c58f1c22SToby Isaac 
5444c58f1c22SToby Isaac   Not Collective
5445c58f1c22SToby Isaac 
5446c58f1c22SToby Isaac   Input Parameters:
5447c58f1c22SToby Isaac + dm - The DM object
5448c58f1c22SToby Isaac - n  - the label number
5449c58f1c22SToby Isaac 
5450c58f1c22SToby Isaac   Output Parameter:
5451c58f1c22SToby Isaac . name - the label name
5452c58f1c22SToby Isaac 
5453c58f1c22SToby Isaac   Level: intermediate
5454c58f1c22SToby Isaac 
5455c58f1c22SToby Isaac .keywords: mesh
5456c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5457c58f1c22SToby Isaac @*/
5458c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5459c58f1c22SToby Isaac {
5460c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5461c58f1c22SToby Isaac   PetscInt  l    = 0;
5462c58f1c22SToby Isaac 
5463c58f1c22SToby Isaac   PetscFunctionBegin;
5464c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5465c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5466c58f1c22SToby Isaac   while (next) {
5467c58f1c22SToby Isaac     if (l == n) {
5468c58f1c22SToby Isaac       *name = next->label->name;
5469c58f1c22SToby Isaac       PetscFunctionReturn(0);
5470c58f1c22SToby Isaac     }
5471c58f1c22SToby Isaac     ++l;
5472c58f1c22SToby Isaac     next = next->next;
5473c58f1c22SToby Isaac   }
5474c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5475c58f1c22SToby Isaac }
5476c58f1c22SToby Isaac 
5477c58f1c22SToby Isaac /*@C
5478c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5479c58f1c22SToby Isaac 
5480c58f1c22SToby Isaac   Not Collective
5481c58f1c22SToby Isaac 
5482c58f1c22SToby Isaac   Input Parameters:
5483c58f1c22SToby Isaac + dm   - The DM object
5484c58f1c22SToby Isaac - name - The label name
5485c58f1c22SToby Isaac 
5486c58f1c22SToby Isaac   Output Parameter:
5487c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5488c58f1c22SToby Isaac 
5489c58f1c22SToby Isaac   Level: intermediate
5490c58f1c22SToby Isaac 
5491c58f1c22SToby Isaac .keywords: mesh
5492c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5493c58f1c22SToby Isaac @*/
5494c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5495c58f1c22SToby Isaac {
5496c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5497c58f1c22SToby Isaac   PetscErrorCode ierr;
5498c58f1c22SToby Isaac 
5499c58f1c22SToby Isaac   PetscFunctionBegin;
5500c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5501c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5502c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5503c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5504c58f1c22SToby Isaac   while (next) {
5505c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5506c58f1c22SToby Isaac     if (*hasLabel) break;
5507c58f1c22SToby Isaac     next = next->next;
5508c58f1c22SToby Isaac   }
5509c58f1c22SToby Isaac   PetscFunctionReturn(0);
5510c58f1c22SToby Isaac }
5511c58f1c22SToby Isaac 
5512c58f1c22SToby Isaac /*@C
5513c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5514c58f1c22SToby Isaac 
5515c58f1c22SToby Isaac   Not Collective
5516c58f1c22SToby Isaac 
5517c58f1c22SToby Isaac   Input Parameters:
5518c58f1c22SToby Isaac + dm   - The DM object
5519c58f1c22SToby Isaac - name - The label name
5520c58f1c22SToby Isaac 
5521c58f1c22SToby Isaac   Output Parameter:
5522c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5523c58f1c22SToby Isaac 
5524c58f1c22SToby Isaac   Level: intermediate
5525c58f1c22SToby Isaac 
5526c58f1c22SToby Isaac .keywords: mesh
5527c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5528c58f1c22SToby Isaac @*/
5529c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5530c58f1c22SToby Isaac {
5531c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5532c58f1c22SToby Isaac   PetscBool      hasLabel;
5533c58f1c22SToby Isaac   PetscErrorCode ierr;
5534c58f1c22SToby Isaac 
5535c58f1c22SToby Isaac   PetscFunctionBegin;
5536c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5537c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5538c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5539c58f1c22SToby Isaac   *label = NULL;
5540c58f1c22SToby Isaac   while (next) {
5541c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5542c58f1c22SToby Isaac     if (hasLabel) {
5543c58f1c22SToby Isaac       *label = next->label;
5544c58f1c22SToby Isaac       break;
5545c58f1c22SToby Isaac     }
5546c58f1c22SToby Isaac     next = next->next;
5547c58f1c22SToby Isaac   }
5548c58f1c22SToby Isaac   PetscFunctionReturn(0);
5549c58f1c22SToby Isaac }
5550c58f1c22SToby Isaac 
5551c58f1c22SToby Isaac /*@C
5552c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5553c58f1c22SToby Isaac 
5554c58f1c22SToby Isaac   Not Collective
5555c58f1c22SToby Isaac 
5556c58f1c22SToby Isaac   Input Parameters:
5557c58f1c22SToby Isaac + dm - The DM object
5558c58f1c22SToby Isaac - n  - the label number
5559c58f1c22SToby Isaac 
5560c58f1c22SToby Isaac   Output Parameter:
5561c58f1c22SToby Isaac . label - the label
5562c58f1c22SToby Isaac 
5563c58f1c22SToby Isaac   Level: intermediate
5564c58f1c22SToby Isaac 
5565c58f1c22SToby Isaac .keywords: mesh
5566c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5567c58f1c22SToby Isaac @*/
5568c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5569c58f1c22SToby Isaac {
5570c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5571c58f1c22SToby Isaac   PetscInt    l    = 0;
5572c58f1c22SToby Isaac 
5573c58f1c22SToby Isaac   PetscFunctionBegin;
5574c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5575c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5576c58f1c22SToby Isaac   while (next) {
5577c58f1c22SToby Isaac     if (l == n) {
5578c58f1c22SToby Isaac       *label = next->label;
5579c58f1c22SToby Isaac       PetscFunctionReturn(0);
5580c58f1c22SToby Isaac     }
5581c58f1c22SToby Isaac     ++l;
5582c58f1c22SToby Isaac     next = next->next;
5583c58f1c22SToby Isaac   }
5584c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5585c58f1c22SToby Isaac }
5586c58f1c22SToby Isaac 
5587c58f1c22SToby Isaac /*@C
5588c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5589c58f1c22SToby Isaac 
5590c58f1c22SToby Isaac   Not Collective
5591c58f1c22SToby Isaac 
5592c58f1c22SToby Isaac   Input Parameters:
5593c58f1c22SToby Isaac + dm   - The DM object
5594c58f1c22SToby Isaac - label - The DMLabel
5595c58f1c22SToby Isaac 
5596c58f1c22SToby Isaac   Level: developer
5597c58f1c22SToby Isaac 
5598c58f1c22SToby Isaac .keywords: mesh
5599c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5600c58f1c22SToby Isaac @*/
5601c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5602c58f1c22SToby Isaac {
5603c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5604c58f1c22SToby Isaac   PetscBool      hasLabel;
5605c58f1c22SToby Isaac   PetscErrorCode ierr;
5606c58f1c22SToby Isaac 
5607c58f1c22SToby Isaac   PetscFunctionBegin;
5608c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5609c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5610c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5611c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5612c58f1c22SToby Isaac   tmpLabel->label  = label;
5613c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5614c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5615c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5616c58f1c22SToby Isaac   PetscFunctionReturn(0);
5617c58f1c22SToby Isaac }
5618c58f1c22SToby Isaac 
5619c58f1c22SToby Isaac /*@C
5620c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5621c58f1c22SToby Isaac 
5622c58f1c22SToby Isaac   Not Collective
5623c58f1c22SToby Isaac 
5624c58f1c22SToby Isaac   Input Parameters:
5625c58f1c22SToby Isaac + dm   - The DM object
5626c58f1c22SToby Isaac - name - The label name
5627c58f1c22SToby Isaac 
5628c58f1c22SToby Isaac   Output Parameter:
5629c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5630c58f1c22SToby Isaac 
5631c58f1c22SToby Isaac   Level: developer
5632c58f1c22SToby Isaac 
5633c58f1c22SToby Isaac .keywords: mesh
5634c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5635c58f1c22SToby Isaac @*/
5636c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5637c58f1c22SToby Isaac {
5638c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5639c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5640c58f1c22SToby Isaac   PetscBool      hasLabel;
5641c58f1c22SToby Isaac   PetscErrorCode ierr;
5642c58f1c22SToby Isaac 
5643c58f1c22SToby Isaac   PetscFunctionBegin;
5644c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5645c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5646c58f1c22SToby Isaac   *label = NULL;
5647c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5648c58f1c22SToby Isaac   while (next) {
5649c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5650c58f1c22SToby Isaac     if (hasLabel) {
5651c58f1c22SToby Isaac       if (last) last->next       = next->next;
5652c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5653c58f1c22SToby Isaac       next->next = NULL;
5654c58f1c22SToby Isaac       *label     = next->label;
5655c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5656c58f1c22SToby Isaac       if (hasLabel) {
5657c58f1c22SToby Isaac         dm->depthLabel = NULL;
5658c58f1c22SToby Isaac       }
5659c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5660c58f1c22SToby Isaac       break;
5661c58f1c22SToby Isaac     }
5662c58f1c22SToby Isaac     last = next;
5663c58f1c22SToby Isaac     next = next->next;
5664c58f1c22SToby Isaac   }
5665c58f1c22SToby Isaac   PetscFunctionReturn(0);
5666c58f1c22SToby Isaac }
5667c58f1c22SToby Isaac 
5668c58f1c22SToby Isaac /*@C
5669c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5670c58f1c22SToby Isaac 
5671c58f1c22SToby Isaac   Not Collective
5672c58f1c22SToby Isaac 
5673c58f1c22SToby Isaac   Input Parameters:
5674c58f1c22SToby Isaac + dm   - The DM object
5675c58f1c22SToby Isaac - name - The label name
5676c58f1c22SToby Isaac 
5677c58f1c22SToby Isaac   Output Parameter:
5678c58f1c22SToby Isaac . output - The flag for output
5679c58f1c22SToby Isaac 
5680c58f1c22SToby Isaac   Level: developer
5681c58f1c22SToby Isaac 
5682c58f1c22SToby Isaac .keywords: mesh
5683c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5684c58f1c22SToby Isaac @*/
5685c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5686c58f1c22SToby Isaac {
5687c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5688c58f1c22SToby Isaac   PetscErrorCode ierr;
5689c58f1c22SToby Isaac 
5690c58f1c22SToby Isaac   PetscFunctionBegin;
5691c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5692c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5693c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5694c58f1c22SToby Isaac   while (next) {
5695c58f1c22SToby Isaac     PetscBool flg;
5696c58f1c22SToby Isaac 
5697c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5698c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5699c58f1c22SToby Isaac     next = next->next;
5700c58f1c22SToby Isaac   }
5701c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5702c58f1c22SToby Isaac }
5703c58f1c22SToby Isaac 
5704c58f1c22SToby Isaac /*@C
5705c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5706c58f1c22SToby Isaac 
5707c58f1c22SToby Isaac   Not Collective
5708c58f1c22SToby Isaac 
5709c58f1c22SToby Isaac   Input Parameters:
5710c58f1c22SToby Isaac + dm     - The DM object
5711c58f1c22SToby Isaac . name   - The label name
5712c58f1c22SToby Isaac - output - The flag for output
5713c58f1c22SToby Isaac 
5714c58f1c22SToby Isaac   Level: developer
5715c58f1c22SToby Isaac 
5716c58f1c22SToby Isaac .keywords: mesh
5717c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5718c58f1c22SToby Isaac @*/
5719c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5720c58f1c22SToby Isaac {
5721c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5722c58f1c22SToby Isaac   PetscErrorCode ierr;
5723c58f1c22SToby Isaac 
5724c58f1c22SToby Isaac   PetscFunctionBegin;
5725c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5726c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5727c58f1c22SToby Isaac   while (next) {
5728c58f1c22SToby Isaac     PetscBool flg;
5729c58f1c22SToby Isaac 
5730c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5731c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5732c58f1c22SToby Isaac     next = next->next;
5733c58f1c22SToby Isaac   }
5734c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5735c58f1c22SToby Isaac }
5736c58f1c22SToby Isaac 
5737c58f1c22SToby Isaac 
5738c58f1c22SToby Isaac /*@
5739c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5740c58f1c22SToby Isaac 
5741c58f1c22SToby Isaac   Collective on DM
5742c58f1c22SToby Isaac 
5743c58f1c22SToby Isaac   Input Parameter:
57442e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5745c58f1c22SToby Isaac 
5746c58f1c22SToby Isaac   Output Parameter:
57472e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5748c58f1c22SToby Isaac 
5749c58f1c22SToby Isaac   Level: intermediate
5750c58f1c22SToby Isaac 
5751c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5752c58f1c22SToby Isaac 
5753c58f1c22SToby Isaac .keywords: mesh
5754c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5755c58f1c22SToby Isaac @*/
5756c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5757c58f1c22SToby Isaac {
5758c58f1c22SToby Isaac   PetscInt       numLabels, l;
5759c58f1c22SToby Isaac   PetscErrorCode ierr;
5760c58f1c22SToby Isaac 
5761c58f1c22SToby Isaac   PetscFunctionBegin;
5762c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5763c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5764c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5765c58f1c22SToby Isaac     DMLabel     label, labelNew;
5766c58f1c22SToby Isaac     const char *name;
5767c58f1c22SToby Isaac     PetscBool   flg;
5768c58f1c22SToby Isaac 
5769c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5770c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5771c58f1c22SToby Isaac     if (flg) continue;
5772c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5773c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5774c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5775c58f1c22SToby Isaac   }
5776c58f1c22SToby Isaac   PetscFunctionReturn(0);
5777c58f1c22SToby Isaac }
5778a8fb8f29SToby Isaac 
5779a8fb8f29SToby Isaac /*@
5780a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5781a8fb8f29SToby Isaac 
5782a8fb8f29SToby Isaac   Input Parameter:
5783a8fb8f29SToby Isaac . dm - The DM object
5784a8fb8f29SToby Isaac 
5785a8fb8f29SToby Isaac   Output Parameter:
5786a8fb8f29SToby Isaac . cdm - The coarse DM
5787a8fb8f29SToby Isaac 
5788a8fb8f29SToby Isaac   Level: intermediate
5789a8fb8f29SToby Isaac 
5790a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5791a8fb8f29SToby Isaac @*/
5792a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5793a8fb8f29SToby Isaac {
5794a8fb8f29SToby Isaac   PetscFunctionBegin;
5795a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5796a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5797a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5798a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5799a8fb8f29SToby Isaac }
5800a8fb8f29SToby Isaac 
5801a8fb8f29SToby Isaac /*@
5802a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5803a8fb8f29SToby Isaac 
5804a8fb8f29SToby Isaac   Input Parameters:
5805a8fb8f29SToby Isaac + dm - The DM object
5806a8fb8f29SToby Isaac - cdm - The coarse DM
5807a8fb8f29SToby Isaac 
5808a8fb8f29SToby Isaac   Level: intermediate
5809a8fb8f29SToby Isaac 
5810a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5811a8fb8f29SToby Isaac @*/
5812a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5813a8fb8f29SToby Isaac {
5814a8fb8f29SToby Isaac   PetscErrorCode ierr;
5815a8fb8f29SToby Isaac 
5816a8fb8f29SToby Isaac   PetscFunctionBegin;
5817a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5818a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5819a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5820a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5821a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5822a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5823a8fb8f29SToby Isaac }
5824a8fb8f29SToby Isaac 
582588bdff64SToby Isaac /*@
582688bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
582788bdff64SToby Isaac 
582888bdff64SToby Isaac   Input Parameter:
582988bdff64SToby Isaac . dm - The DM object
583088bdff64SToby Isaac 
583188bdff64SToby Isaac   Output Parameter:
583288bdff64SToby Isaac . fdm - The fine DM
583388bdff64SToby Isaac 
583488bdff64SToby Isaac   Level: intermediate
583588bdff64SToby Isaac 
583688bdff64SToby Isaac .seealso: DMSetFineDM()
583788bdff64SToby Isaac @*/
583888bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
583988bdff64SToby Isaac {
584088bdff64SToby Isaac   PetscFunctionBegin;
584188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
584288bdff64SToby Isaac   PetscValidPointer(fdm, 2);
584388bdff64SToby Isaac   *fdm = dm->fineMesh;
584488bdff64SToby Isaac   PetscFunctionReturn(0);
584588bdff64SToby Isaac }
584688bdff64SToby Isaac 
584788bdff64SToby Isaac /*@
584888bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
584988bdff64SToby Isaac 
585088bdff64SToby Isaac   Input Parameters:
585188bdff64SToby Isaac + dm - The DM object
585288bdff64SToby Isaac - fdm - The fine DM
585388bdff64SToby Isaac 
585488bdff64SToby Isaac   Level: intermediate
585588bdff64SToby Isaac 
585688bdff64SToby Isaac .seealso: DMGetFineDM()
585788bdff64SToby Isaac @*/
585888bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
585988bdff64SToby Isaac {
586088bdff64SToby Isaac   PetscErrorCode ierr;
586188bdff64SToby Isaac 
586288bdff64SToby Isaac   PetscFunctionBegin;
586388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
586488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
586588bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
586688bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
586788bdff64SToby Isaac   dm->fineMesh = fdm;
586888bdff64SToby Isaac   PetscFunctionReturn(0);
586988bdff64SToby Isaac }
587088bdff64SToby Isaac 
5871a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5872a6ba4734SToby Isaac 
5873a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5874a6ba4734SToby Isaac {
5875a6ba4734SToby Isaac   PetscErrorCode ierr;
5876a6ba4734SToby Isaac 
5877a6ba4734SToby Isaac   PetscFunctionBegin;
5878e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
5879a6ba4734SToby Isaac   PetscFunctionReturn(0);
5880a6ba4734SToby Isaac }
5881a6ba4734SToby Isaac 
5882a6ba4734SToby Isaac /*@C
5883a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5884a6ba4734SToby Isaac 
5885a6ba4734SToby Isaac   Input Parameters:
58864c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
5887f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5888a6ba4734SToby Isaac . name        - The BC name
5889a6ba4734SToby Isaac . labelname   - The label defining constrained points
5890a6ba4734SToby Isaac . field       - The field to constrain
5891a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5892a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5893a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5894a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5895a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5896a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5897a6ba4734SToby Isaac 
5898a6ba4734SToby Isaac   Options Database Keys:
5899a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5900a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5901a6ba4734SToby Isaac 
5902a6ba4734SToby Isaac   Level: developer
5903a6ba4734SToby Isaac 
5904a6ba4734SToby Isaac .seealso: DMGetBoundary()
5905a6ba4734SToby Isaac @*/
5906a30ec4eaSSatish 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)
5907a6ba4734SToby Isaac {
5908a6ba4734SToby Isaac   PetscErrorCode ierr;
5909a6ba4734SToby Isaac 
5910a6ba4734SToby Isaac   PetscFunctionBegin;
5911a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5912f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
5913a6ba4734SToby Isaac   PetscFunctionReturn(0);
5914a6ba4734SToby Isaac }
5915a6ba4734SToby Isaac 
5916a6ba4734SToby Isaac /*@
5917a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5918a6ba4734SToby Isaac 
5919a6ba4734SToby Isaac   Input Parameters:
5920a6ba4734SToby Isaac . dm - The mesh object
5921a6ba4734SToby Isaac 
5922a6ba4734SToby Isaac   Output Parameters:
5923a6ba4734SToby Isaac . numBd - The number of BC
5924a6ba4734SToby Isaac 
5925a6ba4734SToby Isaac   Level: intermediate
5926a6ba4734SToby Isaac 
5927a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5928a6ba4734SToby Isaac @*/
5929a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5930a6ba4734SToby Isaac {
593158ebd649SToby Isaac   PetscErrorCode ierr;
5932a6ba4734SToby Isaac 
5933a6ba4734SToby Isaac   PetscFunctionBegin;
5934a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
593558ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
5936a6ba4734SToby Isaac   PetscFunctionReturn(0);
5937a6ba4734SToby Isaac }
5938a6ba4734SToby Isaac 
5939a6ba4734SToby Isaac /*@C
59401c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
5941a6ba4734SToby Isaac 
5942a6ba4734SToby Isaac   Input Parameters:
5943a6ba4734SToby Isaac + dm          - The mesh object
5944a6ba4734SToby Isaac - bd          - The BC number
5945a6ba4734SToby Isaac 
5946a6ba4734SToby Isaac   Output Parameters:
5947f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5948a6ba4734SToby Isaac . name        - The BC name
5949a6ba4734SToby Isaac . labelname   - The label defining constrained points
5950a6ba4734SToby Isaac . field       - The field to constrain
5951a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5952a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5953a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5954a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5955a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5956a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5957a6ba4734SToby Isaac 
5958a6ba4734SToby Isaac   Options Database Keys:
5959a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5960a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5961a6ba4734SToby Isaac 
5962a6ba4734SToby Isaac   Level: developer
5963a6ba4734SToby Isaac 
5964a6ba4734SToby Isaac .seealso: DMAddBoundary()
5965a6ba4734SToby Isaac @*/
5966a30ec4eaSSatish 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)
5967a6ba4734SToby Isaac {
596858ebd649SToby Isaac   PetscErrorCode ierr;
5969a6ba4734SToby Isaac 
5970a6ba4734SToby Isaac   PetscFunctionBegin;
5971a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5972f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
5973a6ba4734SToby Isaac   PetscFunctionReturn(0);
5974a6ba4734SToby Isaac }
5975a6ba4734SToby Isaac 
5976e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
5977e6f8dbb6SToby Isaac {
5978dff059c6SToby Isaac   DMBoundary *lastnext;
5979e6f8dbb6SToby Isaac   DSBoundary dsbound;
5980e6f8dbb6SToby Isaac   PetscErrorCode ierr;
5981e6f8dbb6SToby Isaac 
5982e6f8dbb6SToby Isaac   PetscFunctionBegin;
5983e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
598447a1f5adSToby Isaac   if (dm->boundary) {
598547a1f5adSToby Isaac     DMBoundary next = dm->boundary;
598647a1f5adSToby Isaac 
598747a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
598847a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
598947a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
599047a1f5adSToby Isaac     while (next) {
599147a1f5adSToby Isaac       DMBoundary b = next;
599247a1f5adSToby Isaac 
599347a1f5adSToby Isaac       next = b->next;
599447a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
5995a6ba4734SToby Isaac     }
599647a1f5adSToby Isaac     dm->boundary = NULL;
5997a6ba4734SToby Isaac   }
599847a1f5adSToby Isaac 
5999dff059c6SToby Isaac   lastnext = &(dm->boundary);
6000e6f8dbb6SToby Isaac   while (dsbound) {
6001e6f8dbb6SToby Isaac     DMBoundary dmbound;
6002e6f8dbb6SToby Isaac 
6003e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6004e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6005e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6006e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
600747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6008dff059c6SToby Isaac     *lastnext = dmbound;
6009dff059c6SToby Isaac     lastnext = &(dmbound->next);
6010dff059c6SToby Isaac     dsbound = dsbound->next;
6011a6ba4734SToby Isaac   }
6012a6ba4734SToby Isaac   PetscFunctionReturn(0);
6013a6ba4734SToby Isaac }
6014a6ba4734SToby Isaac 
6015a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6016a6ba4734SToby Isaac {
6017b95f2879SToby Isaac   DMBoundary     b;
6018a6ba4734SToby Isaac   PetscErrorCode ierr;
6019a6ba4734SToby Isaac 
6020a6ba4734SToby Isaac   PetscFunctionBegin;
6021a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6022a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6023a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6024e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6025b95f2879SToby Isaac   b = dm->boundary;
6026a6ba4734SToby Isaac   while (b && !(*isBd)) {
6027e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6028e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
60293424c85cSToby Isaac 
60303424c85cSToby Isaac     if (label) {
6031a6ba4734SToby Isaac       PetscInt i;
6032a6ba4734SToby Isaac 
6033e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6034e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6035a6ba4734SToby Isaac       }
6036a6ba4734SToby Isaac     }
6037a6ba4734SToby Isaac     b = b->next;
6038a6ba4734SToby Isaac   }
6039a6ba4734SToby Isaac   PetscFunctionReturn(0);
6040a6ba4734SToby Isaac }
60414d6f44ffSToby Isaac 
60424d6f44ffSToby Isaac /*@C
60434d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
60444d6f44ffSToby Isaac 
60454d6f44ffSToby Isaac   Input Parameters:
60464d6f44ffSToby Isaac + dm      - The DM
60470709b2feSToby Isaac . time    - The time
60484d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
60494d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
60504d6f44ffSToby Isaac - mode    - The insertion mode for values
60514d6f44ffSToby Isaac 
60524d6f44ffSToby Isaac   Output Parameter:
60534d6f44ffSToby Isaac . X - vector
60544d6f44ffSToby Isaac 
60554d6f44ffSToby Isaac    Calling sequence of func:
60560709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
60574d6f44ffSToby Isaac 
60584d6f44ffSToby Isaac +  dim - The spatial dimension
60594d6f44ffSToby Isaac .  x   - The coordinates
60604d6f44ffSToby Isaac .  Nf  - The number of fields
60614d6f44ffSToby Isaac .  u   - The output field values
60624d6f44ffSToby Isaac -  ctx - optional user-defined function context
60634d6f44ffSToby Isaac 
60644d6f44ffSToby Isaac   Level: developer
60654d6f44ffSToby Isaac 
60662716604bSToby Isaac .seealso: DMComputeL2Diff()
60674d6f44ffSToby Isaac @*/
60680709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
60694d6f44ffSToby Isaac {
60704d6f44ffSToby Isaac   Vec            localX;
60714d6f44ffSToby Isaac   PetscErrorCode ierr;
60724d6f44ffSToby Isaac 
60734d6f44ffSToby Isaac   PetscFunctionBegin;
60744d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60754d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
60760709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
60774d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
60784d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
60794d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
60804d6f44ffSToby Isaac   PetscFunctionReturn(0);
60814d6f44ffSToby Isaac }
60824d6f44ffSToby Isaac 
60830709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
60844d6f44ffSToby Isaac {
60854d6f44ffSToby Isaac   PetscErrorCode ierr;
60864d6f44ffSToby Isaac 
60874d6f44ffSToby Isaac   PetscFunctionBegin;
60884d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60894d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
60904d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
60910709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
60924d6f44ffSToby Isaac   PetscFunctionReturn(0);
60934d6f44ffSToby Isaac }
60944d6f44ffSToby Isaac 
60951c531cf8SMatthew 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)
60964d6f44ffSToby Isaac {
60974d6f44ffSToby Isaac   PetscErrorCode ierr;
60984d6f44ffSToby Isaac 
60994d6f44ffSToby Isaac   PetscFunctionBegin;
61004d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61014d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
61024d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
61031c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61044d6f44ffSToby Isaac   PetscFunctionReturn(0);
61054d6f44ffSToby Isaac }
61062716604bSToby Isaac 
61078c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
61088c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
61098c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
61108c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6111191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
61128c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
61138c6c5593SMatthew G. Knepley {
61148c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
61158c6c5593SMatthew G. Knepley 
61168c6c5593SMatthew G. Knepley   PetscFunctionBegin;
61178c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61188c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
61198c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
61208c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
61218c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
61228c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
61238c6c5593SMatthew G. Knepley }
61248c6c5593SMatthew G. Knepley 
61251c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
61268c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
61278c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
61288c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6129191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
61308c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
61318c6c5593SMatthew G. Knepley {
61328c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
61338c6c5593SMatthew G. Knepley 
61348c6c5593SMatthew G. Knepley   PetscFunctionBegin;
61358c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61368c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
61378c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
61388c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
61391c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
61408c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
61418c6c5593SMatthew G. Knepley }
61428c6c5593SMatthew G. Knepley 
61432716604bSToby Isaac /*@C
61442716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
61452716604bSToby Isaac 
61462716604bSToby Isaac   Input Parameters:
61472716604bSToby Isaac + dm    - The DM
61480709b2feSToby Isaac . time  - The time
61492716604bSToby Isaac . funcs - The functions to evaluate for each field component
61502716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
61512716604bSToby Isaac - X     - The coefficient vector u_h
61522716604bSToby Isaac 
61532716604bSToby Isaac   Output Parameter:
61542716604bSToby Isaac . diff - The diff ||u - u_h||_2
61552716604bSToby Isaac 
61562716604bSToby Isaac   Level: developer
61572716604bSToby Isaac 
61581189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
61592716604bSToby Isaac @*/
61600709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
61612716604bSToby Isaac {
61622716604bSToby Isaac   PetscErrorCode ierr;
61632716604bSToby Isaac 
61642716604bSToby Isaac   PetscFunctionBegin;
61652716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6166b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
61672716604bSToby Isaac   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2Diff",((PetscObject)dm)->type_name);
61680709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
61692716604bSToby Isaac   PetscFunctionReturn(0);
61702716604bSToby Isaac }
6171b698f381SToby Isaac 
6172b698f381SToby Isaac /*@C
6173b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6174b698f381SToby Isaac 
6175b698f381SToby Isaac   Input Parameters:
6176b698f381SToby Isaac + dm    - The DM
6177b698f381SToby Isaac , time  - The time
6178b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6179b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6180b698f381SToby Isaac . X     - The coefficient vector u_h
6181b698f381SToby Isaac - n     - The vector to project along
6182b698f381SToby Isaac 
6183b698f381SToby Isaac   Output Parameter:
6184b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6185b698f381SToby Isaac 
6186b698f381SToby Isaac   Level: developer
6187b698f381SToby Isaac 
6188b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6189b698f381SToby Isaac @*/
6190b698f381SToby 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)
6191b698f381SToby Isaac {
6192b698f381SToby Isaac   PetscErrorCode ierr;
6193b698f381SToby Isaac 
6194b698f381SToby Isaac   PetscFunctionBegin;
6195b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6196b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6197b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6198b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6199b698f381SToby Isaac   PetscFunctionReturn(0);
6200b698f381SToby Isaac }
6201b698f381SToby Isaac 
62022a16baeaSToby Isaac /*@C
62032a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
62042a16baeaSToby Isaac 
62052a16baeaSToby Isaac   Input Parameters:
62062a16baeaSToby Isaac + dm    - The DM
62072a16baeaSToby Isaac . time  - The time
62082a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
62092a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
62102a16baeaSToby Isaac - X     - The coefficient vector u_h
62112a16baeaSToby Isaac 
62122a16baeaSToby Isaac   Output Parameter:
62132a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
62142a16baeaSToby Isaac 
62152a16baeaSToby Isaac   Level: developer
62162a16baeaSToby Isaac 
62171189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62182a16baeaSToby Isaac @*/
62191189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
62202a16baeaSToby Isaac {
62212a16baeaSToby Isaac   PetscErrorCode ierr;
62222a16baeaSToby Isaac 
62232a16baeaSToby Isaac   PetscFunctionBegin;
62242a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62252a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62262a16baeaSToby Isaac   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
62272a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
62282a16baeaSToby Isaac   PetscFunctionReturn(0);
62292a16baeaSToby Isaac }
62302a16baeaSToby Isaac 
6231df0b854cSToby Isaac /*@C
6232df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6233cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6234df0b854cSToby Isaac 
6235df0b854cSToby Isaac   Collective on dm
6236df0b854cSToby Isaac 
6237df0b854cSToby Isaac   Input parameters:
6238df0b854cSToby Isaac + dm - the pre-adaptation DM object
6239a1b0c543SToby Isaac - label - label with the flags
6240df0b854cSToby Isaac 
6241df0b854cSToby Isaac   Output parameters:
62420d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6243df0b854cSToby Isaac 
6244df0b854cSToby Isaac   Level: intermediate
62450d1cd5e0SMatthew G. Knepley 
62460d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6247df0b854cSToby Isaac @*/
62480d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6249df0b854cSToby Isaac {
6250df0b854cSToby Isaac   PetscErrorCode ierr;
6251df0b854cSToby Isaac 
6252df0b854cSToby Isaac   PetscFunctionBegin;
6253df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6254a1b0c543SToby Isaac   PetscValidPointer(label,2);
62550d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
62560d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
62576f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
62580d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
62590d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
62600d1cd5e0SMatthew G. Knepley }
62610d1cd5e0SMatthew G. Knepley 
62620d1cd5e0SMatthew G. Knepley /*@C
62630d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
62640d1cd5e0SMatthew G. Knepley 
62650d1cd5e0SMatthew G. Knepley   Input Parameters:
62660d1cd5e0SMatthew G. Knepley + dm - The DM object
62670d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
62686f25b0d8SLisandro 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_".
62690d1cd5e0SMatthew G. Knepley 
62700d1cd5e0SMatthew G. Knepley   Output Parameter:
62710d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
62720d1cd5e0SMatthew G. Knepley 
62730d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
62740d1cd5e0SMatthew G. Knepley 
62750d1cd5e0SMatthew G. Knepley   Level: advanced
62760d1cd5e0SMatthew G. Knepley 
62770d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
62780d1cd5e0SMatthew G. Knepley @*/
62790d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
62800d1cd5e0SMatthew G. Knepley {
62810d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
62820d1cd5e0SMatthew G. Knepley 
62830d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
62840d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62850d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
62860d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
62870d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
62886f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
62896f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
62900d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6291df0b854cSToby Isaac   PetscFunctionReturn(0);
6292df0b854cSToby Isaac }
6293c4088d22SMatthew G. Knepley 
6294502a2867SDave May /*@C
6295502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6296502a2867SDave May 
6297502a2867SDave May  Not Collective
6298502a2867SDave May 
6299502a2867SDave May  Input Parameter:
6300502a2867SDave May  . dm    - The DM
6301502a2867SDave May 
6302502a2867SDave May  Output Parameter:
6303502a2867SDave May  . nranks - the number of neighbours
6304502a2867SDave May  . ranks - the neighbors ranks
6305502a2867SDave May 
6306502a2867SDave May  Notes:
6307502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6308502a2867SDave May 
6309502a2867SDave May  Level: beginner
6310502a2867SDave May 
6311dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6312502a2867SDave May @*/
6313502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6314502a2867SDave May {
6315502a2867SDave May   PetscErrorCode ierr;
6316502a2867SDave May 
6317502a2867SDave May   PetscFunctionBegin;
6318502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6319502a2867SDave May   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMGetNeighbors",((PetscObject)dm)->type_name);
6320502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6321502a2867SDave May   PetscFunctionReturn(0);
6322502a2867SDave May }
6323502a2867SDave May 
6324531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6325531c7667SBarry Smith 
6326531c7667SBarry Smith /*
6327531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6328531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6329531c7667SBarry Smith */
6330531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6331531c7667SBarry Smith {
6332531c7667SBarry Smith   PetscErrorCode ierr;
6333531c7667SBarry Smith 
6334531c7667SBarry Smith   PetscFunctionBegin;
6335531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6336531c7667SBarry Smith     Vec x1local;
6337531c7667SBarry Smith     DM  dm;
6338531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6339531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6340531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6341531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6342531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6343531c7667SBarry Smith     x1   = x1local;
6344531c7667SBarry Smith   }
6345531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6346531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6347531c7667SBarry Smith     DM  dm;
6348531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6349531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6350531c7667SBarry Smith   }
6351531c7667SBarry Smith   PetscFunctionReturn(0);
6352531c7667SBarry Smith }
6353531c7667SBarry Smith 
6354531c7667SBarry Smith /*@
6355531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6356531c7667SBarry Smith 
6357531c7667SBarry Smith     Input Parameter:
6358531c7667SBarry Smith .    coloring - the MatFDColoring object
6359531c7667SBarry Smith 
6360531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6361531c7667SBarry Smith 
63621b266c99SBarry Smith     Level: advanced
63631b266c99SBarry Smith 
6364531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6365531c7667SBarry Smith @*/
6366531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6367531c7667SBarry Smith {
6368531c7667SBarry Smith   PetscFunctionBegin;
6369531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6370531c7667SBarry Smith   PetscFunctionReturn(0);
6371531c7667SBarry Smith }
6372