xref: /petsc/src/dm/interface/dm.c (revision 3d8e37012e427a430cfb6de3010cd73330894810)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
50c312b8eSJed Brown #include <petscsf.h>
62764a2aaSMatthew G. Knepley #include <petscds.h>
747c6ae99SBarry Smith 
8732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
91ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1067a56275SMatthew G Knepley 
11bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
12bff4a2f0SMatthew G. Knepley 
13a4121054SBarry Smith /*@
14de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
15a4121054SBarry Smith 
16a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
17a4121054SBarry Smith    error when you try to use the vector.
18a4121054SBarry Smith 
19a4121054SBarry Smith   Collective on MPI_Comm
20a4121054SBarry Smith 
21a4121054SBarry Smith   Input Parameter:
22a4121054SBarry Smith . comm - The communicator for the DM object
23a4121054SBarry Smith 
24a4121054SBarry Smith   Output Parameter:
25a4121054SBarry Smith . dm - The DM object
26a4121054SBarry Smith 
27a4121054SBarry Smith   Level: beginner
28a4121054SBarry Smith 
298472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
30a4121054SBarry Smith @*/
317087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
32a4121054SBarry Smith {
33a4121054SBarry Smith   DM             v;
34a4121054SBarry Smith   PetscErrorCode ierr;
35a4121054SBarry Smith 
36a4121054SBarry Smith   PetscFunctionBegin;
371411c6eeSJed Brown   PetscValidPointer(dm,2);
380298fd71SBarry Smith   *dm = NULL;
398b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
40607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
42607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
43a4121054SBarry Smith 
4473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
45e7c4fc90SDmitry Karpeev 
460298fd71SBarry Smith   v->ltogmap                  = NULL;
471411c6eeSJed Brown   v->bs                       = 1;
48171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
4988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
51c58f1c22SToby Isaac   v->labels                   = NULL;
52c58f1c22SToby Isaac   v->depthLabel               = NULL;
530298fd71SBarry Smith   v->defaultSection           = NULL;
540298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
55fba222abSToby Isaac   v->defaultConstraintSection = NULL;
56fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
57c6b900c6SMatthew G. Knepley   v->L                        = NULL;
58c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
595dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
609a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
61435a35e8SMatthew G Knepley   {
62435a35e8SMatthew G Knepley     PetscInt i;
63435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
640298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
65435a35e8SMatthew G Knepley     }
66435a35e8SMatthew G Knepley   }
672764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6814f150ffSMatthew G. Knepley   v->dmBC = NULL;
69a8fb8f29SToby Isaac   v->coarseMesh = NULL;
70f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
71cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
72c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
73b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
74bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
75c58f1c22SToby Isaac   v->labels->refct = 1;
761411c6eeSJed Brown   *dm = v;
77a4121054SBarry Smith   PetscFunctionReturn(0);
78a4121054SBarry Smith }
79a4121054SBarry Smith 
8038221697SMatthew G. Knepley /*@
8138221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
8238221697SMatthew G. Knepley 
8338221697SMatthew G. Knepley   Collective on MPI_Comm
8438221697SMatthew G. Knepley 
8538221697SMatthew G. Knepley   Input Parameter:
8638221697SMatthew G. Knepley . dm - The original DM object
8738221697SMatthew G. Knepley 
8838221697SMatthew G. Knepley   Output Parameter:
8938221697SMatthew G. Knepley . newdm  - The new DM object
9038221697SMatthew G. Knepley 
9138221697SMatthew G. Knepley   Level: beginner
9238221697SMatthew G. Knepley 
9338221697SMatthew G. Knepley .keywords: DM, topology, create
9438221697SMatthew G. Knepley @*/
9538221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9638221697SMatthew G. Knepley {
9738221697SMatthew G. Knepley   PetscSF        sf;
9838221697SMatthew G. Knepley   Vec            coords;
9938221697SMatthew G. Knepley   void          *ctx;
100a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
10138221697SMatthew G. Knepley   PetscErrorCode ierr;
10238221697SMatthew G. Knepley 
10338221697SMatthew G. Knepley   PetscFunctionBegin;
10438221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10538221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10638221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
107c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
108c58f1c22SToby Isaac   dm->labels->refct++;
109c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
110c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1111de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1121de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
11338221697SMatthew G. Knepley   if (dm->ops->clone) {
11438221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
11538221697SMatthew G. Knepley   }
1163f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
11738221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
11838221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
11938221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
12038221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
121be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
122be4c1c3eSMatthew G. Knepley     DM           ncdm;
123be4c1c3eSMatthew G. Knepley     PetscSection cs;
1245a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
125be4c1c3eSMatthew G. Knepley 
126be4c1c3eSMatthew G. Knepley     ierr = DMGetDefaultSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
127be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1285a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1295a0206caSToby Isaac     if (pEndMax >= 0) {
130be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
13113bffc4cSMatthew G. Knepley       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
132a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
133be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
134be4c1c3eSMatthew G. Knepley     }
135be4c1c3eSMatthew G. Knepley   }
136a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
137a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
13838221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   if (coords) {
14038221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   } else {
14238221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
14338221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
14438221697SMatthew G. Knepley   }
14590b157c4SStefano Zampini   {
14690b157c4SStefano Zampini     PetscBool             isper;
147c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1485dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
14990b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
15090b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
151c6b900c6SMatthew G. Knepley   }
15238221697SMatthew G. Knepley   PetscFunctionReturn(0);
15338221697SMatthew G. Knepley }
15438221697SMatthew G. Knepley 
1559a42bb27SBarry Smith /*@C
156564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1579a42bb27SBarry Smith 
1588472ad0fSDave May    Logically Collective on DM
1599a42bb27SBarry Smith 
1609a42bb27SBarry Smith    Input Parameter:
1619a42bb27SBarry Smith +  da - initial distributed array
1628154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1639a42bb27SBarry Smith 
1649a42bb27SBarry Smith    Options Database:
165dd85299cSBarry Smith .   -dm_vec_type ctype
1669a42bb27SBarry Smith 
1679a42bb27SBarry Smith    Level: intermediate
1689a42bb27SBarry Smith 
1698472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1709a42bb27SBarry Smith @*/
17119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1729a42bb27SBarry Smith {
1739a42bb27SBarry Smith   PetscErrorCode ierr;
1749a42bb27SBarry Smith 
1759a42bb27SBarry Smith   PetscFunctionBegin;
1769a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1779a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
17819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1799a42bb27SBarry Smith   PetscFunctionReturn(0);
1809a42bb27SBarry Smith }
1819a42bb27SBarry Smith 
182c0dedaeaSBarry Smith /*@C
183c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
184c0dedaeaSBarry Smith 
1858472ad0fSDave May    Logically Collective on DM
186c0dedaeaSBarry Smith 
187c0dedaeaSBarry Smith    Input Parameter:
188c0dedaeaSBarry Smith .  da - initial distributed array
189c0dedaeaSBarry Smith 
190c0dedaeaSBarry Smith    Output Parameter:
191c0dedaeaSBarry Smith .  ctype - the vector type
192c0dedaeaSBarry Smith 
193c0dedaeaSBarry Smith    Level: intermediate
194c0dedaeaSBarry Smith 
1958472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
196c0dedaeaSBarry Smith @*/
197c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
198c0dedaeaSBarry Smith {
199c0dedaeaSBarry Smith   PetscFunctionBegin;
200c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
201c0dedaeaSBarry Smith   *ctype = da->vectype;
202c0dedaeaSBarry Smith   PetscFunctionReturn(0);
203c0dedaeaSBarry Smith }
204c0dedaeaSBarry Smith 
2055f1ad066SMatthew G Knepley /*@
20634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2075f1ad066SMatthew G Knepley 
2085f1ad066SMatthew G Knepley   Not collective
2095f1ad066SMatthew G Knepley 
2105f1ad066SMatthew G Knepley   Input Parameter:
2115f1ad066SMatthew G Knepley . v - The Vec
2125f1ad066SMatthew G Knepley 
2135f1ad066SMatthew G Knepley   Output Parameter:
2145f1ad066SMatthew G Knepley . dm - The DM
2155f1ad066SMatthew G Knepley 
2165f1ad066SMatthew G Knepley   Level: intermediate
2175f1ad066SMatthew G Knepley 
2185f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2195f1ad066SMatthew G Knepley @*/
2205f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2215f1ad066SMatthew G Knepley {
2225f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2235f1ad066SMatthew G Knepley 
2245f1ad066SMatthew G Knepley   PetscFunctionBegin;
2255f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2265f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2275f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2285f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2295f1ad066SMatthew G Knepley }
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley /*@
232d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2335f1ad066SMatthew G Knepley 
2345f1ad066SMatthew G Knepley   Not collective
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   Input Parameters:
2375f1ad066SMatthew G Knepley + v - The Vec
2385f1ad066SMatthew G Knepley - dm - The DM
2395f1ad066SMatthew G Knepley 
240d9805387SMatthew G. Knepley   Note: This is NOT the same as DMCreateGlobalVector() since it does not change the view methods or perform other customization, but merely sets the DM member.
241d9805387SMatthew G. Knepley 
2425f1ad066SMatthew G Knepley   Level: intermediate
2435f1ad066SMatthew G Knepley 
2445f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2455f1ad066SMatthew G Knepley @*/
2465f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2475f1ad066SMatthew G Knepley {
2485f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2495f1ad066SMatthew G Knepley 
2505f1ad066SMatthew G Knepley   PetscFunctionBegin;
2515f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
252d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2535f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2545f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2555f1ad066SMatthew G Knepley }
2565f1ad066SMatthew G Knepley 
257521d9a4cSLisandro Dalcin /*@C
258521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
259521d9a4cSLisandro Dalcin 
260521d9a4cSLisandro Dalcin    Logically Collective on DM
261521d9a4cSLisandro Dalcin 
262521d9a4cSLisandro Dalcin    Input Parameter:
263521d9a4cSLisandro Dalcin +  dm - the DM context
264a2b5a043SBarry Smith -  ctype - the matrix type
265521d9a4cSLisandro Dalcin 
266521d9a4cSLisandro Dalcin    Options Database:
267521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
268521d9a4cSLisandro Dalcin 
269521d9a4cSLisandro Dalcin    Level: intermediate
270521d9a4cSLisandro Dalcin 
271c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
272521d9a4cSLisandro Dalcin @*/
27319fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
274521d9a4cSLisandro Dalcin {
275521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
27688f0584fSBarry Smith 
277521d9a4cSLisandro Dalcin   PetscFunctionBegin;
278521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
279521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
28019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
281521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
282521d9a4cSLisandro Dalcin }
283521d9a4cSLisandro Dalcin 
284c0dedaeaSBarry Smith /*@C
285c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
286c0dedaeaSBarry Smith 
287c0dedaeaSBarry Smith    Logically Collective on DM
288c0dedaeaSBarry Smith 
289c0dedaeaSBarry Smith    Input Parameter:
290c0dedaeaSBarry Smith .  dm - the DM context
291c0dedaeaSBarry Smith 
292c0dedaeaSBarry Smith    Output Parameter:
293c0dedaeaSBarry Smith .  ctype - the matrix type
294c0dedaeaSBarry Smith 
295c0dedaeaSBarry Smith    Options Database:
296c0dedaeaSBarry Smith .   -dm_mat_type ctype
297c0dedaeaSBarry Smith 
298c0dedaeaSBarry Smith    Level: intermediate
299c0dedaeaSBarry Smith 
300c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
301c0dedaeaSBarry Smith @*/
302c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
303c0dedaeaSBarry Smith {
304c0dedaeaSBarry Smith   PetscFunctionBegin;
305c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
306c0dedaeaSBarry Smith   *ctype = dm->mattype;
307c0dedaeaSBarry Smith   PetscFunctionReturn(0);
308c0dedaeaSBarry Smith }
309c0dedaeaSBarry Smith 
310c688c046SMatthew G Knepley /*@
31134f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
312c688c046SMatthew G Knepley 
313c688c046SMatthew G Knepley   Not collective
314c688c046SMatthew G Knepley 
315c688c046SMatthew G Knepley   Input Parameter:
316c688c046SMatthew G Knepley . A - The Mat
317c688c046SMatthew G Knepley 
318c688c046SMatthew G Knepley   Output Parameter:
319c688c046SMatthew G Knepley . dm - The DM
320c688c046SMatthew G Knepley 
321c688c046SMatthew G Knepley   Level: intermediate
322c688c046SMatthew G Knepley 
323c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
324c688c046SMatthew G Knepley @*/
325c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
326c688c046SMatthew G Knepley {
327c688c046SMatthew G Knepley   PetscErrorCode ierr;
328c688c046SMatthew G Knepley 
329c688c046SMatthew G Knepley   PetscFunctionBegin;
330c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
331c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
332c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
333c688c046SMatthew G Knepley   PetscFunctionReturn(0);
334c688c046SMatthew G Knepley }
335c688c046SMatthew G Knepley 
336c688c046SMatthew G Knepley /*@
337c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
338c688c046SMatthew G Knepley 
339c688c046SMatthew G Knepley   Not collective
340c688c046SMatthew G Knepley 
341c688c046SMatthew G Knepley   Input Parameters:
342c688c046SMatthew G Knepley + A - The Mat
343c688c046SMatthew G Knepley - dm - The DM
344c688c046SMatthew G Knepley 
345c688c046SMatthew G Knepley   Level: intermediate
346c688c046SMatthew G Knepley 
347c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
348c688c046SMatthew G Knepley @*/
349c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
350c688c046SMatthew G Knepley {
351c688c046SMatthew G Knepley   PetscErrorCode ierr;
352c688c046SMatthew G Knepley 
353c688c046SMatthew G Knepley   PetscFunctionBegin;
354c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3558865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
356c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
357c688c046SMatthew G Knepley   PetscFunctionReturn(0);
358c688c046SMatthew G Knepley }
359c688c046SMatthew G Knepley 
3609a42bb27SBarry Smith /*@C
3619a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3626757b960SDave May    DM options in the database.
3639a42bb27SBarry Smith 
3648353ddbbSDave May    Logically Collective on DM
3659a42bb27SBarry Smith 
3669a42bb27SBarry Smith    Input Parameter:
3678353ddbbSDave May +  da - the DM context
3689a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3699a42bb27SBarry Smith 
3709a42bb27SBarry Smith    Notes:
3719a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3729a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3739a42bb27SBarry Smith 
3749a42bb27SBarry Smith    Level: advanced
3759a42bb27SBarry Smith 
3768353ddbbSDave May .keywords: DM, set, options, prefix, database
3779a42bb27SBarry Smith 
3789a42bb27SBarry Smith .seealso: DMSetFromOptions()
3799a42bb27SBarry Smith @*/
3807087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3819a42bb27SBarry Smith {
3829a42bb27SBarry Smith   PetscErrorCode ierr;
3839a42bb27SBarry Smith 
3849a42bb27SBarry Smith   PetscFunctionBegin;
3859a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3869a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
387691be533SLawrence Mitchell   if (dm->sf) {
388691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
389691be533SLawrence Mitchell   }
390691be533SLawrence Mitchell   if (dm->defaultSF) {
391691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
392691be533SLawrence Mitchell   }
3939a42bb27SBarry Smith   PetscFunctionReturn(0);
3949a42bb27SBarry Smith }
3959a42bb27SBarry Smith 
39631697293SDave May /*@C
39731697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
39831697293SDave May    DM options in the database.
39931697293SDave May 
40031697293SDave May    Logically Collective on DM
40131697293SDave May 
40231697293SDave May    Input Parameters:
40331697293SDave May +  dm - the DM context
40431697293SDave May -  prefix - the prefix string to prepend to all DM option requests
40531697293SDave May 
40631697293SDave May    Notes:
40731697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
40831697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
40931697293SDave May 
41031697293SDave May    Level: advanced
41131697293SDave May 
41231697293SDave May .keywords: DM, append, options, prefix, database
41331697293SDave May 
41431697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
41531697293SDave May @*/
41631697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
41731697293SDave May {
41831697293SDave May   PetscErrorCode ierr;
41931697293SDave May 
42031697293SDave May   PetscFunctionBegin;
42131697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42231697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
42331697293SDave May   PetscFunctionReturn(0);
42431697293SDave May }
42531697293SDave May 
42631697293SDave May /*@C
42731697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
42831697293SDave May    DM options in the database.
42931697293SDave May 
43031697293SDave May    Not Collective
43131697293SDave May 
43231697293SDave May    Input Parameters:
43331697293SDave May .  dm - the DM context
43431697293SDave May 
43531697293SDave May    Output Parameters:
43631697293SDave May .  prefix - pointer to the prefix string used is returned
43731697293SDave May 
43831697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
43931697293SDave May    sufficient length to hold the prefix.
44031697293SDave May 
44131697293SDave May    Level: advanced
44231697293SDave May 
44331697293SDave May .keywords: DM, set, options, prefix, database
44431697293SDave May 
44531697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
44631697293SDave May @*/
44731697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
44831697293SDave May {
44931697293SDave May   PetscErrorCode ierr;
45031697293SDave May 
45131697293SDave May   PetscFunctionBegin;
45231697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45331697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
45431697293SDave May   PetscFunctionReturn(0);
45531697293SDave May }
45631697293SDave May 
45788bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
45888bdff64SToby Isaac {
45988bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
46088bdff64SToby Isaac   DMNamedVecLink nlink;
46188bdff64SToby Isaac   PetscErrorCode ierr;
46288bdff64SToby Isaac 
46388bdff64SToby Isaac   PetscFunctionBegin;
464aab5bcd8SJed Brown   *ncrefct = 0;
46588bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
46688bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
46788bdff64SToby Isaac     if (dm->localin[i])  refct--;
46888bdff64SToby Isaac     if (dm->globalin[i]) refct--;
46988bdff64SToby Isaac   }
47088bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
47188bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
47288bdff64SToby Isaac   if (dm->x) {
47388bdff64SToby Isaac     DM obj;
47488bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
47588bdff64SToby Isaac     if (obj == dm) refct--;
47688bdff64SToby Isaac   }
47788bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
47888bdff64SToby Isaac     refct--;
47988bdff64SToby Isaac     if (recurseCoarse) {
48088bdff64SToby Isaac       PetscInt coarseCount;
48188bdff64SToby Isaac 
48288bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
48388bdff64SToby Isaac       refct += coarseCount;
48488bdff64SToby Isaac     }
48588bdff64SToby Isaac   }
48688bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
48788bdff64SToby Isaac     refct--;
48888bdff64SToby Isaac     if (recurseFine) {
48988bdff64SToby Isaac       PetscInt fineCount;
49088bdff64SToby Isaac 
49188bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
49288bdff64SToby Isaac       refct += fineCount;
49388bdff64SToby Isaac     }
49488bdff64SToby Isaac   }
49588bdff64SToby Isaac   *ncrefct = refct;
49688bdff64SToby Isaac   PetscFunctionReturn(0);
49788bdff64SToby Isaac }
49888bdff64SToby Isaac 
499354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
500354557abSToby Isaac {
501354557abSToby Isaac   PetscErrorCode ierr;
502354557abSToby Isaac 
503354557abSToby Isaac   PetscFunctionBegin;
504354557abSToby Isaac   if (!--(dm->labels->refct)) {
505354557abSToby Isaac     DMLabelLink next = dm->labels->next;
506354557abSToby Isaac 
507354557abSToby Isaac     /* destroy the labels */
508354557abSToby Isaac     while (next) {
509354557abSToby Isaac       DMLabelLink tmp = next->next;
510354557abSToby Isaac 
511354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
512354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
513354557abSToby Isaac       next = tmp;
514354557abSToby Isaac     }
515354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
516354557abSToby Isaac   }
517354557abSToby Isaac   PetscFunctionReturn(0);
518354557abSToby Isaac }
519354557abSToby Isaac 
52047c6ae99SBarry Smith /*@
5218472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
52247c6ae99SBarry Smith 
52347c6ae99SBarry Smith     Collective on DM
52447c6ae99SBarry Smith 
52547c6ae99SBarry Smith     Input Parameter:
52647c6ae99SBarry Smith .   dm - the DM object to destroy
52747c6ae99SBarry Smith 
52847c6ae99SBarry Smith     Level: developer
52947c6ae99SBarry Smith 
530e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
53147c6ae99SBarry Smith 
53247c6ae99SBarry Smith @*/
533fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
53447c6ae99SBarry Smith {
53588bdff64SToby Isaac   PetscInt       i, cnt;
536dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
53747c6ae99SBarry Smith   PetscErrorCode ierr;
53847c6ae99SBarry Smith 
53947c6ae99SBarry Smith   PetscFunctionBegin;
5406bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5416bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
54287e657c6SBarry Smith 
54388bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
54488bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
54588bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
54688bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
547732e2eb9SMatthew G Knepley   /*
548732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
549732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
550732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
551732e2eb9SMatthew G Knepley   */
5526bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5536bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
554732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5556bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5566bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
557732e2eb9SMatthew G Knepley   }
558f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5590298fd71SBarry Smith   (*dm)->namedglobal = NULL;
560f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5612348bcf4SPeter Brune     nnext = nlink->next;
5622348bcf4SPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
5632348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5642348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5652348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5662348bcf4SPeter Brune   }
567f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5680298fd71SBarry Smith   (*dm)->namedlocal = NULL;
569f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
570f490541aSPeter Brune     nnext = nlink->next;
571f490541aSPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
572f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
573f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
574f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
575f490541aSPeter Brune   }
5762348bcf4SPeter Brune 
577b17ce1afSJed Brown   /* Destroy the list of hooks */
578c833c3b5SJed Brown   {
579c833c3b5SJed Brown     DMCoarsenHookLink link,next;
580b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
581b17ce1afSJed Brown       next = link->next;
582b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
583b17ce1afSJed Brown     }
5840298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
585c833c3b5SJed Brown   }
586c833c3b5SJed Brown   {
587c833c3b5SJed Brown     DMRefineHookLink link,next;
588c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
589c833c3b5SJed Brown       next = link->next;
590c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
591c833c3b5SJed Brown     }
5920298fd71SBarry Smith     (*dm)->refinehook = NULL;
593c833c3b5SJed Brown   }
594be081cd6SPeter Brune   {
595be081cd6SPeter Brune     DMSubDomainHookLink link,next;
596be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
597be081cd6SPeter Brune       next = link->next;
598be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
599be081cd6SPeter Brune     }
6000298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
601be081cd6SPeter Brune   }
602baf369e7SPeter Brune   {
603baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
604baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
605baf369e7SPeter Brune       next = link->next;
606baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
607baf369e7SPeter Brune     }
6080298fd71SBarry Smith     (*dm)->gtolhook = NULL;
609baf369e7SPeter Brune   }
610d4d07f1eSToby Isaac   {
611d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
612d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
613d4d07f1eSToby Isaac       next = link->next;
614d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
615d4d07f1eSToby Isaac     }
616d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
617d4d07f1eSToby Isaac   }
618aa1993deSMatthew G Knepley   /* Destroy the work arrays */
619aa1993deSMatthew G Knepley   {
620aa1993deSMatthew G Knepley     DMWorkLink link,next;
621aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
622aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
623aa1993deSMatthew G Knepley       next = link->next;
624aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
625aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
626aa1993deSMatthew G Knepley     }
6270298fd71SBarry Smith     (*dm)->workin = NULL;
628aa1993deSMatthew G Knepley   }
629c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
630c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
631c58f1c22SToby Isaac 
632c58f1c22SToby Isaac     /* destroy the labels */
633c58f1c22SToby Isaac     while (next) {
634c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
635c58f1c22SToby Isaac 
636c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
637c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
638c58f1c22SToby Isaac       next = tmp;
639c58f1c22SToby Isaac     }
640c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
641c58f1c22SToby Isaac   }
642e6f8dbb6SToby Isaac   {
643e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
644e6f8dbb6SToby Isaac     while (next) {
645e6f8dbb6SToby Isaac       DMBoundary b = next;
646e6f8dbb6SToby Isaac 
647e6f8dbb6SToby Isaac       next = b->next;
648e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
649e6f8dbb6SToby Isaac     }
650e6f8dbb6SToby Isaac   }
651b17ce1afSJed Brown 
65252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
65352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
65452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
65552536dc3SBarry Smith 
6561a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6571a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6581a266240SBarry Smith   }
65987e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
66071cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6614dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6626bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6636bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
664073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
66588ed4aceSMatthew G Knepley 
66688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
66788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6688b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
669fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
670fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
67188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
67288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6738e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
674af122d2aSMatthew G Knepley 
67588bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
67688bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
67788bdff64SToby Isaac   }
678a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
67988bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
68088bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
68188bdff64SToby Isaac   }
68288bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
6836636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6846636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6856636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6865dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6876636e97aSMatthew G Knepley 
6882764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
68914f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
690e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
691e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
692732e2eb9SMatthew G Knepley 
6936bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
694435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
695732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
69647c6ae99SBarry Smith   PetscFunctionReturn(0);
69747c6ae99SBarry Smith }
69847c6ae99SBarry Smith 
699d7bf68aeSBarry Smith /*@
700d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
701d7bf68aeSBarry Smith 
702d7bf68aeSBarry Smith     Collective on DM
703d7bf68aeSBarry Smith 
704d7bf68aeSBarry Smith     Input Parameter:
705d7bf68aeSBarry Smith .   dm - the DM object to setup
706d7bf68aeSBarry Smith 
707d7bf68aeSBarry Smith     Level: developer
708d7bf68aeSBarry Smith 
709e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
710d7bf68aeSBarry Smith 
711d7bf68aeSBarry Smith @*/
7127087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
713d7bf68aeSBarry Smith {
714d7bf68aeSBarry Smith   PetscErrorCode ierr;
715d7bf68aeSBarry Smith 
716d7bf68aeSBarry Smith   PetscFunctionBegin;
717171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7188387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
719d7bf68aeSBarry Smith   if (dm->ops->setup) {
720d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
721d7bf68aeSBarry Smith   }
7228387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
723d7bf68aeSBarry Smith   PetscFunctionReturn(0);
724d7bf68aeSBarry Smith }
725d7bf68aeSBarry Smith 
726d7bf68aeSBarry Smith /*@
727d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
728d7bf68aeSBarry Smith 
729d7bf68aeSBarry Smith     Collective on DM
730d7bf68aeSBarry Smith 
731d7bf68aeSBarry Smith     Input Parameter:
732d7bf68aeSBarry Smith .   dm - the DM object to set options for
733d7bf68aeSBarry Smith 
734732e2eb9SMatthew G Knepley     Options Database:
7354164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7364164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7374164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7384164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
739732e2eb9SMatthew G Knepley 
740d7bf68aeSBarry Smith     Level: developer
741d7bf68aeSBarry Smith 
742e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
743d7bf68aeSBarry Smith 
744d7bf68aeSBarry Smith @*/
7457087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
746d7bf68aeSBarry Smith {
7477781c08eSBarry Smith   char           typeName[256];
748ca266f36SBarry Smith   PetscBool      flg;
749d7bf68aeSBarry Smith   PetscErrorCode ierr;
750d7bf68aeSBarry Smith 
751d7bf68aeSBarry Smith   PetscFunctionBegin;
752171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
753f1fd5e65SToby Isaac   if (dm->prob) {
754f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
755f1fd5e65SToby Isaac   }
756691be533SLawrence Mitchell   if (dm->sf) {
757691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
758691be533SLawrence Mitchell   }
759691be533SLawrence Mitchell   if (dm->defaultSF) {
760691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
761691be533SLawrence Mitchell   }
7623194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7630298fd71SBarry Smith   ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL);CHKERRQ(ierr);
764a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
765f9ba7244SBarry Smith   if (flg) {
766f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
767f9ba7244SBarry Smith   }
768a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
769073dac72SJed Brown   if (flg) {
770521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
771073dac72SJed Brown   }
7720298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
773f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
774e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
775f9ba7244SBarry Smith   }
776f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
7770633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
77882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
779d7bf68aeSBarry Smith   PetscFunctionReturn(0);
780d7bf68aeSBarry Smith }
781d7bf68aeSBarry Smith 
782fc9bc008SSatish Balay /*@C
783224748a4SBarry Smith     DMView - Views a DM
78447c6ae99SBarry Smith 
78547c6ae99SBarry Smith     Collective on DM
78647c6ae99SBarry Smith 
78747c6ae99SBarry Smith     Input Parameter:
78847c6ae99SBarry Smith +   dm - the DM object to view
78947c6ae99SBarry Smith -   v - the viewer
79047c6ae99SBarry Smith 
791224748a4SBarry Smith     Level: beginner
79247c6ae99SBarry Smith 
793e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
79447c6ae99SBarry Smith 
79547c6ae99SBarry Smith @*/
7967087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
79747c6ae99SBarry Smith {
79847c6ae99SBarry Smith   PetscErrorCode ierr;
79932c0f0efSBarry Smith   PetscBool      isbinary;
80047c6ae99SBarry Smith 
80147c6ae99SBarry Smith   PetscFunctionBegin;
802171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8033014e516SBarry Smith   if (!v) {
804ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8053014e516SBarry Smith   }
80698c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
80732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
80832c0f0efSBarry Smith   if (isbinary) {
80955849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
81032c0f0efSBarry Smith     char     type[256];
81132c0f0efSBarry Smith 
81232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
81332c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
81432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
81532c0f0efSBarry Smith   }
8160c010503SBarry Smith   if (dm->ops->view) {
8170c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
81847c6ae99SBarry Smith   }
81947c6ae99SBarry Smith   PetscFunctionReturn(0);
82047c6ae99SBarry Smith }
82147c6ae99SBarry Smith 
82247c6ae99SBarry Smith /*@
8238472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
82447c6ae99SBarry Smith 
82547c6ae99SBarry Smith     Collective on DM
82647c6ae99SBarry Smith 
82747c6ae99SBarry Smith     Input Parameter:
82847c6ae99SBarry Smith .   dm - the DM object
82947c6ae99SBarry Smith 
83047c6ae99SBarry Smith     Output Parameter:
83147c6ae99SBarry Smith .   vec - the global vector
83247c6ae99SBarry Smith 
833073dac72SJed Brown     Level: beginner
83447c6ae99SBarry Smith 
835e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith @*/
8387087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
83947c6ae99SBarry Smith {
84047c6ae99SBarry Smith   PetscErrorCode ierr;
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith   PetscFunctionBegin;
843171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
84547c6ae99SBarry Smith   PetscFunctionReturn(0);
84647c6ae99SBarry Smith }
84747c6ae99SBarry Smith 
84847c6ae99SBarry Smith /*@
8498472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
85047c6ae99SBarry Smith 
85147c6ae99SBarry Smith     Not Collective
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith     Input Parameter:
85447c6ae99SBarry Smith .   dm - the DM object
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith     Output Parameter:
85747c6ae99SBarry Smith .   vec - the local vector
85847c6ae99SBarry Smith 
859073dac72SJed Brown     Level: beginner
86047c6ae99SBarry Smith 
861e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith @*/
8647087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
86547c6ae99SBarry Smith {
86647c6ae99SBarry Smith   PetscErrorCode ierr;
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith   PetscFunctionBegin;
869171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87047c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
87147c6ae99SBarry Smith   PetscFunctionReturn(0);
87247c6ae99SBarry Smith }
87347c6ae99SBarry Smith 
8741411c6eeSJed Brown /*@
8751411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8761411c6eeSJed Brown 
8771411c6eeSJed Brown    Collective on DM
8781411c6eeSJed Brown 
8791411c6eeSJed Brown    Input Parameter:
8801411c6eeSJed Brown .  dm - the DM that provides the mapping
8811411c6eeSJed Brown 
8821411c6eeSJed Brown    Output Parameter:
8831411c6eeSJed Brown .  ltog - the mapping
8841411c6eeSJed Brown 
8851411c6eeSJed Brown    Level: intermediate
8861411c6eeSJed Brown 
8871411c6eeSJed Brown    Notes:
8881411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8891411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8901411c6eeSJed Brown 
891fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8921411c6eeSJed Brown @*/
8937087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8941411c6eeSJed Brown {
895bff27382SMatthew G. Knepley   PetscInt       bs = -1, bsLocal, bsMin, bsMax;
8961411c6eeSJed Brown   PetscErrorCode ierr;
8971411c6eeSJed Brown 
8981411c6eeSJed Brown   PetscFunctionBegin;
8991411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9001411c6eeSJed Brown   PetscValidPointer(ltog,2);
9011411c6eeSJed Brown   if (!dm->ltogmap) {
90237d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
90337d0c07bSMatthew G Knepley 
90437d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
90537d0c07bSMatthew G Knepley     if (section) {
906a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
90737d0c07bSMatthew G Knepley       PetscInt       *ltog;
908ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
90937d0c07bSMatthew G Knepley 
91037d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
91137d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
912ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
913ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
91437d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
915a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
91637d0c07bSMatthew G Knepley 
91737d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
91837d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
919a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
920a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
92137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
9221a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
9231a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
9241a7dc684SMatthew G. Knepley         if (dof) {
925b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
926b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
9271a7dc684SMatthew G. Knepley         }
92837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
929a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
930a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
93137d0c07bSMatthew G Knepley         }
93237d0c07bSMatthew G Knepley       }
933bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
934bff27382SMatthew G. Knepley       bsLocal = bs;
935bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
936bff27382SMatthew G. Knepley       bsLocal = bs < 0 ? bsMax : bs;
937bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
938bff27382SMatthew G. Knepley       if (bsMin != bsMax) {bs = 1;}
939bff27382SMatthew G. Knepley       else                {bs = bsMax;}
9407591dbb2SMatthew G. Knepley       bs   = bs < 0 ? 1 : bs;
9417591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
942ccf3bd66SMatthew G. Knepley       if (bs > 1) {
943ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
944ccf3bd66SMatthew G. Knepley         n /= bs;
945ccf3bd66SMatthew G. Knepley       }
946ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9473bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
94837d0c07bSMatthew G Knepley     } else {
949184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
950184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9511411c6eeSJed Brown     }
95237d0c07bSMatthew G Knepley   }
9531411c6eeSJed Brown   *ltog = dm->ltogmap;
9541411c6eeSJed Brown   PetscFunctionReturn(0);
9551411c6eeSJed Brown }
9561411c6eeSJed Brown 
9571411c6eeSJed Brown /*@
9581411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9591411c6eeSJed Brown 
9601411c6eeSJed Brown    Not Collective
9611411c6eeSJed Brown 
9621411c6eeSJed Brown    Input Parameter:
9631411c6eeSJed Brown .  dm - the DM with block structure
9641411c6eeSJed Brown 
9651411c6eeSJed Brown    Output Parameter:
9661411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9671411c6eeSJed Brown 
9681411c6eeSJed Brown    Level: intermediate
9691411c6eeSJed Brown 
970fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9711411c6eeSJed Brown @*/
9727087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9731411c6eeSJed Brown {
9741411c6eeSJed Brown   PetscFunctionBegin;
9751411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9761411c6eeSJed Brown   PetscValidPointer(bs,2);
9771411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
9781411c6eeSJed Brown   *bs = dm->bs;
9791411c6eeSJed Brown   PetscFunctionReturn(0);
9801411c6eeSJed Brown }
9811411c6eeSJed Brown 
98247c6ae99SBarry Smith /*@
9838472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith     Collective on DM
98647c6ae99SBarry Smith 
98747c6ae99SBarry Smith     Input Parameter:
98847c6ae99SBarry Smith +   dm1 - the DM object
98947c6ae99SBarry Smith -   dm2 - the second, finer DM object
99047c6ae99SBarry Smith 
99147c6ae99SBarry Smith     Output Parameter:
99247c6ae99SBarry Smith +  mat - the interpolation
99347c6ae99SBarry Smith -  vec - the scaling (optional)
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith     Level: developer
99647c6ae99SBarry Smith 
99785afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
99885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
999d52bd9f3SBarry Smith 
10001f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1001d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
100285afcc9aSBarry Smith 
100385afcc9aSBarry Smith 
10043ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith @*/
1007e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
100847c6ae99SBarry Smith {
100947c6ae99SBarry Smith   PetscErrorCode ierr;
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith   PetscFunctionBegin;
1012171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1013171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1014cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
101525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1016cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
101747c6ae99SBarry Smith   PetscFunctionReturn(0);
101847c6ae99SBarry Smith }
101947c6ae99SBarry Smith 
10203ad4599aSBarry Smith /*@
10213ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10223ad4599aSBarry Smith 
10233ad4599aSBarry Smith     Collective on DM
10243ad4599aSBarry Smith 
10253ad4599aSBarry Smith     Input Parameter:
10263ad4599aSBarry Smith +   dm1 - the DM object
10273ad4599aSBarry Smith -   dm2 - the second, finer DM object
10283ad4599aSBarry Smith 
10293ad4599aSBarry Smith     Output Parameter:
10303ad4599aSBarry Smith .  mat - the restriction
10313ad4599aSBarry Smith 
10323ad4599aSBarry Smith 
10333ad4599aSBarry Smith     Level: developer
10343ad4599aSBarry Smith 
10353ad4599aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
10363ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10373ad4599aSBarry Smith 
10383ad4599aSBarry Smith 
10393ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10403ad4599aSBarry Smith 
10413ad4599aSBarry Smith @*/
10423ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10433ad4599aSBarry Smith {
10443ad4599aSBarry Smith   PetscErrorCode ierr;
10453ad4599aSBarry Smith 
10463ad4599aSBarry Smith   PetscFunctionBegin;
10473ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10483ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10493ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10507294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
10513ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10523ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10533ad4599aSBarry Smith   PetscFunctionReturn(0);
10543ad4599aSBarry Smith }
10553ad4599aSBarry Smith 
105647c6ae99SBarry Smith /*@
10578472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
105847c6ae99SBarry Smith 
105947c6ae99SBarry Smith     Collective on DM
106047c6ae99SBarry Smith 
106147c6ae99SBarry Smith     Input Parameter:
106247c6ae99SBarry Smith +   dm1 - the DM object
106347c6ae99SBarry Smith -   dm2 - the second, finer DM object
106447c6ae99SBarry Smith 
106547c6ae99SBarry Smith     Output Parameter:
10666dbf9973SLawrence Mitchell .   mat - the injection
106747c6ae99SBarry Smith 
106847c6ae99SBarry Smith     Level: developer
106947c6ae99SBarry Smith 
107085afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
107185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
107285afcc9aSBarry Smith 
1073e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
107447c6ae99SBarry Smith 
107547c6ae99SBarry Smith @*/
10766dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
107747c6ae99SBarry Smith {
107847c6ae99SBarry Smith   PetscErrorCode ierr;
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith   PetscFunctionBegin;
1081171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1082171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10830c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
10846dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
108547c6ae99SBarry Smith   PetscFunctionReturn(0);
108647c6ae99SBarry Smith }
108747c6ae99SBarry Smith 
1088b412c318SBarry Smith /*@
1089b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
109047c6ae99SBarry Smith 
109147c6ae99SBarry Smith     Collective on DM
109247c6ae99SBarry Smith 
109347c6ae99SBarry Smith     Input Parameter:
109447c6ae99SBarry Smith +   dm - the DM object
10955bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith     Output Parameter:
109847c6ae99SBarry Smith .   coloring - the coloring
109947c6ae99SBarry Smith 
110047c6ae99SBarry Smith     Level: developer
110147c6ae99SBarry Smith 
1102b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
110347c6ae99SBarry Smith 
1104aab9d709SJed Brown @*/
1105b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
110647c6ae99SBarry Smith {
110747c6ae99SBarry Smith   PetscErrorCode ierr;
110847c6ae99SBarry Smith 
110947c6ae99SBarry Smith   PetscFunctionBegin;
1110171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1111ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1112b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
111347c6ae99SBarry Smith   PetscFunctionReturn(0);
111447c6ae99SBarry Smith }
111547c6ae99SBarry Smith 
1116b412c318SBarry Smith /*@
11178472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
111847c6ae99SBarry Smith 
111947c6ae99SBarry Smith     Collective on DM
112047c6ae99SBarry Smith 
112147c6ae99SBarry Smith     Input Parameter:
1122b412c318SBarry Smith .   dm - the DM object
112347c6ae99SBarry Smith 
112447c6ae99SBarry Smith     Output Parameter:
112547c6ae99SBarry Smith .   mat - the empty Jacobian
112647c6ae99SBarry Smith 
1127073dac72SJed Brown     Level: beginner
112847c6ae99SBarry Smith 
112994013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
113094013140SBarry Smith        do not need to do it yourself.
113194013140SBarry Smith 
113294013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
11337889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
113494013140SBarry Smith 
113594013140SBarry 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
113694013140SBarry Smith        internally by PETSc.
113794013140SBarry Smith 
113894013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1139aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
114094013140SBarry Smith 
1141b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
114247c6ae99SBarry Smith 
1143aab9d709SJed Brown @*/
1144b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
114547c6ae99SBarry Smith {
114647c6ae99SBarry Smith   PetscErrorCode ierr;
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith   PetscFunctionBegin;
1149171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1150607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1151c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1152c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1153b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
115447c6ae99SBarry Smith   PetscFunctionReturn(0);
115547c6ae99SBarry Smith }
115647c6ae99SBarry Smith 
1157732e2eb9SMatthew G Knepley /*@
1158950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1159732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1160732e2eb9SMatthew G Knepley 
11618472ad0fSDave May   Logically Collective on DM
1162732e2eb9SMatthew G Knepley 
1163732e2eb9SMatthew G Knepley   Input Parameter:
1164732e2eb9SMatthew G Knepley + dm - the DM
1165732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1166732e2eb9SMatthew G Knepley 
1167732e2eb9SMatthew G Knepley   Level: developer
1168b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1169732e2eb9SMatthew G Knepley @*/
1170732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1171732e2eb9SMatthew G Knepley {
1172732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1173732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1174732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1175732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1176732e2eb9SMatthew G Knepley }
1177732e2eb9SMatthew G Knepley 
1178b06ff27eSHong Zhang /*@
1179b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1180b06ff27eSHong Zhang     but the array for values will not be allocated.
1181b06ff27eSHong Zhang 
1182b06ff27eSHong Zhang   Logically Collective on DM
1183b06ff27eSHong Zhang 
1184b06ff27eSHong Zhang   Input Parameter:
1185b06ff27eSHong Zhang + dm - the DM
1186b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1187b06ff27eSHong Zhang 
1188b06ff27eSHong Zhang   Level: developer
1189b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1190b06ff27eSHong Zhang @*/
1191b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1192b06ff27eSHong Zhang {
1193b06ff27eSHong Zhang   PetscFunctionBegin;
1194b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1195b06ff27eSHong Zhang   dm->structure_only = only;
1196b06ff27eSHong Zhang   PetscFunctionReturn(0);
1197b06ff27eSHong Zhang }
1198b06ff27eSHong Zhang 
1199a89ea682SMatthew G Knepley /*@C
1200aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1201a89ea682SMatthew G Knepley 
1202a89ea682SMatthew G Knepley   Not Collective
1203a89ea682SMatthew G Knepley 
1204a89ea682SMatthew G Knepley   Input Parameters:
1205a89ea682SMatthew G Knepley + dm - the DM object
1206aa1993deSMatthew G Knepley . count - The minium size
1207aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1208a89ea682SMatthew G Knepley 
1209a89ea682SMatthew G Knepley   Output Parameter:
1210a89ea682SMatthew G Knepley . array - the work array
1211a89ea682SMatthew G Knepley 
1212a89ea682SMatthew G Knepley   Level: developer
1213a89ea682SMatthew G Knepley 
1214a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1215a89ea682SMatthew G Knepley @*/
1216aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1217a89ea682SMatthew G Knepley {
1218a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1219aa1993deSMatthew G Knepley   DMWorkLink     link;
1220854ce69bSBarry Smith   size_t         dsize;
1221a89ea682SMatthew G Knepley 
1222a89ea682SMatthew G Knepley   PetscFunctionBegin;
1223a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1224aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1225aa1993deSMatthew G Knepley   if (dm->workin) {
1226aa1993deSMatthew G Knepley     link       = dm->workin;
1227aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1228aa1993deSMatthew G Knepley   } else {
1229b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1230a89ea682SMatthew G Knepley   }
1231854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1232854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1233aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1234854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1235854ce69bSBarry Smith     link->bytes = dsize*count;
1236aa1993deSMatthew G Knepley   }
1237aa1993deSMatthew G Knepley   link->next   = dm->workout;
1238aa1993deSMatthew G Knepley   dm->workout  = link;
1239aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1240a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1241a89ea682SMatthew G Knepley }
1242a89ea682SMatthew G Knepley 
1243aa1993deSMatthew G Knepley /*@C
1244aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1245aa1993deSMatthew G Knepley 
1246aa1993deSMatthew G Knepley   Not Collective
1247aa1993deSMatthew G Knepley 
1248aa1993deSMatthew G Knepley   Input Parameters:
1249aa1993deSMatthew G Knepley + dm - the DM object
1250aa1993deSMatthew G Knepley . count - The minium size
1251aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1252aa1993deSMatthew G Knepley 
1253aa1993deSMatthew G Knepley   Output Parameter:
1254aa1993deSMatthew G Knepley . array - the work array
1255aa1993deSMatthew G Knepley 
1256aa1993deSMatthew G Knepley   Level: developer
1257aa1993deSMatthew G Knepley 
1258aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1259aa1993deSMatthew G Knepley @*/
1260aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1261aa1993deSMatthew G Knepley {
1262aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1263aa1993deSMatthew G Knepley 
1264aa1993deSMatthew G Knepley   PetscFunctionBegin;
1265aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1266aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1267aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1268aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1269aa1993deSMatthew G Knepley       *p           = link->next;
1270aa1993deSMatthew G Knepley       link->next   = dm->workin;
1271aa1993deSMatthew G Knepley       dm->workin   = link;
12720298fd71SBarry Smith       *(void**)mem = NULL;
1273aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1274aa1993deSMatthew G Knepley     }
1275aa1993deSMatthew G Knepley   }
1276aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1277aa1993deSMatthew G Knepley }
1278e7c4fc90SDmitry Karpeev 
1279435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1280435a35e8SMatthew G Knepley {
1281435a35e8SMatthew G Knepley   PetscFunctionBegin;
1282435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
128382f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1284435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1285435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1286435a35e8SMatthew G Knepley }
1287435a35e8SMatthew G Knepley 
12884f3b5142SJed Brown /*@C
12894d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
12904d343eeaSMatthew G Knepley 
12914d343eeaSMatthew G Knepley   Not collective
12924d343eeaSMatthew G Knepley 
12934d343eeaSMatthew G Knepley   Input Parameter:
12944d343eeaSMatthew G Knepley . dm - the DM object
12954d343eeaSMatthew G Knepley 
12964d343eeaSMatthew G Knepley   Output Parameters:
12970298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
12980298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
12990298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13004d343eeaSMatthew G Knepley 
13014d343eeaSMatthew G Knepley   Level: intermediate
13024d343eeaSMatthew G Knepley 
130321c9b008SJed Brown   Notes:
130421c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
130521c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
130621c9b008SJed Brown   PetscFree().
130721c9b008SJed Brown 
13084d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13094d343eeaSMatthew G Knepley @*/
131037d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13114d343eeaSMatthew G Knepley {
131237d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13134d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13144d343eeaSMatthew G Knepley 
13154d343eeaSMatthew G Knepley   PetscFunctionBegin;
13164d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
131769ca1f37SDmitry Karpeev   if (numFields) {
131869ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
131969ca1f37SDmitry Karpeev     *numFields = 0;
132069ca1f37SDmitry Karpeev   }
132137d0c07bSMatthew G Knepley   if (fieldNames) {
132237d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13230298fd71SBarry Smith     *fieldNames = NULL;
132469ca1f37SDmitry Karpeev   }
132569ca1f37SDmitry Karpeev   if (fields) {
132669ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13270298fd71SBarry Smith     *fields = NULL;
132869ca1f37SDmitry Karpeev   }
132937d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
133037d0c07bSMatthew G Knepley   if (section) {
133137d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
133237d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
133337d0c07bSMatthew G Knepley 
133437d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
133537d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1336dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
133737d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
133837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
133937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
134037d0c07bSMatthew G Knepley     }
134137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
134237d0c07bSMatthew G Knepley       PetscInt gdof;
134337d0c07bSMatthew G Knepley 
134437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
134537d0c07bSMatthew G Knepley       if (gdof > 0) {
134637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
134737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
134837d0c07bSMatthew G Knepley 
134937d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
135037d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
135137d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
135237d0c07bSMatthew G Knepley         }
135337d0c07bSMatthew G Knepley       }
135437d0c07bSMatthew G Knepley     }
135537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1356785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
135737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
135837d0c07bSMatthew G Knepley     }
135937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
136037d0c07bSMatthew G Knepley       PetscInt gdof, goff;
136137d0c07bSMatthew G Knepley 
136237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
136337d0c07bSMatthew G Knepley       if (gdof > 0) {
136437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
136537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
136637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
136737d0c07bSMatthew G Knepley 
136837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
136937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
137037d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
137137d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
137237d0c07bSMatthew G Knepley           }
137337d0c07bSMatthew G Knepley         }
137437d0c07bSMatthew G Knepley       }
137537d0c07bSMatthew G Knepley     }
13768865f1eaSKarl Rupp     if (numFields) *numFields = nF;
137737d0c07bSMatthew G Knepley     if (fieldNames) {
1378785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
137937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
138037d0c07bSMatthew G Knepley         const char *fieldName;
138137d0c07bSMatthew G Knepley 
138237d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
138337d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
138437d0c07bSMatthew G Knepley       }
138537d0c07bSMatthew G Knepley     }
138637d0c07bSMatthew G Knepley     if (fields) {
1387785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
138837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
138982f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
139037d0c07bSMatthew G Knepley       }
139137d0c07bSMatthew G Knepley     }
139237d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
13938865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
13948865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
139569ca1f37SDmitry Karpeev   }
13964d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
13974d343eeaSMatthew G Knepley }
13984d343eeaSMatthew G Knepley 
139916621825SDmitry Karpeev 
140016621825SDmitry Karpeev /*@C
140116621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
140216621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
140316621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1404e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1405e7c4fc90SDmitry Karpeev 
1406e7c4fc90SDmitry Karpeev   Not collective
1407e7c4fc90SDmitry Karpeev 
1408e7c4fc90SDmitry Karpeev   Input Parameter:
1409e7c4fc90SDmitry Karpeev . dm - the DM object
1410e7c4fc90SDmitry Karpeev 
1411e7c4fc90SDmitry Karpeev   Output Parameters:
14120298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14130298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14140298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14150298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1416e7c4fc90SDmitry Karpeev 
1417e7c4fc90SDmitry Karpeev   Level: intermediate
1418e7c4fc90SDmitry Karpeev 
1419e7c4fc90SDmitry Karpeev   Notes:
1420e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1421e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1422e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1423e7c4fc90SDmitry Karpeev 
1424e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1425e7c4fc90SDmitry Karpeev @*/
142616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1427e7c4fc90SDmitry Karpeev {
1428e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1429e7c4fc90SDmitry Karpeev 
1430e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1431e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14328865f1eaSKarl Rupp   if (len) {
14338865f1eaSKarl Rupp     PetscValidPointer(len,2);
14348865f1eaSKarl Rupp     *len = 0;
14358865f1eaSKarl Rupp   }
14368865f1eaSKarl Rupp   if (namelist) {
14378865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14388865f1eaSKarl Rupp     *namelist = 0;
14398865f1eaSKarl Rupp   }
14408865f1eaSKarl Rupp   if (islist) {
14418865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14428865f1eaSKarl Rupp     *islist = 0;
14438865f1eaSKarl Rupp   }
14448865f1eaSKarl Rupp   if (dmlist) {
14458865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14468865f1eaSKarl Rupp     *dmlist = 0;
14478865f1eaSKarl Rupp   }
1448f3f0edfdSDmitry Karpeev   /*
1449f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1450f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1451f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1452f3f0edfdSDmitry Karpeev    */
1453ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
145416621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1455435a35e8SMatthew G Knepley     PetscSection section;
1456435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1457435a35e8SMatthew G Knepley 
1458435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1459435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1460435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1461f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
146203dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
146303dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
146403dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1465435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1466435a35e8SMatthew G Knepley         const char *fieldName;
1467435a35e8SMatthew G Knepley 
146803dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
146903dc3394SMatthew G. Knepley         if (namelist) {
1470435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1471435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1472435a35e8SMatthew G Knepley         }
147303dc3394SMatthew G. Knepley       }
1474435a35e8SMatthew G Knepley     } else {
147569ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1476e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
14770298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1478e7c4fc90SDmitry Karpeev     }
14798865f1eaSKarl Rupp   } else {
148016621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
148116621825SDmitry Karpeev   }
148216621825SDmitry Karpeev   PetscFunctionReturn(0);
148316621825SDmitry Karpeev }
148416621825SDmitry Karpeev 
1485564cec59SMatthew G. Knepley /*@
1486435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1487435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1488435a35e8SMatthew G Knepley 
1489435a35e8SMatthew G Knepley   Not collective
1490435a35e8SMatthew G Knepley 
1491435a35e8SMatthew G Knepley   Input Parameters:
1492435a35e8SMatthew G Knepley + dm - the DM object
1493435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
14940298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1495435a35e8SMatthew G Knepley 
1496435a35e8SMatthew G Knepley   Output Parameters:
1497435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1498435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1499435a35e8SMatthew G Knepley 
1500435a35e8SMatthew G Knepley   Level: intermediate
1501435a35e8SMatthew G Knepley 
1502435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1503435a35e8SMatthew G Knepley @*/
1504435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1505435a35e8SMatthew G Knepley {
1506435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1507435a35e8SMatthew G Knepley 
1508435a35e8SMatthew G Knepley   PetscFunctionBegin;
1509435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1510435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15118865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15128865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1513435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1514435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
151582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1516435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1517435a35e8SMatthew G Knepley }
1518435a35e8SMatthew G Knepley 
151916621825SDmitry Karpeev 
152016621825SDmitry Karpeev /*@C
15218d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
15228d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
15238d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
15248d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
15258d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
152616621825SDmitry Karpeev 
152716621825SDmitry Karpeev   Not collective
152816621825SDmitry Karpeev 
152916621825SDmitry Karpeev   Input Parameter:
153016621825SDmitry Karpeev . dm - the DM object
153116621825SDmitry Karpeev 
153216621825SDmitry Karpeev   Output Parameters:
15330298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
15340298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
15350298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
15360298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
15370298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
153816621825SDmitry Karpeev 
153916621825SDmitry Karpeev   Level: intermediate
154016621825SDmitry Karpeev 
154116621825SDmitry Karpeev   Notes:
154216621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
154316621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
154416621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
154516621825SDmitry Karpeev 
15468d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
154716621825SDmitry Karpeev @*/
15488d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
154916621825SDmitry Karpeev {
155016621825SDmitry Karpeev   PetscErrorCode      ierr;
1551be081cd6SPeter Brune   DMSubDomainHookLink link;
1552be081cd6SPeter Brune   PetscInt            i,l;
155316621825SDmitry Karpeev 
155416621825SDmitry Karpeev   PetscFunctionBegin;
155516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
155614a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
15570298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
15580298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
15590298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
15600298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1561f3f0edfdSDmitry Karpeev   /*
1562f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1563f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1564f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1565f3f0edfdSDmitry Karpeev    */
1566ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
156716621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1568be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
156914a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1570f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1571be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1572be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1573be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1574be081cd6SPeter Brune         }
1575648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1576e7c4fc90SDmitry Karpeev       }
157714a18fd3SPeter Brune     }
157814a18fd3SPeter Brune     if (len) *len = l;
157914a18fd3SPeter Brune   }
1580e30e807fSPeter Brune   PetscFunctionReturn(0);
1581e30e807fSPeter Brune }
1582e30e807fSPeter Brune 
1583e30e807fSPeter Brune 
1584e30e807fSPeter Brune /*@C
1585e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1586e30e807fSPeter Brune 
1587e30e807fSPeter Brune   Not collective
1588e30e807fSPeter Brune 
1589e30e807fSPeter Brune   Input Parameters:
1590e30e807fSPeter Brune + dm - the DM object
1591e30e807fSPeter Brune . n  - the number of subdomain scatters
1592e30e807fSPeter Brune - subdms - the local subdomains
1593e30e807fSPeter Brune 
1594e30e807fSPeter Brune   Output Parameters:
1595e30e807fSPeter Brune + n     - the number of scatters returned
1596e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1597e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1598e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1599e30e807fSPeter Brune 
1600e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1601e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1602e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1603e30e807fSPeter Brune   solution and residual data.
1604e30e807fSPeter Brune 
1605e30e807fSPeter Brune   Level: developer
1606e30e807fSPeter Brune 
1607e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1608e30e807fSPeter Brune @*/
1609e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1610e30e807fSPeter Brune {
1611e30e807fSPeter Brune   PetscErrorCode ierr;
1612e30e807fSPeter Brune 
1613e30e807fSPeter Brune   PetscFunctionBegin;
1614e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1615e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1616e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1617e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
16186668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1619e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1620e7c4fc90SDmitry Karpeev }
1621e7c4fc90SDmitry Karpeev 
162247c6ae99SBarry Smith /*@
162347c6ae99SBarry Smith   DMRefine - Refines a DM object
162447c6ae99SBarry Smith 
162547c6ae99SBarry Smith   Collective on DM
162647c6ae99SBarry Smith 
162747c6ae99SBarry Smith   Input Parameter:
162847c6ae99SBarry Smith + dm   - the DM object
162991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
163047c6ae99SBarry Smith 
163147c6ae99SBarry Smith   Output Parameter:
16320298fd71SBarry Smith . dmf - the refined DM, or NULL
1633ae0a1c52SMatthew G Knepley 
16340298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
163547c6ae99SBarry Smith 
163647c6ae99SBarry Smith   Level: developer
163747c6ae99SBarry Smith 
1638e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
163947c6ae99SBarry Smith @*/
16407087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
164147c6ae99SBarry Smith {
164247c6ae99SBarry Smith   PetscErrorCode   ierr;
1643c833c3b5SJed Brown   DMRefineHookLink link;
164447c6ae99SBarry Smith 
164547c6ae99SBarry Smith   PetscFunctionBegin;
1646732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16471ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1648fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
164947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
16504057135bSMatthew G Knepley   if (*dmf) {
165143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
16528865f1eaSKarl Rupp 
16538cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
16548865f1eaSKarl Rupp 
1655644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
16560598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1657656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
16588865f1eaSKarl Rupp 
1659e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1660c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
16618865f1eaSKarl Rupp       if (link->refinehook) {
16628865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
16638865f1eaSKarl Rupp       }
1664c833c3b5SJed Brown     }
1665c833c3b5SJed Brown   }
16661ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1667c833c3b5SJed Brown   PetscFunctionReturn(0);
1668c833c3b5SJed Brown }
1669c833c3b5SJed Brown 
1670bb9467b5SJed Brown /*@C
1671c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1672c833c3b5SJed Brown 
1673c833c3b5SJed Brown    Logically Collective
1674c833c3b5SJed Brown 
1675c833c3b5SJed Brown    Input Arguments:
1676c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1677c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1678c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
16790298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1680c833c3b5SJed Brown 
1681c833c3b5SJed Brown    Calling sequence of refinehook:
1682c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1683c833c3b5SJed Brown 
1684c833c3b5SJed Brown +  coarse - coarse level DM
1685c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1686c833c3b5SJed Brown -  ctx - optional user-defined function context
1687c833c3b5SJed Brown 
1688c833c3b5SJed Brown    Calling sequence for interphook:
1689c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1690c833c3b5SJed Brown 
1691c833c3b5SJed Brown +  coarse - coarse level DM
1692c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1693c833c3b5SJed Brown .  fine - fine level DM to update
1694c833c3b5SJed Brown -  ctx - optional user-defined function context
1695c833c3b5SJed Brown 
1696c833c3b5SJed Brown    Level: advanced
1697c833c3b5SJed Brown 
1698c833c3b5SJed Brown    Notes:
1699c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1700c833c3b5SJed Brown 
1701c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1702c833c3b5SJed Brown 
1703bb9467b5SJed Brown    This function is currently not available from Fortran.
1704bb9467b5SJed Brown 
1705c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1706c833c3b5SJed Brown @*/
1707c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1708c833c3b5SJed Brown {
1709c833c3b5SJed Brown   PetscErrorCode   ierr;
1710c833c3b5SJed Brown   DMRefineHookLink link,*p;
1711c833c3b5SJed Brown 
1712c833c3b5SJed Brown   PetscFunctionBegin;
1713c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1714*3d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
1715*3d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
1716*3d8e3701SJed Brown   }
171795dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1718c833c3b5SJed Brown   link->refinehook = refinehook;
1719c833c3b5SJed Brown   link->interphook = interphook;
1720c833c3b5SJed Brown   link->ctx        = ctx;
17210298fd71SBarry Smith   link->next       = NULL;
1722c833c3b5SJed Brown   *p               = link;
1723c833c3b5SJed Brown   PetscFunctionReturn(0);
1724c833c3b5SJed Brown }
1725c833c3b5SJed Brown 
1726*3d8e3701SJed Brown /*@C
1727*3d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
1728*3d8e3701SJed Brown 
1729*3d8e3701SJed Brown    Logically Collective
1730*3d8e3701SJed Brown 
1731*3d8e3701SJed Brown    Input Arguments:
1732*3d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1733*3d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
1734*3d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1735*3d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1736*3d8e3701SJed Brown 
1737*3d8e3701SJed Brown    Level: advanced
1738*3d8e3701SJed Brown 
1739*3d8e3701SJed Brown    Notes:
1740*3d8e3701SJed Brown    This function does nothing if the hook is not in the list.
1741*3d8e3701SJed Brown 
1742*3d8e3701SJed Brown    This function is currently not available from Fortran.
1743*3d8e3701SJed Brown 
1744*3d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1745*3d8e3701SJed Brown @*/
1746*3d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1747*3d8e3701SJed Brown {
1748*3d8e3701SJed Brown   PetscErrorCode   ierr;
1749*3d8e3701SJed Brown   DMRefineHookLink link,*p;
1750*3d8e3701SJed Brown 
1751*3d8e3701SJed Brown   PetscFunctionBegin;
1752*3d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1753*3d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
1754*3d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
1755*3d8e3701SJed Brown       link = *p;
1756*3d8e3701SJed Brown       *p = link->next;
1757*3d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
1758*3d8e3701SJed Brown       break;
1759*3d8e3701SJed Brown     }
1760*3d8e3701SJed Brown   }
1761*3d8e3701SJed Brown   PetscFunctionReturn(0);
1762*3d8e3701SJed Brown }
1763*3d8e3701SJed Brown 
1764c833c3b5SJed Brown /*@
1765c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1766c833c3b5SJed Brown 
1767c833c3b5SJed Brown    Collective if any hooks are
1768c833c3b5SJed Brown 
1769c833c3b5SJed Brown    Input Arguments:
1770c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1771c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1772c833c3b5SJed Brown -  fine - finer DM to update
1773c833c3b5SJed Brown 
1774c833c3b5SJed Brown    Level: developer
1775c833c3b5SJed Brown 
1776c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1777c833c3b5SJed Brown @*/
1778c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1779c833c3b5SJed Brown {
1780c833c3b5SJed Brown   PetscErrorCode   ierr;
1781c833c3b5SJed Brown   DMRefineHookLink link;
1782c833c3b5SJed Brown 
1783c833c3b5SJed Brown   PetscFunctionBegin;
1784c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
17858865f1eaSKarl Rupp     if (link->interphook) {
17868865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
17878865f1eaSKarl Rupp     }
17884057135bSMatthew G Knepley   }
178947c6ae99SBarry Smith   PetscFunctionReturn(0);
179047c6ae99SBarry Smith }
179147c6ae99SBarry Smith 
1792eb3f98d2SBarry Smith /*@
1793eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1794eb3f98d2SBarry Smith 
1795eb3f98d2SBarry Smith     Not Collective
1796eb3f98d2SBarry Smith 
1797eb3f98d2SBarry Smith     Input Parameter:
1798eb3f98d2SBarry Smith .   dm - the DM object
1799eb3f98d2SBarry Smith 
1800eb3f98d2SBarry Smith     Output Parameter:
1801eb3f98d2SBarry Smith .   level - number of refinements
1802eb3f98d2SBarry Smith 
1803eb3f98d2SBarry Smith     Level: developer
1804eb3f98d2SBarry Smith 
18056a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1806eb3f98d2SBarry Smith 
1807eb3f98d2SBarry Smith @*/
1808eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1809eb3f98d2SBarry Smith {
1810eb3f98d2SBarry Smith   PetscFunctionBegin;
1811eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1812eb3f98d2SBarry Smith   *level = dm->levelup;
1813eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1814eb3f98d2SBarry Smith }
1815eb3f98d2SBarry Smith 
1816fef3a512SBarry Smith /*@
1817fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1818fef3a512SBarry Smith 
1819fef3a512SBarry Smith     Not Collective
1820fef3a512SBarry Smith 
1821fef3a512SBarry Smith     Input Parameter:
1822fef3a512SBarry Smith +   dm - the DM object
1823fef3a512SBarry Smith -   level - number of refinements
1824fef3a512SBarry Smith 
1825fef3a512SBarry Smith     Level: advanced
1826fef3a512SBarry Smith 
1827fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1828fef3a512SBarry Smith 
1829fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1830fef3a512SBarry Smith 
1831fef3a512SBarry Smith @*/
1832fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1833fef3a512SBarry Smith {
1834fef3a512SBarry Smith   PetscFunctionBegin;
1835fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1836fef3a512SBarry Smith   dm->levelup = level;
1837fef3a512SBarry Smith   PetscFunctionReturn(0);
1838fef3a512SBarry Smith }
1839fef3a512SBarry Smith 
1840bb9467b5SJed Brown /*@C
1841baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1842baf369e7SPeter Brune 
1843baf369e7SPeter Brune    Logically Collective
1844baf369e7SPeter Brune 
1845baf369e7SPeter Brune    Input Arguments:
1846baf369e7SPeter Brune +  dm - the DM
1847baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1848baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
18490298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1850baf369e7SPeter Brune 
1851baf369e7SPeter Brune    Calling sequence for beginhook:
1852baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1853baf369e7SPeter Brune 
1854baf369e7SPeter Brune +  dm - global DM
1855baf369e7SPeter Brune .  g - global vector
1856baf369e7SPeter Brune .  mode - mode
1857baf369e7SPeter Brune .  l - local vector
1858baf369e7SPeter Brune -  ctx - optional user-defined function context
1859baf369e7SPeter Brune 
1860baf369e7SPeter Brune 
1861baf369e7SPeter Brune    Calling sequence for endhook:
1862ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1863baf369e7SPeter Brune 
1864baf369e7SPeter Brune +  global - global DM
1865baf369e7SPeter Brune -  ctx - optional user-defined function context
1866baf369e7SPeter Brune 
1867baf369e7SPeter Brune    Level: advanced
1868baf369e7SPeter Brune 
1869baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1870baf369e7SPeter Brune @*/
1871baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1872baf369e7SPeter Brune {
1873baf369e7SPeter Brune   PetscErrorCode          ierr;
1874baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1875baf369e7SPeter Brune 
1876baf369e7SPeter Brune   PetscFunctionBegin;
1877baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1878baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
187995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1880baf369e7SPeter Brune   link->beginhook = beginhook;
1881baf369e7SPeter Brune   link->endhook   = endhook;
1882baf369e7SPeter Brune   link->ctx       = ctx;
18830298fd71SBarry Smith   link->next      = NULL;
1884baf369e7SPeter Brune   *p              = link;
1885baf369e7SPeter Brune   PetscFunctionReturn(0);
1886baf369e7SPeter Brune }
1887baf369e7SPeter Brune 
18884c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
18894c274da1SToby Isaac {
18904c274da1SToby Isaac   Mat cMat;
18914c274da1SToby Isaac   Vec cVec;
18924c274da1SToby Isaac   PetscSection section, cSec;
18934c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18944c274da1SToby Isaac   PetscErrorCode ierr;
18954c274da1SToby Isaac 
18964c274da1SToby Isaac   PetscFunctionBegin;
18974c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18984c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
18994c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
19005db9a05bSToby Isaac     PetscInt nRows;
19015db9a05bSToby Isaac 
19025db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
19035db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
19044c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19057711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19064c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
19074c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19084c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19094c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19104c274da1SToby Isaac       if (dof) {
19114c274da1SToby Isaac         PetscScalar *vals;
19124c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
19134c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
19144c274da1SToby Isaac       }
19154c274da1SToby Isaac     }
19164c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19174c274da1SToby Isaac   }
19184c274da1SToby Isaac   PetscFunctionReturn(0);
19194c274da1SToby Isaac }
19204c274da1SToby Isaac 
192147c6ae99SBarry Smith /*@
192247c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
192347c6ae99SBarry Smith 
192447c6ae99SBarry Smith     Neighbor-wise Collective on DM
192547c6ae99SBarry Smith 
192647c6ae99SBarry Smith     Input Parameters:
192747c6ae99SBarry Smith +   dm - the DM object
192847c6ae99SBarry Smith .   g - the global vector
192947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
193047c6ae99SBarry Smith -   l - the local vector
193147c6ae99SBarry Smith 
193247c6ae99SBarry Smith 
193347c6ae99SBarry Smith     Level: beginner
193447c6ae99SBarry Smith 
1935e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
193647c6ae99SBarry Smith 
193747c6ae99SBarry Smith @*/
19387087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
193947c6ae99SBarry Smith {
19407128ae9fSMatthew G Knepley   PetscSF                 sf;
194147c6ae99SBarry Smith   PetscErrorCode          ierr;
1942baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
194347c6ae99SBarry Smith 
194447c6ae99SBarry Smith   PetscFunctionBegin;
1945171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1946baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
19478865f1eaSKarl Rupp     if (link->beginhook) {
19488865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
19498865f1eaSKarl Rupp     }
1950baf369e7SPeter Brune   }
19517128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19527128ae9fSMatthew G Knepley   if (sf) {
1953ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1954ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
19557128ae9fSMatthew G Knepley 
195682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19577128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1958ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19597128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19607128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1961ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19627128ae9fSMatthew G Knepley   } else {
1963843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19647128ae9fSMatthew G Knepley   }
196547c6ae99SBarry Smith   PetscFunctionReturn(0);
196647c6ae99SBarry Smith }
196747c6ae99SBarry Smith 
196847c6ae99SBarry Smith /*@
196947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
197047c6ae99SBarry Smith 
197147c6ae99SBarry Smith     Neighbor-wise Collective on DM
197247c6ae99SBarry Smith 
197347c6ae99SBarry Smith     Input Parameters:
197447c6ae99SBarry Smith +   dm - the DM object
197547c6ae99SBarry Smith .   g - the global vector
197647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
197747c6ae99SBarry Smith -   l - the local vector
197847c6ae99SBarry Smith 
197947c6ae99SBarry Smith 
198047c6ae99SBarry Smith     Level: beginner
198147c6ae99SBarry Smith 
1982e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
198347c6ae99SBarry Smith 
198447c6ae99SBarry Smith @*/
19857087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
198647c6ae99SBarry Smith {
19877128ae9fSMatthew G Knepley   PetscSF                 sf;
198847c6ae99SBarry Smith   PetscErrorCode          ierr;
1989ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1990ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1991baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
199247c6ae99SBarry Smith 
199347c6ae99SBarry Smith   PetscFunctionBegin;
1994171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19957128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19967128ae9fSMatthew G Knepley   if (sf) {
199782f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19987128ae9fSMatthew G Knepley 
19997128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2000ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20017128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20027128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2003ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20047128ae9fSMatthew G Knepley   } else {
2005843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20067128ae9fSMatthew G Knepley   }
20074c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2008baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2009baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2010baf369e7SPeter Brune   }
201147c6ae99SBarry Smith   PetscFunctionReturn(0);
201247c6ae99SBarry Smith }
201347c6ae99SBarry Smith 
2014d4d07f1eSToby Isaac /*@C
2015d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2016d4d07f1eSToby Isaac 
2017d4d07f1eSToby Isaac    Logically Collective
2018d4d07f1eSToby Isaac 
2019d4d07f1eSToby Isaac    Input Arguments:
2020d4d07f1eSToby Isaac +  dm - the DM
2021d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2022d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2023d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2024d4d07f1eSToby Isaac 
2025d4d07f1eSToby Isaac    Calling sequence for beginhook:
2026d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2027d4d07f1eSToby Isaac 
2028d4d07f1eSToby Isaac +  dm - global DM
2029d4d07f1eSToby Isaac .  l - local vector
2030d4d07f1eSToby Isaac .  mode - mode
2031d4d07f1eSToby Isaac .  g - global vector
2032d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2033d4d07f1eSToby Isaac 
2034d4d07f1eSToby Isaac 
2035d4d07f1eSToby Isaac    Calling sequence for endhook:
2036d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2037d4d07f1eSToby Isaac 
2038d4d07f1eSToby Isaac +  global - global DM
2039d4d07f1eSToby Isaac .  l - local vector
2040d4d07f1eSToby Isaac .  mode - mode
2041d4d07f1eSToby Isaac .  g - global vector
2042d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2043d4d07f1eSToby Isaac 
2044d4d07f1eSToby Isaac    Level: advanced
2045d4d07f1eSToby Isaac 
2046d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2047d4d07f1eSToby Isaac @*/
2048d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2049d4d07f1eSToby Isaac {
2050d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2051d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2052d4d07f1eSToby Isaac 
2053d4d07f1eSToby Isaac   PetscFunctionBegin;
2054d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2055d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
205695dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2057d4d07f1eSToby Isaac   link->beginhook = beginhook;
2058d4d07f1eSToby Isaac   link->endhook   = endhook;
2059d4d07f1eSToby Isaac   link->ctx       = ctx;
2060d4d07f1eSToby Isaac   link->next      = NULL;
2061d4d07f1eSToby Isaac   *p              = link;
2062d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2063d4d07f1eSToby Isaac }
2064d4d07f1eSToby Isaac 
20654c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
20664c274da1SToby Isaac {
20674c274da1SToby Isaac   Mat cMat;
20684c274da1SToby Isaac   Vec cVec;
20694c274da1SToby Isaac   PetscSection section, cSec;
20704c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20714c274da1SToby Isaac   PetscErrorCode ierr;
20724c274da1SToby Isaac 
20734c274da1SToby Isaac   PetscFunctionBegin;
20744c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20754c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20764c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
20775db9a05bSToby Isaac     PetscInt nRows;
20785db9a05bSToby Isaac 
20795db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
20805db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
20814c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
20827711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
20834c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20844c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20854c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20864c274da1SToby Isaac       if (dof) {
20874c274da1SToby Isaac         PetscInt d;
20884c274da1SToby Isaac         PetscScalar *vals;
20894c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
20904c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
20914c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
20924c274da1SToby Isaac          * we just extracted */
20934c274da1SToby Isaac         for (d = 0; d < dof; d++) {
20944c274da1SToby Isaac           vals[d] = 0.;
20954c274da1SToby Isaac         }
20964c274da1SToby Isaac       }
20974c274da1SToby Isaac     }
20984c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
20994c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21004c274da1SToby Isaac   }
21014c274da1SToby Isaac   PetscFunctionReturn(0);
21024c274da1SToby Isaac }
21034c274da1SToby Isaac 
210447c6ae99SBarry Smith /*@
21059a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21069a42bb27SBarry Smith 
21079a42bb27SBarry Smith     Neighbor-wise Collective on DM
21089a42bb27SBarry Smith 
21099a42bb27SBarry Smith     Input Parameters:
21109a42bb27SBarry Smith +   dm - the DM object
2111f6813fd5SJed Brown .   l - the local vector
21121eb28f2eSBarry 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.
21131eb28f2eSBarry Smith -   g - the global vector
21149a42bb27SBarry Smith 
2115d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
211684330215SMatthew 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.
21179a42bb27SBarry Smith 
21189a42bb27SBarry Smith     Level: beginner
21199a42bb27SBarry Smith 
2120e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
21219a42bb27SBarry Smith 
21229a42bb27SBarry Smith @*/
21237087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
21249a42bb27SBarry Smith {
21257128ae9fSMatthew G Knepley   PetscSF                 sf;
212684330215SMatthew G. Knepley   PetscSection            s, gs;
2127d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2128ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2129ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
213084330215SMatthew G. Knepley   PetscBool               isInsert;
213184330215SMatthew G. Knepley   PetscErrorCode          ierr;
21329a42bb27SBarry Smith 
21339a42bb27SBarry Smith   PetscFunctionBegin;
2134171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2135d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2136d4d07f1eSToby Isaac     if (link->beginhook) {
2137d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2138d4d07f1eSToby Isaac     }
2139d4d07f1eSToby Isaac   }
21404c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
21417128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
214284330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21437128ae9fSMatthew G Knepley   switch (mode) {
21447128ae9fSMatthew G Knepley   case INSERT_VALUES:
21457128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2146304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
214784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21487128ae9fSMatthew G Knepley   case ADD_VALUES:
21497128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2150304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
215184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21527128ae9fSMatthew G Knepley   default:
215382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21547128ae9fSMatthew G Knepley   }
215584330215SMatthew G. Knepley   if (sf && !isInsert) {
2156ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21577128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2158a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2159ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
216084330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
216184330215SMatthew G. Knepley   } else if (s && isInsert) {
216284330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
216384330215SMatthew G. Knepley 
216484330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
216584330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
216684330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2167ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
216884330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
216984330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2170b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
217184330215SMatthew G. Knepley 
217284330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
217303442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
217484330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2175b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
217684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
217784330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2178b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
217903442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2180b3b16f48SMatthew 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);
2181b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2182b3b16f48SMatthew G. Knepley       if (!gcdof) {
218384330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2184b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2185b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
218684330215SMatthew G. Knepley         const PetscInt *cdofs;
218784330215SMatthew G. Knepley         PetscInt        cind = 0;
218884330215SMatthew G. Knepley 
218984330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2190b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
219184330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2192b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
219384330215SMatthew G. Knepley         }
2194b3b16f48SMatthew 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);
219584330215SMatthew G. Knepley     }
2196ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21977128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
21987128ae9fSMatthew G Knepley   } else {
2199843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22007128ae9fSMatthew G Knepley   }
22019a42bb27SBarry Smith   PetscFunctionReturn(0);
22029a42bb27SBarry Smith }
22039a42bb27SBarry Smith 
22049a42bb27SBarry Smith /*@
22059a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
220647c6ae99SBarry Smith 
220747c6ae99SBarry Smith     Neighbor-wise Collective on DM
220847c6ae99SBarry Smith 
220947c6ae99SBarry Smith     Input Parameters:
221047c6ae99SBarry Smith +   dm - the DM object
2211f6813fd5SJed Brown .   l - the local vector
221247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2213f6813fd5SJed Brown -   g - the global vector
221447c6ae99SBarry Smith 
221547c6ae99SBarry Smith 
221647c6ae99SBarry Smith     Level: beginner
221747c6ae99SBarry Smith 
2218e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
221947c6ae99SBarry Smith 
222047c6ae99SBarry Smith @*/
22217087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
222247c6ae99SBarry Smith {
22237128ae9fSMatthew G Knepley   PetscSF                 sf;
222484330215SMatthew G. Knepley   PetscSection            s;
2225d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
222684330215SMatthew G. Knepley   PetscBool               isInsert;
222784330215SMatthew G. Knepley   PetscErrorCode          ierr;
222847c6ae99SBarry Smith 
222947c6ae99SBarry Smith   PetscFunctionBegin;
2230171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22317128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
223284330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22337128ae9fSMatthew G Knepley   switch (mode) {
22347128ae9fSMatthew G Knepley   case INSERT_VALUES:
22357128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
223684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22377128ae9fSMatthew G Knepley   case ADD_VALUES:
22387128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
223984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22407128ae9fSMatthew G Knepley   default:
224182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22427128ae9fSMatthew G Knepley   }
224384330215SMatthew G. Knepley   if (sf && !isInsert) {
2244ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2245ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
224684330215SMatthew G. Knepley 
2247ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22487128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2249a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2250ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22517128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
225284330215SMatthew G. Knepley   } else if (s && isInsert) {
22537128ae9fSMatthew G Knepley   } else {
2254843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22557128ae9fSMatthew G Knepley   }
2256d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2257d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2258d4d07f1eSToby Isaac   }
225947c6ae99SBarry Smith   PetscFunctionReturn(0);
226047c6ae99SBarry Smith }
226147c6ae99SBarry Smith 
2262f089877aSRichard Tran Mills /*@
2263bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2264bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2265d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2266f089877aSRichard Tran Mills 
2267bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2268f089877aSRichard Tran Mills 
2269f089877aSRichard Tran Mills    Input Parameters:
2270f089877aSRichard Tran Mills +  dm - the DM object
2271bc0a1609SRichard Tran Mills .  g - the original local vector
2272bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2273f089877aSRichard Tran Mills 
2274bc0a1609SRichard Tran Mills    Output Parameter:
2275bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2276f089877aSRichard Tran Mills 
2277f089877aSRichard Tran Mills    Level: intermediate
2278f089877aSRichard Tran Mills 
2279bc0a1609SRichard Tran Mills    Notes:
2280bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2281bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2282bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2283bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2284bc0a1609SRichard Tran Mills 
2285bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2286bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2287f089877aSRichard Tran Mills 
2288f089877aSRichard Tran Mills @*/
2289f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2290f089877aSRichard Tran Mills {
2291f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2292f089877aSRichard Tran Mills 
2293f089877aSRichard Tran Mills   PetscFunctionBegin;
2294f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2295f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2296f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2297f089877aSRichard Tran Mills }
2298f089877aSRichard Tran Mills 
2299f089877aSRichard Tran Mills /*@
2300bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2301bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2302d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2303f089877aSRichard Tran Mills 
2304bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2305f089877aSRichard Tran Mills 
2306f089877aSRichard Tran Mills    Input Parameters:
2307bc0a1609SRichard Tran Mills +  da - the DM object
2308bc0a1609SRichard Tran Mills .  g - the original local vector
2309bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2310f089877aSRichard Tran Mills 
2311bc0a1609SRichard Tran Mills    Output Parameter:
2312bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2313f089877aSRichard Tran Mills 
2314f089877aSRichard Tran Mills    Level: intermediate
2315f089877aSRichard Tran Mills 
2316bc0a1609SRichard Tran Mills    Notes:
2317bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2318bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2319bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2320bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2321bc0a1609SRichard Tran Mills 
2322bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2323bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2324f089877aSRichard Tran Mills 
2325f089877aSRichard Tran Mills @*/
2326f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2327f089877aSRichard Tran Mills {
2328f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2329f089877aSRichard Tran Mills 
2330f089877aSRichard Tran Mills   PetscFunctionBegin;
2331f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2332f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2333f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2334f089877aSRichard Tran Mills }
2335f089877aSRichard Tran Mills 
2336f089877aSRichard Tran Mills 
233747c6ae99SBarry Smith /*@
233847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
233947c6ae99SBarry Smith 
234047c6ae99SBarry Smith     Collective on DM
234147c6ae99SBarry Smith 
234247c6ae99SBarry Smith     Input Parameter:
234347c6ae99SBarry Smith +   dm - the DM object
234491d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
234547c6ae99SBarry Smith 
234647c6ae99SBarry Smith     Output Parameter:
234747c6ae99SBarry Smith .   dmc - the coarsened DM
234847c6ae99SBarry Smith 
234947c6ae99SBarry Smith     Level: developer
235047c6ae99SBarry Smith 
2351e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
235247c6ae99SBarry Smith 
235347c6ae99SBarry Smith @*/
23547087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
235547c6ae99SBarry Smith {
235647c6ae99SBarry Smith   PetscErrorCode    ierr;
2357b17ce1afSJed Brown   DMCoarsenHookLink link;
235847c6ae99SBarry Smith 
235947c6ae99SBarry Smith   PetscFunctionBegin;
2360171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2361fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
236247a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
236347c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
23648892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2365a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
236643842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
23678cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2368644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
23690598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2370656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2371e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2372b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2373b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2374b17ce1afSJed Brown   }
237547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2376b17ce1afSJed Brown   PetscFunctionReturn(0);
2377b17ce1afSJed Brown }
2378b17ce1afSJed Brown 
2379bb9467b5SJed Brown /*@C
2380b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2381b17ce1afSJed Brown 
2382b17ce1afSJed Brown    Logically Collective
2383b17ce1afSJed Brown 
2384b17ce1afSJed Brown    Input Arguments:
2385b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2386b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2387b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
23880298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2389b17ce1afSJed Brown 
2390b17ce1afSJed Brown    Calling sequence of coarsenhook:
2391b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2392b17ce1afSJed Brown 
2393b17ce1afSJed Brown +  fine - fine level DM
2394b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2395b17ce1afSJed Brown -  ctx - optional user-defined function context
2396b17ce1afSJed Brown 
2397b17ce1afSJed Brown    Calling sequence for restricthook:
2398c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2399b17ce1afSJed Brown 
2400b17ce1afSJed Brown +  fine - fine level DM
2401b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2402c833c3b5SJed Brown .  rscale - scaling vector for restriction
2403c833c3b5SJed Brown .  inject - matrix restricting by injection
2404b17ce1afSJed Brown .  coarse - coarse level DM to update
2405b17ce1afSJed Brown -  ctx - optional user-defined function context
2406b17ce1afSJed Brown 
2407b17ce1afSJed Brown    Level: advanced
2408b17ce1afSJed Brown 
2409b17ce1afSJed Brown    Notes:
2410b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2411b17ce1afSJed Brown 
2412b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2413b17ce1afSJed Brown 
2414b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2415b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2416b17ce1afSJed Brown 
2417bb9467b5SJed Brown    This function is currently not available from Fortran.
2418bb9467b5SJed Brown 
2419dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2420b17ce1afSJed Brown @*/
2421b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2422b17ce1afSJed Brown {
2423b17ce1afSJed Brown   PetscErrorCode    ierr;
2424b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2425b17ce1afSJed Brown 
2426b17ce1afSJed Brown   PetscFunctionBegin;
2427b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
24281e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
24291e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
24301e3d8eccSJed Brown   }
243195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2432b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2433b17ce1afSJed Brown   link->restricthook = restricthook;
2434b17ce1afSJed Brown   link->ctx          = ctx;
24350298fd71SBarry Smith   link->next         = NULL;
2436b17ce1afSJed Brown   *p                 = link;
2437b17ce1afSJed Brown   PetscFunctionReturn(0);
2438b17ce1afSJed Brown }
2439b17ce1afSJed Brown 
2440dc822a44SJed Brown /*@C
2441dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2442dc822a44SJed Brown 
2443dc822a44SJed Brown    Logically Collective
2444dc822a44SJed Brown 
2445dc822a44SJed Brown    Input Arguments:
2446dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2447dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2448dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2449dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2450dc822a44SJed Brown 
2451dc822a44SJed Brown    Level: advanced
2452dc822a44SJed Brown 
2453dc822a44SJed Brown    Notes:
2454dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2455dc822a44SJed Brown 
2456dc822a44SJed Brown    This function is currently not available from Fortran.
2457dc822a44SJed Brown 
2458dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2459dc822a44SJed Brown @*/
2460dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2461dc822a44SJed Brown {
2462dc822a44SJed Brown   PetscErrorCode    ierr;
2463dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2464dc822a44SJed Brown 
2465dc822a44SJed Brown   PetscFunctionBegin;
2466dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2467dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2468dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2469dc822a44SJed Brown       link = *p;
2470dc822a44SJed Brown       *p = link->next;
2471dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2472dc822a44SJed Brown       break;
2473dc822a44SJed Brown     }
2474dc822a44SJed Brown   }
2475dc822a44SJed Brown   PetscFunctionReturn(0);
2476dc822a44SJed Brown }
2477dc822a44SJed Brown 
2478dc822a44SJed Brown 
2479b17ce1afSJed Brown /*@
2480b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2481b17ce1afSJed Brown 
2482b17ce1afSJed Brown    Collective if any hooks are
2483b17ce1afSJed Brown 
2484b17ce1afSJed Brown    Input Arguments:
2485b17ce1afSJed Brown +  fine - finer DM to use as a base
2486b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2487b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2488b17ce1afSJed Brown -  coarse - coarer DM to update
2489b17ce1afSJed Brown 
2490b17ce1afSJed Brown    Level: developer
2491b17ce1afSJed Brown 
2492b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2493b17ce1afSJed Brown @*/
2494b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2495b17ce1afSJed Brown {
2496b17ce1afSJed Brown   PetscErrorCode    ierr;
2497b17ce1afSJed Brown   DMCoarsenHookLink link;
2498b17ce1afSJed Brown 
2499b17ce1afSJed Brown   PetscFunctionBegin;
2500b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
25018865f1eaSKarl Rupp     if (link->restricthook) {
25028865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
25038865f1eaSKarl Rupp     }
2504b17ce1afSJed Brown   }
250547c6ae99SBarry Smith   PetscFunctionReturn(0);
250647c6ae99SBarry Smith }
250747c6ae99SBarry Smith 
2508bb9467b5SJed Brown /*@C
2509be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
25105dbd56e3SPeter Brune 
25115dbd56e3SPeter Brune    Logically Collective
25125dbd56e3SPeter Brune 
25135dbd56e3SPeter Brune    Input Arguments:
25145dbd56e3SPeter Brune +  global - global DM
2515ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
25165dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
25170298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
25185dbd56e3SPeter Brune 
2519ec4806b8SPeter Brune 
2520ec4806b8SPeter Brune    Calling sequence for ddhook:
2521ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2522ec4806b8SPeter Brune 
2523ec4806b8SPeter Brune +  global - global DM
2524ec4806b8SPeter Brune .  block  - block DM
2525ec4806b8SPeter Brune -  ctx - optional user-defined function context
2526ec4806b8SPeter Brune 
25275dbd56e3SPeter Brune    Calling sequence for restricthook:
2528ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
25295dbd56e3SPeter Brune 
25305dbd56e3SPeter Brune +  global - global DM
25315dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
25325dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2533ec4806b8SPeter Brune .  block  - block DM
25345dbd56e3SPeter Brune -  ctx - optional user-defined function context
25355dbd56e3SPeter Brune 
25365dbd56e3SPeter Brune    Level: advanced
25375dbd56e3SPeter Brune 
25385dbd56e3SPeter Brune    Notes:
2539ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
25405dbd56e3SPeter Brune 
25415dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
25425dbd56e3SPeter Brune 
25435dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2544ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
25455dbd56e3SPeter Brune 
2546bb9467b5SJed Brown    This function is currently not available from Fortran.
2547bb9467b5SJed Brown 
25485dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
25495dbd56e3SPeter Brune @*/
2550be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
25515dbd56e3SPeter Brune {
25525dbd56e3SPeter Brune   PetscErrorCode      ierr;
2553be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
25545dbd56e3SPeter Brune 
25555dbd56e3SPeter Brune   PetscFunctionBegin;
25565dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2557be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
255895dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
25595dbd56e3SPeter Brune   link->restricthook = restricthook;
2560be081cd6SPeter Brune   link->ddhook       = ddhook;
25615dbd56e3SPeter Brune   link->ctx          = ctx;
25620298fd71SBarry Smith   link->next         = NULL;
25635dbd56e3SPeter Brune   *p                 = link;
25645dbd56e3SPeter Brune   PetscFunctionReturn(0);
25655dbd56e3SPeter Brune }
25665dbd56e3SPeter Brune 
25675dbd56e3SPeter Brune /*@
2568be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
25695dbd56e3SPeter Brune 
25705dbd56e3SPeter Brune    Collective if any hooks are
25715dbd56e3SPeter Brune 
25725dbd56e3SPeter Brune    Input Arguments:
25735dbd56e3SPeter Brune +  fine - finer DM to use as a base
2574be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2575be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
25765dbd56e3SPeter Brune -  coarse - coarer DM to update
25775dbd56e3SPeter Brune 
25785dbd56e3SPeter Brune    Level: developer
25795dbd56e3SPeter Brune 
25805dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
25815dbd56e3SPeter Brune @*/
2582be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
25835dbd56e3SPeter Brune {
25845dbd56e3SPeter Brune   PetscErrorCode      ierr;
2585be081cd6SPeter Brune   DMSubDomainHookLink link;
25865dbd56e3SPeter Brune 
25875dbd56e3SPeter Brune   PetscFunctionBegin;
2588be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
25898865f1eaSKarl Rupp     if (link->restricthook) {
25908865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
25918865f1eaSKarl Rupp     }
25925dbd56e3SPeter Brune   }
25935dbd56e3SPeter Brune   PetscFunctionReturn(0);
25945dbd56e3SPeter Brune }
25955dbd56e3SPeter Brune 
25965fe1f584SPeter Brune /*@
25976a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
25985fe1f584SPeter Brune 
25995fe1f584SPeter Brune     Not Collective
26005fe1f584SPeter Brune 
26015fe1f584SPeter Brune     Input Parameter:
26025fe1f584SPeter Brune .   dm - the DM object
26035fe1f584SPeter Brune 
26045fe1f584SPeter Brune     Output Parameter:
26056a7d9d85SPeter Brune .   level - number of coarsenings
26065fe1f584SPeter Brune 
26075fe1f584SPeter Brune     Level: developer
26085fe1f584SPeter Brune 
26096a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
26105fe1f584SPeter Brune 
26115fe1f584SPeter Brune @*/
26125fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
26135fe1f584SPeter Brune {
26145fe1f584SPeter Brune   PetscFunctionBegin;
26155fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26165fe1f584SPeter Brune   *level = dm->leveldown;
26175fe1f584SPeter Brune   PetscFunctionReturn(0);
26185fe1f584SPeter Brune }
26195fe1f584SPeter Brune 
26205fe1f584SPeter Brune 
26215fe1f584SPeter Brune 
262247c6ae99SBarry Smith /*@C
262347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
262447c6ae99SBarry Smith 
262547c6ae99SBarry Smith     Collective on DM
262647c6ae99SBarry Smith 
262747c6ae99SBarry Smith     Input Parameter:
262847c6ae99SBarry Smith +   dm - the DM object
262947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
263047c6ae99SBarry Smith 
263147c6ae99SBarry Smith     Output Parameter:
263247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
263347c6ae99SBarry Smith 
263447c6ae99SBarry Smith     Level: developer
263547c6ae99SBarry Smith 
2636e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
263747c6ae99SBarry Smith 
263847c6ae99SBarry Smith @*/
26397087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
264047c6ae99SBarry Smith {
264147c6ae99SBarry Smith   PetscErrorCode ierr;
264247c6ae99SBarry Smith 
264347c6ae99SBarry Smith   PetscFunctionBegin;
2644171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2645ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
264647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
264747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
264847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
264947c6ae99SBarry Smith   } else if (dm->ops->refine) {
265047c6ae99SBarry Smith     PetscInt i;
265147c6ae99SBarry Smith 
2652ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
265347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2654ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
265547c6ae99SBarry Smith     }
2656ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
265747c6ae99SBarry Smith   PetscFunctionReturn(0);
265847c6ae99SBarry Smith }
265947c6ae99SBarry Smith 
266047c6ae99SBarry Smith /*@C
266147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
266247c6ae99SBarry Smith 
266347c6ae99SBarry Smith     Collective on DM
266447c6ae99SBarry Smith 
266547c6ae99SBarry Smith     Input Parameter:
266647c6ae99SBarry Smith +   dm - the DM object
266747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
266847c6ae99SBarry Smith 
266947c6ae99SBarry Smith     Output Parameter:
267047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
267147c6ae99SBarry Smith 
267247c6ae99SBarry Smith     Level: developer
267347c6ae99SBarry Smith 
2674e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
267547c6ae99SBarry Smith 
267647c6ae99SBarry Smith @*/
26777087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
267847c6ae99SBarry Smith {
267947c6ae99SBarry Smith   PetscErrorCode ierr;
268047c6ae99SBarry Smith 
268147c6ae99SBarry Smith   PetscFunctionBegin;
2682171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2683ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
268447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
268547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
268647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
268747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
268847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
268947c6ae99SBarry Smith     PetscInt i;
269047c6ae99SBarry Smith 
2691ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
269247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2693ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
269447c6ae99SBarry Smith     }
2695ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
269647c6ae99SBarry Smith   PetscFunctionReturn(0);
269747c6ae99SBarry Smith }
269847c6ae99SBarry Smith 
269947c6ae99SBarry Smith /*@
2700e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
270147c6ae99SBarry Smith    grids associated with two DMs.
270247c6ae99SBarry Smith 
270347c6ae99SBarry Smith    Collective on DM
270447c6ae99SBarry Smith 
270547c6ae99SBarry Smith    Input Parameters:
270647c6ae99SBarry Smith +  dmc - the coarse grid DM
270747c6ae99SBarry Smith -  dmf - the fine grid DM
270847c6ae99SBarry Smith 
270947c6ae99SBarry Smith    Output Parameters:
271047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
271147c6ae99SBarry Smith 
271247c6ae99SBarry Smith    Level: intermediate
271347c6ae99SBarry Smith 
271447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
271547c6ae99SBarry Smith 
2716e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
271747c6ae99SBarry Smith @*/
2718e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
271947c6ae99SBarry Smith {
272047c6ae99SBarry Smith   PetscErrorCode ierr;
272147c6ae99SBarry Smith 
272247c6ae99SBarry Smith   PetscFunctionBegin;
2723171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2724171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
272547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
272647c6ae99SBarry Smith   PetscFunctionReturn(0);
272747c6ae99SBarry Smith }
272847c6ae99SBarry Smith 
27291a266240SBarry Smith /*@C
27301a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
27311a266240SBarry Smith 
27321a266240SBarry Smith     Not Collective
27331a266240SBarry Smith 
27341a266240SBarry Smith     Input Parameters:
27351a266240SBarry Smith +   dm - the DM object
27361a266240SBarry Smith -   destroy - the destroy function
27371a266240SBarry Smith 
27381a266240SBarry Smith     Level: intermediate
27391a266240SBarry Smith 
2740e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
27411a266240SBarry Smith 
2742f07f9ceaSJed Brown @*/
27431a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
27441a266240SBarry Smith {
27451a266240SBarry Smith   PetscFunctionBegin;
2746171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27471a266240SBarry Smith   dm->ctxdestroy = destroy;
27481a266240SBarry Smith   PetscFunctionReturn(0);
27491a266240SBarry Smith }
27501a266240SBarry Smith 
2751b07ff414SBarry Smith /*@
27521b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
275347c6ae99SBarry Smith 
275447c6ae99SBarry Smith     Not Collective
275547c6ae99SBarry Smith 
275647c6ae99SBarry Smith     Input Parameters:
275747c6ae99SBarry Smith +   dm - the DM object
275847c6ae99SBarry Smith -   ctx - the user context
275947c6ae99SBarry Smith 
276047c6ae99SBarry Smith     Level: intermediate
276147c6ae99SBarry Smith 
2762e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
276347c6ae99SBarry Smith 
276447c6ae99SBarry Smith @*/
27651b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
276647c6ae99SBarry Smith {
276747c6ae99SBarry Smith   PetscFunctionBegin;
2768171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
276947c6ae99SBarry Smith   dm->ctx = ctx;
277047c6ae99SBarry Smith   PetscFunctionReturn(0);
277147c6ae99SBarry Smith }
277247c6ae99SBarry Smith 
277347c6ae99SBarry Smith /*@
27741b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
277547c6ae99SBarry Smith 
277647c6ae99SBarry Smith     Not Collective
277747c6ae99SBarry Smith 
277847c6ae99SBarry Smith     Input Parameter:
277947c6ae99SBarry Smith .   dm - the DM object
278047c6ae99SBarry Smith 
278147c6ae99SBarry Smith     Output Parameter:
278247c6ae99SBarry Smith .   ctx - the user context
278347c6ae99SBarry Smith 
278447c6ae99SBarry Smith     Level: intermediate
278547c6ae99SBarry Smith 
2786e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
278747c6ae99SBarry Smith 
278847c6ae99SBarry Smith @*/
27891b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
279047c6ae99SBarry Smith {
279147c6ae99SBarry Smith   PetscFunctionBegin;
2792171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27931b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
279447c6ae99SBarry Smith   PetscFunctionReturn(0);
279547c6ae99SBarry Smith }
279647c6ae99SBarry Smith 
279708da532bSDmitry Karpeev /*@C
2798df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
279908da532bSDmitry Karpeev 
280008da532bSDmitry Karpeev     Logically Collective on DM
280108da532bSDmitry Karpeev 
280208da532bSDmitry Karpeev     Input Parameter:
280308da532bSDmitry Karpeev +   dm - the DM object
28040298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
280508da532bSDmitry Karpeev 
280608da532bSDmitry Karpeev     Level: intermediate
280708da532bSDmitry Karpeev 
2808835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
280908da532bSDmitry Karpeev          DMSetJacobian()
281008da532bSDmitry Karpeev 
281108da532bSDmitry Karpeev @*/
281208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
281308da532bSDmitry Karpeev {
281408da532bSDmitry Karpeev   PetscFunctionBegin;
281508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
281608da532bSDmitry Karpeev   PetscFunctionReturn(0);
281708da532bSDmitry Karpeev }
281808da532bSDmitry Karpeev 
281908da532bSDmitry Karpeev /*@
282008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
282108da532bSDmitry Karpeev 
282208da532bSDmitry Karpeev     Not Collective
282308da532bSDmitry Karpeev 
282408da532bSDmitry Karpeev     Input Parameter:
282508da532bSDmitry Karpeev .   dm - the DM object to destroy
282608da532bSDmitry Karpeev 
282708da532bSDmitry Karpeev     Output Parameter:
282808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
282908da532bSDmitry Karpeev 
283008da532bSDmitry Karpeev     Level: developer
283108da532bSDmitry Karpeev 
283274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
283308da532bSDmitry Karpeev 
283408da532bSDmitry Karpeev @*/
283508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
283608da532bSDmitry Karpeev {
283708da532bSDmitry Karpeev   PetscFunctionBegin;
283808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
283908da532bSDmitry Karpeev   PetscFunctionReturn(0);
284008da532bSDmitry Karpeev }
284108da532bSDmitry Karpeev 
284208da532bSDmitry Karpeev /*@C
284308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
284408da532bSDmitry Karpeev 
284508da532bSDmitry Karpeev     Logically Collective on DM
284608da532bSDmitry Karpeev 
284708da532bSDmitry Karpeev     Input Parameters:
2848907376e6SBarry Smith .   dm - the DM object
284908da532bSDmitry Karpeev 
285008da532bSDmitry Karpeev     Output parameters:
285108da532bSDmitry Karpeev +   xl - lower bound
285208da532bSDmitry Karpeev -   xu - upper bound
285308da532bSDmitry Karpeev 
2854907376e6SBarry Smith     Level: advanced
2855907376e6SBarry Smith 
2856907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
285708da532bSDmitry Karpeev 
285874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
285908da532bSDmitry Karpeev 
286008da532bSDmitry Karpeev @*/
286108da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
286208da532bSDmitry Karpeev {
286308da532bSDmitry Karpeev   PetscErrorCode ierr;
28645fd66863SKarl Rupp 
286508da532bSDmitry Karpeev   PetscFunctionBegin;
286608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
286708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
286808da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
286908da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
28708865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
287108da532bSDmitry Karpeev   PetscFunctionReturn(0);
287208da532bSDmitry Karpeev }
287308da532bSDmitry Karpeev 
2874b0ae01b7SPeter Brune /*@
2875b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2876b0ae01b7SPeter Brune 
2877b0ae01b7SPeter Brune     Not Collective
2878b0ae01b7SPeter Brune 
2879b0ae01b7SPeter Brune     Input Parameter:
2880b0ae01b7SPeter Brune .   dm - the DM object
2881b0ae01b7SPeter Brune 
2882b0ae01b7SPeter Brune     Output Parameter:
2883b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2884b0ae01b7SPeter Brune 
2885b0ae01b7SPeter Brune     Level: developer
2886b0ae01b7SPeter Brune 
2887b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2888b0ae01b7SPeter Brune 
2889b0ae01b7SPeter Brune @*/
2890b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2891b0ae01b7SPeter Brune {
2892b0ae01b7SPeter Brune   PetscFunctionBegin;
2893b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2894b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2895b0ae01b7SPeter Brune }
2896b0ae01b7SPeter Brune 
28973ad4599aSBarry Smith /*@
28983ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
28993ad4599aSBarry Smith 
29003ad4599aSBarry Smith     Not Collective
29013ad4599aSBarry Smith 
29023ad4599aSBarry Smith     Input Parameter:
29033ad4599aSBarry Smith .   dm - the DM object
29043ad4599aSBarry Smith 
29053ad4599aSBarry Smith     Output Parameter:
29063ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
29073ad4599aSBarry Smith 
29083ad4599aSBarry Smith     Level: developer
29093ad4599aSBarry Smith 
29103ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
29113ad4599aSBarry Smith 
29123ad4599aSBarry Smith @*/
29133ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
29143ad4599aSBarry Smith {
29153ad4599aSBarry Smith   PetscFunctionBegin;
29163ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
29173ad4599aSBarry Smith   PetscFunctionReturn(0);
29183ad4599aSBarry Smith }
29193ad4599aSBarry Smith 
2920748fac09SDmitry Karpeev /*@C
292108da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
292208da532bSDmitry Karpeev 
292308da532bSDmitry Karpeev     Collective on DM
292408da532bSDmitry Karpeev 
292508da532bSDmitry Karpeev     Input Parameter:
292608da532bSDmitry Karpeev +   dm - the DM object
29270298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
292808da532bSDmitry Karpeev 
292908da532bSDmitry Karpeev     Level: developer
293008da532bSDmitry Karpeev 
293174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
293208da532bSDmitry Karpeev 
293308da532bSDmitry Karpeev @*/
293408da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
293508da532bSDmitry Karpeev {
293608da532bSDmitry Karpeev   PetscErrorCode ierr;
29375fd66863SKarl Rupp 
293808da532bSDmitry Karpeev   PetscFunctionBegin;
293908da532bSDmitry Karpeev   if (x) {
294008da532bSDmitry Karpeev     if (!dm->x) {
294108da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
294208da532bSDmitry Karpeev     }
294308da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
29448865f1eaSKarl Rupp   } else if (dm->x) {
294508da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
294608da532bSDmitry Karpeev   }
294708da532bSDmitry Karpeev   PetscFunctionReturn(0);
294808da532bSDmitry Karpeev }
294908da532bSDmitry Karpeev 
29500298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2951264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2952264ace61SBarry Smith 
2953264ace61SBarry Smith /*@C
2954264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2955264ace61SBarry Smith 
2956264ace61SBarry Smith   Collective on DM
2957264ace61SBarry Smith 
2958264ace61SBarry Smith   Input Parameters:
2959264ace61SBarry Smith + dm     - The DM object
2960264ace61SBarry Smith - method - The name of the DM type
2961264ace61SBarry Smith 
2962264ace61SBarry Smith   Options Database Key:
2963264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2964264ace61SBarry Smith 
2965264ace61SBarry Smith   Notes:
2966e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2967264ace61SBarry Smith 
2968264ace61SBarry Smith   Level: intermediate
2969264ace61SBarry Smith 
2970264ace61SBarry Smith .keywords: DM, set, type
2971264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2972264ace61SBarry Smith @*/
297319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2974264ace61SBarry Smith {
2975264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2976264ace61SBarry Smith   PetscBool      match;
2977264ace61SBarry Smith   PetscErrorCode ierr;
2978264ace61SBarry Smith 
2979264ace61SBarry Smith   PetscFunctionBegin;
2980264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2981251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2982264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2983264ace61SBarry Smith 
29840f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
29851c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2986ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2987264ace61SBarry Smith 
2988264ace61SBarry Smith   if (dm->ops->destroy) {
2989264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
29900298fd71SBarry Smith     dm->ops->destroy = NULL;
2991264ace61SBarry Smith   }
2992264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2993264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2994264ace61SBarry Smith   PetscFunctionReturn(0);
2995264ace61SBarry Smith }
2996264ace61SBarry Smith 
2997264ace61SBarry Smith /*@C
2998264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2999264ace61SBarry Smith 
3000264ace61SBarry Smith   Not Collective
3001264ace61SBarry Smith 
3002264ace61SBarry Smith   Input Parameter:
3003264ace61SBarry Smith . dm  - The DM
3004264ace61SBarry Smith 
3005264ace61SBarry Smith   Output Parameter:
3006264ace61SBarry Smith . type - The DM type name
3007264ace61SBarry Smith 
3008264ace61SBarry Smith   Level: intermediate
3009264ace61SBarry Smith 
3010264ace61SBarry Smith .keywords: DM, get, type, name
3011264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3012264ace61SBarry Smith @*/
301319fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3014264ace61SBarry Smith {
3015264ace61SBarry Smith   PetscErrorCode ierr;
3016264ace61SBarry Smith 
3017264ace61SBarry Smith   PetscFunctionBegin;
3018264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3019c959eef4SJed Brown   PetscValidPointer(type,2);
3020607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3021264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3022264ace61SBarry Smith   PetscFunctionReturn(0);
3023264ace61SBarry Smith }
3024264ace61SBarry Smith 
302567a56275SMatthew G Knepley /*@C
302667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
302767a56275SMatthew G Knepley 
302867a56275SMatthew G Knepley   Collective on DM
302967a56275SMatthew G Knepley 
303067a56275SMatthew G Knepley   Input Parameters:
303167a56275SMatthew G Knepley + dm - the DM
303267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
303367a56275SMatthew G Knepley 
303467a56275SMatthew G Knepley   Output Parameter:
303567a56275SMatthew G Knepley . M - pointer to new DM
303667a56275SMatthew G Knepley 
303767a56275SMatthew G Knepley   Notes:
303867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
303967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
304067a56275SMatthew G Knepley   of the input DM.
304167a56275SMatthew G Knepley 
304267a56275SMatthew G Knepley   Level: intermediate
304367a56275SMatthew G Knepley 
304467a56275SMatthew G Knepley .seealso: DMCreate()
304567a56275SMatthew G Knepley @*/
304619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
304767a56275SMatthew G Knepley {
304867a56275SMatthew G Knepley   DM             B;
304967a56275SMatthew G Knepley   char           convname[256];
3050c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
305167a56275SMatthew G Knepley   PetscErrorCode ierr;
305267a56275SMatthew G Knepley 
305367a56275SMatthew G Knepley   PetscFunctionBegin;
305467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
305567a56275SMatthew G Knepley   PetscValidType(dm,1);
305667a56275SMatthew G Knepley   PetscValidPointer(M,3);
3057251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3058c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3059c067b6caSMatthew G. Knepley   if (sametype) {
3060c067b6caSMatthew G. Knepley     *M   = dm;
3061c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3062c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3063c067b6caSMatthew G. Knepley   } else {
30640298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
306567a56275SMatthew G Knepley 
306667a56275SMatthew G Knepley     /*
306767a56275SMatthew G Knepley        Order of precedence:
306867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
306967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
307067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
307167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
307267a56275SMatthew G Knepley        5) Use a really basic converter.
307367a56275SMatthew G Knepley     */
307467a56275SMatthew G Knepley 
307567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
307667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
307767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
307867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
307967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
308067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
30810005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
308267a56275SMatthew G Knepley     if (conv) goto foundconv;
308367a56275SMatthew G Knepley 
308467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
308582f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
308667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
308767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
308867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
308967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
309067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
309167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
30920005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
309367a56275SMatthew G Knepley     if (conv) {
3094fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
309567a56275SMatthew G Knepley       goto foundconv;
309667a56275SMatthew G Knepley     }
309767a56275SMatthew G Knepley 
309867a56275SMatthew G Knepley #if 0
309967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
310067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3101fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
310267a56275SMatthew G Knepley     if (conv) goto foundconv;
310367a56275SMatthew G Knepley 
310467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
310567a56275SMatthew G Knepley     if (dm->ops->convert) {
310667a56275SMatthew G Knepley       conv = dm->ops->convert;
310767a56275SMatthew G Knepley     }
310867a56275SMatthew G Knepley     if (conv) goto foundconv;
310967a56275SMatthew G Knepley #endif
311067a56275SMatthew G Knepley 
311167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
311282f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
311367a56275SMatthew G Knepley 
311467a56275SMatthew G Knepley foundconv:
311567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
311667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
311712fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
311890b157c4SStefano Zampini     {
311990b157c4SStefano Zampini       PetscBool             isper;
312012fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
312112fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
312290b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
312390b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
312412fa691eSMatthew G. Knepley     }
312567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
312667a56275SMatthew G Knepley   }
312767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
312867a56275SMatthew G Knepley   PetscFunctionReturn(0);
312967a56275SMatthew G Knepley }
3130264ace61SBarry Smith 
3131264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3132264ace61SBarry Smith 
3133264ace61SBarry Smith /*@C
31341c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
31351c84c290SBarry Smith 
31361c84c290SBarry Smith   Not Collective
31371c84c290SBarry Smith 
31381c84c290SBarry Smith   Input Parameters:
31391c84c290SBarry Smith + name        - The name of a new user-defined creation routine
31401c84c290SBarry Smith - create_func - The creation routine itself
31411c84c290SBarry Smith 
31421c84c290SBarry Smith   Notes:
31431c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
31441c84c290SBarry Smith 
31451c84c290SBarry Smith 
31461c84c290SBarry Smith   Sample usage:
31471c84c290SBarry Smith .vb
3148bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
31491c84c290SBarry Smith .ve
31501c84c290SBarry Smith 
31511c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
31521c84c290SBarry Smith .vb
31531c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
31541c84c290SBarry Smith     DMSetType(DM,"my_da");
31551c84c290SBarry Smith .ve
31561c84c290SBarry Smith    or at runtime via the option
31571c84c290SBarry Smith .vb
31581c84c290SBarry Smith     -da_type my_da
31591c84c290SBarry Smith .ve
3160264ace61SBarry Smith 
3161264ace61SBarry Smith   Level: advanced
31621c84c290SBarry Smith 
31631c84c290SBarry Smith .keywords: DM, register
3164bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
31651c84c290SBarry Smith 
3166264ace61SBarry Smith @*/
3167bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3168264ace61SBarry Smith {
3169264ace61SBarry Smith   PetscErrorCode ierr;
3170264ace61SBarry Smith 
3171264ace61SBarry Smith   PetscFunctionBegin;
3172a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3173264ace61SBarry Smith   PetscFunctionReturn(0);
3174264ace61SBarry Smith }
3175264ace61SBarry Smith 
3176b859378eSBarry Smith /*@C
317755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3178b859378eSBarry Smith 
3179b859378eSBarry Smith   Collective on PetscViewer
3180b859378eSBarry Smith 
3181b859378eSBarry Smith   Input Parameters:
3182b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3183b859378eSBarry Smith            some related function before a call to DMLoad().
3184b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3185b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3186b859378eSBarry Smith 
3187b859378eSBarry Smith    Level: intermediate
3188b859378eSBarry Smith 
3189b859378eSBarry Smith   Notes:
319055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3191b859378eSBarry Smith 
3192b859378eSBarry Smith   Notes for advanced users:
3193b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3194b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3195b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3196b859378eSBarry Smith   format is
3197b859378eSBarry Smith .vb
3198b859378eSBarry Smith      has not yet been determined
3199b859378eSBarry Smith .ve
3200b859378eSBarry Smith 
3201b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3202b859378eSBarry Smith @*/
3203b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3204b859378eSBarry Smith {
32059331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3206b859378eSBarry Smith   PetscErrorCode ierr;
3207b859378eSBarry Smith 
3208b859378eSBarry Smith   PetscFunctionBegin;
3209b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3210b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
321132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
32129331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
32139331c7a4SMatthew G. Knepley   if (isbinary) {
32149331c7a4SMatthew G. Knepley     PetscInt classid;
32159331c7a4SMatthew G. Knepley     char     type[256];
3216b859378eSBarry Smith 
3217060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
32189200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3219060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
322032c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
32219331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32229331c7a4SMatthew G. Knepley   } else if (ishdf5) {
32239331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32249331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3225b859378eSBarry Smith   PetscFunctionReturn(0);
3226b859378eSBarry Smith }
3227b859378eSBarry Smith 
32287da65231SMatthew G Knepley /******************************** FEM Support **********************************/
32297da65231SMatthew G Knepley 
3230a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3231a6dfd86eSKarl Rupp {
32321d47ebbbSSatish Balay   PetscInt       f;
32331b30c384SMatthew G Knepley   PetscErrorCode ierr;
32341b30c384SMatthew G Knepley 
32357da65231SMatthew G Knepley   PetscFunctionBegin;
323674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32371d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
323857622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
32397da65231SMatthew G Knepley   }
32407da65231SMatthew G Knepley   PetscFunctionReturn(0);
32417da65231SMatthew G Knepley }
32427da65231SMatthew G Knepley 
3243a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3244a6dfd86eSKarl Rupp {
32451b30c384SMatthew G Knepley   PetscInt       f, g;
32467da65231SMatthew G Knepley   PetscErrorCode ierr;
32477da65231SMatthew G Knepley 
32487da65231SMatthew G Knepley   PetscFunctionBegin;
324974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32501d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
325174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
32521d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3253e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
32547da65231SMatthew G Knepley     }
325574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
32567da65231SMatthew G Knepley   }
32577da65231SMatthew G Knepley   PetscFunctionReturn(0);
32587da65231SMatthew G Knepley }
3259e7c4fc90SDmitry Karpeev 
32606113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3261e759306cSMatthew G. Knepley {
32620c5b8624SToby Isaac   PetscInt          localSize, bs;
32630c5b8624SToby Isaac   PetscMPIInt       size;
32640c5b8624SToby Isaac   Vec               x, xglob;
32650c5b8624SToby Isaac   const PetscScalar *xarray;
3266e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3267e759306cSMatthew G. Knepley 
3268e759306cSMatthew G. Knepley   PetscFunctionBegin;
32699852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3270e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3271e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
32726113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
32730c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
32740c5b8624SToby Isaac   if (size > 1) {
32750c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
32760c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
32770c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
32780c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
32790c5b8624SToby Isaac   } else {
32800c5b8624SToby Isaac     xglob = x;
32810c5b8624SToby Isaac   }
32820c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
32830c5b8624SToby Isaac   if (size > 1) {
32840c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
32850c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
32860c5b8624SToby Isaac   }
3287e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3288e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3289e759306cSMatthew G. Knepley }
3290e759306cSMatthew G. Knepley 
329188ed4aceSMatthew G Knepley /*@
329288ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
329388ed4aceSMatthew G Knepley 
329488ed4aceSMatthew G Knepley   Input Parameter:
329588ed4aceSMatthew G Knepley . dm - The DM
329688ed4aceSMatthew G Knepley 
329788ed4aceSMatthew G Knepley   Output Parameter:
329888ed4aceSMatthew G Knepley . section - The PetscSection
329988ed4aceSMatthew G Knepley 
330088ed4aceSMatthew G Knepley   Level: intermediate
330188ed4aceSMatthew G Knepley 
330288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
330388ed4aceSMatthew G Knepley 
330488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
330588ed4aceSMatthew G Knepley @*/
33060adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
33070adebc6cSBarry Smith {
3308fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3309fd59a867SMatthew G. Knepley 
331088ed4aceSMatthew G Knepley   PetscFunctionBegin;
331188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
331288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
33132f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
33142f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3315ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
33162f0f8703SMatthew G. Knepley   }
331788ed4aceSMatthew G Knepley   *section = dm->defaultSection;
331888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
331988ed4aceSMatthew G Knepley }
332088ed4aceSMatthew G Knepley 
332188ed4aceSMatthew G Knepley /*@
332288ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
332388ed4aceSMatthew G Knepley 
332488ed4aceSMatthew G Knepley   Input Parameters:
332588ed4aceSMatthew G Knepley + dm - The DM
332688ed4aceSMatthew G Knepley - section - The PetscSection
332788ed4aceSMatthew G Knepley 
332888ed4aceSMatthew G Knepley   Level: intermediate
332988ed4aceSMatthew G Knepley 
333088ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
333188ed4aceSMatthew G Knepley 
333288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
333388ed4aceSMatthew G Knepley @*/
33340adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
33350adebc6cSBarry Smith {
3336c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3337af122d2aSMatthew G Knepley   PetscInt       f;
333888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
333988ed4aceSMatthew G Knepley 
334088ed4aceSMatthew G Knepley   PetscFunctionBegin;
334188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3342c473ab19SMatthew G. Knepley   if (section) {
33431d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33441d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3345c473ab19SMatthew G. Knepley   }
334688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
334788ed4aceSMatthew G Knepley   dm->defaultSection = section;
3348c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3349af122d2aSMatthew G Knepley   if (numFields) {
3350af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3351af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
33520f21e855SMatthew G. Knepley       PetscObject disc;
3353af122d2aSMatthew G Knepley       const char *name;
3354af122d2aSMatthew G Knepley 
3355af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
33560f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
33570f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3358af122d2aSMatthew G Knepley     }
3359af122d2aSMatthew G Knepley   }
33601d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
33611d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
336288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
336388ed4aceSMatthew G Knepley }
336488ed4aceSMatthew G Knepley 
33659435951eSToby Isaac /*@
33669435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
33679435951eSToby Isaac 
3368e228b242SToby Isaac   not collective
3369e228b242SToby Isaac 
33709435951eSToby Isaac   Input Parameter:
33719435951eSToby Isaac . dm - The DM
33729435951eSToby Isaac 
33739435951eSToby Isaac   Output Parameter:
33749435951eSToby 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.
33759435951eSToby 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.
33769435951eSToby Isaac 
33779435951eSToby Isaac   Level: advanced
33789435951eSToby Isaac 
33799435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
33809435951eSToby Isaac 
33819435951eSToby Isaac .seealso: DMSetDefaultConstraints()
33829435951eSToby Isaac @*/
33839435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
33849435951eSToby Isaac {
33859435951eSToby Isaac   PetscErrorCode ierr;
33869435951eSToby Isaac 
33879435951eSToby Isaac   PetscFunctionBegin;
33889435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33899435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
339045a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
339145a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
33929435951eSToby Isaac   PetscFunctionReturn(0);
33939435951eSToby Isaac }
33949435951eSToby Isaac 
33959435951eSToby Isaac /*@
33969435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
33979435951eSToby Isaac 
33989435951eSToby 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().
33999435951eSToby Isaac 
34009435951eSToby 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.
34019435951eSToby Isaac 
3402e228b242SToby Isaac   collective on dm
3403e228b242SToby Isaac 
34049435951eSToby Isaac   Input Parameters:
34059435951eSToby Isaac + dm - The DM
3406e228b242SToby 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).
3407e228b242SToby 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).
34089435951eSToby Isaac 
34099435951eSToby Isaac   Level: advanced
34109435951eSToby Isaac 
34119435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
34129435951eSToby Isaac 
34139435951eSToby Isaac .seealso: DMGetDefaultConstraints()
34149435951eSToby Isaac @*/
34159435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
34169435951eSToby Isaac {
3417e228b242SToby Isaac   PetscMPIInt result;
34189435951eSToby Isaac   PetscErrorCode ierr;
34199435951eSToby Isaac 
34209435951eSToby Isaac   PetscFunctionBegin;
34219435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3422e228b242SToby Isaac   if (section) {
3423e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3424e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3425f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3426e228b242SToby Isaac   }
3427e228b242SToby Isaac   if (mat) {
3428e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3429e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3430f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3431e228b242SToby Isaac   }
34329435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
34339435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
34349435951eSToby Isaac   dm->defaultConstraintSection = section;
34359435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
34369435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
34379435951eSToby Isaac   dm->defaultConstraintMat = mat;
34389435951eSToby Isaac   PetscFunctionReturn(0);
34399435951eSToby Isaac }
34409435951eSToby Isaac 
3441f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3442507e4973SMatthew G. Knepley /*
3443507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3444507e4973SMatthew G. Knepley 
3445507e4973SMatthew G. Knepley   Input Parameters:
3446507e4973SMatthew G. Knepley + dm - The DM
3447507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3448507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3449507e4973SMatthew G. Knepley 
3450507e4973SMatthew G. Knepley   Level: intermediate
3451507e4973SMatthew G. Knepley 
3452507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3453507e4973SMatthew G. Knepley */
3454f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3455507e4973SMatthew G. Knepley {
3456507e4973SMatthew G. Knepley   MPI_Comm        comm;
3457507e4973SMatthew G. Knepley   PetscLayout     layout;
3458507e4973SMatthew G. Knepley   const PetscInt *ranges;
3459507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3460507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3461507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3462507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3463507e4973SMatthew G. Knepley 
3464507e4973SMatthew G. Knepley   PetscFunctionBegin;
3465507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3466507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3467507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3468507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3469507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3470507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3471507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3472507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3473507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3474507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3475507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3476507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3477f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3478507e4973SMatthew G. Knepley 
3479507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3480507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3481507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3482507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3483507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3484507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3485507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3486507e4973SMatthew 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;}
3487507e4973SMatthew 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;}
3488507e4973SMatthew G. Knepley     if (gdof < 0) {
3489507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3490507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3491507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3492507e4973SMatthew G. Knepley 
3493507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3494507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3495507e4973SMatthew 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;}
3496507e4973SMatthew G. Knepley       }
3497507e4973SMatthew G. Knepley     }
3498507e4973SMatthew G. Knepley   }
3499507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3500507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3501b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3502507e4973SMatthew G. Knepley   if (!gvalid) {
3503507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3504507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3505507e4973SMatthew G. Knepley   }
3506507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3507507e4973SMatthew G. Knepley }
3508f741bcd2SMatthew G. Knepley #endif
3509507e4973SMatthew G. Knepley 
351088ed4aceSMatthew G Knepley /*@
351188ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
351288ed4aceSMatthew G Knepley 
35138b1ab98fSJed Brown   Collective on DM
35148b1ab98fSJed Brown 
351588ed4aceSMatthew G Knepley   Input Parameter:
351688ed4aceSMatthew G Knepley . dm - The DM
351788ed4aceSMatthew G Knepley 
351888ed4aceSMatthew G Knepley   Output Parameter:
351988ed4aceSMatthew G Knepley . section - The PetscSection
352088ed4aceSMatthew G Knepley 
352188ed4aceSMatthew G Knepley   Level: intermediate
352288ed4aceSMatthew G Knepley 
352388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
352488ed4aceSMatthew G Knepley 
352588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
352688ed4aceSMatthew G Knepley @*/
35270adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
35280adebc6cSBarry Smith {
352988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
353088ed4aceSMatthew G Knepley 
353188ed4aceSMatthew G Knepley   PetscFunctionBegin;
353288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
353388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
353488ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3535fd59a867SMatthew G. Knepley     PetscSection s;
3536fd59a867SMatthew G. Knepley 
3537fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3538fd59a867SMatthew 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");
3539fd59a867SMatthew 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");
354015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3541cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3542ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3543685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
354488ed4aceSMatthew G Knepley   }
354588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
354688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
354788ed4aceSMatthew G Knepley }
354888ed4aceSMatthew G Knepley 
3549b21d0597SMatthew G Knepley /*@
3550b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3551b21d0597SMatthew G Knepley 
3552b21d0597SMatthew G Knepley   Input Parameters:
3553b21d0597SMatthew G Knepley + dm - The DM
35545080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3555b21d0597SMatthew G Knepley 
3556b21d0597SMatthew G Knepley   Level: intermediate
3557b21d0597SMatthew G Knepley 
3558b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3559b21d0597SMatthew G Knepley 
3560b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3561b21d0597SMatthew G Knepley @*/
35620adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
35630adebc6cSBarry Smith {
3564b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3565b21d0597SMatthew G Knepley 
3566b21d0597SMatthew G Knepley   PetscFunctionBegin;
3567b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35685080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
35691d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3570b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3571b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3572507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3573f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3574507e4973SMatthew G. Knepley #endif
3575b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3576b21d0597SMatthew G Knepley }
3577b21d0597SMatthew G Knepley 
357888ed4aceSMatthew G Knepley /*@
357988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
358088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
358188ed4aceSMatthew G Knepley 
358288ed4aceSMatthew G Knepley   Input Parameter:
358388ed4aceSMatthew G Knepley . dm - The DM
358488ed4aceSMatthew G Knepley 
358588ed4aceSMatthew G Knepley   Output Parameter:
358688ed4aceSMatthew G Knepley . sf - The PetscSF
358788ed4aceSMatthew G Knepley 
358888ed4aceSMatthew G Knepley   Level: intermediate
358988ed4aceSMatthew G Knepley 
359088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
359188ed4aceSMatthew G Knepley 
359288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
359388ed4aceSMatthew G Knepley @*/
35940adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
35950adebc6cSBarry Smith {
359688ed4aceSMatthew G Knepley   PetscInt       nroots;
359788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
359888ed4aceSMatthew G Knepley 
359988ed4aceSMatthew G Knepley   PetscFunctionBegin;
360088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
360188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
36020298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
360388ed4aceSMatthew G Knepley   if (nroots < 0) {
360488ed4aceSMatthew G Knepley     PetscSection section, gSection;
360588ed4aceSMatthew G Knepley 
360688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
360731ea6d37SMatthew G Knepley     if (section) {
360888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
360988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
361031ea6d37SMatthew G Knepley     } else {
36110298fd71SBarry Smith       *sf = NULL;
361231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
361331ea6d37SMatthew G Knepley     }
361488ed4aceSMatthew G Knepley   }
361588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
361688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
361788ed4aceSMatthew G Knepley }
361888ed4aceSMatthew G Knepley 
361988ed4aceSMatthew G Knepley /*@
362088ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
362188ed4aceSMatthew G Knepley 
362288ed4aceSMatthew G Knepley   Input Parameters:
362388ed4aceSMatthew G Knepley + dm - The DM
362488ed4aceSMatthew G Knepley - sf - The PetscSF
362588ed4aceSMatthew G Knepley 
362688ed4aceSMatthew G Knepley   Level: intermediate
362788ed4aceSMatthew G Knepley 
362888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
362988ed4aceSMatthew G Knepley 
363088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
363188ed4aceSMatthew G Knepley @*/
36320adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
36330adebc6cSBarry Smith {
363488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
363588ed4aceSMatthew G Knepley 
363688ed4aceSMatthew G Knepley   PetscFunctionBegin;
363788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
363888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
363988ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
364088ed4aceSMatthew G Knepley   dm->defaultSF = sf;
364188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
364288ed4aceSMatthew G Knepley }
364388ed4aceSMatthew G Knepley 
364488ed4aceSMatthew G Knepley /*@C
364588ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
364688ed4aceSMatthew G Knepley   describing the data layout.
364788ed4aceSMatthew G Knepley 
364888ed4aceSMatthew G Knepley   Input Parameters:
364988ed4aceSMatthew G Knepley + dm - The DM
365088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
365188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
365288ed4aceSMatthew G Knepley 
365388ed4aceSMatthew G Knepley   Level: intermediate
365488ed4aceSMatthew G Knepley 
365588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
365688ed4aceSMatthew G Knepley @*/
365788ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
365888ed4aceSMatthew G Knepley {
365982f516ccSBarry Smith   MPI_Comm       comm;
366088ed4aceSMatthew G Knepley   PetscLayout    layout;
366188ed4aceSMatthew G Knepley   const PetscInt *ranges;
366288ed4aceSMatthew G Knepley   PetscInt       *local;
366388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3664ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
366588ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
366688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
366788ed4aceSMatthew G Knepley 
366888ed4aceSMatthew G Knepley   PetscFunctionBegin;
366982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
367088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
367188ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
367288ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
367388ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
367488ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
367588ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
367688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
367788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
367888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
367988ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3680ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
36816636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
368288ed4aceSMatthew G Knepley 
36836636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
36846636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3685235fbf56SMatthew 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));
36866636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
368788ed4aceSMatthew G Knepley   }
3688785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3689785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
369088ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
36911f588964SMatthew G Knepley     const PetscInt *cind;
36926636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
369388ed4aceSMatthew G Knepley 
369488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
369588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
369688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
369788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
369888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
36996636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
370088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
37016636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
37026636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
37036636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3704057b4bcdSMatthew 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);
37056636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
37066636e97aSMatthew G Knepley     }
370788ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
370888ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
370988ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
371088ed4aceSMatthew G Knepley     }
371188ed4aceSMatthew G Knepley     if (gdof < 0) {
37126636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
371388ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
371488ed4aceSMatthew G Knepley 
371505376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
371631d3f06eSJed Brown         if (r < 0) r = -(r+2);
371705376888SMatthew 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);
371888ed4aceSMatthew G Knepley         remote[l].rank  = r;
371988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
372088ed4aceSMatthew G Knepley       }
372188ed4aceSMatthew G Knepley     } else {
37226636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
372388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
372488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
372588ed4aceSMatthew G Knepley       }
372688ed4aceSMatthew G Knepley     }
372788ed4aceSMatthew G Knepley   }
37286636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
372988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
373088ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
373188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
373288ed4aceSMatthew G Knepley }
3733af122d2aSMatthew G Knepley 
3734b21d0597SMatthew G Knepley /*@
3735b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3736b21d0597SMatthew G Knepley 
3737b21d0597SMatthew G Knepley   Input Parameter:
3738b21d0597SMatthew G Knepley . dm - The DM
3739b21d0597SMatthew G Knepley 
3740b21d0597SMatthew G Knepley   Output Parameter:
3741b21d0597SMatthew G Knepley . sf - The PetscSF
3742b21d0597SMatthew G Knepley 
3743b21d0597SMatthew G Knepley   Level: intermediate
3744b21d0597SMatthew G Knepley 
3745b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3746b21d0597SMatthew G Knepley 
3747057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3748b21d0597SMatthew G Knepley @*/
37490adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
37500adebc6cSBarry Smith {
3751b21d0597SMatthew G Knepley   PetscFunctionBegin;
3752b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3753b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3754b21d0597SMatthew G Knepley   *sf = dm->sf;
3755b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3756b21d0597SMatthew G Knepley }
3757b21d0597SMatthew G Knepley 
3758057b4bcdSMatthew G Knepley /*@
3759057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3760057b4bcdSMatthew G Knepley 
3761057b4bcdSMatthew G Knepley   Input Parameters:
3762057b4bcdSMatthew G Knepley + dm - The DM
3763057b4bcdSMatthew G Knepley - sf - The PetscSF
3764057b4bcdSMatthew G Knepley 
3765057b4bcdSMatthew G Knepley   Level: intermediate
3766057b4bcdSMatthew G Knepley 
3767057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3768057b4bcdSMatthew G Knepley @*/
37690adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
37700adebc6cSBarry Smith {
3771057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3772057b4bcdSMatthew G Knepley 
3773057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3774057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3775057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3776057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3777057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3778057b4bcdSMatthew G Knepley   dm->sf = sf;
3779057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3780057b4bcdSMatthew G Knepley }
3781057b4bcdSMatthew G Knepley 
37822764a2aaSMatthew G. Knepley /*@
37832764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
37842764a2aaSMatthew G. Knepley 
37852764a2aaSMatthew G. Knepley   Input Parameter:
37862764a2aaSMatthew G. Knepley . dm - The DM
37872764a2aaSMatthew G. Knepley 
37882764a2aaSMatthew G. Knepley   Output Parameter:
37892764a2aaSMatthew G. Knepley . prob - The PetscDS
37902764a2aaSMatthew G. Knepley 
37912764a2aaSMatthew G. Knepley   Level: developer
37922764a2aaSMatthew G. Knepley 
37932764a2aaSMatthew G. Knepley .seealso: DMSetDS()
37942764a2aaSMatthew G. Knepley @*/
37952764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3796af122d2aSMatthew G Knepley {
3797af122d2aSMatthew G Knepley   PetscFunctionBegin;
3798af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37990f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
38000f21e855SMatthew G. Knepley   *prob = dm->prob;
38010f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38020f21e855SMatthew G. Knepley }
38030f21e855SMatthew G. Knepley 
38042764a2aaSMatthew G. Knepley /*@
38052764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
38062764a2aaSMatthew G. Knepley 
38072764a2aaSMatthew G. Knepley   Input Parameters:
38082764a2aaSMatthew G. Knepley + dm - The DM
38092764a2aaSMatthew G. Knepley - prob - The PetscDS
38102764a2aaSMatthew G. Knepley 
38112764a2aaSMatthew G. Knepley   Level: developer
38122764a2aaSMatthew G. Knepley 
38132764a2aaSMatthew G. Knepley .seealso: DMGetDS()
38142764a2aaSMatthew G. Knepley @*/
38152764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
38160f21e855SMatthew G. Knepley {
38170f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38180f21e855SMatthew G. Knepley 
38190f21e855SMatthew G. Knepley   PetscFunctionBegin;
38200f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38212764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
382237316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
38232764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
38240f21e855SMatthew G. Knepley   dm->prob = prob;
38250f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38260f21e855SMatthew G. Knepley }
38270f21e855SMatthew G. Knepley 
38280f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
38290f21e855SMatthew G. Knepley {
38300f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38310f21e855SMatthew G. Knepley 
38320f21e855SMatthew G. Knepley   PetscFunctionBegin;
38330f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38342764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3835af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3836af122d2aSMatthew G Knepley }
3837af122d2aSMatthew G Knepley 
3838af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3839af122d2aSMatthew G Knepley {
38400f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3841af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3842af122d2aSMatthew G Knepley 
3843af122d2aSMatthew G Knepley   PetscFunctionBegin;
3844af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38452764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
38460f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
38470f21e855SMatthew G. Knepley     PetscContainer obj;
38480f21e855SMatthew G. Knepley 
38490f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
38502764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
38510f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3852af122d2aSMatthew G Knepley   }
3853af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3854af122d2aSMatthew G Knepley }
3855af122d2aSMatthew G Knepley 
3856c1929be8SMatthew G. Knepley /*@
3857c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3858c1929be8SMatthew G. Knepley 
3859c1929be8SMatthew G. Knepley   Not collective
3860c1929be8SMatthew G. Knepley 
3861c1929be8SMatthew G. Knepley   Input Parameters:
3862c1929be8SMatthew G. Knepley + dm - The DM
3863c1929be8SMatthew G. Knepley - f  - The field number
3864c1929be8SMatthew G. Knepley 
3865c1929be8SMatthew G. Knepley   Output Parameter:
3866c1929be8SMatthew G. Knepley . field - The discretization object
3867c1929be8SMatthew G. Knepley 
3868c1929be8SMatthew G. Knepley   Level: developer
3869c1929be8SMatthew G. Knepley 
3870c1929be8SMatthew G. Knepley .seealso: DMSetField()
3871c1929be8SMatthew G. Knepley @*/
3872af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3873af122d2aSMatthew G Knepley {
38740f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38750f21e855SMatthew G. Knepley 
3876af122d2aSMatthew G Knepley   PetscFunctionBegin;
3877af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38782764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3879decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3880decb47aaSMatthew G. Knepley }
3881decb47aaSMatthew G. Knepley 
3882c1929be8SMatthew G. Knepley /*@
3883c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3884c1929be8SMatthew G. Knepley 
3885c1929be8SMatthew G. Knepley   Logically collective on DM
3886c1929be8SMatthew G. Knepley 
3887c1929be8SMatthew G. Knepley   Input Parameters:
3888c1929be8SMatthew G. Knepley + dm - The DM
3889c1929be8SMatthew G. Knepley . f  - The field number
3890c1929be8SMatthew G. Knepley - field - The discretization object
3891c1929be8SMatthew G. Knepley 
3892c1929be8SMatthew G. Knepley   Level: developer
3893c1929be8SMatthew G. Knepley 
3894c1929be8SMatthew G. Knepley .seealso: DMGetField()
3895c1929be8SMatthew G. Knepley @*/
3896decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3897decb47aaSMatthew G. Knepley {
3898decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3899decb47aaSMatthew G. Knepley 
3900decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3901decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39022764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3903af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3904af122d2aSMatthew G Knepley }
39056636e97aSMatthew G Knepley 
3906b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3907b64e0483SPeter Brune {
3908b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3909b64e0483SPeter Brune   PetscErrorCode ierr;
3910b64e0483SPeter Brune   Vec coords,ccoords;
39116dbf9973SLawrence Mitchell   Mat inject;
3912b64e0483SPeter Brune   PetscFunctionBegin;
3913b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3914b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3915b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3916b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3917b64e0483SPeter Brune   if (coords && !ccoords) {
3918b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
39196668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
39206dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
39212adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
39226dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3923b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3924b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3925b64e0483SPeter Brune   }
3926b64e0483SPeter Brune   PetscFunctionReturn(0);
3927b64e0483SPeter Brune }
3928b64e0483SPeter Brune 
392903dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
393003dadc2fSPeter Brune {
393103dadc2fSPeter Brune   DM dm_coord,subdm_coord;
393203dadc2fSPeter Brune   PetscErrorCode ierr;
393303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
393403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
393503dadc2fSPeter Brune   PetscFunctionBegin;
393603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
393703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
393803dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
393903dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
394003dadc2fSPeter Brune   if (coords && !ccoords) {
394103dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
39426668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
394303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
394424640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
394503dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
394603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
394703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
394803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
394903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
395003dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
395103dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
395203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
395303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
395403dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
395503dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
395603dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
395703dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
395803dadc2fSPeter Brune   }
395903dadc2fSPeter Brune   PetscFunctionReturn(0);
396003dadc2fSPeter Brune }
396103dadc2fSPeter Brune 
3962c73cfb54SMatthew G. Knepley /*@
3963c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3964c73cfb54SMatthew G. Knepley 
3965c73cfb54SMatthew G. Knepley   Not collective
3966c73cfb54SMatthew G. Knepley 
3967c73cfb54SMatthew G. Knepley   Input Parameter:
3968c73cfb54SMatthew G. Knepley . dm - The DM
3969c73cfb54SMatthew G. Knepley 
3970c73cfb54SMatthew G. Knepley   Output Parameter:
3971c73cfb54SMatthew G. Knepley . dim - The topological dimension
3972c73cfb54SMatthew G. Knepley 
3973c73cfb54SMatthew G. Knepley   Level: beginner
3974c73cfb54SMatthew G. Knepley 
3975c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3976c73cfb54SMatthew G. Knepley @*/
3977c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3978c73cfb54SMatthew G. Knepley {
3979c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3980c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3981c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3982c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3983c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3984c73cfb54SMatthew G. Knepley }
3985c73cfb54SMatthew G. Knepley 
3986c73cfb54SMatthew G. Knepley /*@
3987c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3988c73cfb54SMatthew G. Knepley 
3989c73cfb54SMatthew G. Knepley   Collective on dm
3990c73cfb54SMatthew G. Knepley 
3991c73cfb54SMatthew G. Knepley   Input Parameters:
3992c73cfb54SMatthew G. Knepley + dm - The DM
3993c73cfb54SMatthew G. Knepley - dim - The topological dimension
3994c73cfb54SMatthew G. Knepley 
3995c73cfb54SMatthew G. Knepley   Level: beginner
3996c73cfb54SMatthew G. Knepley 
3997c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3998c73cfb54SMatthew G. Knepley @*/
3999c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4000c73cfb54SMatthew G. Knepley {
4001c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4002c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4003c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4004c73cfb54SMatthew G. Knepley   dm->dim = dim;
4005c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4006c73cfb54SMatthew G. Knepley }
4007c73cfb54SMatthew G. Knepley 
4008793f3fe5SMatthew G. Knepley /*@
4009793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4010793f3fe5SMatthew G. Knepley 
4011793f3fe5SMatthew G. Knepley   Collective on DM
4012793f3fe5SMatthew G. Knepley 
4013793f3fe5SMatthew G. Knepley   Input Parameters:
4014793f3fe5SMatthew G. Knepley + dm - the DM
4015793f3fe5SMatthew G. Knepley - dim - the dimension
4016793f3fe5SMatthew G. Knepley 
4017793f3fe5SMatthew G. Knepley   Output Parameters:
4018793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4019793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4020793f3fe5SMatthew G. Knepley 
4021793f3fe5SMatthew G. Knepley   Note:
4022793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4023793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4024793f3fe5SMatthew G. Knepley   then the interval is empty.
4025793f3fe5SMatthew G. Knepley 
4026793f3fe5SMatthew G. Knepley   Level: intermediate
4027793f3fe5SMatthew G. Knepley 
4028793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4029793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4030793f3fe5SMatthew G. Knepley @*/
4031793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4032793f3fe5SMatthew G. Knepley {
4033793f3fe5SMatthew G. Knepley   PetscInt       d;
4034793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4035793f3fe5SMatthew G. Knepley 
4036793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4037793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4038793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4039793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4040793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4041793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4042793f3fe5SMatthew G. Knepley }
4043793f3fe5SMatthew G. Knepley 
40446636e97aSMatthew G Knepley /*@
40456636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
40466636e97aSMatthew G Knepley 
40476636e97aSMatthew G Knepley   Collective on DM
40486636e97aSMatthew G Knepley 
40496636e97aSMatthew G Knepley   Input Parameters:
40506636e97aSMatthew G Knepley + dm - the DM
40516636e97aSMatthew G Knepley - c - coordinate vector
40526636e97aSMatthew G Knepley 
40536636e97aSMatthew G Knepley   Note:
40546636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
40556636e97aSMatthew G Knepley 
40566636e97aSMatthew G Knepley   Level: intermediate
40576636e97aSMatthew G Knepley 
40586636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40596636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
40606636e97aSMatthew G Knepley @*/
40616636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
40626636e97aSMatthew G Knepley {
40636636e97aSMatthew G Knepley   PetscErrorCode ierr;
40646636e97aSMatthew G Knepley 
40656636e97aSMatthew G Knepley   PetscFunctionBegin;
40666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40676636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
40686636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
40696636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
40706636e97aSMatthew G Knepley   dm->coordinates = c;
40716636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4072b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
407303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
40746636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40756636e97aSMatthew G Knepley }
40766636e97aSMatthew G Knepley 
40776636e97aSMatthew G Knepley /*@
40786636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
40796636e97aSMatthew G Knepley 
40806636e97aSMatthew G Knepley   Collective on DM
40816636e97aSMatthew G Knepley 
40826636e97aSMatthew G Knepley    Input Parameters:
40836636e97aSMatthew G Knepley +  dm - the DM
40846636e97aSMatthew G Knepley -  c - coordinate vector
40856636e97aSMatthew G Knepley 
40866636e97aSMatthew G Knepley   Note:
40876636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
40886636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
40896636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
40906636e97aSMatthew G Knepley 
40916636e97aSMatthew G Knepley   Level: intermediate
40926636e97aSMatthew G Knepley 
40936636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40946636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
40956636e97aSMatthew G Knepley @*/
40966636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
40976636e97aSMatthew G Knepley {
40986636e97aSMatthew G Knepley   PetscErrorCode ierr;
40996636e97aSMatthew G Knepley 
41006636e97aSMatthew G Knepley   PetscFunctionBegin;
41016636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41026636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41036636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41046636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
41058865f1eaSKarl Rupp 
41066636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
41078865f1eaSKarl Rupp 
41086636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41096636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41106636e97aSMatthew G Knepley }
41116636e97aSMatthew G Knepley 
41126636e97aSMatthew G Knepley /*@
41136636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
41146636e97aSMatthew G Knepley 
41156636e97aSMatthew G Knepley   Not Collective
41166636e97aSMatthew G Knepley 
41176636e97aSMatthew G Knepley   Input Parameter:
41186636e97aSMatthew G Knepley . dm - the DM
41196636e97aSMatthew G Knepley 
41206636e97aSMatthew G Knepley   Output Parameter:
41216636e97aSMatthew G Knepley . c - global coordinate vector
41226636e97aSMatthew G Knepley 
41236636e97aSMatthew G Knepley   Note:
41246636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
41256636e97aSMatthew G Knepley 
41266636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
41276636e97aSMatthew G Knepley 
41286636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
41296636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
41306636e97aSMatthew G Knepley 
41316636e97aSMatthew G Knepley   Level: intermediate
41326636e97aSMatthew G Knepley 
41336636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41346636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
41356636e97aSMatthew G Knepley @*/
41366636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
41376636e97aSMatthew G Knepley {
41386636e97aSMatthew G Knepley   PetscErrorCode ierr;
41396636e97aSMatthew G Knepley 
41406636e97aSMatthew G Knepley   PetscFunctionBegin;
41416636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41426636e97aSMatthew G Knepley   PetscValidPointer(c,2);
41431f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
41440298fd71SBarry Smith     DM cdm = NULL;
41456636e97aSMatthew G Knepley 
41466636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
41476636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
41486636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
41496636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
41506636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
41516636e97aSMatthew G Knepley   }
41526636e97aSMatthew G Knepley   *c = dm->coordinates;
41536636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41546636e97aSMatthew G Knepley }
41556636e97aSMatthew G Knepley 
41566636e97aSMatthew G Knepley /*@
41576636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
41586636e97aSMatthew G Knepley 
41596636e97aSMatthew G Knepley   Collective on DM
41606636e97aSMatthew G Knepley 
41616636e97aSMatthew G Knepley   Input Parameter:
41626636e97aSMatthew G Knepley . dm - the DM
41636636e97aSMatthew G Knepley 
41646636e97aSMatthew G Knepley   Output Parameter:
41656636e97aSMatthew G Knepley . c - coordinate vector
41666636e97aSMatthew G Knepley 
41676636e97aSMatthew G Knepley   Note:
41686636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
41696636e97aSMatthew G Knepley 
41706636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
41716636e97aSMatthew G Knepley 
41726636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
41736636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
41746636e97aSMatthew G Knepley 
41756636e97aSMatthew G Knepley   Level: intermediate
41766636e97aSMatthew G Knepley 
41776636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41786636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
41796636e97aSMatthew G Knepley @*/
41806636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
41816636e97aSMatthew G Knepley {
41826636e97aSMatthew G Knepley   PetscErrorCode ierr;
41836636e97aSMatthew G Knepley 
41846636e97aSMatthew G Knepley   PetscFunctionBegin;
41856636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41866636e97aSMatthew G Knepley   PetscValidPointer(c,2);
41871f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
41880298fd71SBarry Smith     DM cdm = NULL;
41896636e97aSMatthew G Knepley 
41906636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
41916636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
41926636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
41936636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
41946636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
41956636e97aSMatthew G Knepley   }
41966636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
41976636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41986636e97aSMatthew G Knepley }
41996636e97aSMatthew G Knepley 
42006636e97aSMatthew G Knepley /*@
42011cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
42026636e97aSMatthew G Knepley 
42036636e97aSMatthew G Knepley   Collective on DM
42046636e97aSMatthew G Knepley 
42056636e97aSMatthew G Knepley   Input Parameter:
42066636e97aSMatthew G Knepley . dm - the DM
42076636e97aSMatthew G Knepley 
42086636e97aSMatthew G Knepley   Output Parameter:
42096636e97aSMatthew G Knepley . cdm - coordinate DM
42106636e97aSMatthew G Knepley 
42116636e97aSMatthew G Knepley   Level: intermediate
42126636e97aSMatthew G Knepley 
42136636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42141cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
42156636e97aSMatthew G Knepley @*/
42166636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
42176636e97aSMatthew G Knepley {
42186636e97aSMatthew G Knepley   PetscErrorCode ierr;
42196636e97aSMatthew G Knepley 
42206636e97aSMatthew G Knepley   PetscFunctionBegin;
42216636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42226636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
42236636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
422482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
42256636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
42266636e97aSMatthew G Knepley   }
42276636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
42286636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42296636e97aSMatthew G Knepley }
4230e87bb0d3SMatthew G Knepley 
42311cfe2091SMatthew G. Knepley /*@
42321cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
42331cfe2091SMatthew G. Knepley 
42341cfe2091SMatthew G. Knepley   Logically Collective on DM
42351cfe2091SMatthew G. Knepley 
42361cfe2091SMatthew G. Knepley   Input Parameters:
42371cfe2091SMatthew G. Knepley + dm - the DM
42381cfe2091SMatthew G. Knepley - cdm - coordinate DM
42391cfe2091SMatthew G. Knepley 
42401cfe2091SMatthew G. Knepley   Level: intermediate
42411cfe2091SMatthew G. Knepley 
42421cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42431cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
42441cfe2091SMatthew G. Knepley @*/
42451cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
42461cfe2091SMatthew G. Knepley {
42471cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
42481cfe2091SMatthew G. Knepley 
42491cfe2091SMatthew G. Knepley   PetscFunctionBegin;
42501cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42511cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4252f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
42531cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
42541cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
42551cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
42561cfe2091SMatthew G. Knepley }
42571cfe2091SMatthew G. Knepley 
425846e270d4SMatthew G. Knepley /*@
425946e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
426046e270d4SMatthew G. Knepley 
426146e270d4SMatthew G. Knepley   Not Collective
426246e270d4SMatthew G. Knepley 
426346e270d4SMatthew G. Knepley   Input Parameter:
426446e270d4SMatthew G. Knepley . dm - The DM object
426546e270d4SMatthew G. Knepley 
426646e270d4SMatthew G. Knepley   Output Parameter:
426746e270d4SMatthew G. Knepley . dim - The embedding dimension
426846e270d4SMatthew G. Knepley 
426946e270d4SMatthew G. Knepley   Level: intermediate
427046e270d4SMatthew G. Knepley 
427146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
427246e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
427346e270d4SMatthew G. Knepley @*/
427446e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
427546e270d4SMatthew G. Knepley {
427646e270d4SMatthew G. Knepley   PetscFunctionBegin;
427746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
427846e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
42799a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
42809a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
42819a9a41abSToby Isaac   }
428246e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
428346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
428446e270d4SMatthew G. Knepley }
428546e270d4SMatthew G. Knepley 
428646e270d4SMatthew G. Knepley /*@
428746e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
428846e270d4SMatthew G. Knepley 
428946e270d4SMatthew G. Knepley   Not Collective
429046e270d4SMatthew G. Knepley 
429146e270d4SMatthew G. Knepley   Input Parameters:
429246e270d4SMatthew G. Knepley + dm  - The DM object
429346e270d4SMatthew G. Knepley - dim - The embedding dimension
429446e270d4SMatthew G. Knepley 
429546e270d4SMatthew G. Knepley   Level: intermediate
429646e270d4SMatthew G. Knepley 
429746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
429846e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
429946e270d4SMatthew G. Knepley @*/
430046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
430146e270d4SMatthew G. Knepley {
430246e270d4SMatthew G. Knepley   PetscFunctionBegin;
430346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
430446e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
430546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
430646e270d4SMatthew G. Knepley }
430746e270d4SMatthew G. Knepley 
4308e8abe2deSMatthew G. Knepley /*@
4309e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4310e8abe2deSMatthew G. Knepley 
4311e8abe2deSMatthew G. Knepley   Not Collective
4312e8abe2deSMatthew G. Knepley 
4313e8abe2deSMatthew G. Knepley   Input Parameter:
4314e8abe2deSMatthew G. Knepley . dm - The DM object
4315e8abe2deSMatthew G. Knepley 
4316e8abe2deSMatthew G. Knepley   Output Parameter:
4317e8abe2deSMatthew G. Knepley . section - The PetscSection object
4318e8abe2deSMatthew G. Knepley 
4319e8abe2deSMatthew G. Knepley   Level: intermediate
4320e8abe2deSMatthew G. Knepley 
4321e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4322e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4323e8abe2deSMatthew G. Knepley @*/
4324e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4325e8abe2deSMatthew G. Knepley {
4326e8abe2deSMatthew G. Knepley   DM             cdm;
4327e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4328e8abe2deSMatthew G. Knepley 
4329e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4330e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4331e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4332e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4333e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4334e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4335e8abe2deSMatthew G. Knepley }
4336e8abe2deSMatthew G. Knepley 
4337e8abe2deSMatthew G. Knepley /*@
4338e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4339e8abe2deSMatthew G. Knepley 
4340e8abe2deSMatthew G. Knepley   Not Collective
4341e8abe2deSMatthew G. Knepley 
4342e8abe2deSMatthew G. Knepley   Input Parameters:
4343e8abe2deSMatthew G. Knepley + dm      - The DM object
434446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4345e8abe2deSMatthew G. Knepley - section - The PetscSection object
4346e8abe2deSMatthew G. Knepley 
4347e8abe2deSMatthew G. Knepley   Level: intermediate
4348e8abe2deSMatthew G. Knepley 
4349e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4350e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4351e8abe2deSMatthew G. Knepley @*/
435246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4353e8abe2deSMatthew G. Knepley {
4354e8abe2deSMatthew G. Knepley   DM             cdm;
4355e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4356e8abe2deSMatthew G. Knepley 
4357e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4358e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
435946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4360e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4361e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
436246e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
43634c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
436446e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
436546e270d4SMatthew G. Knepley 
436646e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
436746e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
436846e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
436946e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
437046e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
437146e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
437246e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
437346e270d4SMatthew G. Knepley     }
43746be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
437546e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
437646e270d4SMatthew G. Knepley   }
4377e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4378e8abe2deSMatthew G. Knepley }
4379e8abe2deSMatthew G. Knepley 
43805dc8c3f7SMatthew G. Knepley /*@C
438190b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
43825dc8c3f7SMatthew G. Knepley 
43835dc8c3f7SMatthew G. Knepley   Input Parameters:
438490b157c4SStefano Zampini . dm      - The DM object
438590b157c4SStefano Zampini 
438690b157c4SStefano Zampini   Output Parameters:
438790b157c4SStefano Zampini + per     - Whether the DM is periodic or not
43885dc8c3f7SMatthew 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
43895dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43905dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43915dc8c3f7SMatthew G. Knepley 
43925dc8c3f7SMatthew G. Knepley   Level: developer
43935dc8c3f7SMatthew G. Knepley 
43945dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43955dc8c3f7SMatthew G. Knepley @*/
439690b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4397c6b900c6SMatthew G. Knepley {
4398c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4399c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
440090b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4401c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4402c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
44035dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4404c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4405c6b900c6SMatthew G. Knepley }
4406c6b900c6SMatthew G. Knepley 
44075dc8c3f7SMatthew G. Knepley /*@C
44085dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
44095dc8c3f7SMatthew G. Knepley 
44105dc8c3f7SMatthew G. Knepley   Input Parameters:
44115dc8c3f7SMatthew G. Knepley + dm      - The DM object
441290b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
44135dc8c3f7SMatthew 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
44145dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44155dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44165dc8c3f7SMatthew G. Knepley 
44175dc8c3f7SMatthew G. Knepley   Level: developer
44185dc8c3f7SMatthew G. Knepley 
44195dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
44205dc8c3f7SMatthew G. Knepley @*/
442190b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4422c6b900c6SMatthew G. Knepley {
4423c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4424c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4425c6b900c6SMatthew G. Knepley 
4426c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4427c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
442890b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
442990b157c4SStefano Zampini   if (maxCell) {
443090b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
443190b157c4SStefano Zampini     PetscValidPointer(L,4);
443290b157c4SStefano Zampini     PetscValidPointer(bd,5);
443390b157c4SStefano Zampini   }
44345dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
44355dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
443690b157c4SStefano Zampini   if (maxCell) {
44375dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
44385dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
443990b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
444090b157c4SStefano Zampini   } else {
444190b157c4SStefano Zampini     dm->periodic = per;
444290b157c4SStefano Zampini   }
4443c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4444c6b900c6SMatthew G. Knepley }
4445c6b900c6SMatthew G. Knepley 
44462e17dfb7SMatthew G. Knepley /*@
44472e17dfb7SMatthew 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.
44482e17dfb7SMatthew G. Knepley 
44492e17dfb7SMatthew G. Knepley   Input Parameters:
44502e17dfb7SMatthew G. Knepley + dm     - The DM
445165da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
445265da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
44532e17dfb7SMatthew G. Knepley 
44542e17dfb7SMatthew G. Knepley   Output Parameter:
44552e17dfb7SMatthew G. Knepley . out - The localized coordinate point
44562e17dfb7SMatthew G. Knepley 
44572e17dfb7SMatthew G. Knepley   Level: developer
44582e17dfb7SMatthew G. Knepley 
44592e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
44602e17dfb7SMatthew G. Knepley @*/
446165da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
44622e17dfb7SMatthew G. Knepley {
44632e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
44642e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
44652e17dfb7SMatthew G. Knepley 
44662e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
44672e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
44682e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
44692e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
44702e17dfb7SMatthew G. Knepley   } else {
447165da65dcSMatthew G. Knepley     if (endpoint) {
447265da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4473da3333bfSMatthew 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)) {
4474da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
447565da65dcSMatthew G. Knepley         } else {
4476da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
447765da65dcSMatthew G. Knepley         }
447865da65dcSMatthew G. Knepley       }
447965da65dcSMatthew G. Knepley     } else {
44802e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
44811118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
44822e17dfb7SMatthew G. Knepley       }
44832e17dfb7SMatthew G. Knepley     }
448465da65dcSMatthew G. Knepley   }
44852e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
44862e17dfb7SMatthew G. Knepley }
44872e17dfb7SMatthew G. Knepley 
44882e17dfb7SMatthew G. Knepley /*
44892e17dfb7SMatthew 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.
44902e17dfb7SMatthew G. Knepley 
44912e17dfb7SMatthew G. Knepley   Input Parameters:
44922e17dfb7SMatthew G. Knepley + dm     - The DM
44932e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
44942e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
44952e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
44962e17dfb7SMatthew G. Knepley 
44972e17dfb7SMatthew G. Knepley   Output Parameter:
44982e17dfb7SMatthew G. Knepley . out - The localized coordinate point
44992e17dfb7SMatthew G. Knepley 
45002e17dfb7SMatthew G. Knepley   Level: developer
45012e17dfb7SMatthew G. Knepley 
45022e17dfb7SMatthew 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
45032e17dfb7SMatthew G. Knepley 
45042e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45052e17dfb7SMatthew G. Knepley */
45062e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
45072e17dfb7SMatthew G. Knepley {
45082e17dfb7SMatthew G. Knepley   PetscInt d;
45092e17dfb7SMatthew G. Knepley 
45102e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45112e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45122e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45132e17dfb7SMatthew G. Knepley   } else {
45142e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4515908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
45162e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
45172e17dfb7SMatthew G. Knepley       } else {
45182e17dfb7SMatthew G. Knepley         out[d] = in[d];
45192e17dfb7SMatthew G. Knepley       }
45202e17dfb7SMatthew G. Knepley     }
45212e17dfb7SMatthew G. Knepley   }
45222e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45232e17dfb7SMatthew G. Knepley }
45242e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
45252e17dfb7SMatthew G. Knepley {
45262e17dfb7SMatthew G. Knepley   PetscInt d;
45272e17dfb7SMatthew G. Knepley 
45282e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45292e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45302e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45312e17dfb7SMatthew G. Knepley   } else {
45322e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4533908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
45342e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
45352e17dfb7SMatthew G. Knepley       } else {
45362e17dfb7SMatthew G. Knepley         out[d] = in[d];
45372e17dfb7SMatthew G. Knepley       }
45382e17dfb7SMatthew G. Knepley     }
45392e17dfb7SMatthew G. Knepley   }
45402e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45412e17dfb7SMatthew G. Knepley }
45422e17dfb7SMatthew G. Knepley 
45432e17dfb7SMatthew G. Knepley /*
45442e17dfb7SMatthew 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.
45452e17dfb7SMatthew G. Knepley 
45462e17dfb7SMatthew G. Knepley   Input Parameters:
45472e17dfb7SMatthew G. Knepley + dm     - The DM
45482e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
45492e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
45502e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
45512e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
45522e17dfb7SMatthew G. Knepley 
45532e17dfb7SMatthew G. Knepley   Output Parameter:
45542e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
45552e17dfb7SMatthew G. Knepley 
45562e17dfb7SMatthew G. Knepley   Level: developer
45572e17dfb7SMatthew G. Knepley 
45582e17dfb7SMatthew 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
45592e17dfb7SMatthew G. Knepley 
45602e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
45612e17dfb7SMatthew G. Knepley */
45622e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
45632e17dfb7SMatthew G. Knepley {
45642e17dfb7SMatthew G. Knepley   PetscInt d;
45652e17dfb7SMatthew G. Knepley 
45662e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45672e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45682e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
45692e17dfb7SMatthew G. Knepley   } else {
45702e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4571908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
45722e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
45732e17dfb7SMatthew G. Knepley       } else {
45742e17dfb7SMatthew G. Knepley         out[d] += in[d];
45752e17dfb7SMatthew G. Knepley       }
45762e17dfb7SMatthew G. Knepley     }
45772e17dfb7SMatthew G. Knepley   }
45782e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45792e17dfb7SMatthew G. Knepley }
45802e17dfb7SMatthew G. Knepley 
458136447a5eSToby Isaac /*@
458236447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
458336447a5eSToby Isaac 
458436447a5eSToby Isaac   Input Parameter:
458536447a5eSToby Isaac . dm - The DM
458636447a5eSToby Isaac 
458736447a5eSToby Isaac   Output Parameter:
458836447a5eSToby Isaac   areLocalized - True if localized
458936447a5eSToby Isaac 
459036447a5eSToby Isaac   Level: developer
459136447a5eSToby Isaac 
459236447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
459336447a5eSToby Isaac @*/
459436447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
459536447a5eSToby Isaac {
459636447a5eSToby Isaac   DM             cdm;
459736447a5eSToby Isaac   PetscSection   coordSection;
459836447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
459936447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
460036447a5eSToby Isaac   PetscErrorCode ierr;
460136447a5eSToby Isaac 
460236447a5eSToby Isaac   PetscFunctionBegin;
460336447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
460492c9c85fSStefano Zampini   if (!dm->periodic) {
46058b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
46068b09590cSToby Isaac     PetscFunctionReturn(0);
46078b09590cSToby Isaac   }
460836447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
460936447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
461036447a5eSToby Isaac   {
461136447a5eSToby Isaac     PetscBool isplex;
461236447a5eSToby Isaac 
461336447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
461436447a5eSToby Isaac     if (isplex) {
461536447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
461636447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
461736447a5eSToby Isaac   }
461836447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
461936447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
46205a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
462136447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
462236447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
462336447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
462436447a5eSToby Isaac       break;
462536447a5eSToby Isaac     }
462636447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
46275a42012cSToby Isaac     if (dof) {
46285a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
462936447a5eSToby Isaac       break;
463036447a5eSToby Isaac     }
463136447a5eSToby Isaac   }
46325a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
463336447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
463436447a5eSToby Isaac   PetscFunctionReturn(0);
463536447a5eSToby Isaac }
463636447a5eSToby Isaac 
463736447a5eSToby Isaac 
46382e17dfb7SMatthew G. Knepley /*@
46392e17dfb7SMatthew G. Knepley   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for each cell
46402e17dfb7SMatthew G. Knepley 
46412e17dfb7SMatthew G. Knepley   Input Parameter:
46422e17dfb7SMatthew G. Knepley . dm - The DM
46432e17dfb7SMatthew G. Knepley 
46442e17dfb7SMatthew G. Knepley   Level: developer
46452e17dfb7SMatthew G. Knepley 
46462e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
46472e17dfb7SMatthew G. Knepley @*/
46482e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
46492e17dfb7SMatthew G. Knepley {
46502e17dfb7SMatthew G. Knepley   DM             cdm;
46512e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
46522e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
46533e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
46543e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4655e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
46563e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
46573e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
46582e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
46592e17dfb7SMatthew G. Knepley 
46602e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46612e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
466292c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
46632e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
46642e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
46652e17dfb7SMatthew G. Knepley   {
46662e17dfb7SMatthew G. Knepley     PetscBool isplex;
46672e17dfb7SMatthew G. Knepley 
46682e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
46692e17dfb7SMatthew G. Knepley     if (isplex) {
46702e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
46713e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
46723e922f36SToby Isaac       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
46733e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
46743e922f36SToby Isaac       newStart = vStart;
46753e922f36SToby Isaac       newEnd   = vEnd;
46763e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
46773e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
46783e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
46793e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
46803e922f36SToby Isaac       }
46812e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
46822e17dfb7SMatthew G. Knepley   }
46832e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
46842e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
46853e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4686e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
46873e922f36SToby Isaac 
46882e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
46892e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
46902e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
46912e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
46923e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
46933e922f36SToby Isaac 
46943e922f36SToby Isaac   ierr = DMGetWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
46953e922f36SToby Isaac   localized = &anchor[bs];
46963e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
46973e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
46983e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
46993e922f36SToby Isaac 
47003e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
47013e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
47023e922f36SToby Isaac       PetscInt     b;
47033e922f36SToby Isaac 
47043e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
47053e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47063e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
47073e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
47083e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
47093e922f36SToby Isaac         for (b = 0; b < bs; b++) {
47103e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
47113e922f36SToby Isaac         }
47123e922f36SToby Isaac         if (b < bs) break;
47133e922f36SToby Isaac       }
47143e922f36SToby Isaac       if (d < dof/bs) {
47153e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
47163e922f36SToby Isaac           PetscInt cdof;
47173e922f36SToby Isaac 
47183e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
47193e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
47203e922f36SToby Isaac         }
47213e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
47223e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
47233e922f36SToby Isaac       }
47243e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47253e922f36SToby Isaac     }
47263e922f36SToby Isaac   }
47273e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
47283e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
47293e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47303e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
47313e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
47323e922f36SToby Isaac     PetscFunctionReturn(0);
47333e922f36SToby Isaac   }
47342e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
47352e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
47362e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
47372e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
47382e17dfb7SMatthew G. Knepley   }
47392e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
47402e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4741c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
47422e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
47432e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
47442e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
47452e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4746c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
47472e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
47482e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
47492e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
47502e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
47512e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
47522e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
47532e17dfb7SMatthew G. Knepley   }
47543e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
47553e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
47563e922f36SToby Isaac 
47572e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
47582e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
47593e922f36SToby Isaac       PetscInt     b, cdof;
47602e17dfb7SMatthew G. Knepley 
47613e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
47623e922f36SToby Isaac       if (!cdof) continue;
47632e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47642e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
47652e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
47662e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
47672e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
47682e17dfb7SMatthew G. Knepley     }
47693e922f36SToby Isaac   }
47703e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47713e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
4772c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
47732e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
47742e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
47752e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
47762e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
47772e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
47782e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47792e17dfb7SMatthew G. Knepley }
47802e17dfb7SMatthew G. Knepley 
4781e87bb0d3SMatthew G Knepley /*@
47823a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4783e87bb0d3SMatthew G Knepley 
47843a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4785e87bb0d3SMatthew G Knepley 
4786e87bb0d3SMatthew G Knepley   Input Parameters:
4787e87bb0d3SMatthew G Knepley + dm - The DM
47883a93e3b7SToby Isaac . v - The Vec of points
478962a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
47903a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4791e87bb0d3SMatthew G Knepley 
479261e3bb9bSMatthew G Knepley   Output Parameter:
479362a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
479462a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
47953a93e3b7SToby Isaac 
4796e87bb0d3SMatthew G Knepley 
4797e87bb0d3SMatthew G Knepley   Level: developer
479861e3bb9bSMatthew G Knepley 
479962a38674SMatthew G. Knepley   Notes:
48003a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
480162a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
48023a93e3b7SToby Isaac 
48033a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
480462a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
48053a93e3b7SToby Isaac 
48063a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
48073a93e3b7SToby Isaac 
480862a38674SMatthew G. Knepley $    const PetscSFNode *cells;
480962a38674SMatthew G. Knepley $    PetscInt           nFound;
481062a38674SMatthew G. Knepley $    const PetscSFNode *found;
481162a38674SMatthew G. Knepley $
481262a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
48133a93e3b7SToby Isaac 
48143a93e3b7SToby 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
48153a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
48163a93e3b7SToby Isaac 
481761e3bb9bSMatthew G Knepley .keywords: point location, mesh
481862a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
481961e3bb9bSMatthew G Knepley @*/
482062a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4821e87bb0d3SMatthew G Knepley {
4822735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4823735aa83eSMatthew G Knepley 
4824e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4825e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4826e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4827e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
48283a93e3b7SToby Isaac   if (*cellSF) {
48293a93e3b7SToby Isaac     PetscMPIInt result;
48303a93e3b7SToby Isaac 
4831e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
4832a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
48333a93e3b7SToby 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");
4834e0fc9d1bSMatthew G. Knepley   } else {
48353a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
48363a93e3b7SToby Isaac   }
483747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4838735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
483962a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
484082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
484147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4842e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4843e87bb0d3SMatthew G Knepley }
484414f150ffSMatthew G. Knepley 
4845f4d763aaSMatthew G. Knepley /*@
4846f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4847f4d763aaSMatthew G. Knepley 
4848f4d763aaSMatthew G. Knepley   Input Parameter:
4849f4d763aaSMatthew G. Knepley . dm - The original DM
4850f4d763aaSMatthew G. Knepley 
4851f4d763aaSMatthew G. Knepley   Output Parameter:
4852f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4853f4d763aaSMatthew G. Knepley 
4854f4d763aaSMatthew G. Knepley   Level: intermediate
4855f4d763aaSMatthew G. Knepley 
4856f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4857f4d763aaSMatthew G. Knepley @*/
485814f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
485914f150ffSMatthew G. Knepley {
4860c26acbdeSMatthew G. Knepley   PetscSection   section;
48612d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
486214f150ffSMatthew G. Knepley   PetscErrorCode ierr;
486314f150ffSMatthew G. Knepley 
486414f150ffSMatthew G. Knepley   PetscFunctionBegin;
486514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
486614f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4867c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4868c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4869127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
48702d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
4871c26acbdeSMatthew G. Knepley     *odm = dm;
4872c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4873c26acbdeSMatthew G. Knepley   }
487414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
48750305aff6SMatthew G. Knepley     PetscDS      ds;
4876c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
487714f150ffSMatthew G. Knepley     PetscSF      sf;
487814f150ffSMatthew G. Knepley 
487914f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
48800305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
48810305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
488214f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
488314f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
488414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
488514f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
488615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
488714f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
488814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
488914f150ffSMatthew G. Knepley   }
489014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
489114f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
489214f150ffSMatthew G. Knepley }
4893f4d763aaSMatthew G. Knepley 
4894f4d763aaSMatthew G. Knepley /*@
4895cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4896f4d763aaSMatthew G. Knepley 
4897f4d763aaSMatthew G. Knepley   Input Parameter:
4898f4d763aaSMatthew G. Knepley . dm - The original DM
4899f4d763aaSMatthew G. Knepley 
4900cdb7a50dSMatthew G. Knepley   Output Parameters:
4901cdb7a50dSMatthew G. Knepley + num - The output sequence number
4902cdb7a50dSMatthew G. Knepley - val - The output sequence value
4903f4d763aaSMatthew G. Knepley 
4904f4d763aaSMatthew G. Knepley   Level: intermediate
4905f4d763aaSMatthew G. Knepley 
4906f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4907f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4908f4d763aaSMatthew G. Knepley 
4909f4d763aaSMatthew G. Knepley .seealso: VecView()
4910f4d763aaSMatthew G. Knepley @*/
4911cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4912f4d763aaSMatthew G. Knepley {
4913f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4914f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4915cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4916cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4917f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4918f4d763aaSMatthew G. Knepley }
4919f4d763aaSMatthew G. Knepley 
4920f4d763aaSMatthew G. Knepley /*@
4921cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4922f4d763aaSMatthew G. Knepley 
4923f4d763aaSMatthew G. Knepley   Input Parameters:
4924f4d763aaSMatthew G. Knepley + dm - The original DM
4925cdb7a50dSMatthew G. Knepley . num - The output sequence number
4926cdb7a50dSMatthew G. Knepley - val - The output sequence value
4927f4d763aaSMatthew G. Knepley 
4928f4d763aaSMatthew G. Knepley   Level: intermediate
4929f4d763aaSMatthew G. Knepley 
4930f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4931f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4932f4d763aaSMatthew G. Knepley 
4933f4d763aaSMatthew G. Knepley .seealso: VecView()
4934f4d763aaSMatthew G. Knepley @*/
4935cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4936f4d763aaSMatthew G. Knepley {
4937f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4938f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4939f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4940cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4941cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4942cdb7a50dSMatthew G. Knepley }
4943cdb7a50dSMatthew G. Knepley 
4944cdb7a50dSMatthew G. Knepley /*@C
4945cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4946cdb7a50dSMatthew G. Knepley 
4947cdb7a50dSMatthew G. Knepley   Input Parameters:
4948cdb7a50dSMatthew G. Knepley + dm   - The original DM
4949cdb7a50dSMatthew G. Knepley . name - The sequence name
4950cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4951cdb7a50dSMatthew G. Knepley 
4952cdb7a50dSMatthew G. Knepley   Output Parameter:
4953cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4954cdb7a50dSMatthew G. Knepley 
4955cdb7a50dSMatthew G. Knepley   Level: intermediate
4956cdb7a50dSMatthew G. Knepley 
4957cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4958cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4959cdb7a50dSMatthew G. Knepley 
4960cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4961cdb7a50dSMatthew G. Knepley @*/
4962cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4963cdb7a50dSMatthew G. Knepley {
4964cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4965cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4966cdb7a50dSMatthew G. Knepley 
4967cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4968cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4969cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4970cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4971cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4972cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4973cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4974cdb7a50dSMatthew G. Knepley     PetscScalar value;
4975cdb7a50dSMatthew G. Knepley 
497639d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
49774aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4978cdb7a50dSMatthew G. Knepley #endif
4979cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4980f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4981f4d763aaSMatthew G. Knepley }
49828e4ac7eaSMatthew G. Knepley 
49838e4ac7eaSMatthew G. Knepley /*@
49848e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
49858e4ac7eaSMatthew G. Knepley 
49868e4ac7eaSMatthew G. Knepley   Not collective
49878e4ac7eaSMatthew G. Knepley 
49888e4ac7eaSMatthew G. Knepley   Input Parameter:
49898e4ac7eaSMatthew G. Knepley . dm - The DM
49908e4ac7eaSMatthew G. Knepley 
49918e4ac7eaSMatthew G. Knepley   Output Parameter:
49928e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
49938e4ac7eaSMatthew G. Knepley 
49948e4ac7eaSMatthew G. Knepley   Level: beginner
49958e4ac7eaSMatthew G. Knepley 
49968e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
49978e4ac7eaSMatthew G. Knepley @*/
49988e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
49998e4ac7eaSMatthew G. Knepley {
50008e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
50018e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50028e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
50038e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
50048e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
50058e4ac7eaSMatthew G. Knepley }
50068e4ac7eaSMatthew G. Knepley 
50078e4ac7eaSMatthew G. Knepley /*@
50088e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
50098e4ac7eaSMatthew G. Knepley 
50108e4ac7eaSMatthew G. Knepley   Collective on dm
50118e4ac7eaSMatthew G. Knepley 
50128e4ac7eaSMatthew G. Knepley   Input Parameters:
50138e4ac7eaSMatthew G. Knepley + dm - The DM
50148e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
50158e4ac7eaSMatthew G. Knepley 
50168e4ac7eaSMatthew G. Knepley   Level: beginner
50178e4ac7eaSMatthew G. Knepley 
50188e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
50198e4ac7eaSMatthew G. Knepley @*/
50208e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
50218e4ac7eaSMatthew G. Knepley {
50228e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
50238e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50248e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
50258e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
50268e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
50278e4ac7eaSMatthew G. Knepley }
5028c58f1c22SToby Isaac 
5029c58f1c22SToby Isaac 
5030c58f1c22SToby Isaac /*@C
5031c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5032c58f1c22SToby Isaac 
5033c58f1c22SToby Isaac   Not Collective
5034c58f1c22SToby Isaac 
5035c58f1c22SToby Isaac   Input Parameters:
5036c58f1c22SToby Isaac + dm   - The DM object
5037c58f1c22SToby Isaac - name - The label name
5038c58f1c22SToby Isaac 
5039c58f1c22SToby Isaac   Level: intermediate
5040c58f1c22SToby Isaac 
5041c58f1c22SToby Isaac .keywords: mesh
5042c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5043c58f1c22SToby Isaac @*/
5044c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5045c58f1c22SToby Isaac {
5046c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5047c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5048c58f1c22SToby Isaac   PetscErrorCode ierr;
5049c58f1c22SToby Isaac 
5050c58f1c22SToby Isaac   PetscFunctionBegin;
5051c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5052c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5053c58f1c22SToby Isaac   while (next) {
5054c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5055c58f1c22SToby Isaac     if (flg) break;
5056c58f1c22SToby Isaac     next = next->next;
5057c58f1c22SToby Isaac   }
5058c58f1c22SToby Isaac   if (!flg) {
5059c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5060c58f1c22SToby Isaac 
5061c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5062c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5063c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5064c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5065c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5066c58f1c22SToby Isaac   }
5067c58f1c22SToby Isaac   PetscFunctionReturn(0);
5068c58f1c22SToby Isaac }
5069c58f1c22SToby Isaac 
5070c58f1c22SToby Isaac /*@C
5071c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5072c58f1c22SToby Isaac 
5073c58f1c22SToby Isaac   Not Collective
5074c58f1c22SToby Isaac 
5075c58f1c22SToby Isaac   Input Parameters:
5076c58f1c22SToby Isaac + dm   - The DM object
5077c58f1c22SToby Isaac . name - The label name
5078c58f1c22SToby Isaac - point - The mesh point
5079c58f1c22SToby Isaac 
5080c58f1c22SToby Isaac   Output Parameter:
5081c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5082c58f1c22SToby Isaac 
5083c58f1c22SToby Isaac   Level: beginner
5084c58f1c22SToby Isaac 
5085c58f1c22SToby Isaac .keywords: mesh
5086c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5087c58f1c22SToby Isaac @*/
5088c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5089c58f1c22SToby Isaac {
5090c58f1c22SToby Isaac   DMLabel        label;
5091c58f1c22SToby Isaac   PetscErrorCode ierr;
5092c58f1c22SToby Isaac 
5093c58f1c22SToby Isaac   PetscFunctionBegin;
5094c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5095c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5096c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5097c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
5098c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5099c58f1c22SToby Isaac   PetscFunctionReturn(0);
5100c58f1c22SToby Isaac }
5101c58f1c22SToby Isaac 
5102c58f1c22SToby Isaac /*@C
5103c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5104c58f1c22SToby Isaac 
5105c58f1c22SToby Isaac   Not Collective
5106c58f1c22SToby Isaac 
5107c58f1c22SToby Isaac   Input Parameters:
5108c58f1c22SToby Isaac + dm   - The DM object
5109c58f1c22SToby Isaac . name - The label name
5110c58f1c22SToby Isaac . point - The mesh point
5111c58f1c22SToby Isaac - value - The label value for this point
5112c58f1c22SToby Isaac 
5113c58f1c22SToby Isaac   Output Parameter:
5114c58f1c22SToby Isaac 
5115c58f1c22SToby Isaac   Level: beginner
5116c58f1c22SToby Isaac 
5117c58f1c22SToby Isaac .keywords: mesh
5118c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5119c58f1c22SToby Isaac @*/
5120c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5121c58f1c22SToby Isaac {
5122c58f1c22SToby Isaac   DMLabel        label;
5123c58f1c22SToby Isaac   PetscErrorCode ierr;
5124c58f1c22SToby Isaac 
5125c58f1c22SToby Isaac   PetscFunctionBegin;
5126c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5127c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5128c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5129c58f1c22SToby Isaac   if (!label) {
5130c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5131c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5132c58f1c22SToby Isaac   }
5133c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5134c58f1c22SToby Isaac   PetscFunctionReturn(0);
5135c58f1c22SToby Isaac }
5136c58f1c22SToby Isaac 
5137c58f1c22SToby Isaac /*@C
5138c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5139c58f1c22SToby Isaac 
5140c58f1c22SToby Isaac   Not Collective
5141c58f1c22SToby Isaac 
5142c58f1c22SToby Isaac   Input Parameters:
5143c58f1c22SToby Isaac + dm   - The DM object
5144c58f1c22SToby Isaac . name - The label name
5145c58f1c22SToby Isaac . point - The mesh point
5146c58f1c22SToby Isaac - value - The label value for this point
5147c58f1c22SToby Isaac 
5148c58f1c22SToby Isaac   Output Parameter:
5149c58f1c22SToby Isaac 
5150c58f1c22SToby Isaac   Level: beginner
5151c58f1c22SToby Isaac 
5152c58f1c22SToby Isaac .keywords: mesh
5153c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5154c58f1c22SToby Isaac @*/
5155c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5156c58f1c22SToby Isaac {
5157c58f1c22SToby Isaac   DMLabel        label;
5158c58f1c22SToby Isaac   PetscErrorCode ierr;
5159c58f1c22SToby Isaac 
5160c58f1c22SToby Isaac   PetscFunctionBegin;
5161c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5162c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5163c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5164c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5165c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5166c58f1c22SToby Isaac   PetscFunctionReturn(0);
5167c58f1c22SToby Isaac }
5168c58f1c22SToby Isaac 
5169c58f1c22SToby Isaac /*@C
5170c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5171c58f1c22SToby Isaac 
5172c58f1c22SToby Isaac   Not Collective
5173c58f1c22SToby Isaac 
5174c58f1c22SToby Isaac   Input Parameters:
5175c58f1c22SToby Isaac + dm   - The DM object
5176c58f1c22SToby Isaac - name - The label name
5177c58f1c22SToby Isaac 
5178c58f1c22SToby Isaac   Output Parameter:
5179c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5180c58f1c22SToby Isaac 
5181c58f1c22SToby Isaac   Level: beginner
5182c58f1c22SToby Isaac 
5183c58f1c22SToby Isaac .keywords: mesh
5184c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5185c58f1c22SToby Isaac @*/
5186c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5187c58f1c22SToby Isaac {
5188c58f1c22SToby Isaac   DMLabel        label;
5189c58f1c22SToby Isaac   PetscErrorCode ierr;
5190c58f1c22SToby Isaac 
5191c58f1c22SToby Isaac   PetscFunctionBegin;
5192c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5193c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5194c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5195c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5196c58f1c22SToby Isaac   *size = 0;
5197c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5198c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5199c58f1c22SToby Isaac   PetscFunctionReturn(0);
5200c58f1c22SToby Isaac }
5201c58f1c22SToby Isaac 
5202c58f1c22SToby Isaac /*@C
5203c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5204c58f1c22SToby Isaac 
5205c58f1c22SToby Isaac   Not Collective
5206c58f1c22SToby Isaac 
5207c58f1c22SToby Isaac   Input Parameters:
5208c58f1c22SToby Isaac + mesh - The DM object
5209c58f1c22SToby Isaac - name - The label name
5210c58f1c22SToby Isaac 
5211c58f1c22SToby Isaac   Output Parameter:
5212c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5213c58f1c22SToby Isaac 
5214c58f1c22SToby Isaac   Level: beginner
5215c58f1c22SToby Isaac 
5216c58f1c22SToby Isaac .keywords: mesh
5217c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5218c58f1c22SToby Isaac @*/
5219c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5220c58f1c22SToby Isaac {
5221c58f1c22SToby Isaac   DMLabel        label;
5222c58f1c22SToby Isaac   PetscErrorCode ierr;
5223c58f1c22SToby Isaac 
5224c58f1c22SToby Isaac   PetscFunctionBegin;
5225c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5226c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5227c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5228c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5229c58f1c22SToby Isaac   *ids = NULL;
5230c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5231c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5232c58f1c22SToby Isaac   PetscFunctionReturn(0);
5233c58f1c22SToby Isaac }
5234c58f1c22SToby Isaac 
5235c58f1c22SToby Isaac /*@C
5236c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5237c58f1c22SToby Isaac 
5238c58f1c22SToby Isaac   Not Collective
5239c58f1c22SToby Isaac 
5240c58f1c22SToby Isaac   Input Parameters:
5241c58f1c22SToby Isaac + dm - The DM object
5242c58f1c22SToby Isaac . name - The label name
5243c58f1c22SToby Isaac - value - The stratum value
5244c58f1c22SToby Isaac 
5245c58f1c22SToby Isaac   Output Parameter:
5246c58f1c22SToby Isaac . size - The stratum size
5247c58f1c22SToby Isaac 
5248c58f1c22SToby Isaac   Level: beginner
5249c58f1c22SToby Isaac 
5250c58f1c22SToby Isaac .keywords: mesh
5251c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5252c58f1c22SToby Isaac @*/
5253c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5254c58f1c22SToby Isaac {
5255c58f1c22SToby Isaac   DMLabel        label;
5256c58f1c22SToby Isaac   PetscErrorCode ierr;
5257c58f1c22SToby Isaac 
5258c58f1c22SToby Isaac   PetscFunctionBegin;
5259c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5260c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5261c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5262c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5263c58f1c22SToby Isaac   *size = 0;
5264c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5265c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5266c58f1c22SToby Isaac   PetscFunctionReturn(0);
5267c58f1c22SToby Isaac }
5268c58f1c22SToby Isaac 
5269c58f1c22SToby Isaac /*@C
5270c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5271c58f1c22SToby Isaac 
5272c58f1c22SToby Isaac   Not Collective
5273c58f1c22SToby Isaac 
5274c58f1c22SToby Isaac   Input Parameters:
5275c58f1c22SToby Isaac + dm - The DM object
5276c58f1c22SToby Isaac . name - The label name
5277c58f1c22SToby Isaac - value - The stratum value
5278c58f1c22SToby Isaac 
5279c58f1c22SToby Isaac   Output Parameter:
5280c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5281c58f1c22SToby Isaac 
5282c58f1c22SToby Isaac   Level: beginner
5283c58f1c22SToby Isaac 
5284c58f1c22SToby Isaac .keywords: mesh
5285c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5286c58f1c22SToby Isaac @*/
5287c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5288c58f1c22SToby Isaac {
5289c58f1c22SToby Isaac   DMLabel        label;
5290c58f1c22SToby Isaac   PetscErrorCode ierr;
5291c58f1c22SToby Isaac 
5292c58f1c22SToby Isaac   PetscFunctionBegin;
5293c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5294c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5295c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5296c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5297c58f1c22SToby Isaac   *points = NULL;
5298c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5299c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5300c58f1c22SToby Isaac   PetscFunctionReturn(0);
5301c58f1c22SToby Isaac }
5302c58f1c22SToby Isaac 
53034de306b1SToby Isaac /*@C
53044de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
53054de306b1SToby Isaac 
53064de306b1SToby Isaac   Not Collective
53074de306b1SToby Isaac 
53084de306b1SToby Isaac   Input Parameters:
53094de306b1SToby Isaac + dm - The DM object
53104de306b1SToby Isaac . name - The label name
53114de306b1SToby Isaac . value - The stratum value
53124de306b1SToby Isaac - points - The stratum points
53134de306b1SToby Isaac 
53144de306b1SToby Isaac   Level: beginner
53154de306b1SToby Isaac 
53164de306b1SToby Isaac .keywords: mesh
53174de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
53184de306b1SToby Isaac @*/
53194de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
53204de306b1SToby Isaac {
53214de306b1SToby Isaac   DMLabel        label;
53224de306b1SToby Isaac   PetscErrorCode ierr;
53234de306b1SToby Isaac 
53244de306b1SToby Isaac   PetscFunctionBegin;
53254de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53264de306b1SToby Isaac   PetscValidCharPointer(name, 2);
53274de306b1SToby Isaac   PetscValidPointer(points, 4);
53284de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
53294de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
53304de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
53314de306b1SToby Isaac   PetscFunctionReturn(0);
53324de306b1SToby Isaac }
53334de306b1SToby Isaac 
5334c58f1c22SToby Isaac /*@C
5335c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5336c58f1c22SToby Isaac 
5337c58f1c22SToby Isaac   Not Collective
5338c58f1c22SToby Isaac 
5339c58f1c22SToby Isaac   Input Parameters:
5340c58f1c22SToby Isaac + dm   - The DM object
5341c58f1c22SToby Isaac . name - The label name
5342c58f1c22SToby Isaac - value - The label value for this point
5343c58f1c22SToby Isaac 
5344c58f1c22SToby Isaac   Output Parameter:
5345c58f1c22SToby Isaac 
5346c58f1c22SToby Isaac   Level: beginner
5347c58f1c22SToby Isaac 
5348c58f1c22SToby Isaac .keywords: mesh
5349c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5350c58f1c22SToby Isaac @*/
5351c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5352c58f1c22SToby Isaac {
5353c58f1c22SToby Isaac   DMLabel        label;
5354c58f1c22SToby Isaac   PetscErrorCode ierr;
5355c58f1c22SToby Isaac 
5356c58f1c22SToby Isaac   PetscFunctionBegin;
5357c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5358c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5359c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5360c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5361c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5362c58f1c22SToby Isaac   PetscFunctionReturn(0);
5363c58f1c22SToby Isaac }
5364c58f1c22SToby Isaac 
5365c58f1c22SToby Isaac /*@
5366c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5367c58f1c22SToby Isaac 
5368c58f1c22SToby Isaac   Not Collective
5369c58f1c22SToby Isaac 
5370c58f1c22SToby Isaac   Input Parameter:
5371c58f1c22SToby Isaac . dm   - The DM object
5372c58f1c22SToby Isaac 
5373c58f1c22SToby Isaac   Output Parameter:
5374c58f1c22SToby Isaac . numLabels - the number of Labels
5375c58f1c22SToby Isaac 
5376c58f1c22SToby Isaac   Level: intermediate
5377c58f1c22SToby Isaac 
5378c58f1c22SToby Isaac .keywords: mesh
5379c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5380c58f1c22SToby Isaac @*/
5381c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5382c58f1c22SToby Isaac {
5383c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5384c58f1c22SToby Isaac   PetscInt  n    = 0;
5385c58f1c22SToby Isaac 
5386c58f1c22SToby Isaac   PetscFunctionBegin;
5387c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5388c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5389c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5390c58f1c22SToby Isaac   *numLabels = n;
5391c58f1c22SToby Isaac   PetscFunctionReturn(0);
5392c58f1c22SToby Isaac }
5393c58f1c22SToby Isaac 
5394c58f1c22SToby Isaac /*@C
5395c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5396c58f1c22SToby Isaac 
5397c58f1c22SToby Isaac   Not Collective
5398c58f1c22SToby Isaac 
5399c58f1c22SToby Isaac   Input Parameters:
5400c58f1c22SToby Isaac + dm - The DM object
5401c58f1c22SToby Isaac - n  - the label number
5402c58f1c22SToby Isaac 
5403c58f1c22SToby Isaac   Output Parameter:
5404c58f1c22SToby Isaac . name - the label name
5405c58f1c22SToby Isaac 
5406c58f1c22SToby Isaac   Level: intermediate
5407c58f1c22SToby Isaac 
5408c58f1c22SToby Isaac .keywords: mesh
5409c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5410c58f1c22SToby Isaac @*/
5411c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5412c58f1c22SToby Isaac {
5413c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5414c58f1c22SToby Isaac   PetscInt  l    = 0;
5415c58f1c22SToby Isaac 
5416c58f1c22SToby Isaac   PetscFunctionBegin;
5417c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5418c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5419c58f1c22SToby Isaac   while (next) {
5420c58f1c22SToby Isaac     if (l == n) {
5421c58f1c22SToby Isaac       *name = next->label->name;
5422c58f1c22SToby Isaac       PetscFunctionReturn(0);
5423c58f1c22SToby Isaac     }
5424c58f1c22SToby Isaac     ++l;
5425c58f1c22SToby Isaac     next = next->next;
5426c58f1c22SToby Isaac   }
5427c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5428c58f1c22SToby Isaac }
5429c58f1c22SToby Isaac 
5430c58f1c22SToby Isaac /*@C
5431c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5432c58f1c22SToby Isaac 
5433c58f1c22SToby Isaac   Not Collective
5434c58f1c22SToby Isaac 
5435c58f1c22SToby Isaac   Input Parameters:
5436c58f1c22SToby Isaac + dm   - The DM object
5437c58f1c22SToby Isaac - name - The label name
5438c58f1c22SToby Isaac 
5439c58f1c22SToby Isaac   Output Parameter:
5440c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5441c58f1c22SToby Isaac 
5442c58f1c22SToby Isaac   Level: intermediate
5443c58f1c22SToby Isaac 
5444c58f1c22SToby Isaac .keywords: mesh
5445c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5446c58f1c22SToby Isaac @*/
5447c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5448c58f1c22SToby Isaac {
5449c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5450c58f1c22SToby Isaac   PetscErrorCode ierr;
5451c58f1c22SToby Isaac 
5452c58f1c22SToby Isaac   PetscFunctionBegin;
5453c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5454c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5455c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5456c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5457c58f1c22SToby Isaac   while (next) {
5458c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5459c58f1c22SToby Isaac     if (*hasLabel) break;
5460c58f1c22SToby Isaac     next = next->next;
5461c58f1c22SToby Isaac   }
5462c58f1c22SToby Isaac   PetscFunctionReturn(0);
5463c58f1c22SToby Isaac }
5464c58f1c22SToby Isaac 
5465c58f1c22SToby Isaac /*@C
5466c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5467c58f1c22SToby Isaac 
5468c58f1c22SToby Isaac   Not Collective
5469c58f1c22SToby Isaac 
5470c58f1c22SToby Isaac   Input Parameters:
5471c58f1c22SToby Isaac + dm   - The DM object
5472c58f1c22SToby Isaac - name - The label name
5473c58f1c22SToby Isaac 
5474c58f1c22SToby Isaac   Output Parameter:
5475c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5476c58f1c22SToby Isaac 
5477c58f1c22SToby Isaac   Level: intermediate
5478c58f1c22SToby Isaac 
5479c58f1c22SToby Isaac .keywords: mesh
5480c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5481c58f1c22SToby Isaac @*/
5482c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5483c58f1c22SToby Isaac {
5484c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5485c58f1c22SToby Isaac   PetscBool      hasLabel;
5486c58f1c22SToby Isaac   PetscErrorCode ierr;
5487c58f1c22SToby Isaac 
5488c58f1c22SToby Isaac   PetscFunctionBegin;
5489c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5490c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5491c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5492c58f1c22SToby Isaac   *label = NULL;
5493c58f1c22SToby Isaac   while (next) {
5494c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5495c58f1c22SToby Isaac     if (hasLabel) {
5496c58f1c22SToby Isaac       *label = next->label;
5497c58f1c22SToby Isaac       break;
5498c58f1c22SToby Isaac     }
5499c58f1c22SToby Isaac     next = next->next;
5500c58f1c22SToby Isaac   }
5501c58f1c22SToby Isaac   PetscFunctionReturn(0);
5502c58f1c22SToby Isaac }
5503c58f1c22SToby Isaac 
5504c58f1c22SToby Isaac /*@C
5505c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5506c58f1c22SToby Isaac 
5507c58f1c22SToby Isaac   Not Collective
5508c58f1c22SToby Isaac 
5509c58f1c22SToby Isaac   Input Parameters:
5510c58f1c22SToby Isaac + dm - The DM object
5511c58f1c22SToby Isaac - n  - the label number
5512c58f1c22SToby Isaac 
5513c58f1c22SToby Isaac   Output Parameter:
5514c58f1c22SToby Isaac . label - the label
5515c58f1c22SToby Isaac 
5516c58f1c22SToby Isaac   Level: intermediate
5517c58f1c22SToby Isaac 
5518c58f1c22SToby Isaac .keywords: mesh
5519c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5520c58f1c22SToby Isaac @*/
5521c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5522c58f1c22SToby Isaac {
5523c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5524c58f1c22SToby Isaac   PetscInt    l    = 0;
5525c58f1c22SToby Isaac 
5526c58f1c22SToby Isaac   PetscFunctionBegin;
5527c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5528c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5529c58f1c22SToby Isaac   while (next) {
5530c58f1c22SToby Isaac     if (l == n) {
5531c58f1c22SToby Isaac       *label = next->label;
5532c58f1c22SToby Isaac       PetscFunctionReturn(0);
5533c58f1c22SToby Isaac     }
5534c58f1c22SToby Isaac     ++l;
5535c58f1c22SToby Isaac     next = next->next;
5536c58f1c22SToby Isaac   }
5537c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5538c58f1c22SToby Isaac }
5539c58f1c22SToby Isaac 
5540c58f1c22SToby Isaac /*@C
5541c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5542c58f1c22SToby Isaac 
5543c58f1c22SToby Isaac   Not Collective
5544c58f1c22SToby Isaac 
5545c58f1c22SToby Isaac   Input Parameters:
5546c58f1c22SToby Isaac + dm   - The DM object
5547c58f1c22SToby Isaac - label - The DMLabel
5548c58f1c22SToby Isaac 
5549c58f1c22SToby Isaac   Level: developer
5550c58f1c22SToby Isaac 
5551c58f1c22SToby Isaac .keywords: mesh
5552c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5553c58f1c22SToby Isaac @*/
5554c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5555c58f1c22SToby Isaac {
5556c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5557c58f1c22SToby Isaac   PetscBool      hasLabel;
5558c58f1c22SToby Isaac   PetscErrorCode ierr;
5559c58f1c22SToby Isaac 
5560c58f1c22SToby Isaac   PetscFunctionBegin;
5561c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5562c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5563c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5564c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5565c58f1c22SToby Isaac   tmpLabel->label  = label;
5566c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5567c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5568c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5569c58f1c22SToby Isaac   PetscFunctionReturn(0);
5570c58f1c22SToby Isaac }
5571c58f1c22SToby Isaac 
5572c58f1c22SToby Isaac /*@C
5573c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5574c58f1c22SToby Isaac 
5575c58f1c22SToby Isaac   Not Collective
5576c58f1c22SToby Isaac 
5577c58f1c22SToby Isaac   Input Parameters:
5578c58f1c22SToby Isaac + dm   - The DM object
5579c58f1c22SToby Isaac - name - The label name
5580c58f1c22SToby Isaac 
5581c58f1c22SToby Isaac   Output Parameter:
5582c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5583c58f1c22SToby Isaac 
5584c58f1c22SToby Isaac   Level: developer
5585c58f1c22SToby Isaac 
5586c58f1c22SToby Isaac .keywords: mesh
5587c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5588c58f1c22SToby Isaac @*/
5589c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5590c58f1c22SToby Isaac {
5591c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5592c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5593c58f1c22SToby Isaac   PetscBool      hasLabel;
5594c58f1c22SToby Isaac   PetscErrorCode ierr;
5595c58f1c22SToby Isaac 
5596c58f1c22SToby Isaac   PetscFunctionBegin;
5597c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5598c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5599c58f1c22SToby Isaac   *label = NULL;
5600c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5601c58f1c22SToby Isaac   while (next) {
5602c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5603c58f1c22SToby Isaac     if (hasLabel) {
5604c58f1c22SToby Isaac       if (last) last->next       = next->next;
5605c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5606c58f1c22SToby Isaac       next->next = NULL;
5607c58f1c22SToby Isaac       *label     = next->label;
5608c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5609c58f1c22SToby Isaac       if (hasLabel) {
5610c58f1c22SToby Isaac         dm->depthLabel = NULL;
5611c58f1c22SToby Isaac       }
5612c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5613c58f1c22SToby Isaac       break;
5614c58f1c22SToby Isaac     }
5615c58f1c22SToby Isaac     last = next;
5616c58f1c22SToby Isaac     next = next->next;
5617c58f1c22SToby Isaac   }
5618c58f1c22SToby Isaac   PetscFunctionReturn(0);
5619c58f1c22SToby Isaac }
5620c58f1c22SToby Isaac 
5621c58f1c22SToby Isaac /*@C
5622c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5623c58f1c22SToby Isaac 
5624c58f1c22SToby Isaac   Not Collective
5625c58f1c22SToby Isaac 
5626c58f1c22SToby Isaac   Input Parameters:
5627c58f1c22SToby Isaac + dm   - The DM object
5628c58f1c22SToby Isaac - name - The label name
5629c58f1c22SToby Isaac 
5630c58f1c22SToby Isaac   Output Parameter:
5631c58f1c22SToby Isaac . output - The flag for output
5632c58f1c22SToby Isaac 
5633c58f1c22SToby Isaac   Level: developer
5634c58f1c22SToby Isaac 
5635c58f1c22SToby Isaac .keywords: mesh
5636c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5637c58f1c22SToby Isaac @*/
5638c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5639c58f1c22SToby Isaac {
5640c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5641c58f1c22SToby Isaac   PetscErrorCode ierr;
5642c58f1c22SToby Isaac 
5643c58f1c22SToby Isaac   PetscFunctionBegin;
5644c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5645c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5646c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5647c58f1c22SToby Isaac   while (next) {
5648c58f1c22SToby Isaac     PetscBool flg;
5649c58f1c22SToby Isaac 
5650c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5651c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5652c58f1c22SToby Isaac     next = next->next;
5653c58f1c22SToby Isaac   }
5654c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5655c58f1c22SToby Isaac }
5656c58f1c22SToby Isaac 
5657c58f1c22SToby Isaac /*@C
5658c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5659c58f1c22SToby Isaac 
5660c58f1c22SToby Isaac   Not Collective
5661c58f1c22SToby Isaac 
5662c58f1c22SToby Isaac   Input Parameters:
5663c58f1c22SToby Isaac + dm     - The DM object
5664c58f1c22SToby Isaac . name   - The label name
5665c58f1c22SToby Isaac - output - The flag for output
5666c58f1c22SToby Isaac 
5667c58f1c22SToby Isaac   Level: developer
5668c58f1c22SToby Isaac 
5669c58f1c22SToby Isaac .keywords: mesh
5670c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5671c58f1c22SToby Isaac @*/
5672c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5673c58f1c22SToby Isaac {
5674c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5675c58f1c22SToby Isaac   PetscErrorCode ierr;
5676c58f1c22SToby Isaac 
5677c58f1c22SToby Isaac   PetscFunctionBegin;
5678c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5679c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5680c58f1c22SToby Isaac   while (next) {
5681c58f1c22SToby Isaac     PetscBool flg;
5682c58f1c22SToby Isaac 
5683c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5684c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5685c58f1c22SToby Isaac     next = next->next;
5686c58f1c22SToby Isaac   }
5687c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5688c58f1c22SToby Isaac }
5689c58f1c22SToby Isaac 
5690c58f1c22SToby Isaac 
5691c58f1c22SToby Isaac /*@
5692c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5693c58f1c22SToby Isaac 
5694c58f1c22SToby Isaac   Collective on DM
5695c58f1c22SToby Isaac 
5696c58f1c22SToby Isaac   Input Parameter:
56972e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5698c58f1c22SToby Isaac 
5699c58f1c22SToby Isaac   Output Parameter:
57002e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5701c58f1c22SToby Isaac 
5702c58f1c22SToby Isaac   Level: intermediate
5703c58f1c22SToby Isaac 
5704c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5705c58f1c22SToby Isaac 
5706c58f1c22SToby Isaac .keywords: mesh
5707c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5708c58f1c22SToby Isaac @*/
5709c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5710c58f1c22SToby Isaac {
5711c58f1c22SToby Isaac   PetscInt       numLabels, l;
5712c58f1c22SToby Isaac   PetscErrorCode ierr;
5713c58f1c22SToby Isaac 
5714c58f1c22SToby Isaac   PetscFunctionBegin;
5715c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5716c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5717c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5718c58f1c22SToby Isaac     DMLabel     label, labelNew;
5719c58f1c22SToby Isaac     const char *name;
5720c58f1c22SToby Isaac     PetscBool   flg;
5721c58f1c22SToby Isaac 
5722c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5723c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5724c58f1c22SToby Isaac     if (flg) continue;
5725c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5726c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5727c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5728c58f1c22SToby Isaac   }
5729c58f1c22SToby Isaac   PetscFunctionReturn(0);
5730c58f1c22SToby Isaac }
5731a8fb8f29SToby Isaac 
5732a8fb8f29SToby Isaac /*@
5733a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5734a8fb8f29SToby Isaac 
5735a8fb8f29SToby Isaac   Input Parameter:
5736a8fb8f29SToby Isaac . dm - The DM object
5737a8fb8f29SToby Isaac 
5738a8fb8f29SToby Isaac   Output Parameter:
5739a8fb8f29SToby Isaac . cdm - The coarse DM
5740a8fb8f29SToby Isaac 
5741a8fb8f29SToby Isaac   Level: intermediate
5742a8fb8f29SToby Isaac 
5743a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5744a8fb8f29SToby Isaac @*/
5745a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5746a8fb8f29SToby Isaac {
5747a8fb8f29SToby Isaac   PetscFunctionBegin;
5748a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5749a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5750a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5751a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5752a8fb8f29SToby Isaac }
5753a8fb8f29SToby Isaac 
5754a8fb8f29SToby Isaac /*@
5755a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5756a8fb8f29SToby Isaac 
5757a8fb8f29SToby Isaac   Input Parameters:
5758a8fb8f29SToby Isaac + dm - The DM object
5759a8fb8f29SToby Isaac - cdm - The coarse DM
5760a8fb8f29SToby Isaac 
5761a8fb8f29SToby Isaac   Level: intermediate
5762a8fb8f29SToby Isaac 
5763a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5764a8fb8f29SToby Isaac @*/
5765a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5766a8fb8f29SToby Isaac {
5767a8fb8f29SToby Isaac   PetscErrorCode ierr;
5768a8fb8f29SToby Isaac 
5769a8fb8f29SToby Isaac   PetscFunctionBegin;
5770a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5771a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5772a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5773a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5774a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5775a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5776a8fb8f29SToby Isaac }
5777a8fb8f29SToby Isaac 
577888bdff64SToby Isaac /*@
577988bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
578088bdff64SToby Isaac 
578188bdff64SToby Isaac   Input Parameter:
578288bdff64SToby Isaac . dm - The DM object
578388bdff64SToby Isaac 
578488bdff64SToby Isaac   Output Parameter:
578588bdff64SToby Isaac . fdm - The fine DM
578688bdff64SToby Isaac 
578788bdff64SToby Isaac   Level: intermediate
578888bdff64SToby Isaac 
578988bdff64SToby Isaac .seealso: DMSetFineDM()
579088bdff64SToby Isaac @*/
579188bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
579288bdff64SToby Isaac {
579388bdff64SToby Isaac   PetscFunctionBegin;
579488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
579588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
579688bdff64SToby Isaac   *fdm = dm->fineMesh;
579788bdff64SToby Isaac   PetscFunctionReturn(0);
579888bdff64SToby Isaac }
579988bdff64SToby Isaac 
580088bdff64SToby Isaac /*@
580188bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
580288bdff64SToby Isaac 
580388bdff64SToby Isaac   Input Parameters:
580488bdff64SToby Isaac + dm - The DM object
580588bdff64SToby Isaac - fdm - The fine DM
580688bdff64SToby Isaac 
580788bdff64SToby Isaac   Level: intermediate
580888bdff64SToby Isaac 
580988bdff64SToby Isaac .seealso: DMGetFineDM()
581088bdff64SToby Isaac @*/
581188bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
581288bdff64SToby Isaac {
581388bdff64SToby Isaac   PetscErrorCode ierr;
581488bdff64SToby Isaac 
581588bdff64SToby Isaac   PetscFunctionBegin;
581688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
581788bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
581888bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
581988bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
582088bdff64SToby Isaac   dm->fineMesh = fdm;
582188bdff64SToby Isaac   PetscFunctionReturn(0);
582288bdff64SToby Isaac }
582388bdff64SToby Isaac 
5824a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5825a6ba4734SToby Isaac 
5826a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5827a6ba4734SToby Isaac {
5828a6ba4734SToby Isaac   PetscErrorCode ierr;
5829a6ba4734SToby Isaac 
5830a6ba4734SToby Isaac   PetscFunctionBegin;
5831e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
5832a6ba4734SToby Isaac   PetscFunctionReturn(0);
5833a6ba4734SToby Isaac }
5834a6ba4734SToby Isaac 
5835a6ba4734SToby Isaac /*@C
5836a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5837a6ba4734SToby Isaac 
5838a6ba4734SToby Isaac   Input Parameters:
58394c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
5840f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5841a6ba4734SToby Isaac . name        - The BC name
5842a6ba4734SToby Isaac . labelname   - The label defining constrained points
5843a6ba4734SToby Isaac . field       - The field to constrain
5844a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5845a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5846a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5847a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5848a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5849a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5850a6ba4734SToby Isaac 
5851a6ba4734SToby Isaac   Options Database Keys:
5852a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5853a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5854a6ba4734SToby Isaac 
5855a6ba4734SToby Isaac   Level: developer
5856a6ba4734SToby Isaac 
5857a6ba4734SToby Isaac .seealso: DMGetBoundary()
5858a6ba4734SToby Isaac @*/
5859a30ec4eaSSatish 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)
5860a6ba4734SToby Isaac {
5861a6ba4734SToby Isaac   PetscErrorCode ierr;
5862a6ba4734SToby Isaac 
5863a6ba4734SToby Isaac   PetscFunctionBegin;
5864a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5865f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
5866a6ba4734SToby Isaac   PetscFunctionReturn(0);
5867a6ba4734SToby Isaac }
5868a6ba4734SToby Isaac 
5869a6ba4734SToby Isaac /*@
5870a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5871a6ba4734SToby Isaac 
5872a6ba4734SToby Isaac   Input Parameters:
5873a6ba4734SToby Isaac . dm - The mesh object
5874a6ba4734SToby Isaac 
5875a6ba4734SToby Isaac   Output Parameters:
5876a6ba4734SToby Isaac . numBd - The number of BC
5877a6ba4734SToby Isaac 
5878a6ba4734SToby Isaac   Level: intermediate
5879a6ba4734SToby Isaac 
5880a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5881a6ba4734SToby Isaac @*/
5882a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5883a6ba4734SToby Isaac {
588458ebd649SToby Isaac   PetscErrorCode ierr;
5885a6ba4734SToby Isaac 
5886a6ba4734SToby Isaac   PetscFunctionBegin;
5887a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
588858ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
5889a6ba4734SToby Isaac   PetscFunctionReturn(0);
5890a6ba4734SToby Isaac }
5891a6ba4734SToby Isaac 
5892a6ba4734SToby Isaac /*@C
58931c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
5894a6ba4734SToby Isaac 
5895a6ba4734SToby Isaac   Input Parameters:
5896a6ba4734SToby Isaac + dm          - The mesh object
5897a6ba4734SToby Isaac - bd          - The BC number
5898a6ba4734SToby Isaac 
5899a6ba4734SToby Isaac   Output Parameters:
5900f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5901a6ba4734SToby Isaac . name        - The BC name
5902a6ba4734SToby Isaac . labelname   - The label defining constrained points
5903a6ba4734SToby Isaac . field       - The field to constrain
5904a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5905a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5906a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5907a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5908a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5909a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5910a6ba4734SToby Isaac 
5911a6ba4734SToby Isaac   Options Database Keys:
5912a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5913a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5914a6ba4734SToby Isaac 
5915a6ba4734SToby Isaac   Level: developer
5916a6ba4734SToby Isaac 
5917a6ba4734SToby Isaac .seealso: DMAddBoundary()
5918a6ba4734SToby Isaac @*/
5919a30ec4eaSSatish 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)
5920a6ba4734SToby Isaac {
592158ebd649SToby Isaac   PetscErrorCode ierr;
5922a6ba4734SToby Isaac 
5923a6ba4734SToby Isaac   PetscFunctionBegin;
5924a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5925f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
5926a6ba4734SToby Isaac   PetscFunctionReturn(0);
5927a6ba4734SToby Isaac }
5928a6ba4734SToby Isaac 
5929e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
5930e6f8dbb6SToby Isaac {
5931dff059c6SToby Isaac   DMBoundary *lastnext;
5932e6f8dbb6SToby Isaac   DSBoundary dsbound;
5933e6f8dbb6SToby Isaac   PetscErrorCode ierr;
5934e6f8dbb6SToby Isaac 
5935e6f8dbb6SToby Isaac   PetscFunctionBegin;
5936e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
593747a1f5adSToby Isaac   if (dm->boundary) {
593847a1f5adSToby Isaac     DMBoundary next = dm->boundary;
593947a1f5adSToby Isaac 
594047a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
594147a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
594247a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
594347a1f5adSToby Isaac     while (next) {
594447a1f5adSToby Isaac       DMBoundary b = next;
594547a1f5adSToby Isaac 
594647a1f5adSToby Isaac       next = b->next;
594747a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
5948a6ba4734SToby Isaac     }
594947a1f5adSToby Isaac     dm->boundary = NULL;
5950a6ba4734SToby Isaac   }
595147a1f5adSToby Isaac 
5952dff059c6SToby Isaac   lastnext = &(dm->boundary);
5953e6f8dbb6SToby Isaac   while (dsbound) {
5954e6f8dbb6SToby Isaac     DMBoundary dmbound;
5955e6f8dbb6SToby Isaac 
5956e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
5957e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
5958e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
5959e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
596047a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
5961dff059c6SToby Isaac     *lastnext = dmbound;
5962dff059c6SToby Isaac     lastnext = &(dmbound->next);
5963dff059c6SToby Isaac     dsbound = dsbound->next;
5964a6ba4734SToby Isaac   }
5965a6ba4734SToby Isaac   PetscFunctionReturn(0);
5966a6ba4734SToby Isaac }
5967a6ba4734SToby Isaac 
5968a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
5969a6ba4734SToby Isaac {
5970b95f2879SToby Isaac   DMBoundary     b;
5971a6ba4734SToby Isaac   PetscErrorCode ierr;
5972a6ba4734SToby Isaac 
5973a6ba4734SToby Isaac   PetscFunctionBegin;
5974a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5975a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
5976a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
5977e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
5978b95f2879SToby Isaac   b = dm->boundary;
5979a6ba4734SToby Isaac   while (b && !(*isBd)) {
5980e6f8dbb6SToby Isaac     DMLabel    label = b->label;
5981e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
59823424c85cSToby Isaac 
59833424c85cSToby Isaac     if (label) {
5984a6ba4734SToby Isaac       PetscInt i;
5985a6ba4734SToby Isaac 
5986e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
5987e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
5988a6ba4734SToby Isaac       }
5989a6ba4734SToby Isaac     }
5990a6ba4734SToby Isaac     b = b->next;
5991a6ba4734SToby Isaac   }
5992a6ba4734SToby Isaac   PetscFunctionReturn(0);
5993a6ba4734SToby Isaac }
59944d6f44ffSToby Isaac 
59954d6f44ffSToby Isaac /*@C
59964d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
59974d6f44ffSToby Isaac 
59984d6f44ffSToby Isaac   Input Parameters:
59994d6f44ffSToby Isaac + dm      - The DM
60000709b2feSToby Isaac . time    - The time
60014d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
60024d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
60034d6f44ffSToby Isaac - mode    - The insertion mode for values
60044d6f44ffSToby Isaac 
60054d6f44ffSToby Isaac   Output Parameter:
60064d6f44ffSToby Isaac . X - vector
60074d6f44ffSToby Isaac 
60084d6f44ffSToby Isaac    Calling sequence of func:
60090709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
60104d6f44ffSToby Isaac 
60114d6f44ffSToby Isaac +  dim - The spatial dimension
60124d6f44ffSToby Isaac .  x   - The coordinates
60134d6f44ffSToby Isaac .  Nf  - The number of fields
60144d6f44ffSToby Isaac .  u   - The output field values
60154d6f44ffSToby Isaac -  ctx - optional user-defined function context
60164d6f44ffSToby Isaac 
60174d6f44ffSToby Isaac   Level: developer
60184d6f44ffSToby Isaac 
60192716604bSToby Isaac .seealso: DMComputeL2Diff()
60204d6f44ffSToby Isaac @*/
60210709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
60224d6f44ffSToby Isaac {
60234d6f44ffSToby Isaac   Vec            localX;
60244d6f44ffSToby Isaac   PetscErrorCode ierr;
60254d6f44ffSToby Isaac 
60264d6f44ffSToby Isaac   PetscFunctionBegin;
60274d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
60284d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
60290709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
60304d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
60314d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
60324d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
60334d6f44ffSToby Isaac   PetscFunctionReturn(0);
60344d6f44ffSToby Isaac }
60354d6f44ffSToby Isaac 
60360709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
60374d6f44ffSToby Isaac {
60384d6f44ffSToby Isaac   PetscErrorCode ierr;
60394d6f44ffSToby Isaac 
60404d6f44ffSToby Isaac   PetscFunctionBegin;
60414d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60424d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
60434d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
60440709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
60454d6f44ffSToby Isaac   PetscFunctionReturn(0);
60464d6f44ffSToby Isaac }
60474d6f44ffSToby Isaac 
60481c531cf8SMatthew 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)
60494d6f44ffSToby Isaac {
60504d6f44ffSToby Isaac   PetscErrorCode ierr;
60514d6f44ffSToby Isaac 
60524d6f44ffSToby Isaac   PetscFunctionBegin;
60534d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60544d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
60554d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
60561c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
60574d6f44ffSToby Isaac   PetscFunctionReturn(0);
60584d6f44ffSToby Isaac }
60592716604bSToby Isaac 
60608c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
60618c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
60628c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
60638c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6064191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
60658c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
60668c6c5593SMatthew G. Knepley {
60678c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
60688c6c5593SMatthew G. Knepley 
60698c6c5593SMatthew G. Knepley   PetscFunctionBegin;
60708c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60718c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
60728c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
60738c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
60748c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
60758c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
60768c6c5593SMatthew G. Knepley }
60778c6c5593SMatthew G. Knepley 
60781c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
60798c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
60808c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
60818c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6082191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
60838c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
60848c6c5593SMatthew G. Knepley {
60858c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
60868c6c5593SMatthew G. Knepley 
60878c6c5593SMatthew G. Knepley   PetscFunctionBegin;
60888c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60898c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
60908c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
60918c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
60921c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
60938c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
60948c6c5593SMatthew G. Knepley }
60958c6c5593SMatthew G. Knepley 
60962716604bSToby Isaac /*@C
60972716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
60982716604bSToby Isaac 
60992716604bSToby Isaac   Input Parameters:
61002716604bSToby Isaac + dm    - The DM
61010709b2feSToby Isaac . time  - The time
61022716604bSToby Isaac . funcs - The functions to evaluate for each field component
61032716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
61042716604bSToby Isaac - X     - The coefficient vector u_h
61052716604bSToby Isaac 
61062716604bSToby Isaac   Output Parameter:
61072716604bSToby Isaac . diff - The diff ||u - u_h||_2
61082716604bSToby Isaac 
61092716604bSToby Isaac   Level: developer
61102716604bSToby Isaac 
61111189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
61122716604bSToby Isaac @*/
61130709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
61142716604bSToby Isaac {
61152716604bSToby Isaac   PetscErrorCode ierr;
61162716604bSToby Isaac 
61172716604bSToby Isaac   PetscFunctionBegin;
61182716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6119b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
61202716604bSToby Isaac   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2Diff",((PetscObject)dm)->type_name);
61210709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
61222716604bSToby Isaac   PetscFunctionReturn(0);
61232716604bSToby Isaac }
6124b698f381SToby Isaac 
6125b698f381SToby Isaac /*@C
6126b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6127b698f381SToby Isaac 
6128b698f381SToby Isaac   Input Parameters:
6129b698f381SToby Isaac + dm    - The DM
6130b698f381SToby Isaac , time  - The time
6131b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6132b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6133b698f381SToby Isaac . X     - The coefficient vector u_h
6134b698f381SToby Isaac - n     - The vector to project along
6135b698f381SToby Isaac 
6136b698f381SToby Isaac   Output Parameter:
6137b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6138b698f381SToby Isaac 
6139b698f381SToby Isaac   Level: developer
6140b698f381SToby Isaac 
6141b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6142b698f381SToby Isaac @*/
6143b698f381SToby 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)
6144b698f381SToby Isaac {
6145b698f381SToby Isaac   PetscErrorCode ierr;
6146b698f381SToby Isaac 
6147b698f381SToby Isaac   PetscFunctionBegin;
6148b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6149b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6150b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6151b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6152b698f381SToby Isaac   PetscFunctionReturn(0);
6153b698f381SToby Isaac }
6154b698f381SToby Isaac 
61552a16baeaSToby Isaac /*@C
61562a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
61572a16baeaSToby Isaac 
61582a16baeaSToby Isaac   Input Parameters:
61592a16baeaSToby Isaac + dm    - The DM
61602a16baeaSToby Isaac . time  - The time
61612a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
61622a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
61632a16baeaSToby Isaac - X     - The coefficient vector u_h
61642a16baeaSToby Isaac 
61652a16baeaSToby Isaac   Output Parameter:
61662a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
61672a16baeaSToby Isaac 
61682a16baeaSToby Isaac   Level: developer
61692a16baeaSToby Isaac 
61701189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
61712a16baeaSToby Isaac @*/
61721189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
61732a16baeaSToby Isaac {
61742a16baeaSToby Isaac   PetscErrorCode ierr;
61752a16baeaSToby Isaac 
61762a16baeaSToby Isaac   PetscFunctionBegin;
61772a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61782a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
61792a16baeaSToby Isaac   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
61802a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
61812a16baeaSToby Isaac   PetscFunctionReturn(0);
61822a16baeaSToby Isaac }
61832a16baeaSToby Isaac 
6184df0b854cSToby Isaac /*@C
6185df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6186cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6187df0b854cSToby Isaac 
6188df0b854cSToby Isaac   Collective on dm
6189df0b854cSToby Isaac 
6190df0b854cSToby Isaac   Input parameters:
6191df0b854cSToby Isaac + dm - the pre-adaptation DM object
6192a1b0c543SToby Isaac - label - label with the flags
6193df0b854cSToby Isaac 
6194df0b854cSToby Isaac   Output parameters:
61950d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6196df0b854cSToby Isaac 
6197df0b854cSToby Isaac   Level: intermediate
61980d1cd5e0SMatthew G. Knepley 
61990d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6200df0b854cSToby Isaac @*/
62010d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6202df0b854cSToby Isaac {
6203df0b854cSToby Isaac   PetscErrorCode ierr;
6204df0b854cSToby Isaac 
6205df0b854cSToby Isaac   PetscFunctionBegin;
6206df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6207a1b0c543SToby Isaac   PetscValidPointer(label,2);
62080d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
62090d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
62106f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
62110d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
62120d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
62130d1cd5e0SMatthew G. Knepley }
62140d1cd5e0SMatthew G. Knepley 
62150d1cd5e0SMatthew G. Knepley /*@C
62160d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
62170d1cd5e0SMatthew G. Knepley 
62180d1cd5e0SMatthew G. Knepley   Input Parameters:
62190d1cd5e0SMatthew G. Knepley + dm - The DM object
62200d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
62216f25b0d8SLisandro 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_".
62220d1cd5e0SMatthew G. Knepley 
62230d1cd5e0SMatthew G. Knepley   Output Parameter:
62240d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
62250d1cd5e0SMatthew G. Knepley 
62260d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
62270d1cd5e0SMatthew G. Knepley 
62280d1cd5e0SMatthew G. Knepley   Level: advanced
62290d1cd5e0SMatthew G. Knepley 
62300d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
62310d1cd5e0SMatthew G. Knepley @*/
62320d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
62330d1cd5e0SMatthew G. Knepley {
62340d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
62350d1cd5e0SMatthew G. Knepley 
62360d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
62370d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62380d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
62390d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
62400d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
62416f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
62426f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
62430d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6244df0b854cSToby Isaac   PetscFunctionReturn(0);
6245df0b854cSToby Isaac }
6246c4088d22SMatthew G. Knepley 
6247502a2867SDave May /*@C
6248502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6249502a2867SDave May 
6250502a2867SDave May  Not Collective
6251502a2867SDave May 
6252502a2867SDave May  Input Parameter:
6253502a2867SDave May  . dm    - The DM
6254502a2867SDave May 
6255502a2867SDave May  Output Parameter:
6256502a2867SDave May  . nranks - the number of neighbours
6257502a2867SDave May  . ranks - the neighbors ranks
6258502a2867SDave May 
6259502a2867SDave May  Notes:
6260502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6261502a2867SDave May 
6262502a2867SDave May  Level: beginner
6263502a2867SDave May 
6264dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6265502a2867SDave May @*/
6266502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6267502a2867SDave May {
6268502a2867SDave May   PetscErrorCode ierr;
6269502a2867SDave May 
6270502a2867SDave May   PetscFunctionBegin;
6271502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6272502a2867SDave May   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMGetNeighbors",((PetscObject)dm)->type_name);
6273502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6274502a2867SDave May   PetscFunctionReturn(0);
6275502a2867SDave May }
6276502a2867SDave May 
6277531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6278531c7667SBarry Smith 
6279531c7667SBarry Smith /*
6280531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6281531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6282531c7667SBarry Smith */
6283531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6284531c7667SBarry Smith {
6285531c7667SBarry Smith   PetscErrorCode ierr;
6286531c7667SBarry Smith 
6287531c7667SBarry Smith   PetscFunctionBegin;
6288531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6289531c7667SBarry Smith     Vec x1local;
6290531c7667SBarry Smith     DM  dm;
6291531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6292531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6293531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6294531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6295531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6296531c7667SBarry Smith     x1   = x1local;
6297531c7667SBarry Smith   }
6298531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6299531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6300531c7667SBarry Smith     DM  dm;
6301531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6302531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6303531c7667SBarry Smith   }
6304531c7667SBarry Smith   PetscFunctionReturn(0);
6305531c7667SBarry Smith }
6306531c7667SBarry Smith 
6307531c7667SBarry Smith /*@
6308531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6309531c7667SBarry Smith 
6310531c7667SBarry Smith     Input Parameter:
6311531c7667SBarry Smith .    coloring - the MatFDColoring object
6312531c7667SBarry Smith 
6313531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6314531c7667SBarry Smith 
63151b266c99SBarry Smith     Level: advanced
63161b266c99SBarry Smith 
6317531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6318531c7667SBarry Smith @*/
6319531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6320531c7667SBarry Smith {
6321531c7667SBarry Smith   PetscFunctionBegin;
6322531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6323531c7667SBarry Smith   PetscFunctionReturn(0);
6324531c7667SBarry Smith }
6325